Accept attributes in inline image syntax
[multimarkdown] / XSLT / memoir.xslt
1 <?xml version='1.0' encoding='utf-8'?>
2
3 <!-- XHTML-to-Memoir converter by Fletcher Penney
4         specifically designed for use with MultiMarkdown created XHTML
5
6         Uses the LaTeX memoir class for output  
7         
8         MultiMarkdown Version 2.0.b5
9         
10         $Id: memoir.xslt 479 2008-01-12 23:04:13Z fletcher $
11 -->
12
13 <!-- 
14 # Copyright (C) 2005-2008  Fletcher T. Penney <fletcher@fletcherpenney.net>
15 #
16 # This program is free software; you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation; either version 2 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 # GNU General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the
28 #    Free Software Foundation, Inc.
29 #    59 Temple Place, Suite 330
30 #    Boston, MA 02111-1307 USA
31 -->
32
33         
34 <xsl:stylesheet
35         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
36         xmlns:html="http://www.w3.org/1999/xhtml"
37         version="1.0">
38
39         <xsl:import href="xhtml2latex.xslt"/>
40         
41         <xsl:template match="/">
42                 <xsl:apply-templates select="html:html/html:head"/>
43                 <xsl:apply-templates select="html:html/html:body"/>
44                 <xsl:call-template name="latex-footer"/>
45         </xsl:template>
46
47         <xsl:template name="latex-footer">
48                 <xsl:text>%
49 % Back Matter
50 %
51
52 \backmatter
53 %\appendixpage
54
55 %       Bibliography
56 \bibliographystyle{\mybibliostyle}
57 \bibliocommand
58
59 %       Glossary
60 \printglossary
61
62
63 %       Index
64 \printindex
65
66 \end{document}
67 </xsl:text>
68         </xsl:template>
69
70         <xsl:template name="latex-document-class">
71                 <xsl:text>\documentclass[10pt,oneside]{memoir}
72 \usepackage{layouts}[2001/04/29]
73 \makeglossary
74 \makeindex
75
76 \def\mychapterstyle{default}
77 \def\mypagestyle{headings}
78 \def\revision{}
79
80 </xsl:text>
81         </xsl:template>
82
83         <xsl:template name="latex-paper-size">
84                 <xsl:text>%%% need more space for ToC page numbers
85 \setpnumwidth{2.55em}
86 \setrmarg{3.55em}
87
88 %%% need more space for ToC section numbers
89 \cftsetindents{part}{0em}{3em}
90 \cftsetindents{chapter}{0em}{3em}
91 \cftsetindents{section}{3em}{3em}
92 \cftsetindents{subsection}{4.5em}{3.9em}
93 \cftsetindents{subsubsection}{8.4em}{4.8em}
94 \cftsetindents{paragraph}{10.7em}{5.7em}
95 \cftsetindents{subparagraph}{12.7em}{6.7em}
96
97 %%% need more space for LoF numbers
98 \cftsetindents{figure}{0em}{3.0em}
99
100 %%% and do the same for the LoT
101 \cftsetindents{table}{0em}{3.0em}
102
103 %%% set up the page layout
104 \settrimmedsize{\stockheight}{\stockwidth}{*}   % Use entire page
105 \settrims{0pt}{0pt}
106
107 \setlrmarginsandblock{1.5in}{1.5in}{*}
108 \setulmarginsandblock{1.5in}{1.5in}{*}
109
110 \setmarginnotes{17pt}{51pt}{\onelineskip}
111 \setheadfoot{\onelineskip}{2\onelineskip}
112 \setheaderspaces{*}{2\onelineskip}{*}
113 \checkandfixthelayout
114
115 </xsl:text>
116         </xsl:template>
117
118         <xsl:template name="latex-title">
119                         <xsl:text>
120 \chapterstyle{\mychapterstyle}
121 \pagestyle{\mypagestyle}
122
123 %
124 %               Front Matter
125 %
126
127 \frontmatter
128
129
130 % Title Page
131
132 \maketitle
133 \clearpage
134
135 </xsl:text>
136         </xsl:template>
137
138         <xsl:template name="latex-copyright">
139                         <xsl:text>% Copyright Page
140 \vspace*{\fill}
141
142 \setlength{\parindent}{0pt}
143
144 \ifx\mycopyright\undefined
145 \else
146         \textcopyright{} \mycopyright
147 \fi
148
149 \revision
150
151 \begin{center}
152 \framebox{ \parbox[t]{1.5in}{\centering Formatted for \LaTeX  \\ 
153  by MultiMarkdown}}
154 \end{center}
155
156 \setlength{\parindent}{1em}
157 \clearpage
158
159 </xsl:text>
160         </xsl:template>
161
162         <xsl:template name="latex-begin-body">
163                 <xsl:text>% Table of Contents
164 \tableofcontents
165 %\listoffigures                 % activate to include a List of Figures
166 %\listoftables                  % activate to include a List of Tables
167
168
169 %
170 % Main Content
171 %
172
173
174 % Layout settings
175 \setlength{\parindent}{1em}
176
177 \mainmatter
178 </xsl:text>
179         </xsl:template>
180
181         <!-- Rename Bibliography -->
182         <xsl:template name="rename-bibliography">
183                 <xsl:param name="source" />
184                 <xsl:text>\renewcommand\bibname{</xsl:text>
185                 <xsl:value-of select="$source" />
186                 <xsl:text>}
187 </xsl:text>
188         </xsl:template>
189
190         <!-- Convert headers into chapters, etc -->
191         
192         <xsl:template match="html:h1">
193                 <xsl:text>\part{</xsl:text>
194                 <xsl:apply-templates select="node()"/>
195                 <xsl:text>}</xsl:text>
196                 <xsl:value-of select="$newline"/>
197                 <xsl:text>\label{</xsl:text>
198                 <xsl:value-of select="@id"/>
199                 <xsl:text>}</xsl:text>
200                 <xsl:value-of select="$newline"/>
201                 <xsl:value-of select="$newline"/>
202         </xsl:template>
203
204         <xsl:template match="html:h2">
205                 <xsl:text>\chapter{</xsl:text>
206                 <xsl:apply-templates select="node()"/>
207                 <xsl:text>}</xsl:text>
208                 <xsl:value-of select="$newline"/>
209                 <xsl:text>\label{</xsl:text>
210                 <xsl:value-of select="@id"/>
211                 <xsl:text>}</xsl:text>
212                 <xsl:value-of select="$newline"/>
213                 <xsl:value-of select="$newline"/>
214         </xsl:template>
215
216         <xsl:template match="html:h3">
217                 <xsl:text>\section{</xsl:text>
218                 <xsl:apply-templates select="node()"/>
219                 <xsl:text>}</xsl:text>
220                 <xsl:value-of select="$newline"/>
221                 <xsl:text>\label{</xsl:text>
222                 <xsl:value-of select="@id"/>
223                 <xsl:text>}</xsl:text>
224                 <xsl:value-of select="$newline"/>
225                 <xsl:value-of select="$newline"/>
226         </xsl:template>
227
228         <xsl:template match="html:h4">
229                 <xsl:text>\subsection{</xsl:text>
230                 <xsl:apply-templates select="node()"/>
231                 <xsl:text>}</xsl:text>
232                 <xsl:value-of select="$newline"/>
233                 <xsl:text>\label{</xsl:text>
234                 <xsl:value-of select="@id"/>
235                 <xsl:text>}</xsl:text>
236                 <xsl:value-of select="$newline"/>
237                 <xsl:value-of select="$newline"/>
238         </xsl:template>
239
240         <xsl:template match="html:h5">
241                 <xsl:text>\subsubsection{</xsl:text>
242                 <xsl:apply-templates select="node()"/>
243                 <xsl:text>}</xsl:text>
244                 <xsl:value-of select="$newline"/>
245                 <xsl:text>\label{</xsl:text>
246                 <xsl:value-of select="@id"/>
247                 <xsl:text>}</xsl:text>
248                 <xsl:value-of select="$newline"/>
249                 <xsl:value-of select="$newline"/>
250         </xsl:template>
251
252         <xsl:template match="html:h6">
253                 <xsl:text>{\itshape </xsl:text>
254                 <xsl:apply-templates select="node()"/>
255                 <xsl:text>}</xsl:text>
256                 <xsl:value-of select="$newline"/>
257                 <xsl:text>\label{</xsl:text>
258                 <xsl:value-of select="@id"/>
259                 <xsl:text>}</xsl:text>
260                 <xsl:value-of select="$newline"/>
261                 <xsl:value-of select="$newline"/>
262         </xsl:template>
263
264         <!-- add support for Appendices - include a part or chapter named 'Appendices' to trigger-->
265         
266         <xsl:template match="html:h2[translate(.,'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
267         'abcdefghijklmnopqrstuvwxyz') = 'appendices']">
268                 <xsl:text>\appendixpage*
269 \appendix</xsl:text>
270                 <xsl:value-of select="$newline"/>
271                 <xsl:value-of select="$newline"/>
272         </xsl:template>
273
274         <xsl:template match="html:h1[translate(.,'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
275         'abcdefghijklmnopqrstuvwxyz') = 'appendices']">
276                 <xsl:text>\appendixpage*
277 \appendix</xsl:text>
278                 <xsl:value-of select="$newline"/>
279                 <xsl:value-of select="$newline"/>
280         </xsl:template>
281
282         <!-- support for abstracts -->
283         
284         <xsl:template match="html:h2[1][translate(.,'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
285         'abcdefghijklmnopqrstuvwxyz') = 'abstract']">
286                 <xsl:text>\begin{abstract}</xsl:text>
287                 <xsl:value-of select="$newline"/>
288                 <xsl:text>\label{</xsl:text>
289                 <xsl:value-of select="@id"/>
290                 <xsl:text>}</xsl:text>
291                 <xsl:value-of select="$newline"/>
292                 <xsl:text>\addcontentsline{toc}{chapter}{</xsl:text>
293                 <xsl:apply-templates select="node()"/>
294                 <xsl:text>}</xsl:text>
295                 <xsl:value-of select="$newline"/>
296                 <xsl:value-of select="$newline"/>
297         </xsl:template>
298
299         <xsl:template match="html:h2[position()='2'][preceding-sibling::html:h2[position()='1'][translate(.,'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
300                 'abcdefghijklmnopqrstuvwxyz') = 'abstract']]">
301                 <xsl:text>\end{abstract}</xsl:text>
302                 <xsl:value-of select="$newline"/>
303                 <xsl:value-of select="$newline"/>
304                 <xsl:text>\chapter{</xsl:text>
305                 <xsl:apply-templates select="node()"/>
306                 <xsl:text>}</xsl:text>
307                 <xsl:value-of select="$newline"/>
308                 <xsl:text>\label{</xsl:text>
309                 <xsl:value-of select="@id"/>
310                 <xsl:text>}</xsl:text>
311                 <xsl:value-of select="$newline"/>
312                 <xsl:value-of select="$newline"/>
313         </xsl:template>
314
315         <xsl:template match="html:h3[count(preceding-sibling::html:h2) = '1'][preceding-sibling::html:h2[position()='1'][translate(.,'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
316                 'abcdefghijklmnopqrstuvwxyz') = 'abstract']]">
317                 <xsl:text>\section*{</xsl:text>
318                 <xsl:apply-templates select="node()"/>
319                 <xsl:text>}</xsl:text>
320                 <xsl:value-of select="$newline"/>
321                 <xsl:text>\label{</xsl:text>
322                 <xsl:value-of select="@id"/>
323                 <xsl:text>}</xsl:text>
324                 <xsl:value-of select="$newline"/>
325                 <xsl:value-of select="$newline"/>
326         </xsl:template>
327
328         <xsl:template match="html:h4[count(preceding-sibling::html:h2) = '1'][preceding-sibling::html:h2[position()='1'][translate(.,'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
329                 'abcdefghijklmnopqrstuvwxyz') = 'abstract']]">
330                 <xsl:text>\subsection*{</xsl:text>
331                 <xsl:apply-templates select="node()"/>
332                 <xsl:text>}</xsl:text>
333                 <xsl:value-of select="$newline"/>
334                 <xsl:text>\label{</xsl:text>
335                 <xsl:value-of select="@id"/>
336                 <xsl:text>}</xsl:text>
337                 <xsl:value-of select="$newline"/>
338                 <xsl:value-of select="$newline"/>
339         </xsl:template>
340
341         <!-- code block -->
342         <xsl:template match="html:pre[child::html:code]">
343                 <xsl:text>\begin{adjustwidth}{2.5em}{2.5em}
344 \begin{verbatim}
345
346 </xsl:text>
347                 <xsl:value-of select="./html:code"/>
348                 <xsl:text>
349
350 \end{verbatim}
351 \end{adjustwidth}
352
353 </xsl:text>
354         </xsl:template>
355
356         <!-- no code, so treat as poetry -->
357         <xsl:template match="html:pre">
358                 <xsl:text>\begin{adjustwidth}{4em}{4em}
359 \setverbatimfont{\normalfont}
360 \begin{verbatim}
361
362 </xsl:text>
363                 <xsl:value-of select="."/>
364                 <xsl:text>
365 \end{verbatim}
366 \end{adjustwidth}
367
368 </xsl:text>
369         </xsl:template>
370
371         
372         <!-- epigraph (a blockquote immediately following a header 1-3) -->
373         <xsl:template match="html:blockquote[preceding-sibling::*[1][local-name() = 'h1' or local-name() = 'h2' or local-name() = 'h2' or local-name() = 'h3' ]]">
374                 <xsl:text>\epigraph{</xsl:text>
375                 <xsl:apply-templates select="node()"/>
376                 <xsl:text>}
377
378 </xsl:text>
379         </xsl:template>
380
381         <!-- epigraph author (a blockquote within blockquote) -->
382         <xsl:template match="html:blockquote[last()][parent::*[preceding-sibling::*[1][local-name() = 'h1' or local-name() = 'h2' or local-name() = 'h2' or local-name() = 'h3']]] ">
383                 <xsl:text>}{</xsl:text>
384                 <xsl:apply-templates select="node()"/>
385         </xsl:template>
386
387         <!-- Memoir handles glossaries differently -->
388
389         <xsl:template match="html:li" mode="glossary">
390                 <xsl:if test="parent::html:ol/parent::html:div/@class = 'footnotes'">
391                         <xsl:if test="concat('#',@id) = $footnoteId">
392                                 <xsl:apply-templates select="html:span[@class='glossary sort']" mode="glossary"/>
393                                 <xsl:apply-templates select="html:span[@class='glossary name']" mode="glossary"/>
394                                 <xsl:text>{</xsl:text>
395                                 <xsl:apply-templates select="html:p" mode="glossary"/>
396                                 <xsl:text>}</xsl:text>
397                         </xsl:if>
398                 </xsl:if>
399         </xsl:template>
400
401         <xsl:template match="html:span[@class='glossary name']" mode="glossary">
402                 <xsl:text>{</xsl:text>
403                 <xsl:apply-templates select="node()"/>
404                 <xsl:text>}</xsl:text>
405         </xsl:template>
406         
407         <xsl:template match="html:span[@class='glossary sort']" mode="glossary">
408                 <xsl:text>(</xsl:text>
409                 <xsl:apply-templates select="node()"/>
410                 <xsl:text>)</xsl:text>
411         </xsl:template>
412
413 </xsl:stylesheet>