forked from chrisconlon/micro-metrics
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmxdom2simplehtml.xsl
454 lines (384 loc) · 15.4 KB
/
mxdom2simplehtml.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
447
448
449
450
451
452
453
454
<?xml version="1.0" encoding="utf-8"?>
<!--
This is an XSL stylesheet which converts mscript XML files into HTML.
Use the XSLT command to perform the conversion.
Copyright 1984-2015 The MathWorks, Inc.
-->
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp " "> <!ENTITY reg "®"> ]>
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:mwsh="http://www.mathworks.com/namespace/mcode/v1/syntaxhighlight.dtd"
exclude-result-prefixes="mwsh">
<xsl:output method="html"
indent="no"
doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"/>
<xsl:strip-space elements="mwsh:code"/>
<xsl:variable name="title">
<xsl:variable name="dTitle" select="//steptitle[@style='document']"/>
<xsl:choose>
<xsl:when test="$dTitle"><xsl:value-of select="$dTitle"/></xsl:when>
<xsl:otherwise><xsl:value-of select="mscript/m-file"/></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:template match="mscript">
<html>
<!-- head -->
<head>
<xsl:comment>
This HTML was auto-generated from MATLAB code.
To make changes, update the MATLAB code and republish this document.
</xsl:comment>
<title><xsl:value-of select="$title"/></title>
<meta name="generator">
<xsl:attribute name="content">MATLAB <xsl:value-of select="version"/></xsl:attribute>
</meta>
<link rel="schema.DC" href="http://purl.org/dc/elements/1.1/" />
<meta name="DC.date">
<xsl:attribute name="content"><xsl:value-of select="date"/></xsl:attribute>
</meta>
<meta name="DC.source">
<xsl:attribute name="content"><xsl:value-of select="m-file"/>.m</xsl:attribute>
</meta>
<xsl:call-template name="stylesheet"/>
</head>
<body>
<xsl:call-template name="header"/>
<div class="content">
<!-- Determine if the there should be an introduction section. -->
<xsl:variable name="hasIntro" select="count(cell[@style = 'overview'])"/>
<!-- If there is an introduction, display it. -->
<xsl:if test = "$hasIntro">
<h1><xsl:apply-templates select="cell[1]/steptitle"/></h1>
<xsl:comment>introduction</xsl:comment>
<xsl:apply-templates select="cell[1]/text"/>
<!-- There can be text output if there was a parse error. -->
<xsl:apply-templates select="cell[1]/mcodeoutput"/>
<xsl:comment>/introduction</xsl:comment>
</xsl:if>
<xsl:variable name="body-cells" select="cell[not(@style = 'overview')]"/>
<!-- Include contents if there are titles for any subsections. -->
<xsl:if test="count(cell/steptitle[not(@style = 'document')])">
<xsl:call-template name="contents">
<xsl:with-param name="body-cells" select="$body-cells"/>
</xsl:call-template>
</xsl:if>
<!-- Loop over each cell -->
<xsl:for-each select="$body-cells">
<!-- Title of cell -->
<xsl:if test="steptitle">
<xsl:variable name="headinglevel">
<xsl:choose>
<xsl:when test="steptitle[@style = 'document']">h1</xsl:when>
<xsl:otherwise>h2</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:element name="{$headinglevel}">
<xsl:if test="not(steptitle[@style = 'document'])">
<xsl:attribute name="id">
<xsl:value-of select="position()"/>
</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="steptitle"/>
</xsl:element>
</xsl:if>
<!-- Contents of each cell -->
<xsl:apply-templates select="text"/>
<xsl:apply-templates select="mcode-xmlized"/>
<xsl:apply-templates select="mcodeoutput|img"/>
</xsl:for-each>
<xsl:call-template name="footer"/>
</div>
<!-- Include MathJax scripts -->
<!-- (only when needed: img equations, embedded latex, or latex output) -->
<xsl:if test="count(//img[@class='equation']) or count(//latex) or count(//mcodeoutput[starts-with(normalize-space(.),'<latex>')])">
<xsl:call-template name="mathjax"/>
</xsl:if>
<xsl:apply-templates select="originalCode"/>
</body>
</html>
</xsl:template>
<xsl:template name="stylesheet">
<style type="text/css">
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,font,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td{margin:0;padding:0;border:0;outline:0;font-size:100%;vertical-align:baseline;background:transparent}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:before,blockquote:after,q:before,q:after{content:'';content:none}:focus{outine:0}ins{text-decoration:none}del{text-decoration:line-through}table{border-collapse:collapse;border-spacing:0}
html { min-height:100%; margin-bottom:1px; }
html body { height:100%; margin:0px; font-family:Arial, Helvetica, sans-serif; font-size:10px; color:#000; line-height:140%; background:#fff none; overflow-y:scroll; }
html body td { vertical-align:top; text-align:left; }
h1 { padding:0px; margin:0px 0px 25px; font-family:Arial, Helvetica, sans-serif; font-size:1.5em; color:#d55000; line-height:100%; font-weight:normal; }
h2 { padding:0px; margin:0px 0px 8px; font-family:Arial, Helvetica, sans-serif; font-size:1.2em; color:#000; font-weight:bold; line-height:140%; border-bottom:1px solid #d6d4d4; display:block; }
h3 { padding:0px; margin:0px 0px 5px; font-family:Arial, Helvetica, sans-serif; font-size:1.1em; color:#000; font-weight:bold; line-height:140%; }
a { color:#005fce; text-decoration:none; }
a:hover { color:#005fce; text-decoration:underline; }
a:visited { color:#004aa0; text-decoration:none; }
p { padding:0px; margin:0px 0px 20px; }
img { padding:0px; margin:0px 0px 20px; border:none; }
p img, pre img, tt img, li img, h1 img, h2 img { margin-bottom:0px; }
ul { padding:0px; margin:0px 0px 20px 23px; list-style:square; }
ul li { padding:0px; margin:0px 0px 7px 0px; }
ul li ul { padding:5px 0px 0px; margin:0px 0px 7px 23px; }
ul li ol li { list-style:decimal; }
ol { padding:0px; margin:0px 0px 20px 0px; list-style:decimal; }
ol li { padding:0px; margin:0px 0px 7px 23px; list-style-type:decimal; }
ol li ol { padding:5px 0px 0px; margin:0px 0px 7px 0px; }
ol li ol li { list-style-type:lower-alpha; }
ol li ul { padding-top:7px; }
ol li ul li { list-style:square; }
.content { font-size:1.2em; line-height:140%; padding: 20px; }
pre, code { font-size:12px; }
tt { font-size: 1.2em; }
pre { margin:0px 0px 20px; }
pre.codeinput { padding:10px; border:1px solid #d3d3d3; background:#f7f7f7; }
pre.codeoutput { padding:10px 11px; margin:0px 0px 20px; color:#4c4c4c; }
pre.error { color:red; }
@media print { pre.codeinput, pre.codeoutput { word-wrap:break-word; width:100%; } }
span.keyword { color:#0000FF }
span.comment { color:#228B22 }
span.string { color:#A020F0 }
span.untermstring { color:#B20000 }
span.syscmd { color:#B28C00 }
.footer { width:auto; padding:10px 0px; margin:25px 0px 0px; border-top:1px dotted #878787; font-size:0.8em; line-height:140%; font-style:italic; color:#878787; text-align:left; float:none; }
.footer p { margin:0px; }
.footer a { color:#878787; }
.footer a:hover { color:#878787; text-decoration:underline; }
.footer a:visited { color:#878787; }
table th { padding:7px 5px; text-align:left; vertical-align:middle; border: 1px solid #d6d4d4; font-weight:bold; }
table td { padding:7px 5px; text-align:left; vertical-align:top; border:1px solid #d6d4d4; }
</style>
</xsl:template>
<!-- Header -->
<xsl:template name="header">
</xsl:template>
<!-- Footer -->
<xsl:template name="footer">
<p class="footer">
<xsl:value-of select="copyright"/><br/>
<a href="https://www.mathworks.com/products/matlab/">Published with MATLAB® R<xsl:value-of select="release"/></a><br/>
</p>
</xsl:template>
<!-- MathJax -->
<xsl:template name="mathjax">
<script type="text/x-mathjax-config">
// https://stackoverflow.com/a/14631703/97160
MathJax.Extension.myImg2jax = {
version: "1.0",
PreProcess: function (element) {
var images = element.getElementsByTagName("img");
for (var i = images.length - 1; i >= 0; i--) {
var img = images[i];
if (img.className === "equation") {
var match = img.alt.match(/^(\$\$?)([\s\S]*)\1$/m);
if (!match) continue;
var script = document.createElement("script");
script.type = "math/tex";
if (match[1] === "$$") {script.type += ";mode=display"}
MathJax.HTML.setScript(script, match[2]);
img.parentNode.replaceChild(script, img);
}
}
}
};
MathJax.Hub.Register.PreProcessor(["PreProcess", MathJax.Extension.myImg2jax]);
</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-AMS_CHTML"></script>
</xsl:template>
<!-- Contents -->
<xsl:template name="contents">
<xsl:param name="body-cells"/>
<h2>Contents</h2>
<div><ul>
<xsl:for-each select="$body-cells">
<xsl:if test="./steptitle">
<li><a><xsl:attribute name="href">#<xsl:value-of select="position()"/></xsl:attribute><xsl:apply-templates select="steptitle"/></a></li>
</xsl:if>
</xsl:for-each>
</ul></div>
</xsl:template>
<!-- HTML Tags in text sections -->
<xsl:template match="p">
<p><xsl:apply-templates/></p>
</xsl:template>
<xsl:template match="ul">
<div><ul><xsl:apply-templates/></ul></div>
</xsl:template>
<xsl:template match="ol">
<div><ol><xsl:apply-templates/></ol></div>
</xsl:template>
<xsl:template match="li">
<li><xsl:apply-templates/></li>
</xsl:template>
<xsl:template match="pre">
<xsl:choose>
<xsl:when test="@class='error'">
<pre class="error"><xsl:apply-templates/></pre>
</xsl:when>
<xsl:otherwise>
<pre><xsl:apply-templates/></pre>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="b">
<b><xsl:apply-templates/></b>
</xsl:template>
<xsl:template match="i">
<i><xsl:apply-templates/></i>
</xsl:template>
<xsl:template match="tt">
<tt><xsl:apply-templates/></tt>
</xsl:template>
<xsl:template match="a">
<a>
<xsl:attribute name="href"><xsl:value-of select="@href"/></xsl:attribute>
<xsl:apply-templates/>
</a>
</xsl:template>
<xsl:template match="html">
<xsl:value-of select="@text" disable-output-escaping="yes"/>
</xsl:template>
<xsl:template match="latex"/>
<!-- Detecting M-Code in Comments-->
<xsl:template match="text/mcode-xmlized">
<pre class="language-matlab"><xsl:apply-templates/><xsl:text><!-- g162495 -->
</xsl:text></pre>
</xsl:template>
<!-- Code input and output -->
<xsl:template match="mcode-xmlized">
<pre class="codeinput"><xsl:apply-templates/><xsl:text><!-- g162495 -->
</xsl:text></pre>
</xsl:template>
<xsl:template match="mcodeoutput">
<xsl:choose>
<xsl:when test="concat(substring(.,0,7),substring(.,string-length(.)-7,7))='<html></html>'">
<xsl:value-of select="substring(.,7,string-length(.)-14)" disable-output-escaping="yes"/>
</xsl:when>
<xsl:otherwise>
<pre>
<xsl:attribute name="class">
<xsl:value-of select="@class"/>
</xsl:attribute>
<xsl:apply-templates/>
</pre>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<!-- Figure and model snapshots and equations -->
<xsl:template match="img[@class='equation']">
<img>
<!-- alt and class are used by MathJax when replacing image with rendered equation -->
<xsl:attribute name="src"><xsl:value-of select="@src"/></xsl:attribute>
<xsl:attribute name="alt"><xsl:value-of select="@alt"/></xsl:attribute>
<xsl:attribute name="class"><xsl:value-of select="@class"/></xsl:attribute>
<xsl:choose>
<xsl:when test="@scale">
<xsl:attribute name="style">
<xsl:if test="@width">
<xsl:text>width:</xsl:text>
<xsl:value-of select="@width"/>
<xsl:text>;</xsl:text>
</xsl:if>
<xsl:if test="@height">
<xsl:text>height:</xsl:text>
<xsl:value-of select="@height"/>
<xsl:text>;</xsl:text>
</xsl:if>
</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:if test="@width">
<xsl:attribute name="width">
<xsl:value-of select="substring-before(@width,'px')"/>
</xsl:attribute>
</xsl:if>
<xsl:if test="@height">
<xsl:attribute name="height">
<xsl:value-of select="substring-before(@height,'px')"/>
</xsl:attribute>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</img>
</xsl:template>
<xsl:template match="img">
<img vspace="5" hspace="5">
<xsl:attribute name="src"><xsl:value-of select="@src"/></xsl:attribute>
<xsl:if test="@width or @height">
<xsl:attribute name="style">
<xsl:if test="@width">
<xsl:text>width:</xsl:text>
<xsl:value-of select="@width"/>
<xsl:text>;</xsl:text>
</xsl:if>
<xsl:if test="@height">
<xsl:text>height:</xsl:text><xsl:value-of select="@height"/>
<xsl:text>;</xsl:text>
</xsl:if>
</xsl:attribute>
</xsl:if>
<xsl:attribute name="alt"><xsl:value-of select="@alt"/></xsl:attribute>
<xsl:text> </xsl:text>
</img>
</xsl:template>
<!-- Stash original code in HTML for easy slurping later. -->
<xsl:template match="originalCode">
<xsl:variable name="xcomment">
<xsl:call-template name="globalReplace">
<xsl:with-param name="outputString" select="."/>
<xsl:with-param name="target" select="'--'"/>
<xsl:with-param name="replacement" select="'REPLACE_WITH_DASH_DASH'"/>
</xsl:call-template>
</xsl:variable>
<xsl:comment>
##### SOURCE BEGIN #####
<xsl:value-of select="$xcomment"/>
##### SOURCE END #####
</xsl:comment>
</xsl:template>
<!-- Colors for syntax-highlighted input code -->
<xsl:template match="mwsh:code">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="mwsh:keywords">
<span class="keyword"><xsl:value-of select="."/></span>
</xsl:template>
<xsl:template match="mwsh:strings">
<span class="string"><xsl:value-of select="."/></span>
</xsl:template>
<xsl:template match="mwsh:comments">
<span class="comment"><xsl:value-of select="."/></span>
</xsl:template>
<xsl:template match="mwsh:unterminated_strings">
<span class="untermstring"><xsl:value-of select="."/></span>
</xsl:template>
<xsl:template match="mwsh:system_commands">
<span class="syscmd"><xsl:value-of select="."/></span>
</xsl:template>
<!-- Footer information -->
<xsl:template match="copyright">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="revision">
<xsl:value-of select="."/>
</xsl:template>
<!-- Search and replace -->
<!-- From http://www.xml.com/lpt/a/2002/06/05/transforming.html -->
<xsl:template name="globalReplace">
<xsl:param name="outputString"/>
<xsl:param name="target"/>
<xsl:param name="replacement"/>
<xsl:choose>
<xsl:when test="contains($outputString,$target)">
<xsl:value-of select=
"concat(substring-before($outputString,$target),$replacement)"/>
<xsl:call-template name="globalReplace">
<xsl:with-param name="outputString"
select="substring-after($outputString,$target)"/>
<xsl:with-param name="target" select="$target"/>
<xsl:with-param name="replacement"
select="$replacement"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$outputString"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>