]> git.draconx.ca Git - homepage.git/blob - layouts/default.xsl
Put some actual material on the site.
[homepage.git] / layouts / default.xsl
1 <?xml version='1.0' encoding='UTF-8' ?>
2 <!--
3   Nick's web site: XHTML output stage
4
5   Copyright © 2018-2019 Nick Bowler
6
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.
11
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.
16
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/>
19 -->
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'>
28
29 <xsl:import href='layouts/functions.xsl' />
30
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' />
34
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"' />
38
39 <func:function name='f:ends-with'>
40   <xsl:param name='a' />
41   <xsl:param name='b' />
42   <func:result
43     select='substring($a, string-length($a)-string-length($b)+1)=$b' />
44 </func:function>
45
46 <xsl:template match='node()|@*'>
47   <xsl:copy><xsl:apply-templates select='node()|@*' /></xsl:copy>
48 </xsl:template>
49
50 <!--
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.
55
56   If an element has any text nodes at all, then it is not pretty-printed and
57   neither are any of its descendents.
58
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.
62 -->
63 <xsl:template match='xhtml:pre'>
64   <xsl:copy>
65     <xsl:apply-templates select='node()|@*' />
66     <xsl:text>&#x2060;</xsl:text>
67   </xsl:copy>
68 </xsl:template>
69
70 <!--
71   Likewise, adding spaces between consecutive span-level elements where
72   none existed before won't go over well.
73 -->
74 <xsl:template name='glue-preceding-span'>
75   <xsl:if test='f:element-is-span(preceding-sibling::node()[1])'>
76     <xsl:text>&#x2060;</xsl:text>
77   </xsl:if>
78 </xsl:template>
79
80 <xsl:template match='*[f:element-is-span()]'>
81   <xsl:call-template name='glue-preceding-span' />
82   <xsl:copy>
83     <xsl:apply-templates select='node()|@*' />
84     <xsl:if test='*'>
85       <!-- avoid breaking within a span element -->
86       <xsl:text>&#x2060;</xsl:text>
87     </xsl:if>
88   </xsl:copy>
89 </xsl:template>
90
91 <!--
92   Manually strip whitespace-only text nodes so the pretty printer can do its
93   thing on remaining elements.
94 -->
95 <xsl:template match='text()[normalize-space(.) = ""]'>
96   <xsl:choose>
97     <!-- preserve anything according to xml:space -->
98     <xsl:when test='ancestor::*[@xml:space][1][@xml:space="preserve"]'>
99       <xsl:copy />
100     </xsl:when>
101     <!-- preserve anything under <pre> -->
102     <xsl:when test='ancestor::xhtml:pre'><xsl:copy /></xsl:when>
103     <!-- preserve whitespace which is the only child node of an element -->
104     <xsl:when test='count(../node()) = 1'><xsl:copy /></xsl:when>
105   </xsl:choose>
106 </xsl:template>
107
108 <!-- Clean up whitespace where harmless to do so -->
109 <xsl:template match='xhtml:p/node()[1][self::text()]'>
110   <xsl:value-of select='f:strip-leading()' />
111 </xsl:template>
112 <xsl:template match='xhtml:p/node()[position()=last()][self::text()]'>
113   <xsl:value-of select='f:strip-trailing()' />
114 </xsl:template>
115
116 <!-- Add rel attributes to external links -->
117 <xsl:template match='xhtml:a[starts-with(@href,"http://")
118                           or starts-with(@href,"https://")
119                           or starts-with(@href,"//")]'>
120   <xsl:variable name='domain'
121     select='substring-before(
122               concat(translate(substring-after(@href, "//"), ":", "/"), "/"),
123               "/")' />
124
125   <xsl:copy>
126     <xsl:apply-templates select='@*' />
127     <xsl:if test='not($domain="draconx.ca"
128                       or f:ends-with($domain, ".draconx.ca"))'>
129       <xsl:attribute name='rel'>
130         <xsl:if test='@rel'>
131           <xsl:value-of select='@rel' />
132           <xsl:text> </xsl:text>
133         </xsl:if>
134         <xsl:text>external noopener noreferrer</xsl:text>
135       </xsl:attribute>
136     </xsl:if>
137     <xsl:apply-templates select='node()' />
138   </xsl:copy>
139 </xsl:template>
140
141 <xsl:template match='xhtml:h2[@id]'>
142   <xsl:variable name='fragment' select='concat("#", @id)' />
143   <xsl:copy>
144     <xsl:apply-templates select='node()|@*' />
145     <xsl:text> </xsl:text>
146     <small class='permalink'>
147       (<a href='{$fragment}'><xsl:value-of select='$fragment' /></a>)
148     </small>
149   </xsl:copy>
150 </xsl:template>
151
152 <xsl:template match='copyright'>
153   <p>
154     <xsl:text>Copyright © </xsl:text>
155     <xsl:value-of select='text()' />
156     <xsl:text>.</xsl:text>
157   </p>
158 </xsl:template>
159
160 <xsl:template match='license'>
161   <p>
162     <xsl:text>Copying and distribution of this material</xsl:text>
163     <xsl:if test='normalize-space(modification-allowed)="yes"'>
164       <xsl:text>, with or without modification,</xsl:text>
165     </xsl:if>
166     <xsl:text> is permitted under the terms of the </xsl:text>
167     <a rel='license'>
168       <xsl:attribute name='href'>
169         <xsl:value-of select='normalize-space(uri)' />
170       </xsl:attribute>
171       <xsl:value-of select='name' />
172     </a>
173     <xsl:text>.</xsl:text>
174   </p>
175 </xsl:template>
176
177 <xsl:template match='source'>
178   <p>
179     <xsl:text>This document was compiled from </xsl:text>
180     <a href='{concat($source-uri, revision, ":", file)}'>
181       <xsl:value-of select='file' />
182     </a>
183     <xsl:text> on </xsl:text>
184     <xsl:value-of select='compiletime' />
185     <xsl:text>.</xsl:text>
186   </p>
187 </xsl:template>
188
189 <xsl:template match='/'>
190   <html>
191     <head>
192       <meta name='viewport' content='width=device-width, initial-scale=1' />
193       <link rel='stylesheet' type='text/css' href='/style.css' />
194       <link rel="icon" href="data:," />
195       <title>
196         <xsl:variable name='page-title' select='string(/document/title)' />
197         <xsl:if test='$page-title and $site-title != $page-title'>
198           <xsl:value-of select='concat($page-title, " – ")' />
199         </xsl:if>
200         <xsl:value-of select='$site-title' />
201       </title>
202     </head>
203     <body>
204       <xsl:apply-templates select='/document/xhtml:html/@*' />
205
206       <xsl:if test='/document/hierarchy/parent'>
207         <p id='sitetitle'>
208           <small><xsl:value-of select='$site-title' /></small>
209         </p>
210         <div id='breadcrumbs'>
211           <strong>Return to: </strong>
212           <ul>
213             <xsl:for-each select='/document/hierarchy/parent'>
214               <li><a href='{uri}'><xsl:value-of select='name'/></a></li>
215             </xsl:for-each>
216           </ul>
217         </div>
218         <hr />
219       </xsl:if>
220
221       <xsl:apply-templates select='/document/xhtml:html/node()' />
222
223       <hr />
224       <div id='footer'>
225         <xsl:apply-templates select='/document/copyright' />
226         <xsl:apply-templates select='/document/license' />
227         <xsl:apply-templates select='/document/source' />
228       </div>
229     </body>
230   </html>
231 </xsl:template>
232
233 </xsl:stylesheet>