<?xml version="1.0" encoding="utf8"?>
<!--
$Id: portable.xsl,v 2.9 2006/06/27 01:04:44 jmates Exp $

The author disclaims all copyrights and releases this document into
the public domain.

For conversion to HTML fragments, suitable for import into Plone.
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.0">
  <xsl:import href="../shared.xsl"/>
  <xsl:output method="html" indent="yes"/>
  <xsl:strip-space elements="*"/>
  <xsl:preserve-space elements="info note warn li"/>
  <xsl:param name="default.uri"/>
<!-- basic root element and section handling. -->
<!-- these will the templates most often overridden -->
<!-- you may also want to handle the summary element -->
  <xsl:template match="eolas">
    <h1>
      <xsl:value-of select="summary/title"/>
    </h1>
    <xsl:apply-templates select="section"/>
    <hr/>
    <xsl:if test="string-length(summary/rights) &gt; 0">
      <p>
        <i>
          <xsl:value-of select="summary/rights"/>
        </i>
      </p>
    </xsl:if>
    <p>
      <tt>
        <xsl:value-of select="summary/version"/>
      </tt>
      <br/>
      <tt>$Id: portable.xsl,v 2.9 2006/06/27 01:04:44 jmates Exp $</tt>
    </p>
  </xsl:template>
  <xsl:template match="section">
    <xsl:apply-templates/>
  </xsl:template>
<!-- sub-section elements follow; these generally will not need -->
<!-- to be overridden -->
  <xsl:template match="title" mode="nosub">
    <xsl:apply-templates/>
  </xsl:template>
  <xsl:template match="title">
    <xsl:variable name="hlevel">
      <xsl:choose>
        <xsl:when test="count(ancestor-or-self::section)+1 &gt; 6">
          <xsl:text>h6</xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <xsl:text>h</xsl:text>
          <xsl:value-of select="count(ancestor-or-self::section)+1"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:element name="{$hlevel}">
      <xsl:element name="a">
        <xsl:attribute name="name">
          <xsl:text>s</xsl:text>
          <xsl:number count="section" level="multiple" format="1.1"/>
        </xsl:attribute>
        <xsl:apply-templates/>
      </xsl:element>
    </xsl:element>
<!-- quick sub-section links -->
    <xsl:if test="count(../section) &gt; 1">
      <p>
        <xsl:for-each select="../section">
          <xsl:if test="string-length(title) &gt; 0">
            <xsl:element name="a">
              <xsl:attribute name="href">
                <xsl:text>#s</xsl:text>
                <xsl:number count="section" level="multiple" format="1.1"/>
              </xsl:attribute>
              <xsl:apply-templates select="title" mode="nosub"/>
            </xsl:element>
            <xsl:if test="not(position()=last())">
              <xsl:text> | </xsl:text>
            </xsl:if>
          </xsl:if>
        </xsl:for-each>
      </p>
    </xsl:if>
  </xsl:template>
  <xsl:template match="info|note|warn">
    <xsl:call-template name="genElement">
      <xsl:with-param name="element" select="'p'"/>
    </xsl:call-template>
  </xsl:template>
<!-- abstract generation of elements <p>, <div>, and similar;
generally stuff that cannot have links... -->
  <xsl:template name="genElement">
    <xsl:param name="element"/>
    <xsl:element name="{$element}">
      <xsl:if test="@xml:lang">
        <xsl:attribute name="class">
          <xsl:text>lang-</xsl:text>
          <xsl:value-of select="@xml:lang"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
<!-- as genElement, but with href wrapper, if required -->
  <xsl:template name="genLinkElement">
    <xsl:param name="element"/>
    <xsl:choose>
      <xsl:when test="@xlink:href and not(ancestor::title)">
        <a href="{@xlink:href}">
          <xsl:call-template name="genElement">
            <xsl:with-param name="element" select="$element"/>
          </xsl:call-template>
        </a>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="genElement">
          <xsl:with-param name="element" select="$element"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="data">
    <xsl:element name="pre">
      <xsl:if test="@xml:lang">
        <xsl:attribute name="class">
          <xsl:text>lang-</xsl:text>
          <xsl:value-of select="@xml:lang"/>
        </xsl:attribute>
      </xsl:if>
<!-- grr, whitespace goes funny if use template for these... -->
      <xsl:for-each select="line|old-line|shell">
        <xsl:if test="@prefix">
          <xsl:value-of select="@prefix"/>
        </xsl:if>
        <xsl:choose>
          <xsl:when test="name()='shell' and string-length(.) = 0">
            <kbd> </kbd>
          </xsl:when>
          <xsl:when test="name()='shell'">
            <kbd>
              <xsl:apply-templates/>
            </kbd>
          </xsl:when>
          <xsl:when test="name()='old-line'">
<!-- better way to do this in lynx? -->
            <span style="text-decoration: line-through;">
              <xsl:apply-templates/>
            </span>
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates/>
          </xsl:otherwise>
        </xsl:choose>
        <xsl:if test="not(position()=last())">
          <br/>
        </xsl:if>
      </xsl:for-each>
    </xsl:element>
  </xsl:template>
<!-- list related stuff -->
  <xsl:template match="list">
    <xsl:call-template name="genElement">
      <xsl:with-param name="element" select="'ul'"/>
    </xsl:call-template>
  </xsl:template>
  <xsl:template match="enum">
    <xsl:call-template name="genElement">
      <xsl:with-param name="element" select="'ol'"/>
    </xsl:call-template>
  </xsl:template>
  <xsl:template match="li">
    <xsl:element name="{name()}">
      <xsl:if test="@xml:lang">
        <xsl:attribute name="class">
          <xsl:text>lang-</xsl:text>
          <xsl:value-of select="@xml:lang"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="link">
    <xsl:choose>
      <xsl:when test="ancestor::title">
        <xsl:apply-templates/>
      </xsl:when>
      <xsl:otherwise>
        <a href="{@xlink:href}">
          <xsl:apply-templates/>
        </a>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="image">
    <xsl:element name="img">
      <xsl:attribute name="src">
        <xsl:value-of select="@xlink:href"/>
      </xsl:attribute>
      <xsl:if test="@width">
        <xsl:attribute name="width">
          <xsl:value-of select="@width"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="@height">
        <xsl:attribute name="height">
          <xsl:value-of select="@height"/>
        </xsl:attribute>
      </xsl:if>
      <xsl:if test="@xml:lang">
        <xsl:attribute name="class">
          <xsl:text>lang-</xsl:text>
          <xsl:value-of select="@xml:lang"/>
        </xsl:attribute>
      </xsl:if>
    </xsl:element>
  </xsl:template>
<!-- special handling for SVG, which normally require an <embed> -->
  <xsl:template match="image-svg">
    <a href="{@xlink:href}">
      <xsl:text>[SVG Image]</xsl:text>
    </a>
  </xsl:template>
<!-- these are for custom markup of data stuff -->
<!-- input is stuff people presumably type in, comment is to demark -->
<!-- comments in code differently for easier reading -->
  <xsl:template match="input">
    <xsl:call-template name="genLinkElement">
      <xsl:with-param name="element" select="'kbd'"/>
    </xsl:call-template>
  </xsl:template>
<!-- "computer-related" markup stuff; let CSS handle how these look -->
  <xsl:template match="cmd|cmd-args|code|file|file-glob|gui|host">
    <xsl:call-template name="genLinkElement">
      <xsl:with-param name="element" select="'tt'"/>
    </xsl:call-template>
  </xsl:template>
<!-- stuff translated directly into HTML -->
  <xsl:template match="em|span">
    <xsl:call-template name="genLinkElement">
      <xsl:with-param name="element" select="name()"/>
    </xsl:call-template>
  </xsl:template>
  <xsl:template name="applekb">
    <xsl:choose>
      <xsl:when test="string-length(name)">
        <xsl:text>Apple KB </xsl:text>
        <xsl:value-of select="kbid"/>
        <xsl:text>: “</xsl:text>
        <xsl:value-of select="name"/>
        <xsl:text>”</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>Apple KB </xsl:text>
        <xsl:value-of select="kbid"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="applekb[ancestor::title]">
    <xsl:call-template name="applekb"/>
  </xsl:template>
  <xsl:template match="applekb">
    <a href="http://www.info.apple.com/kbnum/n{kbid}">
      <xsl:call-template name="applekb"/>
    </a>
  </xsl:template>
<!-- man pages... default link into FreeBSD man CGI for time being -->
  <xsl:template name="man">
    <b>
      <xsl:value-of select="."/>
      <xsl:text>(</xsl:text>
      <xsl:value-of select="@section"/>
      <xsl:text>)</xsl:text>
    </b>
  </xsl:template>
  <xsl:template match="man[ancestor::title]">
    <xsl:call-template name="man"/>
  </xsl:template>
  <xsl:template match="man">
    <xsl:element name="a">
      <xsl:attribute name="href">
        <xsl:choose>
          <xsl:when test="@xlink:href">
            <xsl:value-of select="@xlink:href"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>http://www.FreeBSD.org/cgi/man.cgi?query=</xsl:text>
            <xsl:value-of select="."/>
            <xsl:if test="@section">
              <xsl:text>&amp;sektion=</xsl:text>
              <xsl:value-of select="@section"/>
            </xsl:if>
            <xsl:if test="@system">
              <xsl:text>&amp;manpath=</xsl:text>
              <xsl:value-of select="@system"/>
            </xsl:if>
<!-- not supporting arch/revision tags, as no
            consistent means of hooking them up with
            the FreeBSD man.cgi query; will need to
            setup a localized man.cgi that can search
            for things across archs better... sigh -->
          </xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <xsl:call-template name="man"/>
    </xsl:element>
  </xsl:template>
  <xsl:template match="perl-module[ancestor::title]">
    <xsl:call-template name="genElement">
      <xsl:with-param name="element" select="'b'"/>
    </xsl:call-template>
  </xsl:template>
  <xsl:template match="perl-module">
    <xsl:element name="a">
      <xsl:attribute name="href">
        <xsl:choose>
          <xsl:when test="@xlink:href">
            <xsl:value-of select="@xlink:href"/>
          </xsl:when>
          <xsl:otherwise>
<!--            <xsl:text>http://www.perldoc.com/cpan/</xsl:text> -->
<!-- folds ::'s into / -->
<!--            <xsl:value-of select="translate(normalize-space(translate(text(), ':', ' ')), ' ', '/')"/>
            <xsl:text>.html</xsl:text> -->
            <xsl:text>http://search.cpan.org/perldoc/</xsl:text>
            <xsl:value-of select="text()"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <xsl:call-template name="genElement">
        <xsl:with-param name="element" select="'b'"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:template>
  <xsl:template match="perl-pod">
    <xsl:element name="a">
      <xsl:attribute name="href">
        <xsl:choose>
          <xsl:when test="@xlink:href">
            <xsl:value-of select="@xlink:href"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>http://perldoc.perl.org/</xsl:text>
            <xsl:value-of select="normalize-space(text())"/>
            <xsl:text>.html</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <xsl:call-template name="genElement">
        <xsl:with-param name="element" select="'b'"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:template>
  <xsl:template match="perl-func">
    <xsl:element name="a">
      <xsl:attribute name="href">
        <xsl:choose>
          <xsl:when test="@xlink:href">
            <xsl:value-of select="@xlink:href"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>http://perldoc.perl.org/functions/</xsl:text>
            <xsl:value-of select="normalize-space(text())"/>
            <xsl:text>.html</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <xsl:call-template name="genElement">
        <xsl:with-param name="element" select="'code'"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:template>
  <xsl:template match="rfc[ancestor::title]">
    <xsl:text>[RFC </xsl:text>
    <xsl:value-of select="."/>
    <xsl:text>]</xsl:text>
  </xsl:template>
  <xsl:template match="rfc">
    <xsl:text>[</xsl:text>
    <xsl:element name="a">
      <xsl:attribute name="href">
        <xsl:choose>
          <xsl:when test="@xlink:href">
            <xsl:value-of select="@xlink:href"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text>http://www.rfc-editor.org/rfc/rfc</xsl:text>
            <xsl:value-of select="."/>
            <xsl:text>.txt</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:attribute>
      <xsl:text>RFC </xsl:text>
      <xsl:value-of select="."/>
    </xsl:element>
    <xsl:text>]</xsl:text>
  </xsl:template>
  <xsl:template match="acronym">
    <xsl:variable name="currentAbbr">
      <xsl:value-of select="abbr"/>
    </xsl:variable>
    <xsl:choose>
<!-- funny test is to determine whether this is the first acronym of a particular kind so we can treat it differently -->
      <xsl:when test="generate-id(//acronym[child::abbr=$currentAbbr]) = generate-id()">
<!-- linked or not? -->
        <xsl:choose>
          <xsl:when test="@xlink:href and not(ancestor::title)">
            <a href="{@xlink:href}">
              <xsl:value-of select="name"/>
              <xsl:text> (</xsl:text>
              <xsl:value-of select="abbr"/>
              <xsl:text>)</xsl:text>
            </a>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="name"/>
            <xsl:text> (</xsl:text>
            <xsl:value-of select="abbr"/>
            <xsl:text>)</xsl:text>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:when>
      <xsl:otherwise>
<!-- linked or not? -->
        <xsl:choose>
          <xsl:when test="@xlink:href and not(ancestor::title)">
            <a href="{@xlink:href}">
              <xsl:value-of select="abbr"/>
            </a>
          </xsl:when>
          <xsl:otherwise>
            <xsl:value-of select="abbr"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <xsl:template match="book">
    <xsl:choose>
      <xsl:when test="@xlink:href or @isbn">
        <xsl:element name="a">
          <xsl:attribute name="href">
            <xsl:choose>
              <xsl:when test="@isbn">
                <xsl:text>http://www.amazon.com/o/ASIN/</xsl:text>
                <xsl:value-of select="normalize-space(@isbn)"/>
<!--                <xsl:text>/sialorg-20</xsl:text> -->
              </xsl:when>
              <xsl:when test="@xlink:href">
                <xsl:value-of select="@xlink:href"/>
              </xsl:when>
            </xsl:choose>
          </xsl:attribute>
          <xsl:if test="@isbn">
            <xsl:attribute name="title">
              <xsl:text>ISBN: </xsl:text>
              <xsl:value-of select="normalize-space(@isbn)"/>
            </xsl:attribute>
          </xsl:if>
          <xsl:call-template name="genElement">
            <xsl:with-param name="element" select="'span'"/>
          </xsl:call-template>
        </xsl:element>
      </xsl:when>
      <xsl:otherwise>
        <xsl:call-template name="genElement">
          <xsl:with-param name="element" select="'span'"/>
        </xsl:call-template>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>
