<?xml version="1.0" encoding="iso-8859-1"?>
<!--
Copyright (c) 2002, 2003 Networks Associates Technologies, Inc.
All rights reserved.
Copyright (c) 2004-2005 Simon L. Nielsen

This software was developed for the FreeBSD Project by Chris Costello
at Safeport Network Services and Network Assoicates Laboratories, the
Security Research Division of Network Associates, Inc. under
DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
CHATS research program.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
  xmlns:cvs="http://www.FreeBSD.org/XML/CVS"
  exclude-result-prefixes="cvs">
  <!-- $Id: page.xsl 167 2007-02-12 22:04:33Z simon $ -->
  <!-- <xsl:import href="includes.xsl"/> -->
  <!-- <xsl:import href="heading.xsl"/> -->

  <xsl:output method="xml" encoding="iso-8859-1" indent="yes"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
    doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"/>

  <xsl:variable name="allpages" select="document(document('file.xml')/files/file)" />
  <xsl:key name="menugroups" match="page" use="@parentid"/>

  <!-- This is the main page element. -->
  <xsl:template match="page">

    <!-- To clear up confusion and scoping for other templates which use
	 this attribute. XXX not used ATM -->
    <xsl:variable name="role" select="@role"/>

    <html>
      <head>
        <link rel="stylesheet" type="text/css" href="default.css"/>
        <title>
          <xsl:value-of select="title" />
        </title>
      </head>

      <body>
        <div id="page-menu">
          <xsl:call-template name="page-menu">
            <xsl:with-param name="pageid" select="@id"/>
          </xsl:call-template>
        </div>

        <div id="page-main">
          <h1><xsl:value-of select="title" /></h1>
          <xsl:apply-templates select="section|links|html"/>
        </div>

        <div id="page-footer">
          <div style="align: left; float: left;">
            Copyright 2001-2009 Simon L. Nielsen.  All rights reserved.<br />
            <xsl:value-of select="//cvs:keyword[@name='id']"/>
          </div>
          <div style="text-align: right;">
            <a href="http://validator.w3.org/check/referer"><img
              src="valid-xhtml10.png" alt="Valid XHTML 1.0!"
              style="width:88px;height:31px" class="valid-logos"/></a>
          </div>
        </div>
      </body>
    </html>
  </xsl:template>

  <!-- Mark up individual sections.  This could probably depend a bit less on
       HTML in the XML files, but it's fine for now.
  -->
  <xsl:template match="section">
    <xsl:if test="string(title)">
      <h2><xsl:value-of select="title"/></h2>
    </xsl:if>
    <xsl:apply-templates select="html|links"/>
  </xsl:template>

  <xsl:template name="page-menu">
    <xsl:param name="pageid"/>

    <h2>Menu</h2>

    <ul>
      <xsl:for-each select="$allpages/page[not(@parentid)]">
        <xsl:sort select="@sortid" order="ascending"/>
        <xsl:sort select="@id" order="descending"/>

        <xsl:if test="not(string(title/@hide) or string(menutitle/@hide))">
          <xsl:apply-templates select="." mode="menu">
            <xsl:with-param name="pageid" select="$pageid"/>
          </xsl:apply-templates>
        </xsl:if>
      </xsl:for-each>
    </ul>
  </xsl:template>

  <xsl:template name="menu-level">
    <xsl:param name="curid"/>
    <xsl:param name="pageid"/>

    <xsl:for-each select="$allpages/page[@parentid=$curid and
                          (not(string(menutitle/@hide)) or menutitle/@display != 'yes')]">
      <xsl:sort select="page/@id"/>

      <xsl:apply-templates select="." mode="menu">
        <xsl:with-param name="pageid" select="$pageid"/>
      </xsl:apply-templates>
    </xsl:for-each>
  </xsl:template>

  <!-- Generate a menu item. -->
  <xsl:template match="page" mode="menu">
    <xsl:param name="pageid"/>
    <xsl:variable name="curid" select="@id"/>

    <li>
      <xsl:if test="$pageid = @id">
        <xsl:attribute name="id">
          <xsl:text>menu-active</xsl:text>
        </xsl:attribute>
      </xsl:if>
      <a>
        <xsl:attribute name="href">
          <xsl:value-of select="@id"/>
          <xsl:text>.html</xsl:text>
        </xsl:attribute>

        <xsl:choose>
          <xsl:when test="string(menutitle)">
            <xsl:value-of select="menutitle"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="title"/>
          </xsl:otherwise>
        </xsl:choose>
      </a>
      <xsl:if test="$allpages/page[@parentid=$curid and
                    (not(string(menutitle/@hide)) or menutitle/@display != 'yes')]">
        <ul>
          <xsl:call-template name="menu-level">
            <xsl:with-param name="curid" select="$curid"/>
            <xsl:with-param name="pageid" select="$pageid"/>
          </xsl:call-template>
        </ul>
      </xsl:if>
    </li>
  </xsl:template>

  <!-- ================================================ -->
  <!-- HTML "copy"/handling part: -->
  <!-- ================================================ -->

  <!-- Just copy in the contents of <html></html>. -->
  <xsl:template match="html">
    <xsl:apply-templates/>
  </xsl:template>

  <!-- Copy basic HTML elements. -->
  <!-- Removed: html -->
  <xsl:template match=" a | applet | b | big | blockquote | body | br |
    caption |
    cite | code | col | colgroup | dd | div | dl | dt | em | font |
    form | frame | frameset | head | h1 | h2 | h3 | h4 | h5 | h6 | hr |
    i | iframe | img | link | li | map | meta | noframes | ol |
    p | param | pre | s | script | small | span | strong | style |
    sub | sup | td | th | title | tr | tt | ul | var | table ">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <!-- Little hack to add href automatically to a link, -->
  <!-- ie. <a>foo</a>.. -->
  <xsl:template match="a[not(@href)]">
    <a>
      <xsl:attribute name="href">
        <xsl:value-of select="."/>
      </xsl:attribute>
      <xsl:value-of select="."/>
    </a>
  </xsl:template>

  <!-- Magic... -->
  <xsl:template match="freebsdport">
    <a>
      <xsl:attribute name="href">
        <xsl:text>http://www.freshports.org/</xsl:text>
        <xsl:value-of select="."/>
      </xsl:attribute>
      <xsl:value-of select="."/>
    </a>
  </xsl:template>

  <!-- Links part -->
  <xsl:template match="links">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="category">
    <div class="link-category">
    <h2 class="links"><xsl:value-of select="title"/></h2>
    <ul>
      <xsl:attribute name="class">
        <xsl:text>links</xsl:text>
      </xsl:attribute>
      <xsl:apply-templates select="link"/>
    </ul></div>
  </xsl:template>

  <xsl:template match="link">
    <li>
      <a>
        <xsl:attribute name="href">
          <xsl:value-of select="url"/>
        </xsl:attribute>
        <xsl:value-of select="name"/>
      </a>
      <xsl:if test="string(comment)">
        (<xsl:value-of select="comment"/>)
      </xsl:if>
    </li>
  </xsl:template>

  <xsl:template match="include">
    <xsl:apply-templates select="document(.)"/>
  </xsl:template>


  <!-- DocBook compat -->

  <xsl:template match="email">
    <xsl:text>&lt;</xsl:text>
    <a>
      <xsl:attribute name="href">
        <xsl:text>mailto:</xsl:text>
        <xsl:value-of select="."/>
      </xsl:attribute>
      <xsl:value-of select="."/>
    </a>
    <xsl:text>&gt;</xsl:text>
  </xsl:template>

  <xsl:template match="ulink">
    <a>
      <xsl:attribute name="href">
        <xsl:value-of select="@url"/>
      </xsl:attribute>
      <xsl:choose>
        <xsl:when test="string(.)">
          <xsl:value-of select="."/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="@url"/>
        </xsl:otherwise>
      </xsl:choose>
    </a>
  </xsl:template>

</xsl:stylesheet>
