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

<!--
  - Quotation marks
-->
<xsl:template match="qq">
	<xsl:choose>
		<xsl:when test="not(ancestor::qq)">
			<xsl:call-template name="i18n.print">
				<xsl:with-param name="key" select="'startquote'"/>
			</xsl:call-template>
			<xsl:apply-templates/>
			<xsl:call-template name="i18n.print">
				<xsl:with-param name="key" select="'endquote'"/>
			</xsl:call-template>
		</xsl:when>
		<xsl:otherwise>
			<xsl:call-template name="i18n.print">
				<xsl:with-param name="key" select="'startnestedquote'"/>
			</xsl:call-template>		
			<xsl:apply-templates/>
			<xsl:call-template name="i18n.print">
				<xsl:with-param name="key" select="'endnestedquote'"/>
			</xsl:call-template>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template match="q">
	<xsl:call-template name="i18n.print">
		<xsl:with-param name="key" select="'startsinglequote'"/>
	</xsl:call-template>
	<xsl:apply-templates/>
	<xsl:call-template name="i18n.print">
		<xsl:with-param name="key" select="'endsinglequote'"/>
	</xsl:call-template>
</xsl:template>

<!--
  - Verbatim text
-->
<xsl:template match="verbatim">
	<pre class="verbatim">
	<xsl:apply-templates/>
	</pre>
</xsl:template>

<!--
  - Font size
-->
<xsl:template match="xx-small">
	<span style="font-size: xx-small"><xsl:apply-templates/></span>
</xsl:template>

<xsl:template match="x-small">
	<span style="font-size: x-small"><xsl:apply-templates/></span>
</xsl:template>

<xsl:template match="small">
	<span style="font-size: small"><xsl:apply-templates/></span>
</xsl:template>

<xsl:template match="medium">
	<span style="font-size: medium"><xsl:apply-templates/></span>
</xsl:template>

<xsl:template match="large">
	<span style="font-size: large"><xsl:apply-templates/></span>
</xsl:template>

<xsl:template match="x-large">
	<span style="font-size: x-large"><xsl:apply-templates/></span>
</xsl:template>

<xsl:template match="xx-large">
	<span style="font-size: xx-large"><xsl:apply-templates/></span>
</xsl:template>

<!--
  - Set text in italics.
-->
<xsl:template match="i">
	<i><xsl:apply-templates/></i>
</xsl:template>

<!--
  - Set text in bold face.
-->
<xsl:template match="b">
	<b><xsl:apply-templates/></b>
</xsl:template>

<!--
  - Underline text.
-->
<xsl:template match="u">
	<span style="text-decoration: underline;"><xsl:apply-templates/></span>
</xsl:template>

<!--
  - Set typewriter text.
-->
<xsl:template match="tt">
	<span style="font-family: 'courier new',courier,fixed"><xsl:apply-templates/></span>
</xsl:template>

<!--
  - Prevent line break or hyphenation.
-->
<xsl:template match="nobr">
	<span style="white-space: nowrap;"><xsl:apply-templates/></span>
</xsl:template>

<!--
  - Put a line break.
-->
<xsl:template match="br">
	<br/>
</xsl:template>

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

<!--
  - Set a paragraph.
-->
<xsl:template match="p">
	<xsl:choose>
		<xsl:when test="title">
			<p><span class="p"><xsl:apply-templates select="title"/></span>
			<xsl:apply-templates select="title/following-sibling::*|title/following-sibling::text()"/></p>
		</xsl:when>
		<xsl:otherwise>
			<p><xsl:apply-templates/></p>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--
  - Hyphenation is ignored for html.
-->
<xsl:template match="y"/>

<!--
  - Place a label for a cross-reference.
-->
<xsl:template match="label">
	<a name="{generate-id()}" id="{generate-id()}"></a>
</xsl:template>

<!--
  - Print reference to section containing corresponding label.
-->
<xsl:template match="ref">
	<xsl:variable name="name" select="@name"/>
	<xsl:variable name="filename">
		<xsl:for-each select="//label[@name=$name] | //counter[@label=$name]">
			<xsl:call-template name="ref.filename"/>
		</xsl:for-each>
	</xsl:variable>
	<xsl:variable name="prefix">
		<xsl:for-each select="//label[@name=$name] | //counter[@label=$name]">
			<xsl:call-template name="ref.secprefix"/>
		</xsl:for-each>
	</xsl:variable>
	<xsl:variable name="idnum">
		<xsl:for-each select="//label[@name=$name] | //counter[@label=$name]">
			<xsl:value-of select="generate-id()"/>
		</xsl:for-each>
	</xsl:variable>
	<xsl:if test="$prefix != ''">
		<a href="{$filename}#{$idnum}"><xsl:value-of select="$prefix"/></a>
	</xsl:if>
</xsl:template>

<!--
  - Prints the name of the file containing the referenced label.
-->
<xsl:template match="pageref">
	<xsl:variable name="name" select="@name"/>
	<xsl:variable name="filename">
		<xsl:for-each select="//label[@name=$name] | //counter[@label=$name]">
			<xsl:call-template name="ref.filename"/>
		</xsl:for-each>
	</xsl:variable>
	<xsl:variable name="idnum">
		<xsl:for-each select="//label[@name=$name] | //counter[@label=$name]">
			<xsl:value-of select="generate-id()"/>
		</xsl:for-each>
	</xsl:variable>
	<a href="{$filename}#{$idnum}"><xsl:value-of select="$filename"/></a>
</xsl:template>

<!--
  - Print text-nodes
-->
<xsl:template match="text()">
	<xsl:value-of select="."/>
</xsl:template>

<!--
  - Determine prefix of containing section.
-->
<xsl:template name="ref.secprefix">

	<xsl:choose>
		<xsl:when test="
			name() = 'preface' or
			name() = 'chapter' or
			name() = 'appendix' or
			name() = 'section' or
			name() = 'subsection' or
			name() = 'subsubsection' or
			name() = 'biblio' or
			name() = 'glossary'">
			<xsl:variable name="secnumdepth">
				<xsl:call-template name="util.secnumdepth"/>
			</xsl:variable>
			<xsl:variable name="curdepth">
				<xsl:call-template name="util.curdepth"/>
			</xsl:variable>
			<xsl:choose>
				<xsl:when test="$curdepth = 0">
					<xsl:text></xsl:text>
				</xsl:when>
				<xsl:when test="$secnumdepth >= $curdepth">
					<xsl:call-template name="util.secprefix"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:for-each select="parent::*">
						<xsl:call-template name="ref.secprefix"/>
					</xsl:for-each>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:when>
		<xsl:when test="name() = 'counter'">
			<xsl:call-template name="counter.prefix"/>
		</xsl:when>
		<xsl:when test="name() = 'figure'">
			<xsl:call-template name="figure.prefix"/>
		</xsl:when>
		<xsl:when test="name() = 'table'">
			<xsl:call-template name="table.prefix"/>
		</xsl:when>
		<xsl:when test="name() = 'equation'">
			<xsl:call-template name="equation.prefix"/>
		</xsl:when>
		<xsl:otherwise>
			<xsl:for-each select="parent::*">
				<xsl:call-template name="ref.secprefix"/>
			</xsl:for-each>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<!--
  - Find the name of the file that contains the context node.
-->
<xsl:template name="ref.filename">

	<!-- current nesting depth -->
	<xsl:variable name="curdepth">
		<xsl:call-template name="util.curdepth"/>
	</xsl:variable>
	<!-- depth of chunking -->
	<xsl:variable name="secsplitdepth">
		<xsl:call-template name="util.secsplitdepth"/>
	</xsl:variable>
	
	<!-- recurse or print filename -->
	<xsl:choose>
		<xsl:when test="$secsplitdepth = 0">
			<xsl:text>index.html</xsl:text>
		</xsl:when>
		<xsl:when test="$curdepth > $secsplitdepth">
			<xsl:for-each select="parent::*">
				<xsl:call-template name="ref.filename"/>
			</xsl:for-each>
		</xsl:when>
		<xsl:otherwise>
			<xsl:choose>
				<xsl:when test="name() = 'preface'">
					<xsl:text>preface</xsl:text>
					<xsl:number count="preface"/>
					<xsl:text>.html</xsl:text>
				</xsl:when>
				<xsl:when test="name() = 'biblio' or name() = 'glossary'">
					<xsl:value-of select="name()"/><xsl:text>.html</xsl:text>
				</xsl:when>
				<xsl:when test="
					name() = 'chapter' or
					name() = 'appendix' or
					name() = 'section' or
					name() = 'subsection' or
					name() = 'subsubsection'">
					<xsl:value-of select="name()"/>
					<xsl:call-template name="util.secprefix"/>
					<xsl:text>.html</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<xsl:for-each select="parent::*">
						<xsl:call-template name="ref.filename"/>
					</xsl:for-each>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

</xsl:stylesheet>
