<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
         xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
         xmlns:dc='http://purl.org/dc/elements/1.1/'
	 xmlns:dli="http://njlegallib.rutgers.edu/ns/1.0"
         xmlns="http://www.w3.org/1999/xhtml"
	 version="1.0"
	 exclude-result-prefixes="xsl rdf dc dli">
<!-- 
      Kevin Reiss kreiss@kinoy.rutgers.edu  djvu2xhtml.xsl

      3-09-04 Version .20

      Availabe under the GNU Public License, see file gpl.txt 
     
      PURPOSE: converts an instance of djvuXML format to XHTML.
      The primary functionality provided is the ability to 
      supply an external metadata file specific to the application to 
      embed meta tags for the ouput XHTML document's header.

      The presentational aspects of the XHTML ouput are strictly 
      beta.  The navigational features work well, but the formatting,
      especially for complicated table of contents, tables, footnotes,
      and lists need to be improved.
-->

<!-- include application specific templates -->
<xsl:include href="njardctemplate.xsl"/>

<!-- include global parameters -->
<xsl:include href="params.xsl"/>

  <!-- output declarations -->
  <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"
    encoding="UTF-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" />
  <xsl:preserve-space elements="*"/>

<!-- root template -->
<xsl:template match="DjVuXML">
  <html xmlns="http://www.w3.org/1999/xhtml" lang="{$language}" xml:lang="{$language}">
    <head>
      <!-- create document title 
           if a metafile supplied title will come from this
           otherwise it is the name of the documents source file
       -->
    <xsl:choose>
     <!-- if metafile is passed at the command line, process it -->
     <xsl:when test="$metafile">
      <xsl:apply-templates select="document($metafile)" mode="header"/>
    </xsl:when>
    <xsl:otherwise>
     <title>
      <xsl:value-of select="$alt_title"/>
    </title>
     </xsl:otherwise>
   </xsl:choose>

   <!-- include the stylesheet if it is passed as a parameter -->
   <xsl:if test="$stylesheet">
     <link href="{$stylesheet}" type="text/css" rel="stylesheet"/>
   </xsl:if>
    </head>
    <body>
     <xsl:apply-templates select="BODY"/>
    </body>
  </html>
</xsl:template>


<!-- body template -->
<xsl:template match="BODY">
  <!-- if a metafile supplied create the document header 
       otherwise output default title and header information.
    -->
  <xsl:choose>
    <!-- refering to the metafile again, this should really be more efficietly done -->
    <xsl:when test="$metafile">
      <xsl:apply-templates select="document($metafile)" mode="doctitle"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:if test="$displayheader">
         <xsl:call-template name="header">
	   <xsl:with-param name="title">
	     <xsl:value-of select="$alt_title"/>
	   </xsl:with-param>
	</xsl:call-template>
      </xsl:if>
      <h1><xsl:value-of select="$alt_title"/></h1>
    </xsl:otherwise>
  </xsl:choose>
  <!-- display document pagination 
       in hyperlinked index at start of the 
       document.
    -->
  <xsl:if test="$displaypagination">
    <p class="pagination">
      <xsl:text>Go To Page: </xsl:text>
       <xsl:for-each select="OBJECT">
         <xsl:text>&#160;</xsl:text>
         <a href="#{@usemap}"><xsl:value-of select="position()"/></a>
         <xsl:text>&#160;</xsl:text>
      </xsl:for-each>
    </p>
  </xsl:if>

  <xsl:apply-templates select="OBJECT"/>
  <!-- call document footer -->
  <xsl:if test="$displayfoot">
    <xsl:call-template name="footer"/>
  </xsl:if> 
</xsl:template>

<xsl:template match="OBJECT">
  <div class="page">
    <xsl:if test="$displaynavigation">
   <p class="pagehead"><a name="{@usemap}"></a>
    <xsl:text>View&#160;</xsl:text>
    <!-- create link to DjVu Document Page -->
    <a href="{concat($djvudoc, '?djvuopts&amp;page=', position())}">DjVu</a>
    <xsl:text>&#160;Page&#160;</xsl:text>
    <xsl:value-of select="position()"/>
    <xsl:text> of </xsl:text><xsl:value-of select="last()"/>
    <br/>
    <xsl:text>Go&#160;To:&#160;</xsl:text>
    <!-- conditional to give different navigation for first
         and last pages of the document.  Every other page has
	 the same navigation 
      -->
    <xsl:choose>
    <xsl:when test="position() = 1">
      <a name="first"></a>
      <a href="#{following-sibling::OBJECT/@usemap}">next</a><xsl:text>&#160;</xsl:text>     
      <a href="#last">&gt;</a>
    </xsl:when>
    <xsl:when test="position() = last()">
      <a name="last"></a>
      <a href="#first">&lt;</a><xsl:text>&#160;</xsl:text>     
      <a href="#{preceding-sibling::OBJECT[position() = 1]/@usemap}">prev</a><xsl:text>&#160;</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <a href="#first">&lt;</a>
      <xsl:text>&#160;</xsl:text>
      <a href="#{preceding-sibling::OBJECT[position() = 1]/@usemap}">prev</a><xsl:text>&#160;</xsl:text>     
	<a href="#{following-sibling::OBJECT/@usemap}">next</a><xsl:text>&#160;</xsl:text>
	<a href="#last">&gt;</a>
    </xsl:otherwise>
    </xsl:choose>
   </p>
    </xsl:if>
   <xsl:choose>
     <xsl:when test="position() = 1">
       <!-- apply special processing to page one of docs -->
       <xsl:apply-templates select="HIDDENTEXT/PAGECOLUMN" mode="pageone"/>
     </xsl:when>
     <xsl:otherwise>
       <xsl:apply-templates select="HIDDENTEXT/PAGECOLUMN"/>
     </xsl:otherwise>
   </xsl:choose>
  </div>
</xsl:template>

 <!--  *** General Warning *** 
      The following templates contain some hacks to ignore header that appears
        on the top of each document, as this could negatively impact
        the performance of an indexing engine.  It should not impact the 
        first page of documents as this is typically different. These measures 
	can be turned on or off by global variables that can be set in the 
	"params.xsl" file of the stylesheets.  See this file for a full 
	description of these variables.  
   -->

<xsl:template match="HIDDENTEXT/PAGECOLUMN">
   <xsl:apply-templates select="REGION"/>
 </xsl:template>

<xsl:template match="HIDDENTEXT/PAGECOLUMN" mode="pageone">
   <!-- the following is a hack to ignore header that appears
        on the top of each document, as this could negatively impact
        the performance of an indexing engine.  It should not impact the 
        first page of documents as this is typically different. -->
   <xsl:apply-templates select="REGION" mode="pageone"/>
</xsl:template>

<!-- DjVu XSL docs are divided into Regions for headers, footers and the bodies of the page -->
<xsl:template match="REGION">
  <xsl:choose>
    <xsl:when test="position() = 1">
      <xsl:apply-templates select="PARAGRAPH[position() &gt;= $ignoreheadlines]"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="PARAGRAPH"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<xsl:template match="REGION" mode="pageone">
  <xsl:choose>
    <xsl:when test="position() = 1">
      <xsl:apply-templates select="PARAGRAPH[position() &gt;= $ignorepageonelines]" mode="pageone" />
    </xsl:when>
    <xsl:otherwise>
      <xsl:apply-templates select="PARAGRAPH" mode="pageone"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<xsl:template match="PARAGRAPH" mode="pageone">
  <p>
    <!-- should try and ignore number of pages lines 
         this tries to account the varying degrees to which 
         the djvutoxml command is able to reach -->
  <xsl:apply-templates select="LINE[position() &gt; $pageoneparalines]"/>
  </p>
</xsl:template>


<xsl:template match="PARAGRAPH">
  <p>
    <!-- should try and ignore number of pages lines 
         this tries to account the varying degrees to which 
         the djvutoxml command is able to reach -->
  <xsl:apply-templates select="LINE[position() &gt; $paragraphstartlines]"/>
  </p>
</xsl:template>


<xsl:template match="LINE">
  <xsl:variable name="A"><xsl:value-of select="."/></xsl:variable>
  <xsl:variable name="B">.</xsl:variable>
  <xsl:if test="position() != 1">
    <xsl:text>&#160;</xsl:text>
  </xsl:if>
 <xsl:choose>
  <!-- denote an end paragraph with a double line-break -->
  <xsl:when test="string-length($A) &lt; $paragraph-break">
  <!-- tried to use this test to verify that the last item of 
     line denoted as end paragraph is a period but it
     isn't working

 <xsl:when test="string-length($A) &lt; $paragraph-break and substring($A, string-length($A) - string-length($B) + 1) = $B">

-->
    <xsl:value-of select="normalize-space(.)"/><br/>
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="normalize-space(.)"/><br/>
  </xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>

