aboutsummaryrefslogtreecommitdiff
path: root/utility/formatter.xsl
blob: d9e6efef54eb88ef2aed9e8b017c6b0ed6417922 (plain)
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
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
	version="1.0"
	xmlns="http://www.w3.org/1999/xhtml"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:xalan="http://xml.apache.org/xalan"
	xmlns:InputXSLT="function.inputxslt.application"
	exclude-result-prefixes="xalan InputXSLT"
>

<xsl:template name="plain_formatter">
	<xsl:param name="format"/>
	<xsl:param name="source"/>

	<xsl:copy-of select="InputXSLT:external-command(
		$format,
		$source
	)/self::command/node()"/>
</xsl:template>

<xsl:template name="math_highlighter">
	<xsl:param name="source"/>
	<xsl:param name="arguments"/>

	<xsl:variable name="formatted_expression">
		<xsl:call-template name="plain_formatter">
			<xsl:with-param name="format">
				<xsl:text>katex </xsl:text>
				<xsl:value-of select="$arguments"/>
			</xsl:with-param>
			<xsl:with-param name="source" select="$source"/>
		</xsl:call-template>
	</xsl:variable>

	<xsl:copy-of select="xalan:nodeset($formatted_expression)/node()"/>
</xsl:template>

<xsl:template name="code_highlighter">
	<xsl:param name="source"/>
	<xsl:param name="language"/>

	<xsl:variable name="formatted_code">
		<xsl:call-template name="plain_formatter">
			<xsl:with-param name="format">
				<xsl:text>highlight --out-format=xhtml --inline-css --style=molokai --fragment --enclose-pre --wrap-simple --syntax=</xsl:text>
				<xsl:value-of select="$language"/>
			</xsl:with-param>
			<xsl:with-param name="source" select="$source"/>
		</xsl:call-template>
	</xsl:variable>

	<pre>
		<xsl:copy-of select="xalan:nodeset($formatted_code)/pre/node()"/>
	</pre>
</xsl:template>

<xsl:template match="@*|node()" mode="embellish">
	<xsl:copy>
		<xsl:apply-templates select="@*|node()" mode="embellish"/>
	</xsl:copy>
</xsl:template>

<xsl:template match="h2" mode="embellish">
	<h3>
		<xsl:apply-templates select="node()" mode="embellish"/>
	</h3>
</xsl:template>

<xsl:template match="h3" mode="embellish">
	<h4>
		<xsl:apply-templates select="node()" mode="embellish"/>
	</h4>
</xsl:template>

<xsl:template match="pre" mode="embellish">
	<xsl:call-template name="code_highlighter">
		<xsl:with-param name="source" select="code/text()"/>
		<xsl:with-param name="language">
			<xsl:choose>
				<xsl:when test="@class">
					<xsl:value-of select="@class"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:text>txt</xsl:text>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:with-param>
	</xsl:call-template>
</xsl:template>

<xsl:template match="div[@class = 'figure']" mode="embellish">
	<p>
		<xsl:apply-templates select="img" mode="embellish"/>
	</p>
</xsl:template>

<xsl:template match="div[@class = 'footnotes']/hr" mode="embellish"/>

<xsl:template match="div[@class = 'footnotes']//a[contains(@href, '#fnref')]" mode="embellish">
	<a href="{@href}" class="more">
		<xsl:apply-templates select="node()" mode="embellish"/>
	</a>
</xsl:template>

<xsl:template match="span[contains(@class, 'math')]" mode="embellish">
	<xsl:choose>
		<xsl:when test="contains(@class, 'display')">
			<p class="math">
				<xsl:call-template name="math_highlighter">
					<xsl:with-param name="source" select="text()"/>
					<xsl:with-param name="arguments">
						<xsl:text>--display-mode</xsl:text>
					</xsl:with-param>
				</xsl:call-template>
			</p>
		</xsl:when>
		<xsl:otherwise>
			<span class="math">
				<xsl:call-template name="math_highlighter">
					<xsl:with-param name="source" select="text()"/>
				</xsl:call-template>
			</span>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template name="formatter">
	<xsl:param name="source"/>

	<xsl:variable name="content">
		<xsl:call-template name="plain_formatter">
			<xsl:with-param name="format">pandoc -f markdown -t html --katex --no-highlight</xsl:with-param>
			<xsl:with-param name="source" select="$source"/>
		</xsl:call-template>
	</xsl:variable>

	<xsl:apply-templates select="xalan:nodeset($content)" mode="embellish"/>
</xsl:template>

</xsl:stylesheet>