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

<!--
 - Sets a glossary section
-->
<xsl:template match="glossary">
	<xsl:choose>
		<xsl:when test="/article">
			<xsl:text>\section*{</xsl:text>
		</xsl:when>
		<xsl:otherwise>
			<xsl:text>\chapter*{</xsl:text>
		</xsl:otherwise>
	</xsl:choose>
	<xsl:call-template name="i18n.print">
		<xsl:with-param name="key" select="'glossary'"/>
	</xsl:call-template>
	<xsl:text>}&#x0a;</xsl:text>
	<xsl:apply-templates/>
</xsl:template>

<!--
 - Sets a glossary section
-->
<xsl:template match="abstract">
	<xsl:choose>
		<xsl:when test="/article">
			<xsl:text>\begin{abstract}</xsl:text>
		</xsl:when>
		<xsl:otherwise>
			<xsl:text>\chapter*{</xsl:text>
			<xsl:call-template name="i18n.print">
				<xsl:with-param name="key" select="'abstract'"/>
			</xsl:call-template>
			<xsl:text>}&#x0a;</xsl:text>
		</xsl:otherwise>
	</xsl:choose>
	<xsl:apply-templates/>
	<xsl:if test="/article">
		<xsl:text>\end{abstract}</xsl:text>
	</xsl:if>
</xsl:template>

<!--
 - This can be used to make certain content visible in designated media
 - only. For example, say <span visibility="print">text</span> to make
 - 'text' only visible in printed documents.
-->
<xsl:template match="span">
	<xsl:if test="contains(@visibility,'print') or @visibility='all' or not(@visibility)">
		<xsl:apply-templates/>
	</xsl:if>
</xsl:template>

<!--
  - Entry point for printing the document.
-->
<xsl:template name="section.make">

	<!-- level of chunking -->
	<xsl:variable name="secsplitdepth">
		<xsl:call-template name="util.secsplitdepth"/>
	</xsl:variable>

	<xsl:for-each select="head/following-sibling::*">
		<xsl:call-template name="section.print">
			<xsl:with-param name="curdepth" select="1"/>
			<xsl:with-param name="secsplitdepth" select="$secsplitdepth"/>
		</xsl:call-template>
	</xsl:for-each>
</xsl:template>

<!--
 - Print the section heading. Calls 'section.anchor' to print the title
 - embedded in an anchor that can be referred and jumped to from the
 - table of contents.
-->
<xsl:template name="section.title">
	<!-- choose heading weight, depeding on level -->
	<xsl:choose>
		<xsl:when test="not(title)">
			<xsl:text></xsl:text>
		</xsl:when>
		<xsl:when test="name() = 'preface'">
			<xsl:text>\chapter*{</xsl:text>
				<xsl:apply-templates select="title"/>
			<xsl:text>}&#x0a;&#x0a;</xsl:text>
		</xsl:when>
		<xsl:when test="name() = 'appendix'">
			<xsl:if test="not(preceding-sibling::appendix)">
				<xsl:text>\appendix{}&#x0a;&#x0a;</xsl:text>
			</xsl:if>
			<xsl:text>\chapter{</xsl:text>
				<xsl:apply-templates select="title"/>
			<xsl:text>}&#x0a;&#x0a;</xsl:text>			
		</xsl:when>
		<xsl:otherwise>
			<xsl:text>\</xsl:text><xsl:value-of select="name()"/><xsl:text>{</xsl:text>
				<xsl:apply-templates select="title"/>
			<xsl:text>}&#x0a;&#x0a;</xsl:text>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--
  - Print the contents of a section and call 'section.print' for
  - subsections.
-->
<xsl:template name="section.content">

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

	<!-- print title -->
	<xsl:call-template name="section.title">
		<xsl:with-param name="curdepth" select="$curdepth"/>
	</xsl:call-template>
	<xsl:choose>
		<xsl:when test="not(title)">
			<xsl:apply-templates select="."/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:for-each select="child::*[not(name() = 'title')]">
				<xsl:choose>
					<!-- process content -->
					<xsl:when test="not(name() = 'section' or name() = 'subsection' or name() = 'subsubsection')">
						<xsl:apply-templates select="."/>
					</xsl:when>
					<!-- process subsection -->
					<xsl:otherwise>
						<xsl:call-template name="section.print">
							<xsl:with-param name="curdepth" select="$curdepth + 1"/>
							<xsl:with-param name="secsplitdepth" select="$secsplitdepth"/>
						</xsl:call-template>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:for-each>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--
  - Returns name of file that contains the calling section.
-->
<xsl:template name="section.file">
	<xsl:choose>
		<xsl:when test="name() = 'preface'">
			<xsl:text>preface</xsl:text>
			<xsl:number/>
		</xsl:when>
		<xsl:when test="not(title)">
			<xsl:value-of select="name()"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:value-of select="name()"/>
			<xsl:variable name="tmp">
				<xsl:call-template name="util.secprefix"/>
			</xsl:variable>
			<xsl:value-of select="substring($tmp, 1, string-length($tmp) - 1)"/>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--
  - Recursive template that takes care of splitting the document in
  - multiple output files and printing section contents.
-->
<xsl:template name="section.print">
	
	<xsl:param name="secsplitdepth"/>
	<xsl:param name="curdepth"/>
	
	<xsl:choose>
		<!-- output to separate file -->
		<xsl:when test="$secsplitdepth >= $curdepth">
			<xsl:variable name="filename">
				<xsl:call-template name="section.file"/>
			</xsl:variable>
			<!-- leave include directive -->
			<xsl:text>\input{</xsl:text>
				<xsl:value-of select="$filename"/>
			<xsl:text>}&#x0a;&#x0a;</xsl:text>
			<!-- output to separate file -->
			<xsl:document href="{$filename}.tex" method="text" indent="yes" encoding="UTF-8">
				<xsl:call-template name="section.content">
					<xsl:with-param name="secsplitdepth" select="$secsplitdepth"/>
					<xsl:with-param name="curdepth" select="$curdepth"/>
				</xsl:call-template>
			</xsl:document>
		</xsl:when>
		<!-- output on the spot -->
		<xsl:otherwise>
			<xsl:call-template name="section.content">
				<xsl:with-param name="secsplitdepth" select="$secsplitdepth"/>
				<xsl:with-param name="curdepth" select="$curdepth"/>
			</xsl:call-template>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--
  - A minisection is an odd kind of section. It does not have a fixed place in
  - the section hierarchy. Instead, it can occur pretty much anywhere and only
  - serves the purpose of having a titled series of paragraphs. Minisections
  - are not numbered.
-->
<xsl:template match="minisection">
	<xsl:text>\minisec{</xsl:text>
		<xsl:apply-templates select="title"/>
	<xsl:text>}&#x0a;</xsl:text>
	<xsl:apply-templates select="title/following-sibling::*"/>
</xsl:template>

</xsl:stylesheet>
