/
utils.xsl
276 lines (228 loc) · 11.9 KB
/
utils.xsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<!DOCTYPE xsl:stylesheet>
<!--
Stylesheet with various utily templates, to be imported in tei2html.xsl.
Requires:
localization.xsl : templates for localizing strings.
-->
<xsl:stylesheet
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xd="http://www.pnp-software.com/XSLTdoc"
xmlns:f="urn:stylesheet-functions"
exclude-result-prefixes="f xs xd"
version="2.0"
>
<xd:doc type="stylesheet">
<xd:short>Utility templates and functions, used by tei2html</xd:short>
<xd:detail>This stylesheet contains a number of utility templates and functions, used by tei2html and tei2epub.</xd:detail>
<xd:author>Jeroen Hellingman</xd:author>
<xd:copyright>2011, Jeroen Hellingman</xd:copyright>
</xd:doc>
<!-- ID Generation
Use original ID's when possible to keep ID's stable between versions.
We use generated ID's prepended with 'x' to avoid clashes with original
ID's. Note that the target id generated here should also be generated
on the element being referenced. We cannot use the id() function here,
since we do not use a DTD.
-->
<xd:doc>
<xd:short>Generate an HTML anchor.</xd:short>
<xd:detail>Generate an HTML anchor with an <code>id</code> attribute for the current node.</xd:detail>
</xd:doc>
<xsl:template name="generate-anchor">
<a>
<xsl:call-template name="generate-id-attribute"/>
</a>
</xsl:template>
<xd:doc>
<xd:short>Generate an <code>id</code> attribute.</xd:short>
<xd:detail>Generate an <code>id</code> attribute for the current node.</xd:detail>
</xd:doc>
<xsl:template name="generate-id-attribute">
<xsl:attribute name="id">
<xsl:call-template name="generate-id"/>
</xsl:attribute>
</xsl:template>
<xd:doc>
<xd:short>Generate an <code>id</code>-attribute.</xd:short>
<xd:detail>Generate an <code>id</code>-attribute for a node (default: current node).</xd:detail>
<xd:param>The node for which the <code>id</code>-attribute is generated.</xd:param>
</xd:doc>
<xsl:template name="generate-id-attribute-for">
<xsl:param name="node" select="." as="element()"/>
<xsl:attribute name="id">
<xsl:call-template name="generate-id-for">
<xsl:with-param name="node" select="$node"/>
</xsl:call-template>
</xsl:attribute>
</xsl:template>
<xd:doc>
<xd:short>Generate an <code>id</code>-value.</xd:short>
<xd:detail>Generate an <code>id</code>-value for the current node.</xd:detail>
<xd:param name="node">The node for which the <code>id</code>-value is generated.</xd:param>
</xd:doc>
<xsl:template name="generate-id">
<xsl:call-template name="generate-id-for">
<xsl:with-param name="node" select="."/>
</xsl:call-template>
</xsl:template>
<xd:doc>
<xd:short>Generate a stable <code>id</code>-value.</xd:short>
<xd:detail>
<p>We want to generate ids that are slightly more stable than using generate-id().
The general idea is to use an explicit id if that is present, and otherwise create
an id based on the path to the first ancestor node that does have an id. If,
for example the third paragraph of a division with id '<code>ch2</code>' has no id of itself,
we generate: "<code>ch2_p_3</code>" as an id. The second note in this paragraph would receive
the id "<code>ch2_p_3_note_2</code>".</p>
<table>
<tr><th>Language </th><th>Safe ID syntax </th></tr>
<tr><td>HTML: </td><td><code>[A-Za-z][A-Za-z0-9_:.-]*</code></td></tr>
<tr><td>CSS: </td><td><code>-?[_a-zA-Z]+[_a-zA-Z0-9-]*</code></td></tr>
<tr><td>Combined: </td><td><code>[A-Za-z][A-Za-z0-9_-]*</code></td></tr>
</table>
</xd:detail>
<xd:param name="node">The node for which the <code>id</code>-value is generated (default: the current node).</xd:param>
</xd:doc>
<xsl:template name="generate-stable-id-for">
<xsl:param name="node" select="."/>
<xsl:variable name="node-name" select="name($node)"/>
<xsl:choose>
<!-- We have an explicit id, use that -->
<xsl:when test="$node/@id">
<xsl:message terminate="no">! <xsl:value-of select="$node-name"/> #<xsl:value-of select="$node/@id"/></xsl:message>
<!-- Verify the id is valid for use in HTML and CSS -->
<xsl:if test="not(matches($node/@id,'^[A-Za-z][A-Za-z0-9_-]*$'))">
<xsl:message terminate="no">Warning: source contains id [<xsl:value-of select="$node/@id"/>] that may cause problems in CSS.</xsl:message>
</xsl:if>
<xsl:value-of select="$node/@id"/>
</xsl:when>
<!-- We have reached the root without finding an explicit id -->
<xsl:when test="not($node/..)">
<xsl:message terminate="no">ROOT</xsl:message>
<xsl:text>root</xsl:text>
</xsl:when>
<!-- We have no explicit id: get the stable id of our parent and append ".<node-name>.<count>" -->
<xsl:otherwise>
<xsl:call-template name="generate-stable-id-for">
<xsl:with-param name="node" select="$node/.."/>
</xsl:call-template>
<xsl:text>_</xsl:text><xsl:value-of select="$node-name"/>
<!-- TODO: fix following to count correctly in recursive calls. -->
<xsl:variable name="position" select="count($node/preceding-sibling::*[name(current()) = $node-name])"/>
<xsl:if test="$position > 0">
<xsl:text>_</xsl:text><xsl:value-of select="$position"/>
</xsl:if>
<xsl:message terminate="no">- <xsl:value-of select="$node-name"/> [<xsl:value-of select="$position"/>]</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xd:doc>
<xd:short>Generate an <code>id</code>-value.</xd:short>
<xd:detail>
<p>Generate an <code>id</code>-value for a node (by default the current node).</p>
<p>The generated id will re-use the existing <code>id</code> attribute if present, or use the <code>generate-id()</code> function otherwise.
Such generated id's will be prefixed with the letter 'x'</p>
</xd:detail>
<xd:param name="node">The node for which the <code>id</code>-value is generated.</xd:param>
<xd:param name="position" type="string">A value appended after the generated <code>id</code>.</xd:param>
</xd:doc>
<xsl:template name="generate-id-for">
<xsl:param name="node" select="." as="element()"/>
<xsl:param name="position"/>
<!--
<xsl:call-template name="generate-stable-id-for">
<xsl:with-param name="node" select="$node" />
</xsl:call-template>
-->
<xsl:choose>
<xsl:when test="$node/@id">
<!-- Verify the id is valid for use in HTML and CSS -->
<xsl:if test="not(matches($node/@id,'^[A-Za-z][A-Za-z0-9_-]*$'))">
<xsl:message terminate="no">Warning: source contains id [<xsl:value-of select="$node/@id"/>] that may cause problems in CSS.</xsl:message>
</xsl:if>
<xsl:value-of select="$node/@id"/>
</xsl:when>
<xsl:otherwise>x<xsl:value-of select="generate-id($node)"/></xsl:otherwise>
</xsl:choose>
<xsl:if test="$position">-<xsl:value-of select="$position"/></xsl:if>
</xsl:template>
<xsl:template name="generate-href-attribute">
<xsl:param name="target" select="." as="element()"/>
<xsl:attribute name="href">
<xsl:call-template name="generate-href">
<xsl:with-param name="target" select="$target"/>
</xsl:call-template>
</xsl:attribute>
</xsl:template>
<xsl:template name="generate-footnote-href-attribute">
<xsl:param name="target" select="."/>
<xsl:attribute name="href">
<xsl:call-template name="generate-footnote-href">
<xsl:with-param name="target" select="$target"/>
</xsl:call-template>
</xsl:attribute>
</xsl:template>
<!--====================================================================-->
<!-- Close and Open paragraphs
To accomodate the differences between the TEI and HTML paragraph model,
we sometimes need to close (and reopen) paragraphs, as various elements
are not allowed inside p elements in HTML.
-->
<xsl:template name="closepar">
<!-- insert </p> to close current paragraph as tables in paragraphs are illegal in HTML -->
<xsl:if test="parent::p or parent::note">
<xsl:text disable-output-escaping="yes"></p></xsl:text>
</xsl:if>
</xsl:template>
<xsl:template name="reopenpar">
<xsl:if test="parent::p or parent::note">
<xsl:text disable-output-escaping="yes"><p></xsl:text>
</xsl:if>
</xsl:template>
<!--====================================================================-->
<!-- Language tagging -->
<xsl:template name="set-lang-attribute">
<xsl:if test="@lang">
<xsl:choose>
<xsl:when test="$outputmethod = 'xml'">
<xsl:attribute name="xml:lang"><xsl:value-of select="@lang"/></xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="lang"><xsl:value-of select="@lang"/></xsl:attribute>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
<!--====================================================================-->
<!-- Shortcut for both id and language tagging -->
<xsl:template name="set-lang-id-attributes">
<xsl:call-template name="generate-id-attribute"/>
<xsl:call-template name="set-lang-attribute"/>
</xsl:template>
<!--====================================================================-->
<!-- Generate labels for heads in the correct language -->
<xd:doc>
<xd:short>Translate the <code>type</code>-attribute of a division.</xd:short>
<xd:detail>
<p>Translate the <code>type</code>-attribute of a division to a string in the currently active language.</p>
</xd:detail>
<xd:param name="type" type="string">The value of the <code>type</code>-attribute.</xd:param>
</xd:doc>
<xsl:function name="f:translate-div-type" as="xs:string">
<xsl:param name="type"/>
<xsl:variable name="type" select="lower-case($type)"/>
<xsl:choose>
<xsl:when test="$type=''"><xsl:value-of select="''"/></xsl:when>
<xsl:when test="$type='appendix'"><xsl:value-of select="f:message('msgAppendix')"/></xsl:when>
<xsl:when test="$type='chapter'"><xsl:value-of select="f:message('msgChapter')"/></xsl:when>
<xsl:when test="$type='part'"><xsl:value-of select="f:message('msgPart')"/></xsl:when>
<xsl:when test="$type='book'"><xsl:value-of select="f:message('msgBook')"/></xsl:when>
<xsl:otherwise>
<xsl:message terminate="no">Warning: division's type attribute [<xsl:value-of select="$type"/>] not handled correctly in translate-div-type.</xsl:message>
<xsl:value-of select="''"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
</xsl:stylesheet>