-
Notifications
You must be signed in to change notification settings - Fork 2
/
ahelp_common.xsl
446 lines (396 loc) · 16 KB
/
ahelp_common.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE xsl:stylesheet>
<!--* AHELP XML to HTML convertor using XSL Transformations
*
* Common templates used by ahelp.xsl and ahelp_index.xsl. These stylesheets
* will define a number of parameters used below (don't describe them here
* since they'd quickly become out of date)
*
* Notes:
* . we make use of EXSLT functions for date/time
* (see http://www.exslt.org/).
* Actually, could have used an input parameter to do this
*
* . for search/replace, use a function obtained from
* http://www.exslt.org/str/functions/replace/
* (not an actual EXSLT function since not implemented in libxslt)
*
* . do we need to sent the url parameter to templates now it is
* a 'global' variable?
*
*-->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="http://exslt.org/dates-and-times"
xmlns:str="http://exslt.org/strings"
xmlns:func="http://exslt.org/functions"
xmlns:exsl="http://exslt.org/common"
xmlns:extfuncs="http://hea-www.harvard.edu/~dburke/xsl/extfuncs"
extension-element-prefixes="date str func exsl extfuncs">
<!--* Change this if the filename changes *-->
<xsl:variable name="hack-import-ahelp_common" select="extfuncs:register-import-dependency('ahelp_common.xsl')"/>
<!-- since we can now include "arbitrary" XML in these pages,
because of the site banner, then we need to include
the full XSLT suite (or so it appears) -->
<!-- helper.xsl includes common.xsl -->
<xsl:include href="common.xsl"/>
<!-- too much overlap in helper; and it includes common
<xsl:include href="helper.xsl"/>
-->
<xsl:include href="links.xsl"/>
<xsl:include href="myhtml.xsl"/>
<!--*
* allowed values for these parameters (if there are any)
* - see check-param-allowed template for why want spaces
*-->
<xsl:variable name="allowed-types" select="' live test trial '"/>
<xsl:variable name="allowed-sites" select="' ciao sherpa '"/>
<!--* I THINK SOMETHING IS GOING WRONG .... *-->
<xsl:param name="depth" select="''"/>
<xsl:param name="favicon" select='""'/>
<!--*
* The current date (for the 'last modified' date); lastmod is not used for
* the individual ahelp pages, but lastmodiso is.
*
* Actually, I now want LASTMOD to be set to //LASTMODIFIED for individual
* ahelp files. So try to flip bewtwen the current date and //LASTMODIFIED
* automatically and see if this works.
*
*-->
<xsl:variable name="dt" select="date:date-time()"/>
<xsl:variable name="lastmod">
<xsl:choose>
<xsl:when test="boolean(//LASTMODIFIED)"><xsl:value-of select="//LASTMODIFIED"/></xsl:when>
<xsl:otherwise><xsl:value-of
select="concat(date:day-in-month($dt),' ',date:month-name($dt),' ',date:year($dt))"/></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<!--*
* On Lenin using XSLT version 1.1.28 the following fails with:
* xmlXPathCompOpEval: function month-in-year bound to undefined prefix date
* xmlXPathCompOpEval: function day-in-month bound to undefined prefix date
* so separating out the call to the date function and its use in xsl:number,
* as this seems to work
<xsl:variable name="month2"><xsl:number value="date:month-in-year($dt)" format="01"/></xsl:variable>
<xsl:variable name="day2"><xsl:number value="date:day-in-month($dt)" format="01"/></xsl:variable>
*
*-->
<xsl:variable name="hack-month2"><xsl:value-of select="date:month-in-year($dt)"/></xsl:variable>
<xsl:variable name="hack-day2"><xsl:value-of select="date:day-in-month($dt)"/></xsl:variable>
<xsl:variable name="month2"><xsl:number value="$hack-month2" format="01"/></xsl:variable>
<xsl:variable name="day2"><xsl:number value="$hack-day2" format="01"/></xsl:variable>
<xsl:variable name="lastmodiso"
select="concat(date:year($dt), '-', $month2, '-', $day2)"/>
<!--*
* Quit with an error message if:
* the parameter is undefined
* the parameter doesn't match it's allowed values (optional)
*
* Parameters:
* . pname - string, required
* the name of the parameter that has not been defined
* . pvalue - required
* the value of the parameter that has not been defined
* . allowed - string, optional
* a string containing all the allowed values separated
* on both sides, by a space:
* e.g. " foo bar "
* . slashcheck - integer, optional default=0
* if true then checks that the value ends in a "/"
*
* We could add ' ' to the start/end of $allowed, but can't be bothered
*
*-->
<xsl:template name="check-param">
<xsl:param name="pname"/>
<xsl:param name="pvalue"/>
<xsl:param name="allowed"/>
<xsl:param name="slashcheck" select="0"/>
<!--* check 'existence' *-->
<xsl:if test="$pvalue=''">
<xsl:message terminate="yes">
Error:
the stylesheet has been called without setting the required parameter
<xsl:value-of select="$pname"/>
</xsl:message>
</xsl:if>
<!--* check it's value (optional) *-->
<xsl:if test="$allowed != ''">
<xsl:if test="contains($allowed,concat(' ',$pvalue,' ')) = false()">
<xsl:message terminate="yes">
Error:
the parameter <xsl:value-of select="$pname"/> was set to <xsl:value-of select="$pvalue"/>
when it can only be one of:
<xsl:value-of select="$allowed"/>
</xsl:message>
</xsl:if>
</xsl:if>
<!--* does the parameter end in a / ? *-->
<xsl:if test="boolean($slashcheck)">
<xsl:if test="substring($pvalue,string-length($pvalue))!='/'">
<xsl:message terminate="yes">
Error: <xsl:value-of select="$pname"/> parameter must end in a / character.
<xsl:value-of select="$pname"/>=<xsl:value-of select="$pvalue"/>
</xsl:message>
</xsl:if>
</xsl:if>
</xsl:template> <!--* name=check-param *-->
<!--* start/end paragraphs *-->
<xsl:template name="start-para"><xsl:text disable-output-escaping="yes"><p></xsl:text></xsl:template>
<xsl:template name="end-para"><xsl:text disable-output-escaping="yes"></p></xsl:text></xsl:template>
<!--*
* We 'highlight' the text.
*
* Prior to CIAO 3.1 (v1.19 of this file) this template used to
* be split into two (add-start/end-highlight) AND it used to
* include checks for being called within a PARA block. We have
* consolidated things (which makes the code a lot nicer to read/write)
* and moved the logic for checking within PARA's out to the
* calling templates (since they know - from the DTD - whether they
* are in a PARA)
*
* The contents to highlight are supplied in the parameter contents
* and can be a node set (but already processed). This way we can
* handle a number of calling cases.
*
*-->
<xsl:template name="ahelp-add-highlight">
<xsl:param name="contents" select="''"/>
<xsl:if test="$contents = ''">
<xsl:message terminate="yes">
ERROR: ahelp-add-highlight called with no/empty contents parameter for key=<xsl:value-of select="//ENTRY/@key"/> context=<xsl:value-of select="//ENTRY/@context"/>
</xsl:message>
</xsl:if>
<!--* yay CSS (although highlight is a poor class name) *-->
<pre class="highlight"><xsl:copy-of select="$contents"/></pre>
</xsl:template> <!--* name=ahelp-add-highlight *-->
<!--*
* add a 'title' with an associated HTML anchor. The
* size of the title depends on where we are:
* h5 - within a PARAMLIST (as param title is h4)
* h3 - otherwise
*
* Parameters:
* title - string, required
*
*-->
<xsl:template name="add-title-anchor">
<xsl:param name="title"/>
<xsl:if test="$title=''">
<xsl:message terminate="yes">
*** ERROR: called add-title-anchor with an empty/missing title
</xsl:message>
</xsl:if>
<xsl:variable name="ctitle">
<xsl:value-of select="translate($title,' ','_')"/>
</xsl:variable>
<xsl:variable name="type"><xsl:choose>
<xsl:when test="ancestor::PARAM">h5</xsl:when>
<xsl:otherwise>h3</xsl:otherwise>
</xsl:choose></xsl:variable>
<!--*
* Try to guess whether it's safe to use $title as the anchor.
* This should really check against $ctitle and apply the
* conversion to @title, but that is excessive for now.
* I had tried moving the count out into a variable but then
* it created true/false strings and they are taken as
* being true (an empty string is false), so leave this note
* here for future me.
*
* would like to only write out the warning once but YOLO
* -->
<xsl:element name="{$type}">
<xsl:choose>
<xsl:when test="count(//@title[. = $title]) = 1">
<xsl:attribute name="id"><xsl:value-of select="$ctitle"/></xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:message terminate="no"> NOTE: multiple title='<xsl:value-of select="$title"/>' so can not use as an anchor/id</xsl:message>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="$title"/>
</xsl:element>
</xsl:template> <!--* name=add-title-anchor *-->
<!--*
* add a navbar and the spacer column
*
* Parameters:
* navbar - string, required
* included navbar_{$navbar}.incl
*-->
<xsl:template name="add-navbar">
<xsl:param name="navbar" select="''"/>
<xsl:call-template name="check-nonempty-param">
<xsl:with-param name="name" select="'navbar'"/>
<xsl:with-param name="value" select="$navbar"/>
<xsl:with-param name="template" select="'add-navbar'"/>
</xsl:call-template>
<!--* add the navbar *-->
<xsl:call-template name="add-ssi-include">
<xsl:with-param name="file" select="concat('navbar_', $navbar, '.incl')"/>
</xsl:call-template>
</xsl:template> <!--* name=add-navbar *-->
<!--* taken from helper.xsl *-->
<xsl:template name="add-start-tag">
<xsl:text disable-output-escaping="yes"><</xsl:text>
</xsl:template>
<xsl:template name="add-end-tag">
<xsl:text disable-output-escaping="yes">></xsl:text>
</xsl:template>
<xsl:template name="add-br">
<xsl:text disable-output-escaping="yes"><br></xsl:text>
</xsl:template>
<xsl:template name="add-hr">
<xsl:text disable-output-escaping="yes"><hr></xsl:text>
</xsl:template>
<xsl:template name="add-start-tr">
<xsl:text disable-output-escaping="yes"><tr></xsl:text>
</xsl:template>
<xsl:template name="add-end-tr">
<xsl:text disable-output-escaping="yes"></tr></xsl:text>
</xsl:template>
<xsl:template name="add-start-td">
<xsl:text disable-output-escaping="yes"><td></xsl:text>
</xsl:template>
<xsl:template name="add-end-td">
<xsl:text disable-output-escaping="yes"></td></xsl:text>
</xsl:template>
<xsl:template name="add-start-dl">
<xsl:text disable-output-escaping="yes"><dl></xsl:text>
</xsl:template>
<xsl:template name="add-end-dl">
<xsl:text disable-output-escaping="yes"></dl></xsl:text>
</xsl:template>
<xsl:template name="add-start-pre">
<xsl:text disable-output-escaping="yes"><pre></xsl:text>
</xsl:template>
<xsl:template name="add-end-pre">
<xsl:text disable-output-escaping="yes"></pre></xsl:text>
</xsl:template>
<!-- using myhtml version now
<xsl:template name="add-quote">
<xsl:text disable-output-escaping="yes">"</xsl:text>
</xsl:template>
<xsl:template name="add-nbsp">
<xsl:text disable-output-escaping="yes">&nbsp;</xsl:text>
</xsl:template>
-->
<!-- <xsl:template name="add-font-m1">
<xsl:call-template name="add-start-tag"/>font size="-1"<xsl:call-template name="add-end-tag"/>
</xsl:template>
<xsl:template name="add-end-font">
<xsl:call-template name="add-start-tag"/>/font<xsl:call-template name="add-end-tag"/>
</xsl:template>
-->
<!--***** START: TEMP SEARCH/REPLACE FUNCTION *****-->
<!--*
* the following was obtained from
* http://www.exslt.org/str/functions/replace/
* since libXSLT does not natively support str:replace
*
* Parameters:
* string - string, required
* the text to act on
* search - string, required
* the text to be replaced
* replace - string, required
* the replacement text
*
*
*-->
<func:function name="str:replace">
<xsl:param name="string" select="''" />
<xsl:param name="search" select="/.." />
<xsl:param name="replace" select="/.." />
<xsl:choose>
<xsl:when test="not($string)">
<func:result select="/.." />
</xsl:when>
<xsl:when test="function-available('exsl:node-set')">
<!-- this converts the search and replace arguments to node sets
if they are one of the other XPath types -->
<xsl:variable name="search-nodes-rtf">
<xsl:copy-of select="$search" />
</xsl:variable>
<xsl:variable name="replace-nodes-rtf">
<xsl:copy-of select="$replace" />
</xsl:variable>
<xsl:variable name="replacements-rtf">
<xsl:for-each select="exsl:node-set($search-nodes-rtf)/node()">
<xsl:variable name="pos" select="position()" />
<replace search="{.}">
<xsl:copy-of select="exsl:node-set($replace-nodes-rtf)/node()[$pos]" />
</replace>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="sorted-replacements-rtf">
<xsl:for-each select="exsl:node-set($replacements-rtf)/replace">
<xsl:sort select="string-length(@search)" data-type="number" order="descending" />
<xsl:copy-of select="." />
</xsl:for-each>
</xsl:variable>
<xsl:variable name="result">
<xsl:choose>
<xsl:when test="not($search)">
<xsl:value-of select="$string" />
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="str:_replace">
<xsl:with-param name="string" select="$string" />
<xsl:with-param name="replacements" select="exsl:node-set($sorted-replacements-rtf)/replace" />
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<func:result select="exsl:node-set($result)/node()" />
</xsl:when>
<xsl:otherwise>
<xsl:message terminate="yes">
ERROR: function implementation of str:replace() relies on exsl:node-set().
</xsl:message>
</xsl:otherwise>
</xsl:choose>
</func:function>
<xsl:template name="str:_replace">
<xsl:param name="string" select="''" />
<xsl:param name="replacements" select="/.." />
<xsl:choose>
<xsl:when test="not($string)" />
<xsl:when test="not($replacements)">
<xsl:value-of select="$string" />
</xsl:when>
<xsl:otherwise>
<xsl:variable name="replacement" select="$replacements[1]" />
<xsl:variable name="search" select="$replacement/@search" />
<xsl:choose>
<xsl:when test="not(string($search))">
<xsl:value-of select="substring($string, 1, 1)" />
<xsl:copy-of select="$replacement/node()" />
<xsl:call-template name="str:_replace">
<xsl:with-param name="string" select="substring($string, 2)" />
<xsl:with-param name="replacements" select="$replacements" />
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($string, $search)">
<xsl:call-template name="str:_replace">
<xsl:with-param name="string" select="substring-before($string, $search)" />
<xsl:with-param name="replacements" select="$replacements[position() > 1]" />
</xsl:call-template>
<xsl:copy-of select="$replacement/node()" />
<xsl:call-template name="str:_replace">
<xsl:with-param name="string" select="substring-after($string, $search)" />
<xsl:with-param name="replacements" select="$replacements" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="str:_replace">
<xsl:with-param name="string" select="$string" />
<xsl:with-param name="replacements" select="$replacements[position() > 1]" />
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!--***** END: TEMP SEARCH/REPLACE FUNCTION *****-->
</xsl:stylesheet> <!--* FIN *-->