<!DOCTYPE xsl:stylesheet [
<!--* 
  DOCTYPE xsl:stylesheet PUBLIC 'http://www.w3.org/1999/XSL/Transform'
      '/SGML/Public/W3C/xslt10.dtd' [
    *-->
<!ENTITY lt     "&#38;#60;"      ><!--=less-than sign R:-->
<!ENTITY gt     ">"      ><!--=greater-than sign R:-->
<!ENTITY ldquo "&#8220;"><!-- left double quotation mark,
                              U+201C ISOnum -->
<!ENTITY rdquo "&#8221;"><!-- right double quotation mark,
                              U+201D ISOnum -->
<!ENTITY lsquo "&#8216;">
<!ENTITY rsquo "&#8217;">
<!ENTITY nbsp "&#160;">
<!ENTITY ltri   "&#x25C3;" ><!--/triangleleft B: l triangle, open-->
<!ENTITY rtri   "&#x25B9;" ><!--/triangleright B: r triangle, open-->
<!ENTITY equiv  "&#x2261;" ><!--/equiv R: =identical with-->
<!ENTITY rarr   "&#x2192;" ><!--/rightarrow /to A: =rightward arrow-->

<!ENTITY txtnl "<xsl:text xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>&#xA;</xsl:text>">
]>
<xsl:stylesheet 
		version="1.0"
		xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
 <xsl:import href="tltohtml.xsl"/>

 <!--* scraps *-->
 <xsl:template match="scrap">
  <xsl:element name="div">
   <xsl:attribute name="style">margin-top: 1em</xsl:attribute>
   <!--* Scrap heading *-->
   <xsl:element name="strong">
    <xsl:element name="a">
     <xsl:attribute name="name">
      <xsl:value-of select="generate-id(.)"/>
     </xsl:attribute>
     <xsl:text>&lt; </xsl:text>
     <xsl:call-template name="getscrapnum"/>
     <xsl:text> </xsl:text>
     <xsl:value-of select="@name"/>
     <xsl:if test="@file">
      <xsl:text> [File </xsl:text>
      <xsl:value-of select="@file"/>
      <xsl:text>] </xsl:text>
     </xsl:if>
     <xsl:if test="@prev">
      <xsl:text> [continues </xsl:text>
      <xsl:element name="a">
       <xsl:attribute name="href">
	<xsl:text>#</xsl:text>
	<xsl:value-of select="generate-id(id(@prev))"/>
       </xsl:attribute>
       <xsl:apply-templates select="id(@prev)" mode="getscrapnumref"/>
      </xsl:element>
      <xsl:text>] </xsl:text>
     </xsl:if>
     <xsl:text> &gt; &equiv;</xsl:text>
    </xsl:element>
   </xsl:element>
   <!--* Scrap body *-->
   <xsl:element name="pre">
    <xsl:attribute name="style">margin-left: 2em</xsl:attribute>
    <xsl:apply-templates/>
   </xsl:element>
   <!--* Scrap trailer:  related scraps *-->
   <xsl:call-template name="getcontinuations">
    <xsl:with-param name="headofchain" select="@id"/>
   </xsl:call-template>
   <xsl:call-template name="getinbound">
    <xsl:with-param name="calledscrap" select="@id"/>
   </xsl:call-template>
  </xsl:element>
 </xsl:template>

 <!--* Named templates for getting references to scraps *-->
 <xsl:template name="getscrapnum" mode="getscrapnum" match="scrap">
  <xsl:number level="any" from="/" count="scrap"/>
 </xsl:template>

 <xsl:template name="getscrapref" mode="getscrapref" match="scrap">
  <xsl:choose>
   <xsl:when test="./@name">
    <xsl:value-of select="@name"/>
   </xsl:when>
   <xsl:otherwise>
    <xsl:if test="./@file">
     <xsl:text>[File </xsl:text>
     <xsl:value-of select="@file"/>
     <xsl:text>]</xsl:text>
    </xsl:if>
   </xsl:otherwise>
  </xsl:choose>
  <xsl:text> </xsl:text>
  <!--*
    <xsl:if test="@file">
      <xsl:text> [File </xsl:text>
        <xsl:value-of select="@file"/>
      <xsl:text>] </xsl:text>
    </xsl:if>
  *-->
  <xsl:number level="any" from="/" count="scrap"/>
 </xsl:template>

 <xsl:template name="getscrapnumref" mode="getscrapnumref" match="scrap">
  <xsl:number level="any" from="/" count="scrap"/>
  <xsl:text> </xsl:text>
  <xsl:choose>
   <xsl:when test="./@name">
    <xsl:value-of select="@name"/>
   </xsl:when>
   <xsl:otherwise>
    <xsl:if test="./@file">
     <xsl:text>[File </xsl:text>
     <xsl:value-of select="@file"/>
     <xsl:text>]</xsl:text>
    </xsl:if>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <!--* Named templates for getting lists of related scraps *-->
  <xsl:template name="getcontinuations">
  <xsl:param name="headofchain">foo</xsl:param>
  
  <xsl:element name="span">
   <xsl:attribute name="style">font-size:smaller</xsl:attribute>
   <xsl:choose>
    <xsl:when test="//scrap[@prev=$headofchain]">
     <xsl:text>Continued in </xsl:text>
     <xsl:for-each select="//scrap[@prev=$headofchain]">
      <xsl:if test="position() > 1"><xsl:text>, </xsl:text></xsl:if>
      <xsl:text>&lt; </xsl:text>
      <xsl:element name="a">
       <xsl:attribute name="href">
        <xsl:text>#</xsl:text><xsl:value-of select="generate-id(..)"/>
       </xsl:attribute>
       <xsl:apply-templates select="." mode="getscrapref"/>
      </xsl:element>
      <xsl:text> &gt; </xsl:text>
     </xsl:for-each>
    </xsl:when>
    <xsl:otherwise>
    </xsl:otherwise>
   </xsl:choose>
  </xsl:element>
  <xsl:element name="br"/>
 </xsl:template>
 
 <xsl:template name="getinbound">
  <xsl:param name="calledscrap">foo</xsl:param>
  
  <xsl:element name="span">
   <xsl:attribute name="style">font-size:smaller</xsl:attribute>
   <xsl:choose>
    <xsl:when test="//scrap/ptr[@target=$calledscrap] or
	     //scrap/ref[@target=$calledscrap] ">
     <xsl:text>This code is used in </xsl:text>
     <xsl:for-each select="//scrap/ptr[@target=$calledscrap] |
		   //scrap/ref[@target=$calledscrap]">
      <xsl:text>&lt; </xsl:text>
      <xsl:element name="a">
       <xsl:attribute name="href">
        <xsl:text>#</xsl:text><xsl:value-of select="generate-id(..)"/>
       </xsl:attribute>
       <xsl:apply-templates select=".." mode="getscrapref"/>
      </xsl:element>
      <xsl:text> &gt; </xsl:text>
     </xsl:for-each>
    </xsl:when>
    <xsl:when test="./@prev">
     <!--*
     <xsl:text>This code is a continuation of </xsl:text>
     <xsl:element name="a">
      <xsl:attribute name="href">
       <xsl:text>#</xsl:text>
       <xsl:value-of select="generate-id(id(@prev))"/>
      </xsl:attribute>
      <xsl:apply-templates select="id(@prev)" mode="getscrapnumref"/>
     </xsl:element>
     *-->
     <!--* we could call getinbound with calledscrap=@prev, but it
         * looks odd, so I've cut it back out.
         *-->
    </xsl:when>
    <xsl:when test="./@file">
    <!--*
     <xsl:text>This code is written out to </xsl:text>
     <xsl:element name="i">
      <xsl:value-of select="@file"/>
     </xsl:element>
     <xsl:text>. </xsl:text>
    *-->
    </xsl:when>
    <xsl:otherwise>
     <xsl:text>This code is not used elsewhere.</xsl:text>
    </xsl:otherwise>
   </xsl:choose>
  </xsl:element>
  <xsl:element name="br"/>  <xsl:element name="br"/>
 </xsl:template>

 <!--* Elements inside scraps *-->
 <xsl:template match="scrap/ptr | scrap/ref">
  <xsl:text>{</xsl:text>
  <xsl:element name="em">
   <xsl:attribute name="style">
    <xsl:text>font-family: New Times Roman, serif, Lucida Sans Unicode;</xsl:text>
   </xsl:attribute>
   <xsl:element name="a">
    <xsl:attribute name="href">#<xsl:value-of select="generate-id(id(@target))"/>
    </xsl:attribute>
    <xsl:choose>
     <xsl:when test="id(@target)">
      <xsl:apply-templates select="id(@target)" mode="getscrapref"/>
     </xsl:when>
     <xsl:otherwise>
      <xsl:value-of select="."/>
      <xsl:text> !! BAD REFERENCE! No scrap called </xsl:text>
      <xsl:value-of select="@target"/>
      <xsl:text>!</xsl:text>
     </xsl:otherwise>
    </xsl:choose>
   </xsl:element>
  </xsl:element>
  <xsl:text>}&#xA;</xsl:text>
 </xsl:template>

 <xsl:template match="scrap/lb">
  <xsl:element name="br"/>
 </xsl:template>

 <!--* Index generation *-->
 <xsl:template match="divGen[@type='index-filenames']">
  <xsl:choose>
   <xsl:when test="//scrap[@file]">
    <xsl:element name="ul">
     <xsl:for-each select="//scrap/@file">
      <xsl:sort/>
      <xsl:element name="li">
       <xsl:value-of select="."/>
       <xsl:call-template name="get-scraps-by-file">
	<xsl:with-param name="fn"><xsl:value-of select="."/></xsl:with-param>
       </xsl:call-template>
      </xsl:element>
     </xsl:for-each>
    </xsl:element>
   </xsl:when>
   <xsl:otherwise>
    <xsl:element name="p">[No files are generated by this web.]</xsl:element>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <xsl:template name="get-scraps-by-file">
  <xsl:param name="fn">this is not a file name</xsl:param>
  <xsl:text>: defined in </xsl:text>
  <xsl:for-each select="//scrap[@file=$fn]">
   <xsl:if test="position() > 1"><xsl:text>, </xsl:text></xsl:if>
   <xsl:text>&lt; </xsl:text>
   <xsl:element name="a">
    <xsl:attribute name="href">
     <xsl:text>#</xsl:text><xsl:value-of select="generate-id(.)"/>
    </xsl:attribute>
    <xsl:apply-templates select="." mode="getscrapnumref"/>
   </xsl:element>
   <xsl:text> &gt; </xsl:text>
  </xsl:for-each>
 </xsl:template>


 <xsl:template match="divGen[@type='index-scrapnames']">
  <xsl:choose>
   <xsl:when test="//scrap">
    <xsl:element name="ul">
     <xsl:for-each select="//scrap">
      <xsl:sort select="@name"/>
      <xsl:element name="li">
       <xsl:element name="a">
	<xsl:attribute name="href">#<xsl:value-of select="generate-id()"/></xsl:attribute>
	<xsl:call-template name="getscrapref"/>
       </xsl:element>
      </xsl:element>
     </xsl:for-each>
    </xsl:element>
   </xsl:when>
   <xsl:otherwise>
    <xsl:element name="p">[No scraps are present in this web.]</xsl:element>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <xsl:template match="divGen[@type='revision-history']">
  <xsl:choose>
   <xsl:when test="/TEI.2/teiHeader/revisionDesc/list">
    <xsl:apply-templates select="/TEI.2/teiHeader/revisionDesc/list"/>
   </xsl:when>
   <xsl:otherwise>
    <xsl:element name="p">[There is no revision history in the header
     of this document.]</xsl:element>
   </xsl:otherwise>
  </xsl:choose>
 </xsl:template>

 <!--* Phrase-level elements (Poor Folks' Docbook?) *-->
 <xsl:template match="att">
  <xsl:element name="em">
   <xsl:apply-templates/>
  </xsl:element>
 </xsl:template>
 <xsl:template match="tag">
  <xsl:element name="tt">
   <xsl:text>&lt;</xsl:text>
   <xsl:apply-templates/>
   <xsl:text>&gt;</xsl:text>
  </xsl:element>
 </xsl:template>
 <xsl:template match="code[@lang='sgml']">
  <xsl:element name="tt">
   <xsl:text>&lt;</xsl:text>
   <xsl:apply-templates/>
   <xsl:text>&gt;</xsl:text>
  </xsl:element>
 </xsl:template>

</xsl:stylesheet>
<!-- Keep this comment at the end of the file
Local variables:
mode: xml
sgml-default-dtd-file:(concat sgmlvol "/SGML/Public/Emacs/xslt.ced")
sgml-omittag:t
sgml-shorttag:t
sgml-indent-data:t
sgml-indent-step:1
End:
-->
