<?xml version="1.0" encoding="utf-8" ?>
<!--
   vim: et ts=3 sts=3 sw=3
-->

<xsl:transform version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:func="http://exslt.org/functions"
   xmlns:dyn="http://exslt.org/dynamic"
   xmlns:my="http://www.example.com/my"
   extension-element-prefixes="xsl func dyn my"
   >

   <xsl:output method="html" />

   <!-- Command-line argument: stringparams primary-language "en" -->
   <xsl:param name="primary-language">x-asha</xsl:param>

   <xsl:variable name="order-by">
      <xsl:choose>
         <xsl:when test="$primary-language = 'x-asha'">lexeme-sort</xsl:when>
         <xsl:when test="$primary-language = 'en'">gloss-sort</xsl:when>
         <xsl:otherwise>
            <xsl:message terminate="yes">
               $primary-language must be "x-asha" or "en".
               Use --stringparams primary-language "en".
            </xsl:message>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:variable>
   <xsl:variable name="display-by">
      <xsl:choose>
         <xsl:when test="$primary-language = 'x-asha'">lexeme</xsl:when>
         <xsl:when test="$primary-language = 'en'">gloss</xsl:when>
      </xsl:choose>
   </xsl:variable>

   <xsl:key name="distinct-alphabet" match="entry" use="my:lc(my:first-letter(dyn:evaluate($display-by)))" />
   <xsl:key name="lexemes" match="entry" use="lexeme" />

   <xsl:template match="person">
       <xsl:comment>
           <xsl:value-of select="@role" />: <xsl:value-of select="name" />
           &lt;<xsl:value-of select="email" />&gt;
           <xsl:value-of select="url" />
       </xsl:comment>
   </xsl:template>

   <xsl:template match="lexicon">
      <h2>Main Entries</h2>
      <p>
         <xsl:value-of select="count(entry[not(@type)])" /> entries.
         Browse:
         <xsl:for-each select="entry[not(@type) and generate-id() =
            generate-id(key('distinct-alphabet', my:lc(my:first-letter(dyn:evaluate($display-by)))))]">
            <xsl:sort select="my:first-letter(dyn:evaluate($order-by))" />
            <xsl:variable name="first-letter" select="my:lc(my:first-letter(dyn:evaluate($display-by)))" />
            <a href="{concat('#main-',$first-letter)}">
               <xsl:value-of select="$first-letter" />
            </a>
            <xsl:text> </xsl:text>
         </xsl:for-each>
      </p>

      <xsl:for-each select="entry[not(@type) and generate-id() =
         generate-id(key('distinct-alphabet',my:lc(my:first-letter(dyn:evaluate($display-by)))))]">
         <xsl:sort select="my:first-letter(dyn:evaluate($order-by))"/>
         <xsl:variable name="first-letter" select="my:lc(my:first-letter(dyn:evaluate($display-by)))" />
         <h3>
            <a name="{concat('main-',$first-letter)}">
               <xsl:text> </xsl:text>
            </a>
            <xsl:value-of select="$first-letter"/>
         </h3>
         <ul class="lexemes">
            <xsl:for-each select="key('distinct-alphabet',$first-letter)">
               <xsl:sort select="my:uc(dyn:evaluate($order-by))" />
               <xsl:if test="not(@type)">
                  <xsl:choose>
                     <xsl:when test="$primary-language = 'x-asha'">
                        <xsl:apply-templates select="." mode="x-asha" />
                     </xsl:when>
                     <xsl:when test="$primary-language = 'en'">
                        <xsl:apply-templates select="." mode="en" />
                     </xsl:when>
                  </xsl:choose>
               </xsl:if>
            </xsl:for-each>
         </ul>
      </xsl:for-each>

      <xsl:call-template name="section">
         <xsl:with-param name="title" select="'Proper Names'" />
         <xsl:with-param name="type" select="'name'" />
      </xsl:call-template>

      <xsl:call-template name="section">
         <xsl:with-param name="title" select="'Nonsense Words'" />
         <xsl:with-param name="type" select="'nonsense'" />
      </xsl:call-template>
   </xsl:template>

   <xsl:template name="section">
      <xsl:param name="title" />
      <xsl:param name="type" />
      <xsl:variable name="count" select="count(dyn:evaluate('entry[@type=$type]'))" />

      <xsl:if test="$count &gt; 0">
         <xsl:text>

         </xsl:text>

         <h2><xsl:value-of select="$title" /></h2>
         <p>
            <xsl:value-of select="count(dyn:evaluate('entry[@type=$type]'))" /> entries.
         </p>
         <ul class="lexemes">
            <xsl:choose>
               <xsl:when test="$primary-language = 'x-asha'">
                  <xsl:apply-templates select="dyn:evaluate('entry[@type=$type]')" mode="x-asha">
                     <xsl:sort select="my:uc(dyn:evaluate($order-by))" />
                  </xsl:apply-templates>
               </xsl:when>
               <xsl:when test="$primary-language = 'en'">
                  <xsl:apply-templates select="dyn:evaluate('entry[@type=$type]')" mode="en">
                     <xsl:sort select="my:uc(dyn:evaluate($order-by))" />
                  </xsl:apply-templates>
               </xsl:when>
            </xsl:choose>
         </ul>
      </xsl:if>
   </xsl:template>

   <xsl:template match="entry" mode="x-asha">
      <xsl:text>
      </xsl:text>
      <li>
         <xsl:apply-templates select="lexeme" />
         <xsl:apply-templates select="kateinu[not(@private)]" />
         <xsl:apply-templates select="ipa[not(@private)]" />
         <xsl:apply-templates select="word-class[not(@private)]" />

         <xsl:choose>
            <xsl:when test="string-length(definition) &gt; 0">
               <xsl:apply-templates select="definition[not(@private)]" />
            </xsl:when>
            <xsl:otherwise>
               <xsl:apply-templates select="gloss[not(@private)]" />
            </xsl:otherwise>
         </xsl:choose>
         <xsl:text> </xsl:text>

         <xsl:apply-templates select="." mode="common" />
      </li>
   </xsl:template>

   <xsl:template match="entry" mode="en">
      <xsl:text>
      </xsl:text>
      <li>
         <span class="english lexeme">
            <xsl:apply-templates select="gloss" />
         </span>
         <xsl:if test="string-length(definition) &gt; 0">
            (Full definition: <span class="english">
               <xsl:apply-templates select="definition" />
            </span>)
         </xsl:if>

         <xsl:apply-templates select="word-class" />
         <xsl:apply-templates select="lexeme" />
         <xsl:apply-templates select="kateinu" />
         <xsl:apply-templates select="ipa" />

         <xsl:apply-templates select="." mode="common" />
      </li>
   </xsl:template>

   <xsl:template match="entry" mode="common">
      <xsl:apply-templates select="literal" />

      <xsl:if test="string-length(note[@type='usage' or @type='general' or @type='etymology']) &gt; 0">
         <xsl:apply-templates
            select="(note[(@type='usage' or @type='general' or @type='etymology') and not(@private)])" />
      </xsl:if>

      <xsl:call-template name="each-xref">
         <xsl:with-param name="class" select="'synonym'" />
         <xsl:with-param name="abbreviation" select="'Syn'" />
      </xsl:call-template>

      <xsl:call-template name="each-xref">
         <xsl:with-param name="class" select="'antonym'" />
         <xsl:with-param name="abbreviation" select="'Ant'" />
      </xsl:call-template>

      <xsl:call-template name="each-xref">
         <xsl:with-param name="class" select="'see'" />
         <xsl:with-param name="abbreviation" select="'See also'" />
      </xsl:call-template>

      <xsl:call-template name="each-xref">
         <xsl:with-param name="class" select="'etymology'" />
         <xsl:with-param name="abbreviation" select="'Etym'" />
      </xsl:call-template>

      <xsl:apply-templates select="example" />

      <xsl:if test="count(subentry) &gt; 0">
         <ul class="subentry">
            <xsl:call-template name="subentry">
               <xsl:with-param name="entry" select="subentry" />
            </xsl:call-template>
         </ul>
      </xsl:if>
   </xsl:template>

   <xsl:template match="lexeme">
      <span class="lexeme">
      <span class="ashaille">
         <a>
            <xsl:attribute name="name">
               <xsl:call-template name="anchorize">
                  <xsl:with-param name="text" select="." />
               </xsl:call-template>
            </xsl:attribute>
            <xsl:text> </xsl:text>
         </a>
         <xsl:value-of select="." />
      </span>
      </span>
      <xsl:text> </xsl:text>
   </xsl:template>

   <xsl:template match="ipa">
      <span class="ipa">[<xsl:value-of select="." />]</span>
      <xsl:text> </xsl:text>
   </xsl:template>

   <xsl:template match="word-class">
      <span class="pos"><xsl:value-of select="." />.</span>
      <xsl:text> </xsl:text>
   </xsl:template>

   <xsl:template match="definition">
      <span class="english definition">
         <xsl:if test="count(../definition) &gt; 1">
            <xsl:call-template name="sn">
               <xsl:with-param name="number">
                  <xsl:value-of select="position()" />
               </xsl:with-param>
            </xsl:call-template>
         </xsl:if>
         <xsl:apply-templates />
         <xsl:text>.</xsl:text>
      </span>
   </xsl:template>

   <xsl:template match="gloss">
      <span class="english definition">
         <xsl:if test="count(../gloss) &gt; 1">
            <xsl:call-template name="sn">
               <xsl:with-param name="number">
                  <xsl:value-of select="position()" />
               </xsl:with-param>
            </xsl:call-template>
         </xsl:if>
         <xsl:apply-templates />
         <xsl:text>. </xsl:text>
      </span>
   </xsl:template>

   <xsl:template match="literal">
      <span class="literal">
         Lit: <xsl:apply-templates />
         <xsl:text>. </xsl:text>
      </span>
   </xsl:template>

   <xsl:template match="note[@type='general']">
      <span class="notes"><strong>Notes: </strong>
         <xsl:apply-templates />
         <xsl:text>. </xsl:text>
      </span>
   </xsl:template>

   <xsl:template match="note[@type='usage']">
      <span class="notes"><strong>Usage: </strong>
         <xsl:apply-templates />
         <xsl:text>. </xsl:text>
      </span>
   </xsl:template>

   <xsl:template match="note[@type='etymology']">
      <span class="notes"><strong>Etymology: </strong>
         <xsl:apply-templates />
         <xsl:text>. </xsl:text>
      </span>
   </xsl:template>

   <xsl:template match="disc">
      <span class="notes extra-info"> Discussion:
         <xsl:apply-templates />
         <xsl:text>. </xsl:text>
      </span>
   </xsl:template>

   <xsl:template name="subentry">
      <li>
         <xsl:apply-templates select="subentry/*" />
      </li>
   </xsl:template>

   <xsl:template name="each-xref">
      <xsl:param name="class" />
      <xsl:param name="abbreviation" />

      <xsl:if test="count(xref[@type=$class and not(@private)])">
         <span class="{$class}">
            <xsl:value-of select="concat($abbreviation,': ')" />
            <xsl:apply-templates select="xref[@type=$class]" mode="collect" />
         </span>
      </xsl:if>
   </xsl:template>

   <xsl:template match="xref" mode="collect">
      <xsl:call-template name="gloss-lookup">
         <xsl:with-param name="lexeme">
            <xsl:value-of select="."/>
         </xsl:with-param>
      </xsl:call-template>

      <xsl:choose>
         <xsl:when test="position() = last()">
            <xsl:text>. </xsl:text>
         </xsl:when>
         <xsl:otherwise>
            <xsl:text>, </xsl:text>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:template>

   <xsl:template name="gloss-lookup">
      <xsl:param name="lexeme" />
      <xsl:variable name="gloss" select="key('lexemes', current())/gloss" />
      <xsl:variable name="anchor">
         <xsl:call-template name="anchorize">
            <xsl:with-param name="text" select="." />
         </xsl:call-template>
      </xsl:variable>

      <xsl:choose>
         <xsl:when test="string-length($gloss) &gt; 0">
            <span class="ashaille">
               <a href="{concat('#',$anchor)}"><xsl:value-of select="$lexeme" /></a>
            </span>
            <xsl:if test="my:uc($gloss) != my:uc($lexeme)">
               <xsl:text> </xsl:text>
               <span class="english xref">
                  <xsl:value-of select="$gloss" />
               </span>
            </xsl:if>
         </xsl:when>
         <xsl:otherwise>
            <span class="ashaille">
               <xsl:value-of select="$lexeme" />
            </span>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:template>

   <xsl:template match="es">
      <span class="etymology">
         <xsl:apply-templates />
         <xsl:text>. </xsl:text>
      </span>
   </xsl:template>

   <xsl:template match="example/text[@lang='x-asha']">
      <span class="example">
         Ex: <span class="ashaille"> "<xsl:apply-templates />"
         </span>
         <xsl:text> </xsl:text>
      </span>
   </xsl:template>

   <xsl:template match="example/text[@lang='en']">
      <span class="example">
         <span class="english">
            <xsl:apply-templates />
         </span>
         <xsl:text> </xsl:text>
      </span>
   </xsl:template>

   <xsl:template match="example/text[@lang='en-literal']">
      <span class="example">
         <span class="english">
            lit.
            <span class="literal">
               <xsl:apply-templates />
               <xsl:text> </xsl:text>
            </span>
         </span>
      </span>
   </xsl:template>

   <xsl:template match="date">
      <span class="date">[<xsl:value-of select="." />]</span>
   </xsl:template>

   <xsl:template match="xref/lexeme">
      <span class="{@class}">
         <xsl:apply-templates />
      </span>
   </xsl:template>

   <xsl:template match="xref/lexeme[not(@lang)]">
      <xsl:variable name="word" select="." />
      <xsl:variable name="gloss" select="translate(
         /lexicon/entry[lexeme=$word]/gloss,
         'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
         'abcdefghijklmnopqrstuvwxyz')" />

      <xsl:choose>
         <xsl:when test="$gloss">
            <xsl:variable name="anchor">
               <xsl:call-template name="anchorize">
                  <xsl:with-param name="text" select="." />
               </xsl:call-template>
            </xsl:variable>
            <a class="extra-info" href="{concat('#',$anchor)}">
               <xsl:value-of select="$word" />
               <span class="extra-info"> "<xsl:value-of select="$gloss" />"</span>
            </a>
         </xsl:when>
         <xsl:otherwise>
            <xsl:value-of select="$word" />
            <span class="note"> (not defined)</span>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:template>

   <xsl:template match="sn" name="sn">
      <xsl:param name="number" select="." />
      <span class="sense-number"><xsl:value-of select="$number" />. </span>
   </xsl:template>

   <xsl:template match="lang">
      <xsl:choose>
         <xsl:when test="@class = 'x-asha' and not(parent::definition)">
            <xsl:call-template name="gloss-lookup">
               <xsl:with-param name="lexeme">
                  <xsl:value-of select="."/>
               </xsl:with-param>
            </xsl:call-template>
         </xsl:when>
         <xsl:when test="@class = 'x-asha'">
            <span class="ashaille"><xsl:value-of select="." /></span>
         </xsl:when>
         <xsl:when test="@class = 'en'">
            <span class="english"><xsl:value-of select="." /></span>
         </xsl:when>
         <xsl:otherwise>
            <span class="{@class}"><xsl:value-of select="." /></span>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:template>

   <!-- FIELDS TO NOT DISPLAY -->
   <xsl:template match="date">
   </xsl:template>

   <xsl:template match="a">
      <a href="{@href}"><xsl:value-of select="." /></a>
   </xsl:template>

   <xsl:template match="kateinu">
      <span class="kateinu" style="display: none">
         <xsl:value-of select='.' />
      </span>
   </xsl:template>

   <!-- UTILITY FUNCTIONS -->

   <xsl:template name="anchorize">
      <xsl:param name="text" />
      <xsl:value-of select='translate(translate($text, " ?&apos;():", ""),
         "&#168;¨", "__")' />
   </xsl:template>

   <xsl:variable name="lowercase"
      select="'abcdefghijklmnopqrstuvwxyzáéíóúäëïöüàèìòùîǎěǐǒǔ'"/>
   <xsl:variable name="uppercase"
      select="'ABCDEFGHIJKLMNOPQRSTUVWXYZÁÉÍÚÓÄËÏÖÜÀÈÌÒÙÎǍĚǏǑǓ'"/>

   <func:function name="my:lc">
      <xsl:param name="text" />
      <func:result>
         <xsl:value-of
            select="concat(translate(substring($text,1,1),$uppercase,$lowercase),
            substring($text,2,string-length($text)))"/>
      </func:result>
   </func:function>

   <func:function name="my:uc">
      <xsl:param name="text" />
      <func:result>
         <xsl:value-of
            select="concat(translate(substring($text,1,1),$lowercase,$uppercase),
            substring($text,2,string-length($text)))"/>
      </func:result>
   </func:function>

   <func:function name="my:first-letter">
      <xsl:param name="word" />
      <xsl:variable name="first" select="substring($word,1,1)" />
      <xsl:variable name="result">
         <xsl:choose>
            <xsl:when test="$first = '&#168;' or $first = '¨' or $first = '*'">
               <xsl:text>_</xsl:text> <!-- must be CDATA for anchor -->
            </xsl:when>
            <xsl:when test='$first = "-" or $first = ":" or $first = "(" or $first = "&apos;"'>
               <xsl:value-of
                  select="my:first-letter(substring($word,2,string-length($word)))" />
            </xsl:when>
            <xsl:when test="$first = 'Á' or $first = 'À' or $first = 'Ä' or $first = 'Ǎ'">A</xsl:when>
            <xsl:when test="$first = 'á' or $first = 'à' or $first = 'ä' or $first = 'ǎ'">a</xsl:when>
            <xsl:when test="$first = 'É' or $first = 'È' or $first = 'Ë' or $first = 'Ě'">E</xsl:when>
            <xsl:when test="$first = 'é' or $first = 'è' or $first = 'ë' or $first = 'ě'">e</xsl:when>
            <xsl:when test="$first = 'Í' or $first = 'Ì' or $first = 'Ï' or $first = 'Ǐ'">I</xsl:when>
            <xsl:when test="$first = 'í' or $first = 'ì' or $first = 'ï' or $first = 'ǐ'">i</xsl:when>
            <xsl:when test="$first = 'î́' or $first = 'î̀'">î</xsl:when>
            <xsl:when test="$first = 'Ó' or $first = 'Ò' or $first = 'Ö' or $first = 'Ǒ'">O</xsl:when>
            <xsl:when test="$first = 'ó' or $first = 'ò' or $first = 'ö' or $first = 'ǒ'">o</xsl:when>
            <xsl:when test="$first = 'Ú' or $first = 'Ù' or $first = 'Ü' or $first = 'Ǔ'">U</xsl:when>
            <xsl:when test="$first = 'ú' or $first = 'ù' or $first = 'ü' or $first = 'ǔ'">u</xsl:when>
            <xsl:otherwise>
               <xsl:value-of select="$first" />
            </xsl:otherwise>
         </xsl:choose>
      </xsl:variable>
      <xsl:if test="$result = '-'">
         <xsl:message>
            <xsl:value-of select="$word" />
         </xsl:message>
      </xsl:if>

      <func:result select="$result" />
   </func:function>

</xsl:transform>

