summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/stex/xsl/omdocpost.xsl
blob: d49f692a3877ca066e694cd226083a5fb20eee76 (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
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
<?xml version="1.0" encoding="utf-8"?>
<!-- this style sheet transforms the tex.xml file to OMDoc   -->
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns="http://omdoc.org/ns"
  xmlns:omdoc="http://omdoc.org/ns"
  xmlns:mcd="http://www.w3.org/ns/mathml-cd"
  xmlns:ltx="http://dlmf.nist.gov/LaTeXML"
  xmlns:stex="http://kwarc.info/ns/sTeX"
  xmlns:m="http://www.w3.org/1998/Math/MathML"
  xmlns:om="http://www.openmath.org/OpenMath"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:str="http://exslt.org/strings" 
  extension-element-prefixes="str"
  exclude-result-prefixes="xsl omdoc ltx m om dc">

<xsl:output method="xml" indent="yes" cdata-section-elements="data"/>
<xsl:strip-space elements="*"/>

<!-- these parameters set the paths to the special latexml cds. The default is made so that it works for GenCS -->
<xsl:param name="latexmlcds" select="'../../../slides/extcds/stex'"/>
<xsl:param name="omcds" select="'../../../slides/extcds/omstd'"/>

<xsl:template match="/">
  <xsl:comment>This OMDoc document is generated from an sTeX-encoded one via LaTeXML, you may want to reconsider editing it.</xsl:comment>
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="omdoc:omdoc">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
    <xsl:comment>The data elements</xsl:comment>
    <xsl:apply-templates mode="extract-data"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="*">
  <xsl:copy><xsl:copy-of select="@*"/><xsl:apply-templates/></xsl:copy>
</xsl:template>

<xsl:template match="ltx:ERROR"/>
<xsl:template match="*|text()" mode="extract-data">
  <xsl:apply-templates mode="extract-data"/>
</xsl:template>

<xsl:template match="ltx:Math">
  <xsl:apply-templates select="om:OMOBJ"/> 
</xsl:template>

<!-- we directly access the content of titles -->
<xsl:template match="ltx:title"/>
<xsl:template match="ltx:chapter|ltx:part|ltx:section|ltx:subsection|ltx:subsubsection|ltx:paragraph">
  <omgroup layout="sectioning">
    <xsl:copy-of select="@*"/>
    <metadata>
      <dc:title><xsl:apply-templates select="ltx:title/*|ltx:title/text()"/></dc:title>
    </metadata>
    <xsl:apply-templates/>
  </omgroup>
</xsl:template>

<xsl:template match="ltx:p">
  <p><xsl:copy-of select="@*"/><xsl:apply-templates/></p>
</xsl:template>

<xsl:template match="ltx:para">
  <omtext><xsl:copy-of select="@*"/><CMP><xsl:apply-templates/></CMP></omtext>
</xsl:template>

<xsl:template match="ltx:text">
  <phrase>
    <xsl:copy-of select="@*"/>
    <xsl:if test="@font">
      <xsl:attribute name="class"><xsl:value-of select="@font"/></xsl:attribute>
    </xsl:if>
    <xsl:apply-templates/>
  </phrase>
</xsl:template>


<xsl:template match="ltx:graphics|ltx:verbatim">
  <omlet action="display" data="#{generate-id()}" show="embed" style="{translate(@options,'=',':')}">
    <xsl:copy-of select="@*"/>
  </omlet>
</xsl:template>

<xsl:template match="omdoc:tikz">
  <omlet action="display" data="#{generate-id()}" show="embed" style="{translate(@options,'=',':')}">
    <xsl:copy-of select="@*"/>
  </omlet>
</xsl:template>

<!-- for citations we just extract the bibrefs at the moment. -->
<xsl:template match="ltx:cite"><xsl:apply-templates select="ltx:bibref"/></xsl:template>
<xsl:template match="ltx:bibref">
  <xsl:for-each select="str:tokenize(@bibrefs,',')">
    <ref type="cite" xref="{.}"/>
  </xsl:for-each>
</xsl:template>

<!-- we ignore LaTeXML breaks -->
<xsl:template match="ltx:break"/>

<xsl:template match="ltx:figure[ltx:graphics]">
  <omlet action="display" data="#{generate-id(ltx:graphics)}" show="embed" class="float-figure-{@placement}">
   <xsl:if test="@label">
     <xsl:attribute name="xml:id"><xsl:value-of select="@label"/></xsl:attribute> 
   </xsl:if>
   <xsl:if test="ltx:graphics/@options">
     <xsl:attribute name="style"><xsl:value-of select="translate(ltx:graphics/@options,'=',':')"/></xsl:attribute> 
   </xsl:if>
   <xsl:if test="ltx:caption/*|ltx:caption/text()">
     <metadata><dc:title><xsl:apply-templates select="ltx:caption/*|ltx:caption/text()"/></dc:title></metadata>
   </xsl:if>
  </omlet>
</xsl:template>

<xsl:template match="ltx:graphics" mode="extract-data">
  <private>
    <xsl:attribute name="xml:id"><xsl:value-of select="generate-id()"/></xsl:attribute>
    <xsl:variable name="sources" select="str:tokenize(@sources,',')"/>
    <xsl:variable name="graphic" select="@graphic"/>
    <xsl:for-each select="$sources">
      <xsl:variable name="current" select="."/>
      <xsl:variable name="type">
	<xsl:choose>
	  <xsl:when test="$current='jpg'">
	    <xsl:value-of select="'image/jpeg'"/>
	  </xsl:when>
	  <xsl:when test="$current='jpeg'">
	    <xsl:value-of select="'image/jpeg'"/>
	  </xsl:when>
	  <xsl:when test="$current='png'">
	    <xsl:value-of select="'image/png'"/>
	  </xsl:when>
	  <xsl:when test="$current='gif'">
	    <xsl:value-of select="'image/gif'"/>
	  </xsl:when>
	  <xsl:when test="$current='eps'">
	    <xsl:value-of select="'application/postscript'"/>
	  </xsl:when>
	  <xsl:when test="$current='ps'">
	    <xsl:value-of select="'application/postscript'"/>
	  </xsl:when>
	  <xsl:when test="$current='ai'">
	    <xsl:value-of select="'application/postscript'"/>
	  </xsl:when>
	  <xsl:when test="$current='pdf'">
	    <xsl:value-of select="'application/pdf'"/>
	  </xsl:when>
	  <xsl:otherwise>
	    <xsl:value-of select="$current"/>
	  </xsl:otherwise>
	</xsl:choose>
      </xsl:variable>
      <data format="{$type}" href="{$graphic}.{.}"/>
    </xsl:for-each>
  </private>
</xsl:template>
<xsl:template match="ltx:verbatim" mode="extract-data">
  <private>
    <xsl:attribute name="xml:id"><xsl:value-of select="generate-id()"/></xsl:attribute>
    <data format="text"><xsl:value-of select="text()"/></data>
  </private>
</xsl:template>

<xsl:template match="omdoc:tikz|ltx:tikz" mode="extract-data">
  <private>
    <xsl:attribute name="xml:id"><xsl:value-of select="generate-id()"/></xsl:attribute>
    <data format="tikz"><xsl:value-of select="."/></data>
  </private>
</xsl:template>

<!-- for LaTeXML tables -->
<xsl:template match="ltx:tabular"><table><xsl:apply-templates/></table></xsl:template>
<xsl:template match="ltx:tbody"><xsl:apply-templates/></xsl:template>
<xsl:template match="ltx:tr"><tr><xsl:apply-templates/></tr></xsl:template>
<xsl:template match="ltx:td"><td><xsl:apply-templates/></td></xsl:template>
<xsl:template match="ltx:thead"><xsl:apply-templates/></xsl:template>

<!-- for LaTeXML references from \url -->
<xsl:template match="ltx:ref[@class='url']">
  <link href="{@href}" class="url">
    <xsl:value-of select="ltx:text"/>
  </link>
</xsl:template>

<xsl:template match="ltx:ref[@labelref]">
  <ref type="cite" xref="{@labelref}"/>
</xsl:template>

<!-- LaTeXML listings -->
<xsl:template match="ltx:text[@class='listing']">
  <omdoc:phrase type='progsnippet'><xsl:copy-of select="@*"/><xsl:apply-templates/></omdoc:phrase>
</xsl:template>

<xsl:template match="ltx:listingblock">
  <omlet action='display' show='embed' style='display:block'>
    <xsl:copy-of select="@*"/>
    <code>
      <data format='listingblock'><xsl:apply-templates mode="listing"/></data>
    </code>
  </omlet>
</xsl:template>

<xsl:template match="ltx:tabular" mode="listing">
  <table class="listing"><xsl:apply-templates mode="listing"/></table>
</xsl:template>

<xsl:template match="ltx:tr" mode="listing">
  <tr><xsl:apply-templates mode="listing"/></tr>
</xsl:template>

<xsl:template match="ltx:td" mode="listing">
  <td><xsl:apply-templates mode="listing"/></td>
</xsl:template>

<xsl:template match="ltx:text[not(@*)]" mode="listing">
  <xsl:apply-templates mode="listing"/>
</xsl:template>

<xsl:template match="ltx:text[@font='bold']" mode="listing">
  <keyword><xsl:apply-templates mode="listing"/></keyword>
</xsl:template>

<xsl:template match="ltx:text[@color]" mode="listing">
  <phrase>
    <xsl:attribute name='type'><xsl:text>lstemph</xsl:text></xsl:attribute>
    <xsl:attribute name='style'><xsl:text>color:</xsl:text><xsl:value-of select="@color"/></xsl:attribute>
    <xsl:apply-templates mode="listing"/>
  </phrase>
</xsl:template>

<xsl:template match="ltx:text[@class]" mode="listing">
  <xsl:apply-templates mode="listing"/>
</xsl:template>


<xsl:template match="ltx:*" mode="listing">
  <xsl:message>cannot deal with element <xsl:value-of select="local-name()"/> yet!</xsl:message>
</xsl:template>


<xsl:template match="ltx:equation">
  <om:OMOBJ style="display:block">
    <xsl:apply-templates select="ltx:Math/om:OMOBJ/*"/>
  </om:OMOBJ>
</xsl:template>

<!-- equationgroups come from eqnarray and eqnarray*, they really need OMDoc-level parallel markup -->
<xsl:template match="ltx:equationgroup">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="ltx:equationgroup/ltx:equation">
  <om:OMOBJ style="display:block">
    <xsl:apply-templates select="ltx:MathFork/ltx:Math/om:OMOBJ/*"/>
  </om:OMOBJ>
</xsl:template>


<!-- for notations -->
<xsl:template match="omdoc:rendering">
  <xsl:apply-templates select="." mode="rendering"/>
</xsl:template>
<xsl:template match="ltx:Math" mode="rendering">
  <xsl:apply-templates select="m:math/*" mode="rendering"/>
</xsl:template>

<!-- copy where not specified otherwise -->
<xsl:template match="*" mode="rendering">
  <xsl:copy><xsl:copy-of select="@*[not(name()='argprec')]"/><xsl:apply-templates mode="rendering"/></xsl:copy>
</xsl:template>

<xsl:template match="ltx:text" mode="rendering">
  <text><xsl:value-of select="text()"/></text>
</xsl:template>

<!-- Template to recover the appropriate argument precedence" -->
<xsl:template name="argument-precedence">
  <xsl:param name="count" select="1"/>
  <xsl:param name="precattr" select="1"/>
  <xsl:choose>
    <xsl:when test="$count > 1">
      <xsl:call-template name="argument-precedence">
	<xsl:with-param name="count" select="$count - 1"/>
	<xsl:with-param name="precattr" select="substring-after($precattr,' ')"/>
      </xsl:call-template>
    </xsl:when>  
    <xsl:otherwise>
      <xsl:value-of select="substring-before($precattr,' ')"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

<!-- we have to treat the m:mi that come from an 
     #i argument invocation differently -->
<xsl:template match="m:mi[starts-with(.,'arg:')]|m:mo[starts-with(.,'arg:')]" mode="rendering"> 
  <xsl:variable name="precedence">
    <xsl:call-template name="argument-precedence">
      <xsl:with-param name="count" select="substring-after(.,'arg:')"/>
      <xsl:with-param name="precattr" select="ancestor::omdoc:rendering[1]/@argprec"/>
    </xsl:call-template>
  </xsl:variable>
  <render name="arg{substring-after(.,'arg:')}">
    <xsl:choose>
      <xsl:when test="string($precedence)">
	<xsl:attribute name="precedence"><xsl:value-of select="$precedence"/></xsl:attribute>
      </xsl:when>
      <xsl:when test="string(ancestor::omdoc:rendering[1]/@precedence)">
	<xsl:attribute name="precedence"><xsl:value-of select="ancestor::omdoc:rendering[1]/@precedence"/></xsl:attribute>
      </xsl:when>
      <xsl:otherwise/>
    </xsl:choose>
  </render>
</xsl:template>


<!-- make an mrow around the generated things. -->
<xsl:template match="omdoc:style[@format='pmml']">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <element name="mrow" ns="http://www.w3.org/1998/Math/MathML">
	<xsl:apply-templates/>
      </element>
    </xsl:copy>
</xsl:template>


<xsl:template match="omdoc:separator/ltx:Math">
    <xsl:apply-templates select="m:math/*" mode="elementize"/>
</xsl:template>

<xsl:template match="omdoc:map/ltx:Math">
    <xsl:apply-templates select="m:math/*" mode="elementize"/>
</xsl:template>


<xsl:template match="text()" mode="elementize">
  <text><xsl:value-of select="."/></text>
</xsl:template>

<xsl:template match="ltx:text" mode="elementize">
  <element name="mtext" ns="http://www.w3.org/1998/Math/MathML">
    <xsl:value-of select="."/>
  </element>
</xsl:template>

<xsl:template match="m:*" mode="elementize">
  <element name="{local-name()}" ns="{namespace-uri()}">
    <xsl:for-each select="@*">
      <attribute name="{local-name()}" select="'{.}'"/>
    </xsl:for-each>
    <xsl:apply-templates mode="elementize"/>
  </element>
</xsl:template>

<!-- get rid of the list OMAs LaTeXML uses -->
<xsl:template match="om:OMA[om:OMS[position()=1 and @name='list' and @cd='latexml']]">
  <xsl:apply-templates select="*[position() &gt; 1]"/>
</xsl:template>

<!-- special treatment for latexml symbols -->
<!-- we have a set of special CDs that correspond to the ones latexml postulates -->
<!-- they need to be imported whereever necessary -->
<xsl:template match="omdoc:theory">
  <theory>
    <xsl:copy-of select="@*"/>
    <xsl:if test="//om:OMS[@cd='latexml' and @name='multirelation']">
      <imports from="{$latexmlcds}/multirel.omdoc#multirel"/>
    </xsl:if>
    <xsl:if test="//om:OMS[@cd='latexml' and 
		            (@name='times' or
		             @name='divide' or
		             @name='plus' or
		             @name='minus')]">
      <imports from="{$omcds}/arith1.omdoc#arith1"/>
    </xsl:if>
    <xsl:if test="//om:OMS[@cd='latexml' and 
		            (@name='greater-than' or
		             @name='less-than' or
		             @name='greater-than-or-equals' or
		             @name='less-than-or-equals')]">
      <imports from="{$omcds}/relation1.omdoc#relation1"/>
    </xsl:if>
    <xsl:apply-templates/>
  </theory>
</xsl:template>

<!-- and we need to convert the symbols -->
<xsl:template match="om:OMS[@cd='latexml' and @name='multirelation']">
  <om:OMS cd="multirel" name="multirelation"/>
</xsl:template>

<xsl:template match="om:OMS[@cd='latexml' and @name='multirelation']"/>


<xsl:template match="om:OMS[@cd='latexml' and @name='plus']">
  <om:OMS cd="arith1" name="plus"/>
</xsl:template>

<xsl:template match="om:OMS[@cd='latexml' and @name='minus']">
  <om:OMS cd="arith1" name="minus"/>
</xsl:template>

<xsl:template match="om:OMS[@cd='latexml' and @name='times']">
  <om:OMS cd="arith1" name="times"/>
</xsl:template>

<xsl:template match="om:OMS[@cd='latexml' and @name='divide']">
  <om:OMS cd="arith1" name="divide"/>
</xsl:template>

<xsl:template match="om:OMS[@cd='latexml' and @name='greater-than']">
  <om:OMS cd="relation1" name="gt"/>
</xsl:template>

<xsl:template match="om:OMS[@cd='latexml' and @name='greater-than-or-equals']">
  <om:OMS cd="relation1" name="gt"/>
</xsl:template>

<xsl:template match="om:OMS[@cd='latexml' and @name='less-than']">
  <om:OMS cd="relation1" name="lt"/>
</xsl:template>

<xsl:template match="om:OMS[@cd='latexml' and @name='less-than-or-equals']">
  <om:OMS cd="relation1" name="lt"/>
</xsl:template>

<xsl:template match="om:OMS[@cd='latexml' and @name='not-equals']">
  <om:OMS cd="relation1" name="ne"/>
</xsl:template>

<!-- get rid of the sectioning commands -->
<xsl:template match="omdoc:chapter|omdoc:section|omdoc:subsection|omdoc:subsubsection|omdoc:paragraph|omdoc:subparagraph">
  <omgroup class="{local-name()}">
    <xsl:copy-of select="@*|*"/>
  </omgroup>
</xsl:template>
</xsl:stylesheet>