Accept attributes in inline image syntax
[multimarkdown] / XSLT / xhtml2latex.xslt
1 <?xml version='1.0' encoding='utf-8'?>
2
3 <!-- XHTML-to-Latex converter by Fletcher Penney
4         specifically designed for use with MultiMarkdown created XHTML
5         
6         MultiMarkdown Version 2.0.b5
7         
8         $Id: xhtml2latex.xslt 480 2008-01-12 23:04:28Z fletcher $
9 -->
10
11 <!-- 
12 # Copyright (C) 2005-2008  Fletcher T. Penney <fletcher@fletcherpenney.net>
13 #
14 # This program is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by
16 # the Free Software Foundation; either version 2 of the License, or
17 # (at your option) any later version.
18 #
19 # This program is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 # GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the
26 #    Free Software Foundation, Inc.
27 #    59 Temple Place, Suite 330
28 #    Boston, MA 02111-1307 USA
29 -->
30
31
32
33 <xsl:stylesheet
34         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
35         xmlns:m="http://www.w3.org/1998/Math/MathML"
36         xmlns:html="http://www.w3.org/1999/xhtml"
37         version="1.0">
38
39         <xsl:import href="../MultiMarkdownXSLTMathML/mmltex.xsl"/>
40         <xsl:import href="clean-text.xslt"/>
41
42         <xsl:output method='text' encoding='utf-8'/>
43
44         <xsl:strip-space elements="*" />
45
46         <xsl:variable name="newline">
47 <xsl:text>
48 </xsl:text>
49         </xsl:variable>
50
51         <xsl:param name="footnoteId"/>
52
53         <xsl:decimal-format name="string" NaN="1"/>
54
55         <xsl:template match="*[local-name() = 'title']">
56                 <xsl:text>\def\mytitle{</xsl:text>
57                         <xsl:call-template name="clean-text">
58                                 <xsl:with-param name="source">
59                                         <xsl:value-of select="."/>
60                                 </xsl:with-param>
61                         </xsl:call-template>            
62                 <xsl:text>}
63 </xsl:text>
64         </xsl:template>
65
66
67         <xsl:template match="html:meta">
68                 <xsl:choose>
69                         <xsl:when test="translate(@name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
70                         'abcdefghijklmnopqrstuvwxyz') = 'author'">
71                                 <xsl:text>\def\myauthor{</xsl:text>
72                                 <xsl:call-template name="clean-text">
73                                         <xsl:with-param name="source">
74                                                 <xsl:value-of select="@content"/>
75                                         </xsl:with-param>
76                                 </xsl:call-template>            
77                                 <xsl:text>}
78 </xsl:text>
79                         </xsl:when>
80                 <xsl:when test="translate(@name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
81                 'abcdefghijklmnopqrstuvwxyz') = 'date'">
82                         <xsl:text>\date{</xsl:text>
83                         <xsl:call-template name="clean-text">
84                                 <xsl:with-param name="source">
85                                         <xsl:value-of select="@content"/>
86                                 </xsl:with-param>
87                         </xsl:call-template>            
88                         <xsl:text>}
89 \def\mydate{</xsl:text>
90                         <xsl:call-template name="clean-text">
91                                 <xsl:with-param name="source">
92                                         <xsl:value-of select="@content"/>
93                                 </xsl:with-param>
94                         </xsl:call-template>
95                         <xsl:text>}
96 </xsl:text>
97                 </xsl:when>
98                         <xsl:when test="translate(@name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
99                         'abcdefghijklmnopqrstuvwxyz') = 'affiliation'">
100                                 <xsl:text>\def\affiliation{</xsl:text>
101                                 <xsl:call-template name="replace-substring">
102                                         <!-- put line breaks in -->
103                                         <xsl:with-param name="original">
104                                                 <xsl:call-template name="clean-text">
105                                                         <xsl:with-param name="source">
106                                                                 <xsl:value-of select="@content"/>
107                                                         </xsl:with-param>
108                                                 </xsl:call-template>            
109                                         </xsl:with-param>
110                                         <xsl:with-param name="substring">
111                                                 <xsl:text>   </xsl:text>
112                                         </xsl:with-param>
113                                         <xsl:with-param name="replacement">
114                                                 <xsl:text> \\ </xsl:text>
115                                         </xsl:with-param>
116                                 </xsl:call-template>
117                                 <xsl:text>}
118 </xsl:text>
119                         </xsl:when>
120                         <xsl:when test="translate(@name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
121                         'abcdefghijklmnopqrstuvwxyz') = 'revision'">
122                                 <xsl:text>\def\revision{Revision: </xsl:text>
123                                 <xsl:call-template name="clean-text">
124                                         <xsl:with-param name="source">
125                                                 <xsl:value-of select="@content"/>
126                                         </xsl:with-param>
127                                 </xsl:call-template>            
128                                 <xsl:text>}
129 </xsl:text>
130                         </xsl:when>
131                         <xsl:when test="translate(@name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
132                         'abcdefghijklmnopqrstuvwxyz') = 'email'">
133                                 <xsl:text>\def\email{ \href{mailto:</xsl:text>
134                                 <xsl:call-template name="replace-substring">
135                                         <!-- put line breaks in -->
136                                         <xsl:with-param name="original">
137                                                 <xsl:call-template name="clean-text">
138                                                         <xsl:with-param name="source">
139                                                                 <xsl:value-of select="@content"/>
140                                                         </xsl:with-param>
141                                                 </xsl:call-template>            
142                                         </xsl:with-param>
143                                         <xsl:with-param name="substring">
144                                                 <xsl:text>   </xsl:text>
145                                         </xsl:with-param>
146                                         <xsl:with-param name="replacement">
147                                                 <xsl:text> \\ </xsl:text>
148                                         </xsl:with-param>
149                                 </xsl:call-template>
150                                 <xsl:text>}{</xsl:text>
151                                 <xsl:call-template name="replace-substring">
152                                         <!-- put line breaks in -->
153                                         <xsl:with-param name="original">
154                                                 <xsl:call-template name="clean-text">
155                                                         <xsl:with-param name="source">
156                                                                 <xsl:value-of select="@content"/>
157                                                         </xsl:with-param>
158                                                 </xsl:call-template>            
159                                         </xsl:with-param>
160                                         <xsl:with-param name="substring">
161                                                 <xsl:text>   </xsl:text>
162                                         </xsl:with-param>
163                                         <xsl:with-param name="replacement">
164                                                 <xsl:text> \\ </xsl:text>
165                                         </xsl:with-param>
166                                 </xsl:call-template>
167                                 <xsl:text>}}
168 </xsl:text>
169                         </xsl:when>
170                         <xsl:when test="translate(@name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
171                         'abcdefghijklmnopqrstuvwxyz') = 'web'">
172                                 <xsl:text>\def\web{\href{</xsl:text>
173                                 <xsl:if test="not(starts-with(@content,'http:'))">
174                                         <xsl:text>http://</xsl:text>
175                                 </xsl:if>
176                                 <xsl:call-template name="replace-substring">
177                                         <!-- put line breaks in -->
178                                         <xsl:with-param name="original">
179                                                 <xsl:call-template name="clean-text">
180                                                         <xsl:with-param name="source">
181                                                                 <xsl:value-of select="@content"/>
182                                                         </xsl:with-param>
183                                                 </xsl:call-template>            
184                                         </xsl:with-param>
185                                         <xsl:with-param name="substring">
186                                                 <xsl:text>   </xsl:text>
187                                         </xsl:with-param>
188                                         <xsl:with-param name="replacement">
189                                                 <xsl:text> \\ </xsl:text>
190                                         </xsl:with-param>
191                                 </xsl:call-template>
192                                 <xsl:text>}{</xsl:text>
193                                 <xsl:call-template name="replace-substring">
194                                         <!-- put line breaks in -->
195                                         <xsl:with-param name="original">
196                                                 <xsl:call-template name="clean-text">
197                                                         <xsl:with-param name="source">
198                                                                 <xsl:value-of select="@content"/>
199                                                         </xsl:with-param>
200                                                 </xsl:call-template>            
201                                         </xsl:with-param>
202                                         <xsl:with-param name="substring">
203                                                 <xsl:text>   </xsl:text>
204                                         </xsl:with-param>
205                                         <xsl:with-param name="replacement">
206                                                 <xsl:text> \\ </xsl:text>
207                                         </xsl:with-param>
208                                 </xsl:call-template>
209                                 <xsl:text>}}
210 </xsl:text>
211                         </xsl:when>
212                         <xsl:when test="translate(@name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
213                         'abcdefghijklmnopqrstuvwxyz') = 'keywords'">
214                                 <xsl:text>\def\mykeywords{</xsl:text>
215                                 <xsl:call-template name="replace-substring">
216                                         <xsl:with-param name="original">
217                                                 <xsl:value-of select="@content"/>
218                                         </xsl:with-param>
219                                         <xsl:with-param name="substring">
220                                                 <xsl:text>,,</xsl:text>
221                                         </xsl:with-param>
222                                         <xsl:with-param name="replacement">
223                                                 <xsl:text>,</xsl:text>
224                                         </xsl:with-param>
225                                 </xsl:call-template>
226                                 <xsl:text>}
227 </xsl:text>
228                         </xsl:when>
229                         <xsl:when test="translate(@name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
230                         'abcdefghijklmnopqrstuvwxyz') = 'xmp'">
231                                 <xsl:text>\usepackage{xmpincl}
232 \includexmp{</xsl:text>
233                                 <xsl:call-template name="clean-text">
234                                         <xsl:with-param name="source">
235                                                 <xsl:value-of select="@content"/>
236                                         </xsl:with-param>
237                                 </xsl:call-template>            
238                                 <xsl:text>}
239 </xsl:text>
240                         </xsl:when>
241                         <xsl:when test="translate(@name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
242                         'abcdefghijklmnopqrstuvwxyz') = 'bibtex'">
243                                 <xsl:text>\def\bibliocommand{\bibliography{</xsl:text>
244                                 <xsl:call-template name="clean-text">
245                                         <xsl:with-param name="source">
246                                                 <xsl:value-of select="@content"/>
247                                         </xsl:with-param>
248                                 </xsl:call-template>            
249                                 <xsl:text>}}
250 </xsl:text>
251                         </xsl:when>
252                         <xsl:when test="translate(@name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
253                         'abcdefghijklmnopqrstuvwxyz') = 'bibliographystyle'">
254                                 <xsl:text>\def\mybibliostyle{</xsl:text>
255                                 <xsl:call-template name="clean-text">
256                                         <xsl:with-param name="source">
257                                                 <xsl:value-of select="@content"/>
258                                         </xsl:with-param>
259                                 </xsl:call-template>            
260                                 <xsl:text>}
261 </xsl:text>
262                         </xsl:when>
263                         <xsl:when test="translate(@name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
264                         'abcdefghijklmnopqrstuvwxyz') = 'bibliographytitle'">
265                                 <xsl:call-template name="rename-bibliography">
266                                         <xsl:with-param name="source">
267                                                 <xsl:value-of select="@content"/>
268                                         </xsl:with-param>
269                                 </xsl:call-template>            
270                         </xsl:when>
271                         <xsl:when test="translate(@name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
272                         'abcdefghijklmnopqrstuvwxyz') = 'chapterstyle'">
273                                 <xsl:text>\def\mychapterstyle{</xsl:text>
274                                 <xsl:call-template name="clean-text">
275                                         <xsl:with-param name="source">
276                                                 <xsl:value-of select="@content"/>
277                                         </xsl:with-param>
278                                 </xsl:call-template>            
279                                 <xsl:text>}
280 </xsl:text>
281                         </xsl:when>
282                         <xsl:when test="translate(@name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
283                         'abcdefghijklmnopqrstuvwxyz') = 'pagestyle'">
284                                 <xsl:text>\def\mypagestyle{</xsl:text>
285                                 <xsl:call-template name="clean-text">
286                                         <xsl:with-param name="source">
287                                                 <xsl:value-of select="@content"/>
288                                         </xsl:with-param>
289                                 </xsl:call-template>            
290                                 <xsl:text>}
291 </xsl:text>
292                         </xsl:when>
293                         <xsl:when test="translate(@name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
294                         'abcdefghijklmnopqrstuvwxyz') = 'copyright'">
295                                 <xsl:text>\def\mycopyright{</xsl:text>
296                                 <xsl:call-template name="replace-substring">
297                                         <!-- put line breaks in -->
298                                         <xsl:with-param name="original">
299                                                 <xsl:call-template name="clean-text">
300                                                         <xsl:with-param name="source">
301                                                                 <xsl:value-of select="@content"/>
302                                                         </xsl:with-param>
303                                                 </xsl:call-template>            
304                                         </xsl:with-param>
305                                         <xsl:with-param name="substring">
306                                                 <xsl:text>   </xsl:text>
307                                         </xsl:with-param>
308                                         <xsl:with-param name="replacement">
309                                                 <xsl:text> \\ </xsl:text>
310                                         </xsl:with-param>
311                                 </xsl:call-template>
312                                 <xsl:text>}
313 </xsl:text>
314                         </xsl:when>
315                         <xsl:when test="translate(@name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
316                         'abcdefghijklmnopqrstuvwxyz') = 'closing'">
317                                 <xsl:text>\def\myclosing{</xsl:text>
318                                 <xsl:call-template name="replace-substring">
319                                         <!-- put line breaks in -->
320                                         <xsl:with-param name="original">
321                                                 <xsl:call-template name="clean-text">
322                                                         <xsl:with-param name="source">
323                                                                 <xsl:value-of select="@content"/>
324                                                         </xsl:with-param>
325                                                 </xsl:call-template>            
326                                         </xsl:with-param>
327                                         <xsl:with-param name="substring">
328                                                 <xsl:text>   </xsl:text>
329                                         </xsl:with-param>
330                                         <xsl:with-param name="replacement">
331                                                 <xsl:text> \\ </xsl:text>
332                                         </xsl:with-param>
333                                 </xsl:call-template>
334                                 <xsl:text>}
335 </xsl:text>
336                         </xsl:when>
337                         <xsl:otherwise>
338                                 <xsl:text>\def\</xsl:text>
339                                 <xsl:value-of select="translate(@name,'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
340                                 'abcdefghijklmnopqrstuvwxyz')"/>
341                                 <xsl:text>{</xsl:text>
342                                 <xsl:call-template name="replace-substring">
343                                         <!-- put line breaks in -->
344                                         <xsl:with-param name="original">
345                                                 <xsl:call-template name="clean-text">
346                                                         <xsl:with-param name="source">
347                                                                 <xsl:value-of select="@content"/>
348                                                         </xsl:with-param>
349                                                 </xsl:call-template>            
350                                         </xsl:with-param>
351                                         <xsl:with-param name="substring">
352                                                 <xsl:text>   </xsl:text>
353                                         </xsl:with-param>
354                                         <xsl:with-param name="replacement">
355                                                 <xsl:text> \\ </xsl:text>
356                                         </xsl:with-param>
357                                 </xsl:call-template>
358                                 <xsl:text>}
359 </xsl:text>
360                         </xsl:otherwise>
361                 </xsl:choose>
362         </xsl:template>
363
364         <xsl:template match="html:body">
365                 <xsl:apply-templates select="*"/>
366         </xsl:template>
367
368
369         <xsl:template match="html:head">
370                 <!-- Init Latex -->
371                 <xsl:call-template name="latex-document-class"/>
372                 <xsl:call-template name="latex-paper-size"/>
373                 <xsl:call-template name="latex-header"/>
374                 <xsl:apply-templates select="*"/>
375                 <xsl:call-template name="latex-intro"/>
376                 <xsl:call-template name="latex-title"/>
377                 <xsl:call-template name="latex-copyright"/>
378                 <xsl:call-template name="latex-begin-body"/>
379         </xsl:template>
380
381         <!-- ignore  other information within the header 
382                 This will need to be expanded upon over time -->
383
384         <xsl:template match="html:head/html:style">
385         </xsl:template>
386
387         <xsl:template match="html:head/html:base">
388         </xsl:template>
389
390         <xsl:template match="html:head/html:link">
391         </xsl:template>
392
393         <xsl:template match="html:head/html:object">
394         </xsl:template>
395         
396         <xsl:template match="html:head/html:script">
397         </xsl:template>
398
399
400         <xsl:template name="latex-title">
401                 <!-- Set up title -->
402                 <xsl:text>
403
404 % Title Page
405
406 \maketitle
407
408 </xsl:text>
409         </xsl:template>
410
411         <xsl:template name="latex-copyright">
412                 <!-- Set up copyright -->
413                 <xsl:text>% Copyright Page
414 \ifx\mycopyright\undefined
415 \else
416         \textcopyright{} \mycopyright
417 \fi
418
419
420
421 </xsl:text>
422         </xsl:template>
423
424         <xsl:template name="latex-begin-body">
425                 <xsl:text>%
426 % Main Content
427 %
428
429
430 </xsl:text>
431         </xsl:template>
432                 
433
434         <xsl:template match="text()">
435                 <xsl:call-template name="clean-text">
436                         <xsl:with-param name="source">
437                                 <xsl:value-of select="."/>
438                         </xsl:with-param>
439                 </xsl:call-template>            
440         </xsl:template>
441
442         <xsl:template match="m:*/text()">
443                 <xsl:call-template name="replaceEntities">
444                         <xsl:with-param name="content" select="normalize-space()"/>
445                 </xsl:call-template>
446         </xsl:template>
447         
448         <!-- Rename Bibliography -->
449         <xsl:template name="rename-bibliography">
450                 <xsl:param name="source" />
451                 <xsl:text>\renewcommand\refname{</xsl:text>
452                 <xsl:value-of select="$source" />
453                 <xsl:text>}
454 </xsl:text>
455         </xsl:template>
456
457
458         <!-- paragraphs -->
459         
460         <xsl:template match="html:p">
461                 <xsl:apply-templates select="node()"/>
462                 <xsl:value-of select="$newline"/>
463                 <xsl:value-of select="$newline"/>
464                 <xsl:value-of select="$newline"/>
465         </xsl:template>
466
467         <!-- last paragraph in a blockquote doesn't need extra newline -->
468         <!-- needed for epigraph support -->
469         <xsl:template match="html:p[last()][parent::*[local-name() = 'blockquote']]">
470                 <xsl:apply-templates select="node()"/>
471                 <xsl:value-of select="$newline"/>
472         </xsl:template>
473         
474         <!-- footnote li -->
475         <!-- print contents of the matching footnote -->
476         <xsl:template match="html:li" mode="footnote">
477                 <xsl:if test="parent::html:ol/parent::html:div/@class = 'footnotes'">
478                         <xsl:if test="concat('#',@id) = $footnoteId">
479                                 <xsl:apply-templates select="node()"/>
480                         </xsl:if>
481                 </xsl:if>
482         </xsl:template>
483
484         <!-- last paragraph in footnote does not need trailing space -->
485         <xsl:template match="html:p[last()][parent::html:li[parent::html:ol[parent::html:div[@class='footnotes']]]]">
486                 <xsl:apply-templates select="node()"/>
487         </xsl:template>
488
489         <!-- print contents of the matching footnote as a glossary entry-->
490         <xsl:template match="html:li" mode="glossary">
491                 <xsl:if test="parent::html:ol/parent::html:div/@class = 'footnotes'">
492                         <xsl:if test="concat('#',@id) = $footnoteId">
493                                 <xsl:apply-templates select="html:span" mode="glossary"/>
494                                 <xsl:text>description=</xsl:text>
495                                 <xsl:apply-templates select="html:p" mode="glossary"/>
496                                 <xsl:text>}</xsl:text>
497                         </xsl:if>
498                 </xsl:if>
499         </xsl:template>
500         
501         <xsl:template match="html:p" mode="glossary">
502                 <xsl:apply-templates select="node()"/>
503                 <xsl:text>\\
504 \vspace{24pt}
505 </xsl:text>
506         </xsl:template>
507
508                 <xsl:template match="html:p[last()]" mode="glossary">
509                         <xsl:apply-templates select="node()"/>
510                 </xsl:template>
511         
512         <!-- use these when asked for -->
513         <xsl:template match="html:span[@class='glossary name']" mode="glossary">
514                 <xsl:text>{name=</xsl:text>
515                 <xsl:apply-templates select="node()"/>
516                 <xsl:text>,</xsl:text>
517         </xsl:template>
518         
519         <xsl:template match="html:span[@class='glossary sort']" mode="glossary">
520                 <xsl:text>sort=</xsl:text>
521                 <xsl:apply-templates select="node()"/>
522                 <xsl:text>,</xsl:text>
523         </xsl:template>
524
525         <!-- otherwise, ignore them -->
526         <xsl:template match="html:span[@class='glossary name']">
527         </xsl:template>
528         
529         <xsl:template match="html:span[@class='glossary sort']">
530         </xsl:template>
531
532         <!-- anchors -->
533         <xsl:template match="html:a[@href]">
534                 <xsl:choose>
535                         <!-- footnote (my addition)-->
536                         <xsl:when test="@class = 'footnote'">
537                                 <xsl:text>\footnote{</xsl:text>
538                                 <xsl:apply-templates select="/html:html/html:body/html:div[@class]/html:ol/html:li[@id]" mode="footnote">
539                                         <xsl:with-param name="footnoteId" select="@href"/>
540                                 </xsl:apply-templates>
541                                 <xsl:text>}</xsl:text>
542                         </xsl:when>
543
544                         <xsl:when test="@class = 'footnote glossary'">
545                                 <xsl:text>\glossary</xsl:text>
546                                 <xsl:apply-templates select="/html:html/html:body/html:div[@class]/html:ol/html:li[@id]" mode="glossary">
547                                         <xsl:with-param name="footnoteId" select="@href"/>
548                                 </xsl:apply-templates>
549                                 <xsl:text></xsl:text>
550                         </xsl:when>
551
552                         <xsl:when test="@class = 'reversefootnote'">
553                         </xsl:when>
554
555                         <!-- if href is same as the anchor text, then use \href{} 
556                                 but no footnote -->
557                         <!-- let's try \url{} again for line break reasons -->
558                         <xsl:when test="@href = .">
559                                 <xsl:text>\url{</xsl:text>
560                                 <xsl:call-template name="clean-text">
561                                         <xsl:with-param name="source">
562                                                 <xsl:value-of select="@href"/>
563                                         </xsl:with-param>
564                                 </xsl:call-template>            
565                                 <xsl:text>}</xsl:text>
566                         </xsl:when>
567
568                         <!-- if href is mailto, use \href{} -->
569                         <xsl:when test="starts-with(@href,'mailto:')">
570                                 <xsl:text>\href{</xsl:text>
571                                 <xsl:value-of select="@href"/>
572                                 <xsl:text>}{</xsl:text>
573                                 <xsl:call-template name="clean-text">
574                                         <xsl:with-param name="source">
575                                                 <xsl:value-of select="substring-after(@href,'mailto:')"/>
576                                         </xsl:with-param>
577                                 </xsl:call-template>            
578                                 <xsl:text>}</xsl:text>
579                         </xsl:when>
580                         
581                         <!-- if href is local anchor, use autoref -->
582                         <xsl:when test="starts-with(@href,'#')">
583                                 <xsl:choose>
584                                         <xsl:when test=". = ''">
585                                                 <xsl:text>\autoref{</xsl:text>
586                                                 <xsl:value-of select="substring-after(@href,'#')"/>
587                                                 <xsl:text>}</xsl:text>
588                                         </xsl:when>
589                                         <xsl:otherwise>
590                                                 <xsl:value-of select="."/>
591                                                 <xsl:text> (\autoref{</xsl:text>
592                                                 <xsl:value-of select="substring-after(@href,'#')"/>
593                                                 <xsl:text>})</xsl:text>
594                                         </xsl:otherwise>
595                                 </xsl:choose>
596                         </xsl:when>
597                         
598                         <!-- otherwise, implement an href and put href in footnote
599                                 for printed version -->
600                         <xsl:otherwise>
601                                 <xsl:text>\href{</xsl:text>
602                                 <xsl:value-of select="@href"/>
603                                 <xsl:text>}{</xsl:text>
604                                 <xsl:call-template name="clean-text">
605                                         <xsl:with-param name="source">
606                                                 <xsl:value-of select="."/>
607                                         </xsl:with-param>
608                                 </xsl:call-template>            
609                                 <xsl:text>}\footnote{\href{</xsl:text>
610                                 <xsl:value-of select="@href"/>
611                                 <xsl:text>}{</xsl:text>
612                                 <xsl:call-template name="clean-text">
613                                         <xsl:with-param name="source">
614                                                 <xsl:value-of select="@href"/>
615                                         </xsl:with-param>
616                                 </xsl:call-template>            
617                                 <xsl:text>}}</xsl:text>
618                         </xsl:otherwise>
619                 </xsl:choose>
620         </xsl:template>
621
622         <!-- ordered list -->
623         <xsl:template match="html:ol">
624                 <xsl:text>\begin{enumerate}</xsl:text>
625                 <xsl:value-of select="$newline"/>
626                 <xsl:value-of select="$newline"/>
627                 <xsl:apply-templates select="*"/>
628                 <xsl:text>\end{enumerate}</xsl:text>
629                 <xsl:value-of select="$newline"/>
630                 <xsl:value-of select="$newline"/>
631         </xsl:template>
632
633         <!-- unordered list -->
634         <xsl:template match="html:ul">
635                 <xsl:text>\begin{itemize}</xsl:text>
636                 <xsl:value-of select="$newline"/>
637                 <xsl:value-of select="$newline"/>
638                 <xsl:apply-templates select="*"/>
639                 <xsl:text>\end{itemize}</xsl:text>
640                 <xsl:value-of select="$newline"/>
641                 <xsl:value-of select="$newline"/>
642         </xsl:template>
643         
644         <!-- list item -->
645         <xsl:template match="html:li">
646                 <xsl:text>
647 \item </xsl:text>
648                 <xsl:apply-templates select="node()"/>
649                 <xsl:value-of select="$newline"/>
650         </xsl:template>
651                 
652         <!-- definition list - fake it for compatibility with XHTML version -->
653         <xsl:template match="html:dl">
654                 <xsl:apply-templates select="node()"/>
655                 <xsl:text>
656
657 </xsl:text>
658         </xsl:template>
659         
660         <xsl:template match="html:dt">
661                 <xsl:text>\noindent\textbf{</xsl:text>
662                 <xsl:apply-templates select="node()"/>
663                 <xsl:text>}
664 </xsl:text>
665         </xsl:template>
666
667         <xsl:template match="html:dt[following-sibling::*[1][local-name() = 'dt']]">
668                 <xsl:text>\noindent\textbf{</xsl:text>
669                 <xsl:apply-templates select="node()"/>
670                 <xsl:text>} \\
671 </xsl:text>
672         </xsl:template>
673         
674         <xsl:template match="html:dd">
675                 <xsl:text>\begin{quotation}
676 </xsl:text>
677                 <xsl:apply-templates select="node()"/>
678                 <xsl:text>\end{quotation}
679 </xsl:text>
680         </xsl:template>
681
682         <!-- code span -->
683         <xsl:template match="html:code">
684                 <xsl:text>\texttt{</xsl:text>
685                 <xsl:apply-templates select="node()"/>
686                 <xsl:text>}</xsl:text>
687         </xsl:template>
688
689         <!-- line ending -->
690         <xsl:template match="html:br">
691                 <xsl:text>\\</xsl:text>
692         </xsl:template>
693
694         <!-- blockquote -->
695         <xsl:template match="html:blockquote">
696                 <xsl:text>\begin{quotation}
697 </xsl:text>
698                 <xsl:apply-templates select="node()"/>
699                 <xsl:text>\end{quotation}
700
701 </xsl:text>
702         </xsl:template>
703
704         <!-- emphasis -->
705         <xsl:template match="html:em">
706                 <xsl:text>{\itshape </xsl:text>
707                         <xsl:apply-templates select="node()"/>
708                 <xsl:text>}</xsl:text>
709         </xsl:template>
710
711         <!-- strong -->
712         <xsl:template match="html:strong">
713                 <xsl:text>\textbf{</xsl:text>
714                         <xsl:apply-templates select="node()"/>
715                 <xsl:text>}</xsl:text>
716         </xsl:template>
717         
718         <!-- horizontal rule -->
719         <xsl:template match="html:hr">
720                 <xsl:text>\vskip 2em
721 \hrule height 0.4pt
722 \vskip 2em
723
724 </xsl:text>
725         </xsl:template>
726
727         <!-- images -->
728         <xsl:template match="html:img">
729                 <xsl:text>\begin{figure}
730 </xsl:text>
731                 <xsl:if test="@title">
732                         <xsl:text>\caption{</xsl:text>
733                         <xsl:apply-templates select="@title"/>
734                         <xsl:text>}
735 </xsl:text>
736                 </xsl:if>
737                 <xsl:if test="@id">
738                         <xsl:text>\label{</xsl:text>
739                         <xsl:value-of select="@id"/>
740                         <xsl:text>}
741 </xsl:text>
742                 </xsl:if>
743                 <xsl:text>\begin{center}
744 </xsl:text>
745                 <xsl:if test="@width|@height">
746                         <!-- there are dimensions, so use them -->
747                         <!-- Basically, we allow any units covered by LaTeX, even
748                                 if they are not allowed in XHTML.  px is converted to pt.
749                                 If no units, then assume pt.
750                         -->
751                         <xsl:text>\resizebox{</xsl:text>
752                         <xsl:choose>
753                                 <xsl:when test="@width">
754                                         <xsl:call-template name="replace-substring">
755                                                 <xsl:with-param name="original">
756                                                         <xsl:value-of select="@width"/>
757                                                 </xsl:with-param>
758                                                 <xsl:with-param name="substring">
759                                                         <xsl:text>px</xsl:text>
760                                                 </xsl:with-param>
761                                                 <xsl:with-param name="replacement">
762                                                         <xsl:text>pt</xsl:text>
763                                                 </xsl:with-param>
764                                         </xsl:call-template>
765                                         <xsl:if test="translate(@width, 
766         'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890.'
767         ,'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') = '' ">
768                                                 <!-- no units specified -->
769                                                 <xsl:text>pt</xsl:text>
770                                         </xsl:if>
771                                 </xsl:when> 
772                                 <xsl:otherwise>
773                                         <xsl:text>!</xsl:text>
774                                 </xsl:otherwise>
775                         </xsl:choose>
776                         <xsl:text>}{</xsl:text>
777                         <xsl:choose>
778                                 <xsl:when test="@height">
779                                         <xsl:call-template name="replace-substring">
780                                                 <xsl:with-param name="original">
781                                                         <xsl:value-of select="@height"/>
782                                                 </xsl:with-param>
783                                                 <xsl:with-param name="substring">
784                                                         <xsl:text>px</xsl:text>
785                                                 </xsl:with-param>
786                                                 <xsl:with-param name="replacement">
787                                                         <xsl:text>pt</xsl:text>
788                                                 </xsl:with-param>
789                                         </xsl:call-template>
790                                         <xsl:if test="translate(@height, 
791         'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890.'
792         ,'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') = '' ">
793                                                 <!-- no units specified -->
794                                                 <xsl:text>pt</xsl:text>
795                                         </xsl:if>
796                                 </xsl:when> 
797                                 <xsl:otherwise>
798                                         <xsl:text>!</xsl:text>
799                                 </xsl:otherwise>
800                         </xsl:choose>
801                         <xsl:text>}{</xsl:text>
802                 </xsl:if>
803                 <xsl:if test="not(@width|@height)">
804                         <!-- if no dimensions, then ensure it fits on page
805                                 (of course, this also goes to "max zoom"...)
806                          -->
807                          <xsl:text>\resizebox{1\linewidth}{!}{</xsl:text>
808                 </xsl:if>
809                 <xsl:text>\includegraphics{</xsl:text>
810                 <xsl:value-of select="@src"/>
811                 <xsl:text>}}
812 \end{center}
813 \end{figure}
814 </xsl:text>
815         </xsl:template>
816         
817         <!-- footnotes -->
818         <xsl:template match="html:div">
819                 <xsl:if test="not(@class = 'footnotes')">
820                         <xsl:apply-templates select="node()"/>
821                 </xsl:if>
822         </xsl:template>
823
824         <!-- pull-quotes (a table with no header, and a single column) -->
825         <!-- this is experimental, and I am open to suggestions -->
826         <xsl:template match="html:table[@class='pull-quote']">
827                 <xsl:text>\begin{table}[htbp]
828 \begin{minipage}{\linewidth}
829 \centering
830 </xsl:text>
831                 <xsl:apply-templates select="html:caption"/>
832                 <xsl:text>\begin{tabular}{@{}p{0.5\linewidth}@{}} \\ \toprule </xsl:text>
833                 <xsl:apply-templates select="html:thead"/>
834                 <xsl:apply-templates select="html:tbody"/>
835                 <xsl:apply-templates select="html:tr"/>
836                 <xsl:text>\end{tabular}
837 \end{minipage}
838 \end{table}
839
840 </xsl:text>
841         </xsl:template>
842
843         <!-- tables -->
844         <xsl:template match="html:table">
845                 <xsl:text>\begin{table}[htbp]
846 \begin{minipage}{\linewidth}
847 \setlength{\tymax}{0.5\linewidth}
848 \centering
849 \small
850 </xsl:text>
851                 <xsl:apply-templates select="html:caption"/>
852                 <xsl:text>\begin{tabulary}{\linewidth}{@{}</xsl:text>
853                 <xsl:apply-templates select="html:col"/>
854                 <xsl:text>@{}} \\ \toprule </xsl:text>
855                 <xsl:apply-templates select="html:thead"/>
856                 <xsl:apply-templates select="html:tbody"/>
857                 <xsl:apply-templates select="html:tr"/>
858                 <xsl:text>\end{tabulary}
859 \end{minipage}
860 \end{table}
861
862 </xsl:text>
863         </xsl:template>
864         
865         <xsl:template match="html:tbody">
866                 <xsl:apply-templates select="html:tr"/>
867                 \bottomrule
868         </xsl:template>
869
870         <xsl:template match="html:col">
871                 <xsl:choose>
872                         <xsl:when test="@align='center'">
873                                 <xsl:choose>
874                                         <xsl:when test="@class='extended'">
875                                                 <xsl:text>C</xsl:text>
876                                         </xsl:when>
877                                         <xsl:otherwise>
878                                                 <xsl:text>c</xsl:text>
879                                         </xsl:otherwise>
880                                 </xsl:choose>
881                         </xsl:when>
882                         <xsl:when test="@align='right'">
883                                 <xsl:choose>
884                                         <xsl:when test="@class='extended'">
885                                                 <xsl:text>R</xsl:text>
886                                         </xsl:when>
887                                         <xsl:otherwise>
888                                                 <xsl:text>r</xsl:text>
889                                         </xsl:otherwise>
890                                 </xsl:choose>
891                         </xsl:when>
892                         <xsl:otherwise>
893                                 <xsl:choose>
894                                         <xsl:when test="@class='extended'">
895                                                 <xsl:text>J</xsl:text>
896                                         </xsl:when>
897                                         <xsl:otherwise>
898                                                 <xsl:text>l</xsl:text>
899                                         </xsl:otherwise>
900                                 </xsl:choose>
901                         </xsl:otherwise>
902                 </xsl:choose>
903         </xsl:template>
904
905         <xsl:template match="html:thead">
906                 <xsl:apply-templates select="html:tr" mode="header"/>
907                 <xsl:text>\midrule
908 </xsl:text>
909         </xsl:template>
910         
911         <xsl:template match="html:caption">
912                 <xsl:text>\caption{</xsl:text>
913                         <xsl:apply-templates select="node()"/>
914                 <xsl:text>}
915 </xsl:text>
916                 <xsl:if test="@id">
917                         <xsl:text>\label{</xsl:text>
918                         <xsl:value-of select="@id"/>
919                         <xsl:text>}
920 </xsl:text>
921                 </xsl:if>
922         </xsl:template>
923         
924         <xsl:template match="html:tr" mode="header">
925                 <xsl:text>
926 </xsl:text>
927                 <xsl:apply-templates select="html:td|html:th"/>
928                 <xsl:text> \\ </xsl:text>
929                 <!-- figure out a way to count columns for \cmidrule{x-y} -->
930                 <xsl:apply-templates select="html:td[1]|html:th[1]" mode="cmidrule">
931                         <xsl:with-param name="col" select="1"/>
932                 </xsl:apply-templates>
933         </xsl:template>
934
935         <xsl:template match="html:td|html:th" mode="cmidrule">
936                 <xsl:param name="col"/>
937                 <xsl:param name="end" select="$col+format-number(@colspan,'#','string')-1"/>
938                 <xsl:if test="not(. = '')">
939                         <xsl:text> \cmidrule{</xsl:text>
940                         <xsl:value-of select="$col"/>
941                         <xsl:text>-</xsl:text>
942                         <xsl:value-of select="$end"/>
943                         <xsl:text>}</xsl:text>
944                 </xsl:if>
945                 <xsl:apply-templates select="following-sibling::*[1]" mode="cmidrule">
946                         <xsl:with-param name="col" select="$end+1"/>
947                 </xsl:apply-templates>
948         </xsl:template>
949         
950         <xsl:template match="html:tr[last()]" mode="header">
951                 <xsl:text>
952 </xsl:text>
953                 <xsl:apply-templates select="html:td|html:th"/>
954                 <xsl:text> \\</xsl:text>
955         </xsl:template>
956
957         <xsl:template match="html:tr">
958                 <xsl:apply-templates select="html:td|html:th"/>
959                 <xsl:text> \\
960 </xsl:text>
961         </xsl:template>
962
963         <xsl:template match="html:th|html:td">
964                 <xsl:if test="@colspan">
965                         <xsl:text>\multicolumn{</xsl:text>
966                         <xsl:value-of select="@colspan"/>
967                 </xsl:if>
968                 <xsl:if test="@colspan">
969                         <xsl:text>}{c}{</xsl:text>
970                 </xsl:if>
971                 <xsl:apply-templates select="node()"/>
972                 <xsl:if test="@colspan">
973                         <xsl:text>}</xsl:text>
974                 </xsl:if>
975                 <xsl:if test="position()!=last()">
976                 <xsl:text>&amp;</xsl:text>
977                 </xsl:if>
978         </xsl:template>
979         
980         <!-- Support for Bibliography to BibTeX conversion -->
981         
982         <xsl:template match="html:span[@class='externalcitation']">
983                 <xsl:text>\cite</xsl:text>
984                 <xsl:apply-templates select="html:span" mode="citation"/>
985                 <xsl:apply-templates select="html:a" mode="citation"/>
986                 <xsl:text>}</xsl:text>
987         </xsl:template>
988
989         <xsl:template match="html:span[@class='markdowncitation']">
990                 <xsl:text>~\cite</xsl:text>
991                 <xsl:apply-templates select="html:span" mode="citation"/>
992                 <xsl:apply-templates select="html:a" mode="markdowncitation"/>
993                 <xsl:text>}</xsl:text>
994         </xsl:template>
995         
996         <xsl:template match="html:span[@class='notcited']">
997                 <xsl:text>~\nocite{</xsl:text>
998                 <xsl:value-of select="@id"/>
999                 <xsl:text>}</xsl:text>
1000         </xsl:template>
1001
1002         <xsl:template match="html:a[@id]" mode="citation">
1003                 <xsl:text>{</xsl:text>
1004                 <xsl:value-of select="@id"/>
1005         </xsl:template>
1006
1007         <xsl:template match="html:a[@href]" mode="markdowncitation">
1008                 <xsl:text>{</xsl:text>
1009                 <xsl:value-of select="substring-after(@href,'#')"/>
1010         </xsl:template>
1011
1012         <xsl:template match="html:span[@class='locator']" mode="citation">
1013                 <xsl:text>[</xsl:text>
1014                 <xsl:value-of select="."/>
1015                 <xsl:text>]</xsl:text>
1016         </xsl:template>
1017
1018         <!-- Disabled unless natbib is implemented -->
1019         <xsl:template match="html:span[@class='textual citation']" mode="citation">
1020                 <xsl:text></xsl:text>
1021         </xsl:template>
1022         
1023         <xsl:template match="html:div[@class='bibliography']">
1024                 <xsl:text>\begin{thebibliography}{</xsl:text>
1025                 <xsl:value-of select="count(div[@id])"/>
1026                 <xsl:text>}
1027 </xsl:text>
1028                 <xsl:apply-templates select="html:div"/>
1029                 <xsl:text>
1030                 
1031 \end{thebibliography}
1032                 
1033 </xsl:text>
1034         </xsl:template>
1035                                 
1036         <xsl:template match="html:div[@class='bibliography']/html:div[@id]">
1037                 <xsl:text>
1038
1039 \bibitem{</xsl:text>
1040                 <xsl:value-of select="@id"/>
1041                 <xsl:text>}
1042 </xsl:text>
1043                 <xsl:apply-templates select="html:p/html:span[@class='item']" mode="citation"/>
1044         </xsl:template> 
1045
1046         <xsl:template match="html:span[@class='item']" mode="citation">
1047                 <xsl:apply-templates select="."/>
1048         </xsl:template>
1049         
1050                 
1051         <!-- Default LaTeX code to add -->
1052         
1053         <xsl:template name="latex-header">
1054                 <xsl:text>\usepackage{fancyvrb}                 % Allow \verbatim et al. in footnotes
1055 \usepackage{graphicx}                   % To include graphics in pdf's (jpg, gif, png, etc)
1056 \usepackage{booktabs}                   % Better tables
1057 \usepackage{tabulary}                   % Support longer table cells
1058 \usepackage[utf8]{inputenc}             % For UTF-8 support
1059 \usepackage{xcolor}                             % Allow for color (annotations)
1060
1061 %\geometry{landscape}                   % Activate for rotated page geometry
1062
1063 %\usepackage[parfill]{parskip}  % Activate to begin paragraphs with an empty
1064                                                                 % line rather than an indent
1065
1066
1067 \def\myauthor{Author}                   % In case these were not included in metadata
1068 \def\mytitle{Title}
1069 \def\mykeywords{}
1070 \def\mybibliostyle{plain}
1071 \def\bibliocommand{}
1072
1073 \VerbatimFootnotes
1074 </xsl:text>
1075         </xsl:template>
1076         
1077         <xsl:template name="latex-intro">
1078                                 <xsl:text>
1079
1080 %
1081 %       PDF Stuff
1082 %
1083
1084 %\ifpdf                                                 % Removed for XeLaTeX compatibility
1085 %  \pdfoutput=1                                 % Removed for XeLaTeX compatibility
1086   \usepackage[
1087         plainpages=false,
1088         pdfpagelabels,
1089         pdftitle={\mytitle},
1090         pagebackref,
1091         pdfauthor={\myauthor},
1092         pdfkeywords={\mykeywords}
1093         ]{hyperref}
1094   \usepackage{memhfixc}
1095 %\fi                                                    % Removed for XeLaTeX compatibility
1096
1097
1098 %
1099 % Title Information
1100 %
1101
1102 \ifx\subtitle\undefined
1103 \else
1104         \addtodef{\mytitle}{}{ \\ \subtitle}
1105 \fi
1106
1107 \ifx\affiliation\undefined
1108 \else
1109         \addtodef{\myauthor}{}{ \\ \affiliation}
1110 \fi
1111
1112 \ifx\address\undefined
1113 \else
1114         \addtodef{\myauthor}{}{ \\ \address}
1115 \fi
1116
1117 \ifx\phone\undefined
1118 \else
1119         \addtodef{\myauthor}{}{ \\ \phone}
1120 \fi
1121
1122 \ifx\email\undefined
1123 \else
1124         \addtodef{\myauthor}{}{ \\ \email}
1125 \fi
1126
1127 \ifx\web\undefined
1128         \else
1129                 \addtodef{\myauthor}{}{ \\ \web}
1130 \fi
1131
1132 \title{\mytitle}
1133 \author{\myauthor}
1134
1135 \begin{document}
1136 </xsl:text>
1137         </xsl:template>
1138
1139         <xsl:template name="latex-paper-size">
1140         </xsl:template>
1141
1142         <xsl:template name="latex-footer">
1143                 <xsl:text>% Bibliography
1144 \bibliographystyle{\mybibliostyle}
1145 \bibliocommand
1146
1147 \end{document}
1148 </xsl:text>
1149         </xsl:template>
1150
1151
1152         <!-- Allow for spans to set a color 
1153                 Specifically, this is useful with Scrivener -->
1154         <xsl:template match="html:span[starts-with(@style,'color:')]">
1155                 <xsl:text>{\color[HTML]{</xsl:text>
1156                 <xsl:call-template name="replace-substring">
1157                         <xsl:with-param name="original">
1158                                 <xsl:value-of select="@style"/>
1159                         </xsl:with-param>
1160                         <xsl:with-param name="substring">
1161                                 <xsl:text>color:#</xsl:text>
1162                         </xsl:with-param>
1163                         <xsl:with-param name="replacement">
1164                                 <xsl:text></xsl:text>
1165                         </xsl:with-param>
1166                 </xsl:call-template>
1167                 <xsl:text>} </xsl:text>
1168                 <xsl:apply-templates select="node()"/>
1169                 <xsl:text>}</xsl:text>
1170         </xsl:template>
1171
1172         <!-- replace-substring routine by Doug Tidwell - XSLT, O'Reilly Media -->
1173         <xsl:template name="replace-substring">
1174                 <xsl:param name="original" />
1175                 <xsl:param name="substring" />
1176                 <xsl:param name="replacement" select="''"/>
1177                 <xsl:variable name="first">
1178                         <xsl:choose>
1179                                 <xsl:when test="contains($original, $substring)" >
1180                                         <xsl:value-of select="substring-before($original, $substring)"/>
1181                                 </xsl:when>
1182                                 <xsl:otherwise>
1183                                         <xsl:value-of select="$original"/>
1184                                 </xsl:otherwise>
1185                         </xsl:choose>
1186                 </xsl:variable>
1187                 <xsl:variable name="middle">
1188                         <xsl:choose>
1189                                 <xsl:when test="contains($original, $substring)" >
1190                                         <xsl:value-of select="$replacement"/>
1191                                 </xsl:when>
1192                                 <xsl:otherwise>
1193                                         <xsl:text></xsl:text>
1194                                 </xsl:otherwise>
1195                         </xsl:choose>
1196                 </xsl:variable>
1197                 <xsl:variable name="last">
1198                         <xsl:choose>
1199                                 <xsl:when test="contains($original, $substring)">
1200                                         <xsl:choose>
1201                                                 <xsl:when test="contains(substring-after($original, $substring), $substring)">
1202                                                         <xsl:call-template name="replace-substring">
1203                                                                 <xsl:with-param name="original">
1204                                                                         <xsl:value-of select="substring-after($original, $substring)" />
1205                                                                 </xsl:with-param>
1206                                                                 <xsl:with-param name="substring">
1207                                                                         <xsl:value-of select="$substring" />
1208                                                                 </xsl:with-param>
1209                                                                 <xsl:with-param name="replacement">
1210                                                                         <xsl:value-of select="$replacement" />
1211                                                                 </xsl:with-param>
1212                                                         </xsl:call-template>
1213                                                 </xsl:when>     
1214                                                 <xsl:otherwise>
1215                                                         <xsl:value-of select="substring-after($original, $substring)"/>
1216                                                 </xsl:otherwise>
1217                                         </xsl:choose>
1218                                 </xsl:when>
1219                                 <xsl:otherwise>
1220                                         <xsl:text></xsl:text>
1221                                 </xsl:otherwise>                
1222                         </xsl:choose>                           
1223                 </xsl:variable>         
1224                 <xsl:value-of select="concat($first, $middle, $last)"/>
1225         </xsl:template> 
1226 </xsl:stylesheet>