<xsl:stylesheet
  version="1.0"
  xmlns=""
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  exclude-result-prefixes="#default"
>

<!-- ===============================================================

     Converts ESV XML into XHTML.

     Loosely based on the PHP script on the ESV website with added
     footnote handling.  Note that the XML must have been formatted
     with the "base-element=paragraph" option for this to work.

     Version 1.0 BPE 29/07/2003

     =============================================================== -->

<xsl:output
    method="xml"
    omit-xml-declaration="yes"
    encoding="utf-8"
    indent="yes"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
    doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
/>

<!-- This is provided by the calling script: the stylesheet name -->
<xsl:param name="style"/>

<!-- The name of the main script (used to make links) -->
<xsl:param name="script" select="'lookup.php'"/>


<!-- Main page template ============================================ -->
<xsl:template match="/">

  <html xmlns="http://www.w3.org/1999/xhtml">

  <!-- Page header section with title and stylesheet -->
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <link rel="stylesheet" type="text/css" href="{$style}"/>
    <title><xsl:apply-templates select="//passage/reference"/></title>
  </head>

  <!-- Page body -->
  <body>

    <!-- Title: the Bible reference of the passage being viewed -->
    <h2>
      <xsl:apply-templates select="//passage/reference"/>
    </h2>

    <!-- Do the passage text -->
    <div id="text">
      <xsl:apply-templates select="//passage/content"/>
    </div>

    <!-- Do footnotes section -->
    <xsl:if test="count(//footnote)">
      <div id="footnotes">
        <h3>Footnotes</h3>
        <ul>
          <xsl:apply-templates select="//footnote" mode="footnotes"/>
        </ul>
      </div>
    </xsl:if>

    <!-- Navigation -->
    <div id="navig">
      <table>
        <xsl:apply-templates select="//passage/surrounding-chapters/*"/>
      </table>
    </div>

    <!-- Do the copyright notice -->
    <div id="copyright">
      <xsl:apply-templates select="//copyright"/>
    </div>

    <!-- link to home page -->
    <div id="gohome">
      <p><a href="http://www.edginet.org/">Ben's homepage</a></p>
    </div>

  </body>

  </html>

</xsl:template>

<!-- =============================================================== -->


<xsl:template match="content">

  <xsl:variable name="length" select="string-length(.) div 2"/>

  <xsl:variable name="break">
    <xsl:call-template name="find-break">
      <xsl:with-param name="position" select="1"/>
      <xsl:with-param name="total-length" select="$length"/>
    </xsl:call-template>
  </xsl:variable>

  <table>
    <tr>
      <xsl:variable name="col-1" select="verse-unit[position() &lt; $break]"/>
      <td id="col-1">
        <xsl:apply-templates select="$col-1"/>
        <!-- Fix-up any unclosed paragraphs -->
        <xsl:if test="count($col-1//begin-paragraph) > count($col-1//end-paragraph)">
          <xsl:text disable-output-escaping="yes">&lt;/p></xsl:text>
        </xsl:if>
      </td>

      <xsl:variable name="col-2" select="verse-unit[position() >= $break]"/>
      <td id="col-2">
        <!-- Fix-up any unopened paragraphs -->
        <xsl:if test="count($col-2//end-paragraph) > count($col-2//begin-paragraph)">
          <xsl:text disable-output-escaping="yes">&lt;p class="fake"></xsl:text>
        </xsl:if>
        <xsl:apply-templates select="$col-2"/>
      </td>
    </tr>
  </table>
</xsl:template>

<xsl:template name="find-break">
  <xsl:param name="position"/>
  <xsl:param name="length" select="0"/>
  <xsl:param name="total-length"/>

  <xsl:choose>

    <xsl:when test="$total-length > $length">
      <xsl:variable name="verse" select="(//content/verse-unit)[$position]"/>

      <xsl:call-template name="find-break">
        <xsl:with-param name="position" select="$position + 1"/>
        <xsl:with-param name="length" select="$length + string-length($verse)"/>
        <xsl:with-param name="total-length" select="$total-length"/>
      </xsl:call-template>
    </xsl:when>

    <xsl:otherwise>
      <xsl:value-of select="$position"/>
    </xsl:otherwise>

  </xsl:choose>

</xsl:template>

<!-- Templates for the various elements we are keeping ============= -->

<!-- This is the Bible reference for the text we are looking at -->
<xsl:template match="reference">
  <xsl:apply-templates/>
</xsl:template>

<!-- Navigation links -->
<xsl:template match="previous|current|next">
  <td id="{name()}">
    <xsl:if test="text()">
      <a href="{$script}?passage={text()}" title="{text()}">
        <xsl:if test="name()='previous'">Previous chapter</xsl:if>
        <xsl:if test="name()='current'">This chapter</xsl:if>
        <xsl:if test="name()='next'">Next chapter</xsl:if>
      </a>
    </xsl:if>
    <xsl:text> </xsl:text> <!-- prevent span tags from being abbreviated -->
  </td>
</xsl:template>

<!-- Top level section headings -->
<xsl:template match="heading">
  <h3>
    <xsl:call-template name="copy-attributes"/>
    <xsl:apply-templates/>
  </h3>
</xsl:template>

<!-- Next level section headings -->
<xsl:template match="subheading">
  <h4>
    <xsl:call-template name="copy-attributes"/>
    <xsl:apply-templates/>
  </h4>
</xsl:template>

<xsl:template match="begin-paragraph">
  <xsl:text disable-output-escaping="yes">&lt;p</xsl:text>
  <!-- copy any class info -->
  <xsl:if test="@class">
    <xsl:text> class="</xsl:text>
    <xsl:value-of select="@class"/>
    <xsl:text>"</xsl:text>
  </xsl:if>
  <xsl:text disable-output-escaping="yes">></xsl:text>
</xsl:template>

<xsl:template match="end-paragraph">
  <xsl:text disable-output-escaping="yes">&lt;/p></xsl:text>
</xsl:template>

<!--
    This is not as general as the PHP script, but is sufficient
    according to the DTD
-->
<xsl:template match="begin-line">

  <xsl:text disable-output-escaping="yes">&lt;span</xsl:text>
  <xsl:choose>
    <xsl:when test="@class='indent'">
      <xsl:text> class="indent1"</xsl:text>
    </xsl:when>
    <xsl:when test="@class='indent-2'">
      <xsl:text> class="indent2"</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text> class="indent0"</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:text disable-output-escaping="yes">></xsl:text>

  <!--
  <xsl:if test="ancestor::woc">
    <xsl:text disable-output-escaping="yes">&lt;span class="woc"></xsl:text>
  </xsl:if>
  -->

</xsl:template>

<!-- Line breaks -->
<xsl:template match="end-line">

  <!--
  <xsl:if test="ancestor::woc">
    <xsl:text disable-output-escaping="yes">&lt;/span></xsl:text>
  </xsl:if>
  -->

  <xsl:text disable-output-escaping="yes">&lt;/span></xsl:text>

</xsl:template>

<!-- Ordinary verse numbers -->
<xsl:template match="verse-num">
  <span class="verse-num">
    <xsl:text> </xsl:text>
    <xsl:apply-templates/>
    <!-- prevent numbers from appearing at the end of lines -->
    <!--<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>-->
  </span>
</xsl:template>

<!--
  The order of the next two matters: "begin-chapter=" takes precedence
  over "class=woc" when both are present
-->

<!-- Words of Christ verse numbers -->
<!--
<xsl:template match="verse-num[@class='woc']">
  <span class="verse-num-woc">
    <xsl:apply-templates/>
    <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
  </span>
</xsl:template>
-->

<!-- Chapter beginnings -->
<xsl:template match="verse-num[@begin-chapter]">
  <!-- For better formatting of Psalms we omit the number here -->
  <xsl:choose>
    <xsl:when test="preceding-sibling::*[1][name()='begin-line']">
      <span class="verse-num">1 </span>
    </xsl:when>
    <xsl:otherwise>
      <span class="chapter-num"><xsl:value-of select="@begin-chapter"/></span>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- These translate directly to <span/> elements -->
<!--
<xsl:template match="selah|woc">
-->
<xsl:template match="selah">
  <span class="{name()}"><xsl:apply-templates/></span>
</xsl:template>

<!-- Indentation -->
<xsl:template match="block-indent">
  <div class="block-indent">
    <xsl:apply-templates/>
  </div>
</xsl:template>

<!-- WIDs -->
<xsl:template match="w">
  <span class="word" wid="{@wid}">
    <xsl:apply-templates/>
  </span>
</xsl:template>

<!-- Any span element becomes small caps -->
<xsl:template match="span">
  <span class="sc"><xsl:apply-templates/></span>
</xsl:template>

<!-- Some HTML elements may be present (see DTD).  Copy them. -->
<xsl:template match="i|a|q">
  <xsl:element name="{name()}"><xsl:apply-templates/></xsl:element>
</xsl:template>

<!--
    Footnotes.
    In the body we format a reference to the footnote list generated
    later.  We set the title attribute: with some browsers hovering
    over the footnot reference will pop-up the footnote text.
-->
<xsl:template match="footnote">
  <xsl:variable name="number" select="count(preceding::footnote)+1"/>
  <a class="footnote" title="{.}" href="#fn{$number}">
    <xsl:number value="$number" format="[1]"/>
  </a>
</xsl:template>

<!-- The copyright notice -->
<xsl:template match="copyright">
  <p><xsl:apply-templates/></p>
</xsl:template>

<!-- =============================================================== -->


<!-- The footnote section ========================================== -->

<!--
    Make a list of the footnotes with suitable anchors.  It might be
    useful to generate backlinks into the text sometime.
-->
<xsl:template match="footnote" mode="footnotes">
  <li>
    <!-- <xsl:variable name="number" select="count(preceding::footnote)+1"/> -->
    <xsl:variable name="number" select="position()"/>
    <a id="fn{$number}" name="fn{$number}">
      <xsl:number value="$number" format="[1]"/>
    </a>
    <xsl:text> </xsl:text>
    <xsl:apply-templates/>
  </li>
</xsl:template>

<!-- =============================================================== -->


<!-- Named templates =============================================== -->

<!--
    Copy all the attributes of an element.
-->
<xsl:template name="copy-attributes">
  <xsl:for-each select="@*">
    <xsl:copy/>
  </xsl:for-each>
</xsl:template>

<!-- =============================================================== -->

</xsl:stylesheet>