-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathxmark.xsl
345 lines (339 loc) · 19.3 KB
/
xmark.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
<?xml version="1.0" encoding="UTF-8"?>
<!--
XMARK
XSLT Stylesheet to generate tufte-css styled web pages from CommonMark documents.
(C) 2016 Antonio Vieiro. MIT License.
-->
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:cm='http://commonmark.org/xml/1.0' xmlns:html='http://www.w3.org/1999/xhtml' version='1.1' exclude-result-prefixes='xsl cm html'>
<xsl:output indent='no' version='1.0' omit-xml-declaration='yes' encoding='UTF-8' method='xml' />
<xsl:strip-space elements='cm:heading cm:paragraph cm:text cm:link cm:emph pre'/>
<!-- options to generate table of contents and to include syntax highlighting -->
<xsl:param name="generate.toc" select="'yes'"/>
<xsl:param name='highlight' select="'yes'" />
<!-- title and subtitle -->
<xsl:variable name='title' select='/cm:document/cm:heading[@level = "1"][1]' />
<xsl:variable name='subtitle' select='/cm:document/cm:heading[@level = "2"][1]' />
<!-- entry point to common mark document (cm:document) -->
<xsl:template match="cm:document">
<!-- <!DOCTYPE html> -->
<xsl:text disable-output-escaping='yes'><!DOCTYPE html></xsl:text>
<html>
<head>
<xsl:if test='not($highlight = "no")'>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.1.0/styles/default.min.css" />
</xsl:if>
<link rel="stylesheet" href="tufte-css/tufte.css"/>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title><xsl:value-of select='$title/cm:text/text()' /></title>
<style type="text/css">
.toc-3 { margin-left: 2rem; }
.toc-4 { margin-left: 4rem; }
</style>
</head>
<body>
<article>
<h1><xsl:value-of select='$title/cm:text/text()' /></h1>
<p class='subtitle'><xsl:value-of select='$subtitle/cm:text/text()'/></p>
<!-- Include TOC or not as a margin note -->
<xsl:if test='not($generate.toc = "no")'>
<p>
<label for='toc' class='margin-toggle'>⊕ Table of Contents</label>
<xsl:element name='input'>
<xsl:attribute name='id'>toc</xsl:attribute>
<xsl:attribute name='type'>checkbox</xsl:attribute>
<xsl:attribute name='class'>margin-toggle</xsl:attribute>
</xsl:element>
<span class='marginnote'>
<strong>Table of Contents</strong><br />
<xsl:apply-templates select="//cm:heading" mode="toc"/>
</span>
</p>
</xsl:if>
<xsl:apply-templates/>
</article>
<xsl:if test='not($highlight = "no")'>
<script src='http://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.1.0/highlight.min.js'>//</script>
<script>
try {
hljs.initHighlightingOnLoad();
} catch(e) {
console.log(e);
}
</script>
</xsl:if>
</body>
</html>
</xsl:template>
<!-- heading entry for toc -->
<xsl:template match="cm:heading" mode="toc">
<!-- Ignore title and subtitle headings -->
<xsl:if test='. != $title and . != $subtitle'>
<xsl:element name="a">
<xsl:attribute name='class'>toc-<xsl:value-of select='@level' /></xsl:attribute>
<xsl:attribute name="href">#<xsl:value-of select="generate-id(.)"/></xsl:attribute>
<xsl:for-each select="cm:text">
<xsl:value-of select="text()"/>
</xsl:for-each>
</xsl:element>
<br />
</xsl:if>
</xsl:template>
<!-- heading entry for normal processing -->
<xsl:template match="cm:heading">
<!-- Ignore title and subtitle headings -->
<xsl:if test='. != $title and . != $subtitle'>
<xsl:element name="h{@level}">
<xsl:attribute name="id">
<xsl:value-of select="generate-id(.)"/>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:if>
</xsl:template>
<!-- paragraphs -->
<xsl:template match="cm:paragraph">
<p><xsl:apply-templates/></p>
</xsl:template>
<!-- block quotes and epigraphs -->
<xsl:template match='cm:block_quote'>
<xsl:choose>
<!-- if block_quote followed by another block_quote then this is an epigraph -->
<xsl:when test='*[self::cm:block_quote]'>
<div class='epigraph'>
<blockquote>
<xsl:apply-templates select='*[self::cm:block_quote]/*' />
</blockquote>
</div>
</xsl:when>
<!-- if block_quote followed by non block_quote then 'blockquote' -->
<xsl:otherwise>
<blockquote>
<xsl:apply-templates select='*[not(self::cm:block_quote)]' />
</blockquote>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- text nodes -->
<xsl:template match="cm:text">
<xsl:value-of select="text()"/>
</xsl:template>
<!-- links -->
<xsl:template match="cm:link">
<xsl:choose>
<!-- sidenote urls -->
<xsl:when test='starts-with(@destination, "sidenote")'>
<xsl:variable name='sidenote-id'>
<xsl:value-of select='generate-id(.)' />
</xsl:variable>
<label for='{$sidenote-id}' class='margin-toggle sidenote-number'></label>
<xsl:element name='input'>
<xsl:attribute name='id'><xsl:value-of select='$sidenote-id' /></xsl:attribute>
<xsl:attribute name='type'>checkbox</xsl:attribute>
<xsl:attribute name='class'>margin-toggle</xsl:attribute>
<span class='sidenote'>
<xsl:apply-templates />
</span>
</xsl:element>
</xsl:when>
<!-- margin urls -->
<xsl:when test='starts-with(@destination, "margin")'>
<xsl:variable name='sidenote-id'>
<xsl:value-of select='generate-id(.)' />
</xsl:variable>
<label for='{$sidenote-id}' class='margin-toggle'>⊕</label>
<xsl:element name='input'>
<xsl:attribute name='id'><xsl:value-of select='$sidenote-id' /></xsl:attribute>
<xsl:attribute name='type'>checkbox</xsl:attribute>
<xsl:attribute name='class'>margin-toggle</xsl:attribute>
<span class='marginnote'>
<xsl:apply-templates />
</span>
</xsl:element>
</xsl:when>
<!-- other normal urls -->
<xsl:otherwise>
<xsl:element name="a">
<xsl:attribute name="href">
<xsl:value-of select="@destination"/>
</xsl:attribute>
<xsl:attribute name="title">
<xsl:value-of select="@title"/>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- code spans (inline) -->
<xsl:template match='cm:code'>
<span class='code'>
<xsl:value-of select='text()' />
</span>
</xsl:template>
<!-- code blocks -->
<xsl:template match='cm:code_block'>
<pre class='code'>
<xsl:element name='code'>
<xsl:choose>
<xsl:when test='@info != ""'>
<xsl:attribute name='class'>language-<xsl:value-of select='@info'/> code</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name='class'>nohighlight code</xsl:attribute>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select='text()' />
</xsl:element>
</pre>
</xsl:template>
<!-- lists -->
<xsl:template match="cm:list">
<xsl:choose>
<xsl:when test="@type = "ordered"">
<xsl:element name="ol">
<xsl:attribute name="start">
<xsl:value-of select="@start"/>
</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:when>
<xsl:when test="@type = "bullet"">
<ul>
<xsl:apply-templates/>
</ul>
</xsl:when>
</xsl:choose>
</xsl:template>
<!-- list items -->
<xsl:template match="cm:item">
<li>
<xsl:apply-templates/>
</li>
</xsl:template>
<!-- softbreaks -->
<xsl:template match="cm:softbreak">
<xsl:text>
</xsl:text>
</xsl:template>
<!-- images -->
<xsl:template match="cm:image">
<!-- figure number -->
<xsl:variable name="fignumber" select="1+count(./preceding::cm:image)"/>
<xsl:choose>
<!-- margin figures -->
<xsl:when test='starts-with(@title, "margin")'>
<xsl:variable name='sidenote-id'><xsl:value-of select='generate-id(.)' /></xsl:variable>
<label for='{$sidenote-id}' class='margin-toggle'>⊕</label>
<xsl:element name='input'>
<xsl:attribute name='id'><xsl:value-of select='$sidenote-id' /></xsl:attribute>
<xsl:attribute name='type'>checkbox</xsl:attribute>
<xsl:attribute name='class'>margin-toggle</xsl:attribute>
<span class='marginnote'>
<xsl:element name="img">
<xsl:attribute name="src">
<xsl:value-of select="@destination"/>
</xsl:attribute>
<xsl:attribute name="title">
<xsl:value-of select='substring-after(@title, "margin")'/>
</xsl:attribute>
</xsl:element>
<xsl:apply-templates />
</span>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name='figure'>
<!-- full width figures -->
<xsl:if test='starts-with(@title, "fullwidth")'>
<xsl:attribute name='class'>fullwidth</xsl:attribute>
</xsl:if>
<xsl:element name="img">
<xsl:attribute name="src">
<xsl:value-of select="@destination"/>
</xsl:attribute>
<xsl:attribute name="title">
<xsl:choose>
<xsl:when test='starts-with(@title, "fullwidth")'>
<xsl:value-of select='substring-after(@title, "fullwidth")'/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="@title"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:element>
<figcaption>
Fig. <xsl:value-of select='$fignumber' />. <xsl:apply-templates/>
</figcaption>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- strong and em -->
<xsl:template match="cm:strong">
<strong>
<xsl:apply-templates/>
</strong>
</xsl:template>
<xsl:template match="cm:emph">
<em>
<xsl:apply-templates/>
</em>
</xsl:template>
<!--
Hack template to generate HTML elements from escaped stuff
>/hola> will generate </hola>
-->
<xsl:template name='html-end'>
<xsl:param name='text' />
<xsl:choose>
<xsl:when test='contains($text, ">")'>
<xsl:value-of select='substring-before($text, ">")' />
<xsl:text disable-output-escaping='yes'>></xsl:text>
<xsl:call-template name='html-start'>
<xsl:with-param name='text' select='substring-after($text, ">")' />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name='html-start'>
<xsl:with-param name='text' select='$text' />
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!--
template function to generate a start element
&myhtml; will generate <myhtml>
-->
<xsl:template name='html-start'>
<xsl:param name='text' />
<xsl:choose>
<xsl:when test='contains($text, "<")'>
<xsl:value-of select='substring-before($text, "<")' />
<xsl:text disable-output-escaping='yes'><</xsl:text>
<xsl:call-template name='html-end'>
<xsl:with-param name='text' select='substring-after($text, "<")' />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select='$text' />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- custom HTML entries in markdown are processed here -->
<xsl:template match="cm:html_block">
<xsl:call-template name='html-start'>
<xsl:with-param name='text' select='text()' />
</xsl:call-template>
<xsl:apply-templates />
<!--
Commonmark translates escapes HTML stuff in the XML tree, so, for instance
<aside> becomes <aside>
We would like to replace all < with < and > with > but this is challenging in XSL.
Mainly because XSL forbits '<' in attribute values to avoid the resulting XML being
malformed.
-->
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>