Newer
Older
XML / format-master.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- This will mostly be a list of element names to match, along with the corresponding HTML and LaTeX handling XSLT code. -->
<stylesheet	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
			xmlns:xs="http://www.w3.org/2001/XMLSchema">

	<!--
		Various "global" elements that are frequently used across all documents. Some are passed in as arguments to the stylesheet (e.g., paper number), while others are things that don't change that often, and are thus hard coded (e.g., Oracle version).
	-->
	<include href="modules/global-elements.xml" />
	
	<!-- Output a string containing the date that the document was last built. -->
	<include href="modules/build-date.xml" />

	<!-- Basic page layout elements: paragraphs, page breask, etc. -->
	<include href="modules/basic-page-elements.xml" />

	<!-- Special characters. -->
	<include href="modules/special-characters.xml" />

	<!-- Greek characters. -->
	<include href="modules/greek-characters.xml" />

	<!-- Miscellaneous symbols. -->
	<include href="modules/miscellaneous-symbols.xml" />

	<!-- Emoticons. -->
	<include href="modules/emoticons.xml" />

	<!-- Mathematical operators and symbols. -->
	<include href="modules/mathematical-symbols.xml" />

	<!-- Relational algebra operators. -->
	<include href="modules/relational-algebra.xml" />

	<template name="LaTeX" match="LaTeX|latex">
		<common formats="/latex/xelatex/">{\LaTeX}</common>
		<common formats="/html/xhtml/">L<sup>A</sup>T<sub>E</sub>X</common>
	</template>

	<template name="LaTeX-strip" match="LaTeX|latex" mode="strip">
		<common formats="/html/xhtml/">LaTeX</common>
	</template>

	<!-- In-text "menus", e.g., for displaying things like Windows Start Menu items. -->
	<include href="modules/menus.xml" />

	<!-- Basic text formatting (emphasis, bold, etc.). -->
	<include href="modules/basic-text-formatting.xml" />

	<!-- Inline code and code blocks. -->
	<include href="modules/code-formatting.xml" />

	<!-- Fonts and typefaces. -->
	<include href="modules/fonts.xml" />
	
	<!-- Quoted text. -->
	<include href="modules/quotations.xml" />

	<!-- Lists (itemised, enumerated, definition). -->
	<include href="modules/lists.xml" />

	<!-- Obsolete items (in order to provide informative error messages). -->
	<include href="modules/obsolete.xml" />
	

	<!--
		Question text. This only exists so that we can ensure that appropriate
		<p> tags are correctly inserted. If this template didn't exist, showing
		the answers would generate invalid HTML. The answers are enclosed in a
		<div class="answer">...</div>. Without this template, the answer markup
		ends up embedded inside <p>...</p>, which is invalid.
	-->
	<template name="question" match="question">
		<common formats="/latex/xelatex/"><xsl:apply-templates /></common>
		<common formats="/html/xhtml/">
			<xsl:choose>
				<!--
					Check whether there are actual paragraphs inside the
					question. If so, let them worry about inserting the
					<p> tags.
				-->
				<xsl:when test="count(paragraph|para|p|question|answer|code-block) != 0">
					<xsl:apply-templates />
				</xsl:when>
				<!--
					Otherwise, insert <p> tags surrounding the question content.
				-->
				<xsl:otherwise>
					<p><xsl:apply-templates /></p>
				</xsl:otherwise>
			</xsl:choose>
		</common>
	</template>

	
	<!--
		Sample answers, which may optionally be excluded from the
		output. This can be done globally for all answers by setting the
		style sheet parameter $showanswers to "no", or it can be done
		locally on a question-by-question basis by setting the @hide
		attribute of the element to "yes".
		
		@hide: If "yes", don't include this answer in the output stream.
		The default is to include the answer.
	-->
	<template name="answer" match="answer">
		<common formats="/latex/xelatex/">
			<!--
				It's nice to weed out the sample answer markup at the XSLT processing stage to make LaTeX run faster.
				Note that \showanswers still needs to be called so that LaTeX can format chapter headings like "Answers for ..."
			-->
			<xsl:if test="$showanswers='yes'">
			\begin{answer}
				<xsl:apply-templates />
			\end{answer}

			</xsl:if>
		</common>
		<common formats="/html/xhtml/">
			<xsl:if test="$showanswers='yes'">
				<div class="answer">
					<xsl:apply-templates />
				</div>
			</xsl:if>
		</common>
	</template>
	
	<!--
		Match hidden answers and do nothing.
	-->
	<template name="hidden-answer" match="answer[@hide='yes']" />

	
	<!--
		Sections, subsections, subsubsections, ...
		
		@label: A label that can be used for cross referencing. Any value can be used, as long as it's a legal identifier in both LaTeX and HTML.
		
		@number: Whether or not to generate a section number. It makes the most logical sense for this attribute to be associated with the section element, but the section numbers are actually generated in the "section-title" template (see below). Consequently, this attribute isn't even mentioned in this template.
			'yes'	[default]
			'no'
	-->
	<template name="section" match="section">
		<common formats="/latex/xelatex/">
			<xsl:apply-templates>
				<xsl:with-param name="label">
					<xsl:value-of select="@label" />
					<xsl:if test="not(@label)">THERE_IS_NO_LABEL</xsl:if>
				</xsl:with-param>
			</xsl:apply-templates>
		</common>
		<common formats="/html/xhtml/">
			<xsl:if test="@label"><a id="{@label}"></a></xsl:if>
			<xsl:apply-templates />
		</common>
	</template>
	
	
	<!-- Footnotes. -->
	<include href="modules/footnotes.xml" />
	

	
	<!-- Document preamble stuff: title, author, date. -->
	<template name="preamble-title" match="document/title" mode="preamble">
		<common formats="/latex/xelatex/">
			<xsl:text>\title{</xsl:text>
			<xsl:if test="/document/@class = 'assignment'">
				<xsl:if test="$showanswers='yes'">Sample Solution for </xsl:if>
				<xsl:call-template name="PaperCode" />
				<xsl:text> Assignment </xsl:text>
				<xsl:value-of select="/document/@sequence-number" />
				<xsl:text>: \\</xsl:text>
			</xsl:if>
			<xsl:apply-templates />
			<xsl:text>}</xsl:text>
		</common>
		<!--
			For HTML, strip out any raw HTML formatting (e.g., emphasis), as this is not interpreted within the <title> tag, resulting in raw HTML markup in the window title. Ick. We do this by switching to "strip" mode.
		-->
		<common formats="/html/xhtml/">
			<xsl:if test="( /document/@class = 'tutorial' ) or ( /document/@class = 'laboratory' ) or ( /document/@class = 'assignment' )">
				<xsl:if test="$showanswers='yes'">
					<xsl:choose>
						<xsl:when test="( /document/@class = 'tutorial' ) or ( /document/@class = 'laboratory' )">
							<xsl:text>Selected Answers for </xsl:text>
						</xsl:when>
						<xsl:when test="/document/@class = 'assignment'">
							<xsl:text>Sample Solution for </xsl:text>
						</xsl:when>
					</xsl:choose>
				</xsl:if>
				<xsl:call-template name="PaperCode" />
				<xsl:choose>
					<xsl:when test="/document/@class = 'tutorial'">
						<xsl:text> Tutorial </xsl:text>
					</xsl:when>
					<xsl:when test="/document/@class = 'laboratory'">
						<xsl:text> Lab </xsl:text>
					</xsl:when>
					<xsl:when test="/document/@class = 'assignment'">
						<xsl:text> Assignment </xsl:text>
					</xsl:when>
				</xsl:choose>
				<xsl:value-of select="/document/@sequence-number" />
				<xsl:text>: </xsl:text>
			</xsl:if>
			<xsl:apply-templates mode="strip" />
		</common>
	</template>

	<template name="preamble-author" match="document/author" mode="preamble">
		<common formats="/latex/xelatex/">\author{<xsl:apply-templates />}</common>
	</template>

	<template name="preamble-date" match="document/date" mode="preamble">
		<common formats="/latex/xelatex/">\date{<xsl:apply-templates />}</common>
	</template>

	<template name="document-title" match="document/title">
		<!-- Need to do something sensible for LaTeX here. -->
		<common formats="/latex/xelatex/">
			<!--
				Under the LaTeX framework, the rest of the chapter title text is generated by LaTeX macros, so this is pretty simple.
				I think this is broken?
				
				In fact, I think that for LaTeX documents, this template isn't called at all?? Looking at xml2xslt.xsl, if the document's standalone, "preamble-title" is used, and otherwise, "chapter-title" is used.
			-->
			<xsl:if test="/document/@standalone = 'yes'"><xsl:apply-templates /></xsl:if>
		</common>
		<common formats="/html/xhtml/">
			<h1>
				<xsl:if test="( /document/@class = 'tutorial' ) or ( /document/@class = 'laboratory' ) or ( /document/@class = 'assignment' )">
					<xsl:if test="$showanswers='yes'">
						<xsl:choose>
							<xsl:when test="( /document/@class = 'tutorial' ) or ( /document/@class = 'laboratory' )">
								<xsl:text>Selected Answers for </xsl:text>
							</xsl:when>
							<xsl:when test="/document/@class = 'assignment'">
								<xsl:text>Sample Solution for </xsl:text>
							</xsl:when>
						</xsl:choose>
					</xsl:if>
					<xsl:call-template name="PaperCode" />
					<xsl:choose>
						<xsl:when test="/document/@class = 'tutorial'">
							<xsl:text> Tutorial </xsl:text>
						</xsl:when>
						<xsl:when test="/document/@class = 'laboratory'">
							<xsl:text> Lab </xsl:text>
						</xsl:when>
						<xsl:when test="/document/@class = 'assignment'">
							<xsl:text> Assignment </xsl:text>
						</xsl:when>
					</xsl:choose>
					<xsl:value-of select="/document/@sequence-number" />
					<xsl:text>: </xsl:text>
				</xsl:if>
				<xsl:apply-templates />
			</h1>
		</common>
	</template>

	<!--
		New template for tutorials and labs, which are essentially chapters when included in a course book, but are marked up as documents in themselves.
	-->
	<template name="chapter-title" match="document/title" mode="chapter">
		<!--
			TODO: For LaTeX, filling in the "INFO XXX Tutorial", etc., is currently done in the LaTeX layer in the infrastructure surrounding the \lab and \tutorial macros (i.e., it's magic). This derives from the historical origins of the handbook, and should be moved here for consistency. An initial attempt to just replace calls to \tutorial, etc., with \chapter didn't work properly, as these macros do other things as well, including internal munging of chapter handling. This Will Be Complicated :(.
		-->
		<common formats="/latex/xelatex/">
			<xsl:choose>
				<xsl:when test="/document/@class = 'tutorial'">
					<xsl:text>\tutorial{</xsl:text>
				</xsl:when>
				<xsl:when test="/document/@class = 'laboratory'">
					<xsl:text>\lab{</xsl:text>
				</xsl:when>
				<xsl:otherwise>
					<!-- maybe we should assume that we're in a book documentclass and issue a \chapter here? -->
					<xsl:text>\general{</xsl:text>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:apply-templates />
			<xsl:text>}</xsl:text>
		</common>
		<common formats="/html/xhtml/">
			<xsl:choose>
				<xsl:when test="/document/@class = 'tutorial'">
					<h1><xsl:call-template name="PaperCode" /> Tutorial <xsl:value-of select="/document/@sequence-number" /><xsl:if test="$showanswers='yes'"> Sample Answers</xsl:if>: <br /><xsl:apply-templates /></h1>
				</xsl:when>
				<xsl:when test="/document/@class = 'laboratory'">
					<h1><xsl:call-template name="PaperCode" /> Lab <xsl:value-of select="/document/@sequence-number" /><xsl:if test="$showanswers='yes'"> Sample Answers</xsl:if>: <br /><xsl:apply-templates /></h1>
				</xsl:when>
				<xsl:otherwise>
					<h1><xsl:apply-templates /></h1>
				</xsl:otherwise>
			</xsl:choose>
		</common>
	</template>
	
	<template name="document-author" match="document/author">
		<common formats="/html/xhtml/"><p><xsl:apply-templates /></p></common>
	</template>
	
	<template name="document-date" match="document/date" />

	<!--
		Generate some number of "sub"s so we can produce, e.g., "subsubsection" for LaTeX.
		
		$depth: the number of repetitions (0 to 2, which maps to 1 to 3 below due to counting ancestor section elements coming out one more than expected).
	-->
	<template name="generate-subs">
		<common formats="/latex/xelatex/">
			<xsl:param name="depth">1</xsl:param>
			<xsl:if test="$depth &gt; 1">
				<xsl:if test="$depth &lt; 4">
					<xsl:text>sub</xsl:text>
				</xsl:if>
				<xsl:call-template name="generate-subs">
					<xsl:with-param name="depth" select="$depth - 1" />
				</xsl:call-template>
			</xsl:if>
		</common>
	</template>
	
	<!--
		Generate section title with nested numbering, e.g., 1.1.3.
	-->
	<template name="section-title" match="section/title">
		<common formats="/latex/xelatex/">
			<xsl:param name="label" />
			<xsl:text>\</xsl:text>
			<!-- Generate the correct number of "sub"s for LaTeX. -->
			<xsl:call-template name="generate-subs">
				<xsl:with-param name="depth" select="count( ancestor::section )" />
			</xsl:call-template>
			<xsl:text>section</xsl:text>
			<!--
				Unnumbered section. We reference the @number attribute of the parent section element. (We are guaranteed that the parent is a section element by the match context for this template.)
			-->
			<xsl:if test="../@number = 'no'">
				<xsl:text>*</xsl:text>
			</xsl:if>
			<xsl:text>{</xsl:text>
			<xsl:apply-templates /><xsl:text>}</xsl:text>
			<xsl:if test="$label != 'THERE_IS_NO_LABEL'">
				\label{<xsl:value-of select="$label" />}
			</xsl:if>
		</common>
		<common formats="/html/xhtml/">
			<!-- The depth is used in a couple of places, so we pre-calculate it. -->
			<xsl:variable name="depth">
				<xsl:number value="1 + count(ancestor::section)" />
				<xsl:if test="1 + count(ancestor::section) &gt; 6">6</xsl:if>
			</xsl:variable>
			<xsl:text disable-output-escaping="yes">&lt;h</xsl:text><xsl:number value="$depth" /><xsl:text disable-output-escaping="yes">&gt;</xsl:text>
			<!--
				Unnumbered section, as per above. Note that the sense of the test is reversed: with LaTeX we output _additional_ text to get an unnumbered section, where as with HTML, we _suppress_ the additional text (i.e., the section number, which is generated here). We also only count sections that _are_ numbered.
			-->
			<xsl:if test="not( ../@number ) or ( ../@number != 'no' )">
				<xsl:number count="section[not( @number ) or ( @number != 'no' )]" level="multiple" format="1.1.1.1.1.1" />
				<xsl:text> </xsl:text>
			</xsl:if>
			<xsl:apply-templates />
			<xsl:text disable-output-escaping="yes">&lt;/h</xsl:text><xsl:number value="$depth" /><xsl:text disable-output-escaping="yes">&gt;</xsl:text>
		</common>
	</template>
	
	
	
	
	<!-- Meta-elements. -->
	<include href="modules/meta-elements.xml" />
	
	<!-- Tabular structures (LaTeX {tabular}, HTML <table>). -->
	<include href="modules/tabular.xml" />
	
	<!-- Hyperlinks. -->
	<include href="modules/hyperlinks.xml" />

	<!-- Cross-references. -->
	<include href="modules/cross-references.xml" />
	
	<!-- "Floating" items such as figures and tables. -->
	<include href="modules/floaters.xml" />
	
	<!-- Images. -->
	<include href="modules/images.xml" />
	
	<!-- Include other documents. -->
	<include href="modules/inclusions.xml" />
	
	<!-- LaTeX-specific stuff. -->
	<include href="modules/latex.xml" />
	
	<!-- Embedding raw LaTeX or HTML code. -->
	<include href="modules/native-code.xml" />
	
	<!-- Conditional processing depending on the format. -->
	<include href="modules/conditional-processing.xml" />
		
	<!-- Mathematics. -->
	<include href="modules/maths.xml" />
	
	<!-- Otago-specific items. -->
	<include href="modules/otago-specific.xml" />
	
	<!-- Multi-column page layouts. -->
	<include href="modules/multi-column.xml" />
	
	<!-- Bibliographies and reference lists. -->
	<include href="modules/bibliography.xml" />
	
	<!-- Format numbers. -->
	<include href="modules/number-formatting.xml" />
	

</stylesheet>