<?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">

<!--
  - Returns the smaller one of the passed parameters
-->
<xsl:template name="util.min">
	<xsl:param name="x"/>
	<xsl:param name="y"/>
	<xsl:choose>
		<xsl:when test="$x > $y">
			<xsl:value-of select="$y"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="$x"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--
  - Returns the user-defined section numbering depth or a default
  - value, if the user did not specify anything-
-->
<xsl:template name="util.secnumdepth">
	<!-- how deep is document actually nested? -->
	<xsl:variable name="secdepth">
		<xsl:call-template name="util.secdepth"/>
	</xsl:variable>
	<xsl:variable name="requested_secnumdepth">
		<xsl:choose>
			<xsl:when test="/*[1]/@secnumdepth">
				<xsl:value-of select="/*[1]/@secnumdepth"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:number value="3"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<!-- whichever is smaller -->
	<xsl:call-template name="util.min">
		<xsl:with-param name="x" select="$secdepth"/>
		<xsl:with-param name="y" select="$requested_secnumdepth"/>
	</xsl:call-template>
</xsl:template>

<!--
  - Returns the user-defined listing depth for the table of contents
  - or a default value, if the user did not make specifications.
-->
<xsl:template name="util.tocdepth">
	<!-- how deep is document actually nested? -->
	<xsl:variable name="nesting_depth">
		<xsl:call-template name="util.secdepth"/>
	</xsl:variable>
	<!-- get or set default values for processing parameters -->
	<xsl:variable name="requested_tocdepth">
		<xsl:choose>
			<xsl:when test="/*[1]/@tocdepth">
				<xsl:value-of select="/*[1]/@tocdepth"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:number value="3"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<!-- whichever is smaller -->
	<xsl:call-template name="util.min">
		<xsl:with-param name="x" select="$nesting_depth"/>
		<xsl:with-param name="y" select="$requested_tocdepth"/>
	</xsl:call-template>
</xsl:template>

<!--
  - Returns the user-defined depth to which the document shall be split
  - in individual files or a default value, if the user did not say.
-->
<xsl:template name="util.secsplitdepth">
	<!-- tocdepth is implicit limit for secsplitdepth -->
	<xsl:variable name="tocdepth">
		<xsl:call-template name="util.tocdepth"/>
	</xsl:variable>
	<!-- level of chunking -->
	<xsl:variable name="requested_secsplitdepth">
		<xsl:choose>
			<xsl:when test="/*[1]/@secsplitdepth">
				<xsl:value-of select="/*[1]/@secsplitdepth"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:choose>
					<xsl:when test="/book">
						<xsl:number value="2"/>
					</xsl:when>
					<xsl:when test="/article">
						<xsl:number value="0"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:number value="1"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<!-- select minimum -->
	<xsl:call-template name="util.min">
		<xsl:with-param name="x" select="$tocdepth"/>
		<xsl:with-param name="y" select="$requested_secsplitdepth"/>
	</xsl:call-template>
</xsl:template>

<!--
  - Determines how deep the document is actually nested.
-->
<xsl:template name="util.secdepth">
	<xsl:variable name="depth">
		<xsl:choose>
			<xsl:when test="//subsubsection">
				<xsl:number value="4"/>
			</xsl:when>
			<xsl:when test="//subsection">
				<xsl:number value="3"/>
			</xsl:when>
			<xsl:when test="//section">
				<xsl:number value="2"/>
			</xsl:when>
			<xsl:when test="/*[1]/child::*">
				<xsl:number value="1"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:number value="0"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<xsl:choose>
		<xsl:when test="$depth > 1 and not(//chapter | //appendix)">
			<xsl:value-of select="$depth - 1"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="$depth"/>
		</xsl:otherwise>
	</xsl:choose>
</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="util.secprefix">
	<xsl:variable name="prefix">
		<xsl:choose>
			<xsl:when test="name() = 'preface' or not(title)">
				<xsl:text disable-output-escaping="yes"></xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:if test="ancestor-or-self::chapter">
				<xsl:number count="chapter"/>
					<xsl:text>.</xsl:text>
				</xsl:if>
				<xsl:if test="ancestor-or-self::appendix">
					<xsl:number count="appendix" format="A"/>
					<xsl:text>.</xsl:text>
				</xsl:if>
				<xsl:if test="ancestor-or-self::section">
					<xsl:number count="section"/>
					<xsl:text>.</xsl:text>
				</xsl:if>
				<xsl:if test="ancestor-or-self::subsection">
					<xsl:number count="subsection"/>
					<xsl:text>.</xsl:text>
				</xsl:if>
				<xsl:if test="name() = 'subsubsection'">
					<xsl:number count="subsubsection"/>
					<xsl:text>.</xsl:text>
				</xsl:if>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<xsl:if test="$prefix != ''">
		<xsl:value-of select="substring($prefix, 1, string-length($prefix) - 1)"/>
	</xsl:if>
</xsl:template>

<!--
  - Determine current level in section hierarchy.
-->
<xsl:template name="util.curdepth">
	<xsl:variable name="depth">
		<xsl:choose>
			<xsl:when test="ancestor-or-self::subsubsection">
				<xsl:number value="4"/>
			</xsl:when>
			<xsl:when test="ancestor-or-self::subsection">
				<xsl:number value="3"/>
			</xsl:when>
			<xsl:when test="ancestor-or-self::section">
				<xsl:number value="2"/>
			</xsl:when>
			<xsl:when test="name() = name(/*[1])">
				<xsl:number value="0"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:number value="1"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<xsl:choose>
		<xsl:when test="$depth > 1 and not(ancestor::chapter | ancestor::appendix)">
			<xsl:value-of select="$depth - 1"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="$depth"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

</xsl:stylesheet>
