1 <?xml version='1.0' encoding='UTF-8' ?>
3 Nick's web site: XHTML output stage
5 Copyright © 2018-2022 Nick Bowler
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <https://www.gnu.org/licenses/>
20 <xsl:stylesheet version='1.0'
21 xmlns='http://www.w3.org/1999/xhtml'
22 xmlns:xhtml='http://www.w3.org/1999/xhtml'
23 xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
24 xmlns:func='http://exslt.org/functions'
25 xmlns:exslt='http://exslt.org/common'
26 xmlns:f='http://draconx.ca/my-functions'
27 extension-element-prefixes='exslt func f'
28 exclude-result-prefixes='xhtml'>
30 <xsl:import href='layouts/functions.xsl' />
31 <xsl:output cdata-section-elements='style script' />
33 <xsl:param name='source-uri'
34 select='"//git.draconx.ca/gitweb/homepage.git/"' />
35 <xsl:param name='site-title' select='"The Citrine Citadel"' />
36 <xsl:param name='section-links' select='//document/section-links' />
38 <xsl:template match='node()|@*'>
39 <xsl:copy><xsl:apply-templates select='node()|@*' /></xsl:copy>
42 <xsl:template name='notransform' mode='notransform' match='node()|@*'>
44 <xsl:apply-templates mode='notransform' select='node()|@*' />
48 <!-- Add rel attributes to external links -->
49 <xsl:template match='xhtml:a[starts-with(@href,"http://")
50 or starts-with(@href,"https://")
51 or starts-with(@href,"//")]'>
52 <xsl:variable name='domain'
53 select='substring-before(
54 concat(translate(substring-after(@href, "//"), ":", "/"), "/"),
58 <xsl:apply-templates select='@*' />
59 <xsl:if test='not($domain="draconx.ca"
60 or f:ends-with($domain, ".draconx.ca"))'>
61 <xsl:attribute name='rel'>
63 <xsl:value-of select='@rel' />
64 <xsl:text> </xsl:text>
66 <xsl:text>external noopener noreferrer</xsl:text>
69 <xsl:apply-templates select='node()' />
73 <xsl:template match='xhtml:h2[@id]'>
74 <xsl:variable name='fragment' select='concat("#", @id)' />
76 <xsl:apply-templates select='node()|@*' />
77 <xsl:if test='$section-links = "yes"'>
78 <xsl:text> </xsl:text>
79 <small class='permalink'>
80 (<a href='{$fragment}'><xsl:value-of select='$fragment' /></a>)
87 Convert caption attribute on tables into proper caption elements, to allow
88 a simple way to add captions to kramdown tables.
90 <xsl:template match='@caption[parent::xhtml:table]' />
91 <xsl:template match='xhtml:table[@caption]'>
93 <xsl:apply-templates select='@*' />
94 <caption><xsl:value-of select='normalize-space(@caption)' /></caption>
95 <xsl:apply-templates select='node()' />
100 Delete style elements, as they will get hoisted occur under <head> below.
101 If the generate-listing attribute was specified, produce a code listing
102 where the style attribute was found.
104 <xsl:template match='xhtml:style|@generate-listing[parent::xhtml:style]' />
105 <xsl:template match='xhtml:style[@generate-listing]'>
106 <pre>⁠<code><xsl:value-of select='f:strip-leading(.)' /></code></pre>
110 Attempt to wrap the first bit of linked email addresses to allow
111 linewrapping to occur after the '@'.
113 <xsl:template match='xhtml:a[starts-with(@href,"mailto:")]/text()'>
114 <xsl:variable name='addr' select='substring-after(../@href, "mailto:")' />
116 <xsl:variable name='wrap'
117 select='concat(substring-before($addr, "@"), "@")' />
120 <xsl:when test='contains(., $wrap)'>
121 <xsl:value-of select='substring-before(., $wrap)' />
123 <xsl:value-of select='$wrap' />
125 <xsl:value-of select='substring-after(., $wrap)' />
134 Add a simple way to reference a document node by ID and include the XHTML
135 code listing directly in the document.
137 <xsl:template match='xhtml:generate-xhtml-listing'>
138 <xsl:variable name='target' select='@target' />
140 <xsl:value-of select='f:xhtml-listing(//xhtml:*[@id=$target])' />
144 <!-- For paragraphs containing only kbd elements, wrap in blockquote. -->
145 <xsl:template match='xhtml:p[*[last()=count(../xhtml:kbd|../xhtml:br)]]'>
148 <xsl:apply-templates select='node()|@*' />
154 Wrap each word of text in kbd elements in spans, so they can be styled
155 to avoid linebreaks in the middle of option names and other bad places.
157 <xsl:template name='spanify-text' match='xhtml:kbd/text()'>
158 <xsl:param name='text' select='normalize-space(.)' />
159 <xsl:variable name='firstword' select='substring-before($text, " ")' />
161 <xsl:when test='$firstword'>
162 <span><xsl:value-of select='$firstword' /></span>
163 <xsl:text> </xsl:text>
165 <xsl:when test='$text'>
166 <span><xsl:value-of select='$text' /></span>
169 <xsl:if test='$firstword'>
170 <xsl:call-template name='spanify-text'>
171 <xsl:with-param name='text' select='substring-after($text, " ")' />
176 <xsl:template match='copyright'>
178 <xsl:text>Copyright © </xsl:text>
179 <xsl:value-of select='text()' />
180 <xsl:text>.</xsl:text>
184 <xsl:template match='license'>
185 <xsl:variable name='node' select='.' />
188 <xsl:when test='/document/image[license/identifier != $node/identifier]'>
189 <xsl:text>Except as otherwise noted, copying</xsl:text>
191 <xsl:otherwise>Copying</xsl:otherwise>
193 <xsl:text> and distribution of this material</xsl:text>
194 <xsl:if test='normalize-space(modification-allowed)="yes"'>
195 <xsl:text>, with or without modification,</xsl:text>
197 <xsl:text> is permitted under the terms of the </xsl:text>
199 <xsl:attribute name='href'>
200 <xsl:value-of select='normalize-space(uri)' />
202 <xsl:value-of select='name' />
204 <xsl:text>.</xsl:text>
208 <func:function name='f:matching-child'>
209 <xsl:param name='child' select='./copyright-holder' />
210 <xsl:param name='node' select='.' />
211 <xsl:param name='nodeset' select='$node/../*[name()=name($node)]' />
213 <func:result select='$nodeset[*[name()=name($child)]=$child]' />
216 <func:function name='f:attribution-order'>
217 <xsl:param name='a' />
218 <xsl:param name='b' />
220 <xsl:variable name='docmatch'
221 select='number($a/copyright-holder = /document/copyright-holder)
222 - number($b/copyright-holder = /document/copyright-holder)' />
224 <xsl:variable name='authmatch'
225 select='count(f:matching-child($a/copyright-holder, $a))
226 - count(f:matching-child($b/copyright-holder, $b))' />
228 <xsl:variable name='licmatch'
229 select='count(f:matching-child($a/license, $a))
230 - count(f:matching-child($b/license, $b))' />
233 <xsl:when test='$docmatch'><func:result select='$docmatch' /></xsl:when>
234 <xsl:when test='$authmatch'><func:result select='$authmatch' /></xsl:when>
235 <xsl:when test='$licmatch'><func:result select='$licmatch' /></xsl:when>
236 <xsl:otherwise><func:result select='"nope"' /></xsl:otherwise>
240 <xsl:template match='image/license'>
241 <xsl:text>, </xsl:text>
242 <a href='{uri}' rel='license'><xsl:value-of select='shortname' /></a>
245 <xsl:template match='image'>
247 <xsl:when test='position() = 1'>, except </xsl:when>
248 <xsl:when test='position() = last()'> and </xsl:when>
249 <xsl:otherwise>, </xsl:otherwise>
251 <a href='{uri}'><xsl:value-of select='title' /></a>
252 <xsl:text> © </xsl:text>
253 <xsl:value-of select='copyright' />
254 <xsl:apply-templates select='license' />
257 <xsl:template name='image-attribution'>
258 <xsl:variable name='images-fragment'>
259 <xsl:for-each select='/document/image'>
260 <xsl:sort select='number(copyright-holder = /document/copyright-holder)'
261 data-type='number' order='descending' />
262 <xsl:sort select='count(f:matching-child(copyright-holder))'
263 data-type='number' order='descending' />
264 <xsl:sort select='copyright-holder' order='descending' />
265 <xsl:sort select='count(f:matching-child(license))'
266 data-type='number' order='descending' />
267 <xsl:sort select='license/identifier' order='descending' />
269 <xsl:call-template name='notransform' />
272 <xsl:variable name='images' select='exslt:node-set($images-fragment)/*' />
274 <xsl:variable name='abbrev-split'
275 select='count($images[copyright-holder = $images[1]/copyright-holder
276 and license/identifier = $images[1]/license/identifier])' />
278 <xsl:variable name='abbrev-years-fragment'>
279 <xsl:for-each select='$images[$abbrev-split >= position()]/copyright-year'>
280 <xsl:sort data-type='number' />
281 <copyright-year><xsl:value-of select='.' /></copyright-year>
284 <xsl:variable name='abbrev-years'
285 select='exslt:node-set($abbrev-years-fragment)/*' />
288 <xsl:text>Images © </xsl:text>
289 <xsl:value-of select='$abbrev-years[1]' />
290 <xsl:if test='$abbrev-years[last()] != $abbrev-years[1]'>
291 <xsl:value-of select='concat("–", $abbrev-years[last()])' />
293 <xsl:value-of select='concat(" ", $images[1]/copyright-holder)' />
294 <xsl:apply-templates select='$images[1]/license' />
295 <xsl:apply-templates select='$images[position() > $abbrev-split]' />
296 <xsl:text>.</xsl:text>
300 <xsl:template match='source'>
302 <xsl:text>This document was compiled</xsl:text>
304 <xsl:when test='file'>
305 <xsl:text> from </xsl:text>
306 <a href='{concat($source-uri, "blob/", revision, ":", file)}'>
307 <xsl:value-of select='file' />
310 <xsl:when test='dir'>
311 <xsl:text> from </xsl:text>
312 <a href='{concat($source-uri, "tree/", revision, ":", dir)}'>
313 <xsl:value-of select='dir' />
317 <xsl:text> on </xsl:text>
318 <xsl:value-of select='compiletime' />
319 <xsl:text>.</xsl:text>
323 <!-- Article info block inserted between heading and main contents -->
324 <xsl:template match='xhtml:h1[not(preceding::xhtml:h1)]'>
325 <xsl:variable name='nodes'
326 select='/document/article/published|//xhtml:*[@article-info]' />
328 <xsl:copy><xsl:apply-templates select='node()|@*' /></xsl:copy>
330 <xsl:if test='$nodes'>
331 <div id='article-info'>
332 <xsl:apply-templates mode='article-info' select='$nodes'>
333 <xsl:sort data-type='number'
334 select='number(generate-id(..)=generate-id(/document/article))' />
335 </xsl:apply-templates>
340 <xsl:template mode='article-info' match='/document/article/published'>
342 <xsl:text>Posted </xsl:text>
343 <xsl:value-of select='/document/article/published' />
344 <xsl:if test='/document/article/updated'>
345 <xsl:text>, last updated </xsl:text>
346 <xsl:value-of select='/document/article/updated' />
351 <xsl:template match='*[@article-info]' />
352 <xsl:template mode='article-info' match='*[@article-info]'>
354 <xsl:apply-templates select='@*[local-name() != "article-info"]' />
355 <xsl:apply-templates select='node()' />
359 <xsl:template name='imgpara' match='xhtml:p[count(*)=1]
360 [normalize-space(text())=""]
361 [descendant::xhtml:img]'>
363 <xsl:apply-templates select='@*[local-name() != "class"]' />
364 <xsl:attribute name='class'>
365 <xsl:if test='@class'>
366 <xsl:value-of select='concat(@class, " ")' />
368 <xsl:text>img</xsl:text>
370 <xsl:apply-templates select='node()' />
374 <xsl:key name='gallery'
375 match='xhtml:html/xhtml:p[descendant::*[@generate-gallery]]'
377 ( ( preceding-sibling::*[not(descendant::*[@generate-gallery])][1]
378 /following-sibling::* ) | self::* ) [1])' />
380 <xsl:template match='@generate-gallery' />
381 <xsl:template match='xhtml:html/xhtml:p[descendant::*[@generate-gallery]]' />
382 <xsl:template match='xhtml:html/xhtml:p[key("gallery", generate-id(.))]'>
383 <xsl:variable name='images' select='key("gallery", generate-id(.))' />
385 <xsl:when test='count($images) > 1'>
387 <xsl:attribute name='class'>
388 <xsl:text>gallery</xsl:text>
389 <xsl:if test='//xhtml:a[f:contains-token(@class, "left")
390 or f:contains-token(@class, "right")]'>
391 <xsl:text> inline</xsl:text>
394 <xsl:for-each select='$images'>
395 <xsl:call-template name='imgpara' />
400 <xsl:call-template name='imgpara' />
405 <xsl:template match='/'>
408 <meta name='viewport' content='width=device-width, initial-scale=1' />
409 <link rel='stylesheet' type='text/css' href='/style.css' />
410 <link rel='alternate stylesheet' type='text/css' href='/dark.css'
411 title='Dark Style' />
412 <link rel="icon" href="data:," />
414 <xsl:variable name='page-title' select='string(/document/title)' />
415 <xsl:if test='$page-title and $site-title != $page-title'>
416 <xsl:value-of select='concat($page-title, " – ")' />
418 <xsl:value-of select='$site-title' />
420 <!-- Hoist all style elements to <head> as required by the doctype. -->
421 <xsl:for-each select='//xhtml:style'>
422 <xsl:copy><xsl:apply-templates select='node()|@*' /></xsl:copy>
426 <xsl:apply-templates select='/document/xhtml:html/@*' />
428 <xsl:if test='/document/hierarchy/parent'>
430 <small><xsl:value-of select='$site-title' /></small>
432 <div id='breadcrumbs'>
433 <strong>Return to: </strong>
435 <xsl:for-each select='/document/hierarchy/parent'>
436 <li><a href='{uri}'><xsl:value-of select='name'/></a></li>
443 <xsl:apply-templates select='/document/xhtml:html/node()' />
447 <xsl:apply-templates select='/document/copyright' />
448 <xsl:apply-templates select='/document/license' />
449 <xsl:if test='/document/image[copyright != /document/copyright
450 or license/identifier != /document/license/identifier]'>
451 <xsl:call-template name='image-attribution' />
453 <xsl:apply-templates select='/document/source' />
459 <xsl:include href='layouts/whitespace.xsl' />
460 <xsl:include href='layouts/clickytable.xsl' />