<?xml version="1.0" encoding="UTF-8"?>

<!-- @(#) codetab.xsl -->

<!--

Unicode / ASCII / HTML code table stylesheet
Copyright (C) 2008 Peter Wall

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:template match="code-table">
    <table cellpadding="2" cellspacing="1" border="0">
      <xsl:if test="@width">
        <xsl:attribute name="width">
          <xsl:value-of select="@width"/>
        </xsl:attribute>
      </xsl:if>
      <tr>
        <th class="code-head" title="Decimal code">Dec</th>
        <th class="code-head" title="Hexadecimal code">Hex</th>
        <xsl:if test="@prog">
          <th class="code-head" title="Programming code">Prog</th>
        </xsl:if>
        <xsl:if test="@abbr">
          <th class="code-head" title="ASCII code abbreviation">Abbr</th>
        </xsl:if>
        <xsl:if test="@html">
          <th class="code-head" title="HTML entity">HTML</th>
        </xsl:if>
        <xsl:if test="@ch">
          <th class="code-head" title="Character">Ch</th>
        </xsl:if>
        <th class="code-head" title="Character name">Name</th>
      </tr>
      <xsl:choose>
        <xsl:when test="@file">
          <xsl:variable name="start" select="@start"/>
          <xsl:variable name="end" select="@end"/>
          <xsl:apply-templates
              select="document(string(@file))//code-row
                  [number(@dec)&gt;=number($start) and
                      number(@dec)&lt;=number($end)]">
            <xsl:with-param name="prog" select="@prog"/>
            <xsl:with-param name="abbr" select="@abbr"/>
            <xsl:with-param name="html" select="@html"/>
            <xsl:with-param name="ch" select="@ch"/>
          </xsl:apply-templates>
        </xsl:when>
        <xsl:otherwise>
          <xsl:apply-templates>
            <xsl:with-param name="prog" select="@prog"/>
            <xsl:with-param name="abbr" select="@abbr"/>
            <xsl:with-param name="html" select="@html"/>
            <xsl:with-param name="ch" select="@ch"/>
          </xsl:apply-templates>
        </xsl:otherwise>
      </xsl:choose>
    </table>
  </xsl:template>

  <xsl:template match="code-row">
    <xsl:param name="prog"/>
    <xsl:param name="abbr"/>
    <xsl:param name="html"/>
    <xsl:param name="ch"/>
    <tr class="code-row">
      <td class="code-entry-dec"><xsl:value-of select="@dec"/></td>
      <td class="code-entry-hex">
        <xsl:call-template name="hex-convert">
          <xsl:with-param name="num" select="@dec"/>
        </xsl:call-template>
      </td>
      <xsl:if test="$prog">
        <td class="code-entry-html">
          <xsl:choose>
            <xsl:when test="@prog"><xsl:value-of select="@prog"/></xsl:when>
            <xsl:otherwise>&#160;</xsl:otherwise>
          </xsl:choose>
        </td>
      </xsl:if>
      <xsl:if test="$abbr">
        <td class="code-entry-mnemonic">
          <xsl:value-of select="@abbr"/>
        </td>
      </xsl:if>
      <xsl:if test="$html">
        <td class="code-entry-html">
          <xsl:choose>
            <xsl:when test="@html">&amp;<xsl:value-of
                select="@html"/>;</xsl:when>
            <xsl:otherwise>&#160;</xsl:otherwise>
          </xsl:choose>
        </td>
      </xsl:if>
      <xsl:if test="$ch">
        <xsl:choose>
          <xsl:when test="@ch">
            <td class="code-entry-ch"><xsl:value-of select="@ch"/></td>
          </xsl:when>
          <xsl:otherwise>
            <td class="code-entry">&#160;</td>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:if>
      <td class="code-entry">
        <xsl:choose>
          <xsl:when test="@desc"><xsl:value-of select="@desc"/></xsl:when>
          <xsl:otherwise>&#160;</xsl:otherwise>
        </xsl:choose>
      </td>
    </tr>
  </xsl:template>

  <xsl:template name="hex-convert">
    <xsl:param name="num"/>
    <xsl:variable name="hex" select="'0123456789ABCDEF'"/>
    <xsl:choose>
      <xsl:when test="$num &gt;= 4096">
        <xsl:value-of select="concat(substring($hex,floor($num div 4096)+1,1),
            substring($hex,(floor($num div 256) mod 16)+1,1),
            substring($hex,(floor($num div 16) mod 16)+1,1),
            substring($hex,($num mod 16)+1,1))"/>
      </xsl:when>
      <xsl:when test="$num &gt;= 256">
        <xsl:value-of select="concat(substring($hex,floor($num div 256)+1,1),
            substring($hex,(floor($num div 16) mod 16)+1,1),
            substring($hex,($num mod 16)+1,1))"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="concat(substring($hex,floor($num div 16)+1,1),
            substring($hex,($num mod 16)+1,1))"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>
