<?xml version="1.0"?>
<!DOCTYPE xsl:transform [
<!ENTITY nbsp "&#xa0;">
]>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  exclude-result-prefixes="xlink"
  version="1.1">

  <xsl:import href="iso8601.xsl"/>

  <xsl:output method="html"/>
  
  <xsl:template match="*">
    <p style="color:#ff0000">
      <xsl:value-of select="name()"/>
      <xsl:text>: {</xsl:text>
      <xsl:apply-templates/>
      <xsl:text>}</xsl:text>
    </p>
  </xsl:template>

  <xsl:template match="acronym[@idref]" priority="2">
    <xsl:variable name="acr-decl" select="id(@idref)"/>
    <acronym title="{$acr-decl/@expansion}">
      <xsl:value-of select="$acr-decl"/>
    </acronym>
  </xsl:template>

  <xsl:template match="acronym" priority="1">
    <acronym>
      <xsl:apply-templates/>
    </acronym>
  </xsl:template>

  <xsl:template match="address">
    <address>
      <xsl:apply-templates/>
    </address>
  </xsl:template>

  <xsl:template match="citationtitle[@type='article']">
    <cite class="article">
      <xsl:text>"</xsl:text>
      <xsl:apply-templates/>
      <xsl:text>"</xsl:text>
    </cite>
  </xsl:template>

  <xsl:template match="citationtitle">
    <cite>
      <xsl:apply-templates/>
    </cite>
  </xsl:template>

  <xsl:template match="address/city">
    <xsl:apply-templates/>
    <xsl:choose>
      <xsl:when test="following-sibling::state">
        <xsl:text>, </xsl:text>
      </xsl:when>
      <xsl:when test="following-sibling::postcode">
        <xsl:text> </xsl:text>
      </xsl:when>
      <xsl:when test="following-sibling::*">
        <br/>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <!-- literated -->
  <xsl:template match="div[@type='major'] | website">
    <xsl:call-template name="html-page"/>
  </xsl:template>

  <xsl:template match="div[@type='minor']">
    <div>
      <xsl:apply-templates/>
    </div>
  </xsl:template>

  <xsl:template match="email">
    <a href="mailto:{.}" class="email">
      <xsl:if test="@name">
        <xsl:attribute name="title">
          <xsl:value-of select="@name"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:apply-templates/>
    </a>
  </xsl:template>

  <xsl:template match="emphasis">
    <em>
      <xsl:apply-templates/>
    </em>
  </xsl:template>

  <xsl:template match="link[@idref]">
    <xsl:variable name="home">
      <xsl:if test="ancestor::div[@type='major'][1]/div[@type='major']">
	<xsl:text>../</xsl:text>
      </xsl:if>
      <xsl:for-each select="ancestor::div[@type='major'][position() > 1]">
	<xsl:text>../</xsl:text>
      </xsl:for-each>
    </xsl:variable>
    <xsl:variable name="target" select="id(@idref)"/>
    <a>
      <xsl:attribute name="href">
	<xsl:value-of select="$home"/>
        <xsl:apply-templates select="$target" mode="target-href"/>
      </xsl:attribute>
      <xsl:if test="$target/title">
        <xsl:attribute name="title">
          <xsl:apply-templates select="$target/title" mode="text-only"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:apply-templates/>
    </a>
  </xsl:template>

  <xsl:template match="link[@xlink:href]">
    <a href="{@xlink:href}">
      <xsl:if test="@xlink:title">
        <xsl:attribute name="title">
          <xsl:value-of select="@xlink:title"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:apply-templates/>
    </a>
  </xsl:template>

  <xsl:template match="listitem">
    <li>
      <xsl:apply-templates/>
    </li>
  </xsl:template>

  <xsl:template match="para">
    <p>
      <xsl:apply-templates/>
    </p>
  </xsl:template>

  <xsl:template match="address/nation | address/name | address/street">
    <xsl:apply-templates/>
    <xsl:if test="following-sibling::*">
      <br/>
    </xsl:if>
  </xsl:template>

  <xsl:template match="address/postcode">
    <xsl:apply-templates/>
    <xsl:choose>
      <xsl:when test="following-sibling::city | following-sibling::state">
        <xsl:text> </xsl:text>
      </xsl:when>
      <xsl:when test="following-sibling::*">
        <br/>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="address/state">
    <xsl:apply-templates/>
    <xsl:choose>
      <xsl:when test="following-sibling::postcode">
        <xsl:text> </xsl:text>
      </xsl:when>
      <xsl:when test="following-sibling::*">
        <br/>
      </xsl:when>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="telephone">
    <xsl:apply-templates/>
  </xsl:template>

  <xsl:template match="unorderedlist">
    <ul>
      <xsl:apply-templates/>
    </ul>
  </xsl:template>

  <xsl:template match="div[@type='minor']/title">
    <h2>
      <xsl:apply-templates/>
    </h2>
  </xsl:template>

  <xsl:template match="acronyms | author | keywords | stylesheet |
    title"/>

  <xsl:template match="div" mode="nav">
    <xsl:param name="home" select="''"/>
    <xsl:text> &nbsp; | &nbsp; </xsl:text>
    <a class="nav">
      <xsl:attribute name="href">
	<xsl:value-of select="$home"/>
	<xsl:apply-templates select="." mode="target-href"/>
      </xsl:attribute>
      <xsl:attribute name="title">
	<xsl:apply-templates select="title" mode="text-only"/>
      </xsl:attribute>
      <xsl:value-of select="title/@short"/>
    </a>
  </xsl:template>

  <xsl:template match="website" mode="nav">
    <xsl:param name="home" select="''"/>
    <a href="{$home}index.html" class="nav">
      <xsl:attribute name="title">
	<xsl:apply-templates select="title" mode="text-only"/>
      </xsl:attribute>
      <xsl:value-of select="title/@short"/>
    </a>
  </xsl:template>

  <xsl:template match="div[@type='major']" mode="post-this-nav">
    <xsl:param name="home" select="''"/>
    <xsl:apply-templates
      select="following-sibling::div[@type='major']" mode="nav">
      <xsl:with-param name="home" select="$home"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="div[@type='major']" mode="pre-this-nav">
    <xsl:param name="home" select="''"/>
    <xsl:apply-templates select="/website" mode="nav">
      <xsl:with-param name="home" select="$home"/>
    </xsl:apply-templates>
    <xsl:apply-templates
      select="preceding-sibling::div[@type='major']" mode="nav">
      <xsl:with-param name="home" select="$home"/>
    </xsl:apply-templates>
  </xsl:template>

  <xsl:template match="div[@type='major']" mode="target-href">
    <xsl:for-each select="ancestor::div | fnord">
      <xsl:choose>
	<xsl:when test="@id">
	  <xsl:value-of select="@id"/>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:value-of select="generate-id()"/>
	</xsl:otherwise>
      </xsl:choose>
      <xsl:text>/</xsl:text>
    </xsl:for-each>
    <xsl:choose>
      <xsl:when test="@id">
	<xsl:value-of select="@id"/>
      </xsl:when>
      <xsl:otherwise>
	<xsl:value-of select="generate-id()"/>
      </xsl:otherwise>
    </xsl:choose>
    <xsl:if test="div[@type='major']">
      <xsl:text>/index</xsl:text>
    </xsl:if>
    <xsl:text>.html</xsl:text>
  </xsl:template>

  <xsl:template match="website" mode="target-href">
    <xsl:text>index.html</xsl:text>
  </xsl:template>

  <xsl:template match="text()" mode="text-only">
    <xsl:value-of select="."/>
  </xsl:template>

  <xsl:template match="acronym[@idref]" mode="text-only">
    <xsl:value-of select="id(@idref)"/>
  </xsl:template>

  <xsl:template match="div/div" mode="this-nav" priority="2">
    <xsl:param name="home" select="''"/>
    <div class="nav">
      <p class="nav">
	<xsl:apply-templates select="ancestor::div[last()]"
			     mode="pre-this-nav">
	  <xsl:with-param name="home" select="$home"/>
	</xsl:apply-templates>
	<xsl:apply-templates select="ancestor::div[last()]"
			     mode="nav">
	  <xsl:with-param name="home" select="$home"/>
	</xsl:apply-templates>
	<xsl:apply-templates select="ancestor::div[last()]"
			     mode="post-this-nav">
	  <xsl:with-param name="home" select="$home"/>
	</xsl:apply-templates>
      </p>
    </div>
  </xsl:template>

  <xsl:template match="div[@type='major']" mode="this-nav">
    <xsl:param name="home" select="''"/>
    <div class="nav">
      <p class="nav">
	<xsl:apply-templates select="." mode="pre-this-nav">
	  <xsl:with-param name="home" select="$home"/>
	</xsl:apply-templates>
	<xsl:text> &nbsp; | &nbsp; </xsl:text>
        <span class="nav">
          <xsl:value-of select="title/@short"/>
        </span>
	<xsl:apply-templates select="." mode="post-this-nav">
	  <xsl:with-param name="home" select="$home"/>
	</xsl:apply-templates>
      </p>
    </div>
  </xsl:template>

  <xsl:template match="website" mode="this-nav">
    <xsl:param name="home" select="''"/>
    <div class="nav">
      <p class="nav">
        <span class="nav">
          <xsl:value-of select="title/@short"/>
        </span>
        <xsl:apply-templates select="div[@type='major']" mode="nav"/>
      </p>
    </div>
  </xsl:template>

  <!-- literated -->
  <xsl:template name="footer">
    <xsl:variable name="author"
      select="ancestor-or-self::*
                [author/
                 @xlink:role=
                   'http://crism.maden.org/consulting/xlink-roles/author']
                [1]/
              author
                [@xlink:role=
                   'http://crism.maden.org/consulting/xlink-roles/author']"
    />
    <div class="footer">
      <p class="footer">
        <xsl:text>This page last updated on </xsl:text>
        <xsl:call-template name="process-date">
          <xsl:with-param name="string"
            select="ancestor-or-self::*
                      [@last-update]
                      [1]/
                    @last-update"/>
          <xsl:with-param name="command" select="'f'"/>
        </xsl:call-template>
        <xsl:text> by </xsl:text>
        <a href="{$author/@xlink:href}">
          <xsl:value-of select="$author"/>
        </a>
        <xsl:text>.</xsl:text>
      </p>
    </div>
  </xsl:template>

  <!-- literated -->
  <xsl:template name="header">
    <xsl:param name="home" select="''"/>
    <h1>
      <xsl:apply-templates select="title/child::node()"/>
    </h1>

    <xsl:apply-templates select="." mode="this-nav">
      <xsl:with-param name="home" select="$home"/>
    </xsl:apply-templates>
  </xsl:template>

  <!-- literated -->
  <xsl:template name="html-head">
    <xsl:param name="home" select="''"/>
    <xsl:variable name="author"
      select="ancestor-or-self::*
                [author/
                 @xlink:role=
                   'http://crism.maden.org/consulting/xlink-roles/author']
                [1]/
              author
                [@xlink:role=
                   'http://crism.maden.org/consulting/xlink-roles/author']"
    />
    <head>
      <title>
        <xsl:if test="not(self::website)">
          <xsl:value-of select="/website/title"/>
          <xsl:text>: </xsl:text>
        </xsl:if>
	<xsl:apply-templates select="title" mode="text-only"/>
      </title>
      <link rel="stylesheet"
        href="{$home}{ancestor-or-self::*
                        [stylesheet/
                         @xlink:role=
                           'http://crism.maden.org/consulting/xlink-roles/html-stylesheet']
                        [1]/
                      stylesheet
                        [@xlink:role=
                           'http://crism.maden.org/consulting/xlink-roles/html-stylesheet']/
                      @xlink:href}"
        type="text/css"/>
      <link rev="made"
        href="{$author/@xlink:href}"
        title="{$author}"/>
      <xsl:if test="keywords">
        <meta name="keywords">
          <xsl:attribute name="content">
            <xsl:for-each select="keywords/keyword">
              <xsl:if test="not(position() = 1)">
                <xsl:text> </xsl:text>
              </xsl:if>
              <xsl:value-of select="."/>
            </xsl:for-each>
          </xsl:attribute>
        </meta>
      </xsl:if>
    </head>
  </xsl:template>

  <!-- literated -->
  <xsl:template name="html-page">
    <xsl:variable name="href">
      <xsl:choose>
        <xsl:when test="self::website">
	  <xsl:text>index.html</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:for-each select="ancestor::div | fnord">
            <xsl:choose>
              <xsl:when test="@id">
                <xsl:value-of select="@id"/>
              </xsl:when>
              <xsl:otherwise>
                <xsl:value-of select="generate-id()"/>
              </xsl:otherwise>
            </xsl:choose>
	    <xsl:text>/</xsl:text>
          </xsl:for-each>
          <xsl:choose>
            <xsl:when test="self::div/div[@type='major']">
	      <xsl:choose>
		<xsl:when test="@id">
		  <xsl:value-of select="@id"/>
		</xsl:when>
		<xsl:otherwise>
		  <xsl:value-of select="generate-id()"/>
		</xsl:otherwise>
	      </xsl:choose>
	      <xsl:text>/index.html</xsl:text>
            </xsl:when>
            <xsl:when test="@id">
              <xsl:value-of select="concat(@id, '.html')"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="concat(generate-id(), '.html')"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

    <xsl:variable name="home">
      <xsl:if test="self::div/div[@type='major']">
	<xsl:text>../</xsl:text>
      </xsl:if>
      <xsl:if test="ancestor::div">
        <xsl:for-each select="ancestor::div">
	  <xsl:text>../</xsl:text>
        </xsl:for-each>
      </xsl:if>
    </xsl:variable>

    <xsl:message>
      <xsl:text>Generating </xsl:text>
      <xsl:value-of select="$href"/>
      <xsl:text>.</xsl:text>
    </xsl:message>

    <xsl:document href="{$href}">
      <html>
        <xsl:call-template name="html-head">
          <xsl:with-param name="home" select="$home"/>
        </xsl:call-template>
        <body>
          <xsl:call-template name="header">
            <xsl:with-param name="home" select="$home"/>
          </xsl:call-template>
          <xsl:apply-templates/>
          <xsl:call-template name="footer"/>
        </body>
      </html>
    </xsl:document>
  </xsl:template>

</xsl:transform>
