<?xml version="1.0" encoding="UTF-8"?>
<!--
 - Desc:    This file is part of the eCromedos document preparation system
 - Date:    2006/03/09
 - Author:  Tobias Koch (tkoch@ecromedos.net)
 - License: GNU General Public License, version 2
 - URL:     http://www.ecromedos.net
-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!--
  - Print table of contents.
-->
<xsl:template name="toc.make">

	<!-- depth of listing -->
	<xsl:variable name="tocdepth">
		<xsl:call-template name="util.tocdepth"/>
	</xsl:variable>
	<!-- depth of numbering -->
	<xsl:variable name="secnumdepth">
		<xsl:call-template name="util.secnumdepth"/>
	</xsl:variable>
	<!-- level of chunking -->
	<xsl:variable name="secsplitdepth">
		<xsl:call-template name="util.secsplitdepth"/>
	</xsl:variable>
	<!-- number of columns -->
	<xsl:variable name="columns">
		<xsl:choose>
			<xsl:when test="$secnumdepth >= $tocdepth">
				<xsl:value-of select="$tocdepth + 1"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$tocdepth"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<table border="0" cellspacing="0" cellpadding="2" style="width: 100%">
		<!-- header -->
		<xsl:call-template name="toc.head">
				<xsl:with-param name="columns" select="$columns"/>
		</xsl:call-template>
		<!-- listing -->
		<xsl:if test="$tocdepth > 0 or $secsplitdepth > 0">
			<tr>
				<td colspan="{$columns}" style="font-size:x-large">
					<b>
						<xsl:call-template name="i18n.print">
							<xsl:with-param name="key" select="'contents'"/>
						</xsl:call-template>
					</b>
				</td>			
			</tr>
			<!-- add an empty row -->
			<tr>
				<td colspan="{$columns}">
					<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
				</td>
			</tr>
			<xsl:for-each select="head/following-sibling::*">
				<xsl:call-template name="toc.section.print">
					<xsl:with-param name="curdepth" select="1"/>
					<xsl:with-param name="tocdepth" select="$tocdepth"/>
					<xsl:with-param name="secnumdepth" select="$secnumdepth"/>
					<xsl:with-param name="secsplitdepth" select="$secsplitdepth"/>
					<xsl:with-param name="columns" select="$columns"/>
				</xsl:call-template>
			</xsl:for-each>
		</xsl:if>
		<!-- footer -->
		<xsl:call-template name="toc.foot">
			<xsl:with-param name="columns" select="$columns"/>
		</xsl:call-template>
	</table>
</xsl:template>

<!--
  - Prints author(s) and title.
-->
<xsl:template name="toc.head">

	<xsl:param name="secnumdepth"/>
	<xsl:param name="tocdepth"/>
	<xsl:param name="columns"/>

	<!-- print authors -->
	<xsl:for-each select="head/author">
		<tr>
			<td colspan="{$columns}">
				<xsl:apply-templates select="."/>
			</td>
		</tr>
	</xsl:for-each>
	<!-- add an empty row -->
	<tr>
		<td colspan="{$columns}">
			<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
		</td>
	</tr>
	<!-- print subject -->
	<xsl:if test="head/subject">
		<tr>
			<td colspan="{$columns}">
				<b><xsl:apply-templates select="head/subject"/></b>
			</td>			
		</tr>
	</xsl:if>
	<!-- print title -->
	<tr>
		<td colspan="{$columns}" style="font-size:x-large;font-weight:bold">
			<xsl:apply-templates select="head/title"/>
		</td>
	</tr>
	<!-- print publisher -->
	<xsl:if test="head/publisher">
		<tr>
			<td colspan="{$columns}" style="font-size:xx-small">
				<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
			</td>
		</tr>
		<tr>
			<td colspan="{$columns}" style="font-size:medium">
				<xsl:apply-templates select="head/publisher"/>
			</td>
		</tr>
	</xsl:if>
	<!-- print date -->
	<xsl:if test="head/date">
		<tr>
			<td colspan="{$columns}" style="font-size:xx-small">
				<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
			</td>
		</tr>
		<tr>
			<td colspan="{$columns}" style="font-size:x-small">
				<xsl:apply-templates select="head/date"/>
			</td>			
		</tr>
	</xsl:if>
	<!-- add an empty row -->
	<tr>
		<td colspan="{$columns}">
			<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
		</td>
	</tr>
</xsl:template>

<!--
  - Prints the toc page foot.
-->
<xsl:template name="toc.foot">

	<xsl:param name="secnumdepth"/>
	<xsl:param name="tocdepth"/>
	<xsl:param name="columns"/>

	<tr>
		<td colspan="{$columns}">
			<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
		</td>
	</tr>
</xsl:template>

<!--
  - Determines the numerical prefix for a given node, i.e. A.1.2 for
  - Appendix A, Section 1, Subsection 2.
-->
<xsl:template name="toc.section.prefix">
	<xsl:choose>
		<xsl:when test="name() = 'preface' or not(title)">
			<xsl:text disable-output-escaping="yes">&amp;#x2022;</xsl:text>
		</xsl:when>
		<xsl:otherwise>
			<xsl:variable name="prefix">
				<xsl:call-template name="util.secprefix"/>
			</xsl:variable>
			<xsl:call-template name="i18n.print">
				<xsl:with-param name="key" select="'sectionnumber'"/>
				<xsl:with-param name="number" select="$prefix"/>
			</xsl:call-template>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--
  - Called by 'toc.section.title' to print a requested number of emtpy
  - columns before an indented entry in the table.
-->
<xsl:template name="toc.section.indent">
	<xsl:param name="indentcolumns"/>
	<xsl:if test="$indentcolumns > 1">
		<td>
			<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
		</td>
		<xsl:call-template name="toc.section.indent">
			<xsl:with-param name="indentcolumns" select="$indentcolumns - 1"/>
		</xsl:call-template>
	</xsl:if>
</xsl:template>

<!--
  - This is called by 'toc.section.title' to go back in the hierarchy
  - of sections to the ancestor that is on the chunking level requested
  - by by the user.
-->
<xsl:template name="toc.section.file">
	<xsl:param name="generations"/>
	<xsl:choose>
		<xsl:when test="$generations > 0">
			<xsl:for-each select="parent::*">
				<xsl:call-template name="toc.section.file">
					<xsl:with-param name="generations" select="$generations - 1"/>
				</xsl:call-template>
			</xsl:for-each>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="name()"/>
			<xsl:call-template name="util.secprefix"/>
			<xsl:text>.html</xsl:text>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--
  - We allow labels in titles of sections, but this causes a pair of nested
  - anchors to be created in the toc. So we have to filter out labels explicitely.
-->
<xsl:template name="toc.title">
	<xsl:apply-templates select="child::*[not(name() = 'label')] | text()"/>
</xsl:template>

<!--
  - This is called by 'toc.section.title' to print the actual title
  - of the section that is currently being processed. Depending on the
  - depth of chunking that the user has requested, the template determines in
  - which file the section is located and sets the hyperlink accordingly.
-->
<xsl:template name="toc.section.link">

	<xsl:param name="secsplitdepth"/>
	<xsl:param name="curdepth"/>

	<xsl:choose>
		<!-- user wants chunked output -->
		<xsl:when test="$secsplitdepth > 0">
			<xsl:choose>
				<xsl:when test="name() = 'preface'">
					<a href="preface{position()}.html" id="{generate-id()}" name="{generate-id()}">
						<xsl:apply-templates select="title"/>
					</a>
				</xsl:when>
				<xsl:when test="not(title)">
					<a href="{name()}.html" id="{generate-id()}" name="{generate-id()}">
						<xsl:call-template name="i18n.print">
							<xsl:with-param name="key" select="name()"/>
						</xsl:call-template>
					</a>
				</xsl:when>
				<xsl:otherwise>
					<xsl:variable name="filename">
						<xsl:call-template name="toc.section.file">
							<xsl:with-param name="generations" select="$curdepth - $secsplitdepth"/>
						</xsl:call-template>
					</xsl:variable>
					<xsl:choose>
						<!-- section has its own file -->
						<xsl:when test="$secsplitdepth >= $curdepth">
							<a href="{$filename}" id="{generate-id()}" name="{generate-id()}">
								<xsl:for-each select="title">
									<xsl:call-template name="toc.title"/>
								</xsl:for-each>
							</a>
						</xsl:when>
						<!-- section is contained in ancestor's file -->
						<xsl:otherwise>
							<a href="{$filename}#{generate-id()}" id="{generate-id()}" name="{generate-id()}">
								<xsl:for-each select="title">
									<xsl:call-template name="toc.title"/>
								</xsl:for-each>
							</a>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:when>
		<!-- user wants a single file -->
		<xsl:otherwise>
			<xsl:choose>
				<xsl:when test="not(title)">
					<a href="index.html#{generate-id()}">
						<xsl:call-template name="i18n.print">
							<xsl:with-param name="key" select="name()"/>
						</xsl:call-template>
					</a>
				</xsl:when>
				<xsl:otherwise>
					<a href="index.html#{generate-id()}">
						<xsl:apply-templates select="title"/>
					</a>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--
  - This template prints one line in the table of contents, consisting
  - of an apropriate indentation, a prefix and the title of the section.
-->
<xsl:template name="toc.section.title">

	<xsl:param name="tocdepth"/>
	<xsl:param name="curdepth"/>
	<xsl:param name="secnumdepth"/>
	<xsl:param name="secsplitdepth"/>
	<xsl:param name="columns"/>

	<tr>
		<!-- indent entry -->
		<xsl:choose>
			<xsl:when test="$curdepth > $secnumdepth">
				<xsl:call-template name="toc.section.indent">
					<xsl:with-param name="indentcolumns" select="$curdepth - 1"/>
				</xsl:call-template>			
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="toc.section.indent">
					<xsl:with-param name="indentcolumns" select="$curdepth"/>
				</xsl:call-template>
			</xsl:otherwise>
		</xsl:choose>
		<!-- print prefix -->
		<xsl:if test="$secnumdepth > 0 or $curdepth > 1">
			<td style="width:1em;">
				<xsl:choose>
					<xsl:when test="$curdepth = ($secnumdepth + 1)">
						<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
					</xsl:when>
					<xsl:when test="$curdepth > $secnumdepth">
						<xsl:text disable-output-escaping="yes">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;</xsl:text>
					</xsl:when>
					<xsl:otherwise>
						<xsl:choose>
							<!-- entries on first level in bold -->
							<xsl:when test="$curdepth = 1">
								<b>
									<xsl:call-template name="toc.section.prefix"/>
									<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
								</b>
							</xsl:when>
							<xsl:otherwise>
								<xsl:call-template name="toc.section.prefix"/>
								<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:otherwise>
				</xsl:choose>
			</td>
		</xsl:if>
		<!-- print entry -->
		<xsl:variable name="colspan">
			<xsl:choose>
				<xsl:when test="$secnumdepth >= $curdepth">
					<xsl:value-of select="$columns - $curdepth"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$columns - $curdepth + 1"/>	
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<td colspan="{$colspan}" style="width:100%">
			<xsl:choose>
				<xsl:when test="$curdepth = 1">
					<b>
						<xsl:call-template name="toc.section.link">
							<xsl:with-param name="curdepth" select="$curdepth"/>
							<xsl:with-param name="secsplitdepth" select="$secsplitdepth"/>
						</xsl:call-template>
					</b>
				</xsl:when>
				<xsl:otherwise>
					<xsl:call-template name="toc.section.link">
						<xsl:with-param name="curdepth" select="$curdepth"/>
						<xsl:with-param name="secsplitdepth" select="$secsplitdepth"/>
					</xsl:call-template>
				</xsl:otherwise>
			</xsl:choose>
		</td>
	</tr>
</xsl:template>

<!--
  - Recursive template to print hierachy of sections. You can start
  - and stop at arbitrary depths. Template makes no assumptions about
  - the cascading order, so this can be used for any type of document.
-->
<xsl:template name="toc.section.print">

	<xsl:param name="tocdepth"/>
	<xsl:param name="curdepth"/>
	<xsl:param name="secnumdepth"/>
	<xsl:param name="secsplitdepth"/>
	<xsl:param name="columns"/>

	<xsl:if test="$tocdepth >= $curdepth">
		<!-- print table item -->
		<xsl:call-template name="toc.section.title">
			<xsl:with-param name="curdepth" select="$curdepth"/>
			<xsl:with-param name="tocdepth" select="$tocdepth"/>
			<xsl:with-param name="secnumdepth" select="$secnumdepth"/>
			<xsl:with-param name="secsplitdepth" select="$secsplitdepth"/>
			<xsl:with-param name="columns" select="$columns"/>
		</xsl:call-template>
		<!-- recurse into subsection -->
		<xsl:for-each select="
			child::*[
				name() = 'section' or
				name() = 'subsection' or
				name() = 'subsubsection'
			]">
			<xsl:call-template name="toc.section.print">
				<xsl:with-param name="curdepth" select="$curdepth + 1"/>
				<xsl:with-param name="tocdepth" select="$tocdepth"/>
				<xsl:with-param name="secnumdepth" select="$secnumdepth"/>
				<xsl:with-param name="secsplitdepth" select="$secsplitdepth"/>
				<xsl:with-param name="columns" select="$columns"/>
			</xsl:call-template>
		</xsl:for-each>
		<!-- insert little vspace -->
		<xsl:if test="$curdepth = 1">
			<tr>
				<td colspan="{$columns}" style="font-size:0.5ex">
					<xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>
				</td>
			</tr>
		</xsl:if>
	</xsl:if>
</xsl:template>

</xsl:stylesheet>
