| 1 |
<?xml version='1.0' encoding='UTF-8'?><!-- -*- indent-tabs-mode: nil -*- --> |
| 2 |
<!-- |
| 3 |
This program is free software; you can redistribute it and/or modify it under |
| 4 |
the terms of the GNU Lesser General Public License as published by the Free |
| 5 |
Software Foundation; either version 2 of the License, or (at your option) any |
| 6 |
later version. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 10 |
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 11 |
details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU Lesser General Public License |
| 14 |
along with this program; see the file COPYING.LGPL. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
--> |
| 16 |
|
| 17 |
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
| 18 |
xmlns:mal="http://projectmallard.org/1.0/" |
| 19 |
xmlns:cache="http://projectmallard.org/cache/1.0/" |
| 20 |
xmlns:gloss="http://projectmallard.org/experimental/gloss/" |
| 21 |
xmlns:exsl="http://exslt.org/common" |
| 22 |
xmlns:str="http://exslt.org/strings" |
| 23 |
exclude-result-prefixes="mal cache gloss exsl str" |
| 24 |
version="1.0"> |
| 25 |
|
| 26 |
<!--!!========================================================================== |
| 27 |
Mallard Glossaries |
| 28 |
Common templates for the Mallard Glossary extension. |
| 29 |
|
| 30 |
This stylesheet contains utility templates for locating and linking to terms |
| 31 |
with the Mallard Glossary extension. |
| 32 |
--> |
| 33 |
|
| 34 |
|
| 35 |
<!--++========================================================================== |
| 36 |
mal.gloss.key |
| 37 |
Get a `gloss:term` element from its `id` attribute. |
| 38 |
|
| 39 |
This key returns `gloss:term` elements based on their `id` attribute. This |
| 40 |
key only applies to elements inside a cache file. Make sure to make the cache |
| 41 |
file the context document before calling this key. |
| 42 |
--> |
| 43 |
<xsl:key name="mal.gloss.key" |
| 44 |
match="/cache:cache//mal:info/gloss:term[@id]" |
| 45 |
use="@id"/> |
| 46 |
|
| 47 |
|
| 48 |
<!--**========================================================================== |
| 49 |
mal.gloss.match |
| 50 |
Determine whether a glossary term matches a criterion. |
| 51 |
|
| 52 |
[xsl:params] |
| 53 |
$match: A `gloss:match` element containing criteria. |
| 54 |
$term: A `gloss:term` element to attempt to match. |
| 55 |
|
| 56 |
This template determines whether a glossary term matches a condition, as given |
| 57 |
by a `gloss:match` element. If the term matches, an empty string is output. |
| 58 |
Otherwise, a non-empty string is output. |
| 59 |
|
| 60 |
To determine if a term matches a set of matches, call this template for each |
| 61 |
`gloss:match` element, then check if the concatenated result is empty. |
| 62 |
--> |
| 63 |
<xsl:template name="mal.gloss.match"> |
| 64 |
<xsl:param name="match"/> |
| 65 |
<xsl:param name="term"/> |
| 66 |
<xsl:if test="$match/@tags and not(str:split($match/@tags) = str:split($term/@tags))"> |
| 67 |
<xsl:text>x</xsl:text> |
| 68 |
</xsl:if> |
| 69 |
</xsl:template> |
| 70 |
|
| 71 |
|
| 72 |
<!--**========================================================================== |
| 73 |
mal.gloss.terms |
| 74 |
Output the glossary terms for a page or section. |
| 75 |
|
| 76 |
[xsl:params] |
| 77 |
$node: The glossary `page` or `section` to output terms for. |
| 78 |
|
| 79 |
This template outputs the terms that should be displayed for $node.This output |
| 80 |
is a result tree fragment. To use these results, call `exsl:node-set` on them. |
| 81 |
This template locates all terms throughout all pages and filters them based on |
| 82 |
any `gloss:match` elements in the `info` child of $node, and also excludes |
| 83 |
terms that are matched by child sections of $node. |
| 84 |
|
| 85 |
The filtered terms are then grouped by matching ID. For each unique ID, this |
| 86 |
template outputs a `gloss:term` element with the corresponding `id` attribute. |
| 87 |
Each of these elements contains `title` elements reflecting the titles in the |
| 88 |
actual term definitions. These titles have duplicates removed, compared by the |
| 89 |
space-normalized string value, and are sorted. |
| 90 |
|
| 91 |
These `gloss:term` elements then contain further `gloss:term` elements, which |
| 92 |
are copies of the actual terms with the same ID. These elements have an `xref` |
| 93 |
attribute added containing the ID of the containing page. |
| 94 |
|
| 95 |
The top-level `gloss:term` elements and the `gloss:term` elements they contain |
| 96 |
are not sorted. Only the `title` elements in the top-level `gloss:term` |
| 97 |
elements are sorted. |
| 98 |
--> |
| 99 |
<xsl:template name="mal.gloss.terms"> |
| 100 |
<xsl:param name="node" select="."/> |
| 101 |
<xsl:variable name="allterms_"> |
| 102 |
<xsl:for-each select="$mal.cache//mal:info/gloss:term[@id]"> |
| 103 |
<xsl:variable name="term" select="."/> |
| 104 |
<xsl:variable name="exclude"> |
| 105 |
<xsl:for-each select="$node/ancestor-or-self::*/mal:info/gloss:match"> |
| 106 |
<xsl:call-template name="mal.gloss.match"> |
| 107 |
<xsl:with-param name="match" select="."/> |
| 108 |
<xsl:with-param name="term" select="$term"/> |
| 109 |
</xsl:call-template> |
| 110 |
</xsl:for-each> |
| 111 |
<xsl:for-each select="$node/mal:section/mal:info/gloss:match"> |
| 112 |
<xsl:variable name="secmatch"> |
| 113 |
<xsl:call-template name="mal.gloss.match"> |
| 114 |
<xsl:with-param name="match" select="."/> |
| 115 |
<xsl:with-param name="term" select="$term"/> |
| 116 |
</xsl:call-template> |
| 117 |
</xsl:variable> |
| 118 |
<xsl:if test="$secmatch = ''"> |
| 119 |
<xsl:text>x</xsl:text> |
| 120 |
</xsl:if> |
| 121 |
</xsl:for-each> |
| 122 |
</xsl:variable> |
| 123 |
<xsl:if test="$exclude = ''"> |
| 124 |
<xsl:copy> |
| 125 |
<xsl:attribute name="xref"> |
| 126 |
<xsl:value-of select="ancestor::mal:page[1]/@id"/> |
| 127 |
</xsl:attribute> |
| 128 |
<xsl:for-each select="@*[name() != 'xref'] | *"> |
| 129 |
<xsl:choose> |
| 130 |
<xsl:when test="self::mal:title"> |
| 131 |
<xsl:copy> |
| 132 |
<xsl:attribute name="title"> |
| 133 |
<xsl:value-of select="normalize-space(.)"/> |
| 134 |
</xsl:attribute> |
| 135 |
<xsl:copy-of select="node()"/> |
| 136 |
</xsl:copy> |
| 137 |
</xsl:when> |
| 138 |
<xsl:otherwise> |
| 139 |
<xsl:copy-of select="."/> |
| 140 |
</xsl:otherwise> |
| 141 |
</xsl:choose> |
| 142 |
</xsl:for-each> |
| 143 |
</xsl:copy> |
| 144 |
</xsl:if> |
| 145 |
</xsl:for-each> |
| 146 |
</xsl:variable> |
| 147 |
<xsl:variable name="allterms" select="exsl:node-set($allterms_)/gloss:term"/> |
| 148 |
<xsl:for-each select="$allterms"> |
| 149 |
<xsl:if test="not(@id = preceding-sibling::gloss:term/@id)"> |
| 150 |
<xsl:variable name="id" select="@id"/> |
| 151 |
<gloss:term id="{$id}"> |
| 152 |
<xsl:variable name="entries" select="$allterms/self::gloss:term[@id = $id]"/> |
| 153 |
<xsl:variable name="titles_"> |
| 154 |
<xsl:for-each select="$entries/mal:title"> |
| 155 |
<xsl:copy-of select="."/> |
| 156 |
</xsl:for-each> |
| 157 |
</xsl:variable> |
| 158 |
<xsl:variable name="titles" select="exsl:node-set($titles_)/mal:title"/> |
| 159 |
<xsl:for-each select="$titles"> |
| 160 |
<xsl:sort select="string(.)"/> |
| 161 |
<xsl:if test="not(@title = preceding-sibling::mal:title/@title)"> |
| 162 |
<xsl:copy-of select="."/> |
| 163 |
</xsl:if> |
| 164 |
</xsl:for-each> |
| 165 |
<xsl:copy-of select="$entries"/> |
| 166 |
</gloss:term> |
| 167 |
</xsl:if> |
| 168 |
</xsl:for-each> |
| 169 |
</xsl:template> |
| 170 |
|
| 171 |
</xsl:stylesheet> |