- Split cross-references and hyperlinks into separate modules.
1 parent b0b537e commit 5352bbf8d8820b7ca097e792abbba3d9b31fcc64
nstanger authored on 26 Sep 2011
Showing 3 changed files
View
386
format-master.xml
<common formats="/html/xhtml/">
<strong style="border: 1px solid black;">!!<xsl:apply-templates />!!</strong>
</common>
</template>
 
<!-- Tabular structures (LaTeX {tabular}, HTML <table>). -->
<include href="modules/tabular.xml" />
<!--
Hyperlinks. The content of the element is the hyperlink text.
@label: A label that the hyperlink links to. The value should
be an acceptable label in both LaTeX and HTML. [optional]
@url: A URL that the hyperlink links to. If no link text is
provided, the URL is used as the link text. [optional]
-->
<template name="hyperlink-label" match="hyperlink[@label and node()]">
<common formats="/latex/xelatex/">\hyperref[<xsl:value-of select="@label" />]{<xsl:apply-templates />}</common>
<common formats="/html/xhtml/"><a href="#{@label}"><xsl:apply-templates /></a></common>
</template>
 
<template name="empty-hyperlink-label" match="hyperlink[@label and not(node())]">
<common>
<xsl:message terminate="yes">&lt;hyperlink&gt; with @label must include link text.</xsl:message>
</common>
</template>
 
<template name="hyperlink-url" match="hyperlink[@url and node()]">
<common formats="/latex/xelatex/">\href{<xsl:value-of select="@url" />}{<xsl:apply-templates />}</common>
<common formats="/html/xhtml/"><a href="{@url}"><xsl:apply-templates /></a></common>
</template>
 
<template name="empty-hyperlink-url" match="hyperlink[@url and not(node())]">
<!-- Note: not safe to use the url package here because \url{...} is fragile. -->
<common formats="/latex/xelatex/">\url{<xsl:value-of select="@url" />}</common>
<common formats="/html/xhtml/"><a href="{@url}"><code><xsl:value-of select="@url" /></code></a></common>
</template>
 
<template name="hyperlink-bogus" match="hyperlink[@url and @label]" priority="2">
<common>
<xsl:message terminate="yes">Cannot use both @url and @label in &lt;hyperlink&gt;.</xsl:message>
</common>
</template>
 
<!-- Internal parameterised hyperlink template to be called by the likes of the empty Oracle documentation elements. -->
<template name="hyperlink-internal" match="hyperlink" mode="hyperlink-internal">
<common>
<xsl:param name="url" />
<xsl:param name="label" />
</common>
<common formats="/latex/xelatex/">\href{<xsl:value-of select="$url" />}{<xsl:apply-templates select="exsl:node-set($label)" />}</common>
<common formats="/html/xhtml/"><a href="{$url}"><xsl:apply-templates select="exsl:node-set($label)" /></a></common>
</template>
 
<template name="empty-hyperlink-url-internal" match="hyperlink" mode="empty-hyperlink-internal">
<common><xsl:param name="url" /></common>
<common formats="/latex/xelatex/">\url{<xsl:value-of select="$url" />}</common>
<common formats="/html/xhtml/"><a href="{$url}"><code><xsl:value-of select="$url" /></code></a></common>
</template>
 
<!-- A plain URL (i.e., not necessarily a hyperlink). -->
<template name="url" match="url|uri|email|e-mail|email-address|e-mail-address">
<!--
Hmm, \url{...} appears to turn the URL into a non-functional
link in PDF output (probably an interaction with hyperref).
Not really what we want (as the link aspect is already dealt
with by the <hyperlink> templates), so we'll just use
\texttt{...}.
-->
<common formats="/latex/xelatex/">\nolinkurl{<xsl:apply-templates />}</common>
<common formats="/html/xhtml/"><code><xsl:apply-templates /></code></common>
</template>
 
 
<!--
References. Note the distinction betwen these and hyperlinks: a reference is merely something like "see Section 2.3", and doesn't necessarily imply a hyperlink, or vice versa.
@label: The label of the item that we're referencing. It must be a valid label in both LaTeX and HTML. [required]
@include-pageref: If "yes", also append a page reference (LaTeX only; this is handled by the "item-page-reference" template below).
-->
<template name="reference" match="reference[@label]">
<common>
<!-- Find the element whose label is the same as our label. -->
<xsl:apply-templates select="//*[@label = current()/@label]" mode="reference" />
<xsl:if test="@include-pageref = 'yes'">
<xsl:apply-templates select="//*[@label = current()/@label]" mode="page-reference" />
</xsl:if>
</common>
</template>
<template name="broken-reference" match="reference[not( @label )]">
<common>
<xsl:message terminate="yes">Reference elements must include a label attribute.</xsl:message>
</common>
</template>
<!-- Do a separate template for each type of referenced item; easier. -->
<template name="section-reference" match="section" mode="reference">
<!-- It's probably more consistent to just use \ref* and insert "Section" ourselves rather than let LaTeX do it. -->
<common formats="/latex/xelatex/">
<xsl:text>Section~\ref*{</xsl:text>
<xsl:value-of select="@label" />
<xsl:text>}</xsl:text>
</common>
<common formats="/html/xhtml/">
<xsl:text disable-output-escaping="yes">Section&amp;nbsp;</xsl:text>
<xsl:number count="section" level="multiple" format="1.1.1.1.1.1" />
</common>
</template>
<template name="figure-reference" match="figure" mode="reference">
<common formats="/latex/xelatex/">
<xsl:text>Figure~\ref*{</xsl:text>
<xsl:value-of select="@label" />
<xsl:text>}</xsl:text>
</common>
<common formats="/html/xhtml/">
<xsl:text disable-output-escaping="yes">Figure&amp;nbsp;</xsl:text>
<xsl:choose>
<xsl:when test="$showanswers='yes'">
<xsl:number count="figure" level="any" format="1" />
</xsl:when>
<xsl:otherwise>
<xsl:number count="figure[not(ancestor::answer) and not(ancestor::omit) and not(ancestor::comment)]" level="any" format="1" />
</xsl:otherwise>
</xsl:choose>
</common>
</template>
<template name="table-reference" match="table" mode="reference">
<common formats="/latex/xelatex/">
<xsl:text>Table~\ref*{</xsl:text>
<xsl:value-of select="@label" />
<xsl:text>}</xsl:text>
</common>
<common formats="/html/xhtml/">
<xsl:text disable-output-escaping="yes">Table&amp;nbsp;</xsl:text>
<xsl:choose>
<xsl:when test="$showanswers='yes'">
<xsl:number count="table" level="any" format="1" />
</xsl:when>
<xsl:otherwise>
<xsl:number count="table[not(ancestor::answer) and not(ancestor::omit) and not(ancestor::comment)]" level="any" format="1" />
</xsl:otherwise>
</xsl:choose>
</common>
</template>
<!--
Standalone direct reference to a page (i.e., not associated with some other item like a figure or table). This only really makes sense in LaTeX, as HTML doesn't have page numbers. For HTML, we fudge things by inserting the string "here", which will make sense if the reference is wrapped with a hyperlink (as they usually are).
-->
<template name="page-reference" match="page[@label]" mode="reference">
<common formats="/latex/xelatex/">
<xsl:text>page~\pageref{</xsl:text>
<xsl:value-of select="@label" />
<xsl:text>}</xsl:text>
</common>
<common formats="/html/xhtml/">
<xsl:text disable-output-escaping="yes">here</xsl:text>
</common>
</template>
<!--
Common template for appended page references, as they're identical for all items except for standalone page references, which aren't included in this template. The include-pageref attribute is therefore ignored on standalone page references.
-->
<template name="item-page-reference" match="section|figure|table" mode="page-reference">
<common formats="/latex/xelatex/">
<xsl:text> on page~\pageref{</xsl:text>
<xsl:value-of select="@label" />
<xsl:text>}</xsl:text>
</common>
</template>
<!--
Generate a standalone label for use in direct page references.
TODO: Hmm, this seems to generate the correct page number in LaTeX but the hyperlink is to the wrong page (one lower?). Something to worry about later.
@label: The label to be inserted. It must be a valid label in both LaTeX and HTML. [required]
-->
<template name="page" match="page[@label]">
<common formats="/latex/xelatex/">
<xsl:text>\label{</xsl:text>
<xsl:value-of select="@label" />
<xsl:text>}</xsl:text>
</common>
<common formats="/html/xhtml/">
<a id="{@label}"></a>
</common>
</template>
<template name="broken-page" match="page[not( @label )]">
<common>
<xsl:message terminate="yes">Page elements must include a label attribute.</xsl:message>
</common>
</template>
<!-- Hyperlinks. -->
<include href="modules/hyperlinks.xml" />
 
<!-- Cross-references. -->
<include href="modules/cross-references.xml" />
<!--
Figures & tables (in the LaTeX sense, i.e., "floating" items).
View
148
modules/cross-references.xml 0 → 100755
<?xml version="1.0" encoding="utf-8"?>
 
<!--
Cross-references. Note the distinction betwen these and hyperlinks: a cross-reference is merely something like "see Section 2.3", and doesn't necessarily imply a hyperlink, or vice versa.
The "reference" mode templates generate the main reference text, e.g., "see Section 2.3", "see Figure 1", etc. The "page-reference" mode template generate page reference text, e.g., "on page 23".
-->
 
<stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
 
<!--
Top level template for cross-references. There are separate templates below for each type of referenced item, as this is easier to implement.
@label: The label of the item that we're referencing. It must be a valid label in both LaTeX and HTML. [required]
@include-pageref: If "yes", also append a page reference (LaTeX only; this is handled by the "item-page-reference" template below).
-->
<template name="reference" match="reference[@label]">
<common>
<!-- Find the element whose label is the same as our label. -->
<xsl:apply-templates select="//*[@label = current()/@label]" mode="reference" />
<xsl:if test="@include-pageref = 'yes'">
<xsl:apply-templates select="//*[@label = current()/@label]" mode="page-reference" />
</xsl:if>
</common>
</template>
<!-- ERROR: reference element with no label. -->
<template name="broken-reference" match="reference[not( @label )]">
<common>
<xsl:message terminate="yes">Reference elements must include a label attribute.</xsl:message>
</common>
</template>
<!-- Section reference. -->
<template name="section-reference" match="section" mode="reference">
<!-- It's probably more consistent to just use \ref* and insert "Section" ourselves rather than let LaTeX do it. -->
<common formats="/latex/xelatex/">
<xsl:text>Section~\ref*{</xsl:text>
<xsl:value-of select="@label" />
<xsl:text>}</xsl:text>
</common>
<common formats="/html/xhtml/">
<xsl:text disable-output-escaping="yes">Section&amp;nbsp;</xsl:text>
<xsl:number count="section" level="multiple" format="1.1.1.1.1.1" />
</common>
</template>
<!-- Figure reference. -->
<template name="figure-reference" match="figure" mode="reference">
<common formats="/latex/xelatex/">
<xsl:text>Figure~\ref*{</xsl:text>
<xsl:value-of select="@label" />
<xsl:text>}</xsl:text>
</common>
<common formats="/html/xhtml/">
<xsl:text disable-output-escaping="yes">Figure&amp;nbsp;</xsl:text>
<xsl:choose>
<xsl:when test="$showanswers='yes'">
<xsl:number count="figure" level="any" format="1" />
</xsl:when>
<xsl:otherwise>
<xsl:number count="figure[not(ancestor::answer) and not(ancestor::omit) and not(ancestor::comment)]" level="any" format="1" />
</xsl:otherwise>
</xsl:choose>
</common>
</template>
<!-- Table reference. -->
<template name="table-reference" match="table" mode="reference">
<common formats="/latex/xelatex/">
<xsl:text>Table~\ref*{</xsl:text>
<xsl:value-of select="@label" />
<xsl:text>}</xsl:text>
</common>
<common formats="/html/xhtml/">
<xsl:text disable-output-escaping="yes">Table&amp;nbsp;</xsl:text>
<xsl:choose>
<xsl:when test="$showanswers='yes'">
<xsl:number count="table" level="any" format="1" />
</xsl:when>
<xsl:otherwise>
<xsl:number count="table[not(ancestor::answer) and not(ancestor::omit) and not(ancestor::comment)]" level="any" format="1" />
</xsl:otherwise>
</xsl:choose>
</common>
</template>
<!--
Common template for item page references, as they're identical for all items. Standalone page references (i.e., not to a specific item) are dealt with below.
-->
<template name="item-page-reference" match="section|figure|table" mode="page-reference">
<common formats="/latex/xelatex/">
<xsl:text> on page~\pageref{</xsl:text>
<xsl:value-of select="@label" />
<xsl:text>}</xsl:text>
</common>
</template>
<!--
Generate a standalone label for use in direct page references. Any content within the element is ignored.
TODO: Hmm, this seems to generate the correct page number in LaTeX but the hyperlink is to the wrong page (one lower?). Something to worry about later.
@label: The label to be inserted. It must be a valid label in both LaTeX and HTML. [required]
-->
<template name="page" match="page[@label]">
<common formats="/latex/xelatex/">
<xsl:text>\label{</xsl:text>
<xsl:value-of select="@label" />
<xsl:text>}</xsl:text>
</common>
<common formats="/html/xhtml/">
<a id="{@label}"></a>
</common>
</template>
<!-- ERROR: page element with no label. -->
<template name="broken-page" match="page[not( @label )]">
<common>
<xsl:message terminate="yes">Page elements must include a label attribute.</xsl:message>
</common>
</template>
 
 
<!--
Standalone direct reference to a page (i.e., not associated with some other item like a figure or table). This only really makes sense in LaTeX, as HTML doesn't have page numbers. For HTML, we fudge things by inserting the string "here", which will make sense if the reference is wrapped with a hyperlink (as they usually should be).
-->
<template name="direct-page-reference" match="page[@label]" mode="reference">
<common formats="/latex/xelatex/">
<xsl:text>page~\pageref{</xsl:text>
<xsl:value-of select="@label" />
<xsl:text>}</xsl:text>
</common>
<common formats="/html/xhtml/">
<xsl:text disable-output-escaping="yes">here</xsl:text>
</common>
</template>
</stylesheet>
View
modules/hyperlinks.xml 0 → 100755