1 <?xml version='1.0' encoding='UTF-8' ?>
3 Nick's web site: XHTML output stage
5 Copyright © 2018-2019 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:f='http://draconx.ca/my-functions'
26 extension-element-prefixes='func f'
27 exclude-result-prefixes='xhtml'>
29 <xsl:import href='layouts/functions.xsl' />
31 <xsl:output method='xml' encoding='UTF-8' indent='yes'
32 doctype-public='-//W3C//DTD XHTML 1.1//EN'
33 doctype-system='http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd' />
35 <xsl:param name='source-uri'
36 select='"//git.draconx.ca/gitweb/homepage.git/blob/"' />
37 <xsl:param name='site-title' select='"The Citrine Citadel"' />
39 <func:function name='f:ends-with'>
40 <xsl:param name='a' />
41 <xsl:param name='b' />
43 select='substring($a, string-length($a)-string-length($b)+1)=$b' />
46 <xsl:template match='node()|@*'>
47 <xsl:copy><xsl:apply-templates select='node()|@*' /></xsl:copy>
51 Nokogiri's pretty-printer is a bit weird. Regardless of the indentation
52 setting, if an element has no child text nodes then it will be pretty-
53 printed. This works by adding arbitrary whitespace to that element, and
54 then all of its children are eligible to be pretty-printed.
56 If an element has any text nodes at all, then it is not pretty-printed and
57 neither are any of its descendents.
59 Adding arbitrary whitespace to <pre> is bad, so we inject zero-width non-
60 breaking spaces to prevent this. This will render fine but the spaces
61 should be removed before final output to avoid problems with copy+paste.
63 <xsl:template match='xhtml:pre'>
65 <xsl:apply-templates select='node()|@*' />
66 <xsl:text>⁠</xsl:text>
71 Likewise, adding spaces between consecutive span-level elements where
72 none existed before won't go over well.
74 <xsl:template name='glue-preceding-span'>
75 <xsl:if test='f:element-is-span(preceding-sibling::node()[1])'>
76 <xsl:text>⁠</xsl:text>
80 <xsl:template match='*[f:element-is-span()]'>
81 <xsl:call-template name='glue-preceding-span' />
83 <xsl:apply-templates select='node()|@*' />
84 <xsl:text>⁠</xsl:text> <!-- avoid breaking within a span element -->
89 Manually strip whitespace-only text nodes so the pretty printer can do its
90 thing on remaining elements.
92 <xsl:template match='text()[normalize-space(.) = ""]'>
94 <!-- preserve anything according to xml:space -->
95 <xsl:when test='ancestor::*[@xml:space][1][@xml:space="preserve"]'>
98 <!-- preserve anything under <pre> -->
99 <xsl:when test='ancestor::xhtml:pre'><xsl:copy /></xsl:when>
103 <!-- Clean up whitespace where harmless to do so -->
104 <xsl:template match='xhtml:p/node()[1][self::text()]'>
105 <xsl:value-of select='f:strip-leading()' />
107 <xsl:template match='xhtml:p/node()[position()=last()][self::text()]'>
108 <xsl:value-of select='f:strip-trailing()' />
111 <!-- Add rel attributes to external links -->
112 <xsl:template match='xhtml:a[starts-with(@href,"http://")
113 or starts-with(@href,"https://")
114 or starts-with(@href,"//")]'>
115 <xsl:variable name='domain'
116 select='substring-before(
117 concat(translate(substring-after(@href, "//"), ":", "/"), "/"),
121 <xsl:apply-templates select='@*' />
122 <xsl:if test='not($domain="draconx.ca"
123 or f:ends-with($domain, ".draconx.ca"))'>
124 <xsl:attribute name='rel'>
126 <xsl:value-of select='@rel' />
127 <xsl:text> </xsl:text>
129 <xsl:text>external noopener noreferrer</xsl:text>
132 <xsl:apply-templates select='node()' />
136 <xsl:template match='xhtml:h2[@id]'>
137 <xsl:variable name='fragment' select='concat("#", @id)' />
139 <xsl:apply-templates select='node()|@*' />
140 <xsl:text> </xsl:text>
141 <small class='permalink'>
142 (<a href='{$fragment}'><xsl:value-of select='$fragment' /></a>)
147 <xsl:template match='copyright'>
149 <xsl:text>Copyright © </xsl:text>
150 <xsl:value-of select='text()' />
151 <xsl:text>.</xsl:text>
155 <xsl:template match='license'>
157 <xsl:text>Copying and distribution of this material</xsl:text>
158 <xsl:if test='normalize-space(modification-allowed)="yes"'>
159 <xsl:text>, with or without modification,</xsl:text>
161 <xsl:text> is permitted under the terms of the </xsl:text>
163 <xsl:attribute name='href'>
164 <xsl:value-of select='normalize-space(uri)' />
166 <xsl:value-of select='name' />
168 <xsl:text>.</xsl:text>
172 <xsl:template match='source'>
174 <xsl:text>This document was compiled from </xsl:text>
175 <a href='{concat($source-uri, revision, ":", file)}'>
176 <xsl:value-of select='file' />
178 <xsl:text> on </xsl:text>
179 <xsl:value-of select='compiletime' />
180 <xsl:text>.</xsl:text>
184 <xsl:template match='/'>
187 <meta name='viewport' content='width=device-width, initial-scale=1' />
188 <link rel='stylesheet' type='text/css' href='/style.css' />
189 <link rel="icon" href="data:," />
191 <xsl:variable name='page-title' select='string(/document/title)' />
192 <xsl:if test='$page-title and $site-title != $page-title'>
193 <xsl:value-of select='concat($page-title, " – ")' />
195 <xsl:value-of select='$site-title' />
199 <xsl:apply-templates select='/document/xhtml:html/@*' />
200 <xsl:apply-templates select='/document/xhtml:html/node()' />
204 <xsl:apply-templates select='/document/copyright' />
205 <xsl:apply-templates select='/document/license' />
206 <xsl:apply-templates select='/document/source' />