-
Notifications
You must be signed in to change notification settings - Fork 3
/
calstable2htmltabs.xsl
executable file
·257 lines (217 loc) · 12 KB
/
calstable2htmltabs.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
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http:https://www.w3.org/1999/XSL/Transform"
xmlns:xs="http:https://www.w3.org/2001/XMLSchema"
xmlns:css="http:https://www.w3.org/1996/css"
xmlns:xml2tex="http:https://transpect.io/xml2tex"
xmlns:tr="http:https://transpect.io"
xmlns="http:https://www.w3.org/1999/xhtml"
xpath-default-namespace="http:https://www.w3.org/1999/xhtml"
exclude-result-prefixes="css xs xml2tex"
version="2.0">
<!-- this stylesheets converts CALS tables
to htmltabs
-->
<xsl:import href="functions.xsl"/>
<xsl:import href="http:https://transpect.io/xslt-util/cals2htmltable/xsl/cals2htmltables.xsl"/>
<xsl:import href="http:https://transpect.io/xslt-util/lengths/xsl/lengths.xsl"/>
<xsl:param name="table-col-declaration" select="''" as="xs:string"/>
<xsl:param name="table-first-col-declaration" select="''" as="xs:string"/>
<xsl:param name="table-last-col-declaration" select="''" as="xs:string"/>
<xsl:param name="debug" select="'no'"/>
<xsl:param name="debug-dir-uri" select="'debug'"/>
<!-- mode variables -->
<xsl:variable name="cals2html-table">
<xsl:apply-templates select="/" mode="cals2html-table"/>
</xsl:variable>
<xsl:variable name="html2tabs">
<xsl:apply-templates select="$cals2html-table" mode="html2tabs"/>
</xsl:variable>
<xsl:template name="main">
<xsl:sequence select="$html2tabs"/>
<xsl:if test="$debug eq 'yes'">
<xsl:result-document indent="yes" method="xml"
href="{$debug-dir-uri}/cals2htmltabs/03_cals2htmltable.xml">
<xsl:sequence select="$cals2html-table"/>
</xsl:result-document>
<xsl:result-document indent="yes" method="xml"
href="{$debug-dir-uri}/cals2htmltabs/05html2tabs.xml">
<xsl:sequence select="$cals2html-table"/>
</xsl:result-document>
</xsl:if>
</xsl:template>
<xsl:template match="*:informaltable/@role | *:table/@role | *:entry/@role" mode="cals2html-table">
<xsl:attribute name="class" select="."/>
<xsl:attribute name="role" select="."/>
</xsl:template>
<xsl:template match="*:entry[not(*) and not(normalize-space())]/@*[matches(name(), '^css:padding-(left|right)')]" mode="cals2html-table">
<xsl:attribute name="{name()}" select="'0pt'"/>
<!-- set padding on empty cells to zero to avoid errors if padding exceeds col width, https://redmine.le-tex.de/issues/15094 -->
</xsl:template>
<xsl:template match="@* | node()" mode="html2tabs">
<xsl:copy>
<xsl:apply-templates select="@*, node()" mode="#current"/>
</xsl:copy>
</xsl:template>
<xsl:template match="table" mode="html2tabs">
<xsl:copy>
<xsl:apply-templates select="@*" mode="#current"/>
<xsl:processing-instruction name="htmltabs">
<xsl:text>
\begin{htmltab}</xsl:text>
<xsl:value-of select="xml2tex:atts-to-option(@*)"/>
<xsl:text>
</xsl:text>
</xsl:processing-instruction>
<xsl:apply-templates select="* except (*:tfoot, *:tbody), *:tfoot, *:tbody" mode="#current"/>
<xsl:processing-instruction name="htmltabs"
select="'
\end{htmltab}
'"/>
</xsl:copy>
</xsl:template>
<xsl:template match="caption" mode="htmltabs">
<xsl:processing-instruction name="htmltabs"
select="'\HTcaption{'"/>
<xsl:apply-templates mode="#current"/>
<xsl:processing-instruction name="htmltabs"
select="'}
'"/>
</xsl:template>
<xsl:template match="colgroup" mode="html2tabs">
<xsl:copy>
<xsl:processing-instruction name="htmltabs"
select="concat('\begin{colgroup}', xml2tex:atts-to-option(@*), '
')"/>
<xsl:apply-templates select="@*, *" mode="#current"/>
<xsl:processing-instruction name="htmltabs"
select="'\end{colgroup}
'"/>
</xsl:copy>
</xsl:template>
<xsl:template match="col[@css:width]" mode="html2tabs">
<xsl:variable name="pos" select="position()" as="xs:integer"/>
<xsl:variable name="col-count" select="count(parent::*/col)" as="xs:integer"/>
<xsl:variable name="col-override" as="xs:string?"
select="($table-first-col-declaration[$pos = 1][normalize-space()],
$table-last-col-declaration[$pos = $col-count][normalize-space()],
$table-col-declaration)[1]"/>
<xsl:copy>
<xsl:apply-templates select="@*" mode="#current"/>
<xsl:processing-instruction name="htmltabs"
select="concat('\HTcol[',
($col-override[normalize-space()],
concat('width=',
xml2tex:absolute-to-relative-col-width(@css:width, parent::*/col/@css:width) * 100))[1],
'\%]
')"/>
</xsl:copy>
</xsl:template>
<xsl:template match="thead|tbody|tfoot" mode="html2tabs">
<xsl:copy>
<xsl:apply-templates select="@*" mode="#current"/>
<xsl:processing-instruction name="htmltabs"
select="concat('\begin{', local-name(), '}
')"/>
<xsl:apply-templates select="*" mode="#current"/>
<xsl:processing-instruction name="htmltabs"
select="concat('\end{', local-name(), '}
')"/>
</xsl:copy>
</xsl:template>
<xsl:template match="tr" mode="html2tabs">
<xsl:copy>
<xsl:apply-templates select="@*" mode="#current"/>
<xsl:processing-instruction name="htmltabs"
select="concat('\HTtr',
xml2tex:atts-to-option(@*),
'{%
')"/>
<xsl:apply-templates select="*" mode="#current"/>
<xsl:processing-instruction name="htmltabs"
select="'}
'"/>
</xsl:copy>
</xsl:template>
<xsl:variable name="strut-element-names" select="'^((itemized|ordered)list|inlinemediaobject|mediaobject)$'"/>
<xsl:template match="td|th" mode="html2tabs">
<xsl:copy>
<xsl:apply-templates select="@*" mode="#current"/>
<xsl:variable name="additional-atts" as="attribute()*">
<xsl:apply-templates select="." mode="html2tabs_atts"/>
</xsl:variable>
<xsl:processing-instruction name="htmltabs"
select="concat('\HTtd',
xml2tex:atts-to-option((@*, $additional-atts)),
'{')"/>
<xsl:apply-templates select="node()" mode="#current"/>
<xsl:processing-instruction name="htmltabs"
select="'}
'"/>
</xsl:copy>
</xsl:template>
<!-- normalize trailing spaces -->
<xsl:template match="*[self::td or self::th]/*:para[last()][matches(string-join(descendant::text(),''),'\p{Zs}+$')]/text()[last()][not(following-sibling::node())][matches(.,'\p{Zs}+$')]" mode="html2tabs">
<xsl:sequence select="replace(.,'\p{Zs}+$','')"/>
</xsl:template>
<xsl:template match="td[node()][every $el in * satisfies matches($el/local-name(),$strut-element-names)]" mode="html2tabs_atts">
<xsl:attribute name="no-strut" select="'both'" />
</xsl:template>
<xsl:template match="td[not(every $el in * satisfies matches($el/local-name(),$strut-element-names))]
[*[1][self::*[matches(local-name(),$strut-element-names)]]]" mode="html2tabs_atts">
<xsl:attribute name="no-strut" select="'top'" />
</xsl:template>
<xsl:template match="td[not(every $el in * satisfies matches($el/local-name(),$strut-element-names))]
[*[last()][self::*[matches(local-name(),$strut-element-names)]]]" mode="html2tabs_atts">
<xsl:attribute name="no-strut" select="'bottom'" />
</xsl:template>
<xsl:template match="node()| @*" mode="html2tabs_atts"/>
<xsl:variable name="no-css-atts-in-style" select="'no-strut'"/>
<xsl:function name="xml2tex:atts-to-option" as="xs:string?">
<xsl:param name="atts" as="attribute()*"/>
<xsl:if test="$atts">
<xsl:sequence select="concat('[',
if ($atts[local-name() = 'width'])
then concat('width=', xml2tex:escape-for-tex($atts[local-name() = 'width']),',') else '',
string-join(
(xml2tex:css-atts-to-style-att($atts[namespace-uri() eq 'http:https://www.w3.org/1996/css' or local-name() = $no-css-atts-in-style]
[local-name() ne 'width']
),
$atts[local-name() = ('id', 'class', 'colspan', 'rowspan')]/concat(local-name(), '=', ., '')),
','),
']')"/>
</xsl:if>
</xsl:function>
<xsl:function name="xml2tex:css-atts-to-style-att" as="xs:string?">
<xsl:param name="css-atts" as="attribute()*"/>
<xsl:variable name="prelim" as="attribute(*)*">
<xsl:apply-templates select="$css-atts" mode="xml2tex:css-atts-to-style-att"/>
</xsl:variable>
<xsl:sequence select="if(exists($css-atts))
then concat('style={',
string-join($prelim/concat(local-name(), ':', .),
';'),
'}')
else ()"/>
</xsl:function>
<xsl:template match="@*" mode="xml2tex:css-atts-to-style-att">
<xsl:copy/>
</xsl:template>
<xsl:template match="@*[contains(., '#')]" mode="xml2tex:css-atts-to-style-att">
<xsl:attribute name="{name()}" select="replace(., '(^|[^\\])#', '\\#')"/>
</xsl:template>
<xsl:template match="@*[contains(., '%')]" mode="xml2tex:css-atts-to-style-att">
<xsl:attribute name="{name()}" select="replace(., '%', '\\%')"/>
</xsl:template>
<xsl:template match="@*[. = 'outset']" mode="xml2tex:css-atts-to-style-att">
<xsl:attribute name="{name()}" select="'solid'"/>
</xsl:template>
<xsl:template match="@*[matches(., '^auto$')]" mode="xml2tex:css-atts-to-style-att"/>
<xsl:variable name="css-atts-to-dissolve" select="'^line-height$'"/>
<xsl:template match="@css:*[matches(local-name(),$css-atts-with-generic-values)]
[not(matches(.,$css-generic-values))]" mode="xml2tex:css-atts-to-style-att"/>
<xsl:template match="@css:*[matches(local-name(), $css-atts-to-dissolve)]" mode="xml2tex:css-atts-to-style-att"/>
<xsl:variable name="css-generic-values" select="'(sans-)?serif|monospace|bold|normal|center|left|right|justify|bottom|top|(x?x-)?small(-caps)?|(x?x-)?large|auto|none|collapse|separate'"/>
<xsl:variable name="css-atts-with-generic-values" select="'font-(weight|family|style|variant)|hyphens|border-collapse'"/>
<xsl:template match="@css:writing-mode" mode="xml2tex:css-atts-to-style-att">
<xsl:attribute name="{if (. = ('bt-lr', 'vertical-rl', 'vertical-lr', 'sideways-rl', 'sideways-lr')) then 'css:transform' else name()}"
select="if (. = ('vertical-rl', 'vertical-lr', 'sideways-rl'))
then 'rotate(90deg)'
else if (. = ('bt-lr','sideways-lr')) then 'rotate(-90deg)'
else ."/>
</xsl:template>
<xsl:function name="xml2tex:absolute-to-relative-col-width" as="xs:decimal">
<xsl:param name="colwidth" as="xs:string"/>
<xsl:param name="sum-colwidths" as="xs:string+"/>
<xsl:sequence select="round-half-to-even( tr:length-to-unitless-twip($colwidth)
div sum(for $i in $sum-colwidths
return tr:length-to-unitless-twip($i)),
3)"/>
</xsl:function>
</xsl:stylesheet>