good pdf, some general updates
[librarian.git] / librarian / xslt / wl2tex.xslt
1 <?xml version="1.0" encoding="utf-8"?>
2 <!--
3  
4    This file is part of Librarian, licensed under GNU Affero GPLv3 or later.
5    Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
6   
7 -->
8 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
9     xmlns:wl="http://wolnelektury.pl/functions"
10     xmlns:dc="http://purl.org/dc/elements/1.1/"
11     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
12
13 <xsl:output encoding="utf-8" indent="yes" version="2.0" />
14
15 <xsl:template match="utwor">
16     <TeXML xmlns="http://getfo.sourceforge.net/texml/ns1">
17         <TeXML escape="0">
18         \documentclass[a4paper, oneside, 11pt]{book}
19         \usepackage{wl}
20         </TeXML>
21
22         <xsl:choose>
23             <xsl:when test="@old-morefloats">
24                 <TeXML escape="0">
25                     \IfFileExists{morefloats.sty}{
26                         \usepackage{morefloats}
27                     }{}
28                 </TeXML>
29             </xsl:when>
30             <xsl:otherwise>
31                 <TeXML escape="0">
32                     \usepackage[maxfloats=64]{morefloats}
33                 </TeXML>
34             </xsl:otherwise>
35         </xsl:choose>
36
37         <xsl:apply-templates select="rdf:RDF" mode="titlepage" />
38         <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode='titlepage' />
39
40         <env name="document">
41             <cmd name="maketitle" />
42
43             <xsl:choose>
44                 <xsl:when test="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/nazwa_utworu">
45                     <xsl:apply-templates select="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/autor_utworu" mode="title" />
46                     <!-- title in master -->
47                 </xsl:when>
48                 <xsl:otherwise>
49                     <!-- look for author title in dc -->
50                     <xsl:apply-templates select="rdf:RDF" mode="firstdctitle" />
51                     <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode='firstdctitle' />
52                 </xsl:otherwise>
53             </xsl:choose>
54             <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" />
55             <xsl:apply-templates select="utwor" mode="part" />
56         </env>
57     </TeXML>
58 </xsl:template>
59
60 <xsl:template match="utwor" mode="part">
61     <!-- title for empty dc -->
62     <xsl:choose>
63         <xsl:when test="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/nazwa_utworu">
64             <!-- title in master -->
65         </xsl:when>
66         <xsl:otherwise>
67             <!-- look for title in dc -->
68             <xsl:apply-templates select="rdf:RDF" mode="dctitle" />
69             <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode='dctitle' />
70         </xsl:otherwise>
71     </xsl:choose>
72
73     <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" />
74     <xsl:apply-templates select="utwor" mode="part" />
75 </xsl:template>
76
77 <!-- =================== -->
78 <!-- = MAIN TITLE PAGE = -->
79 <!-- = (from DC)       = -->
80 <!-- =================== -->
81
82 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="titlepage">
83     <xsl:apply-templates select="rdf:RDF" mode="titlepage" />
84 </xsl:template>
85
86 <xsl:template match="rdf:RDF" mode="titlepage">
87     <cmd name='title'><parm>
88         <xsl:value-of select=".//dc:title/text()" />
89     </parm></cmd>
90     <cmd name='author'><parm>
91         <xsl:value-of select="wl:person_name(.//dc:creator/text())" />
92     </parm></cmd>
93 </xsl:template>
94
95
96 <!-- ============== -->
97 <!-- = BOOK TITLE = -->
98 <!-- = (from DC)  = -->
99 <!-- ============== -->
100
101 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="dctitle">
102     <xsl:apply-templates select="rdf:RDF" mode="dctitle" />
103 </xsl:template>
104
105 <xsl:template match="rdf:RDF" mode="dctitle">
106     <cmd name="section*"><parm>
107         <xsl:value-of select=".//dc:title/text()" />
108     </parm></cmd>
109 </xsl:template>
110
111
112 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="firstdctitle">
113     <xsl:apply-templates select="rdf:RDF" mode="firstdctitle" />
114 </xsl:template>
115
116 <xsl:template match="rdf:RDF" mode="firstdctitle">
117     <cmd name="subsection*"><parm>
118         <xsl:value-of select="wl:person_name(.//dc:creator/text())" />
119     </parm></cmd>
120     <cmd name="section*"><parm>
121         <xsl:value-of select=".//dc:title/text()" />
122     </parm></cmd>
123 </xsl:template>
124
125
126 <!-- ============================================================================== -->
127 <!-- = MASTER TAG                                                                 = -->
128 <!-- = (can contain block tags, paragraph tags, standalone tags and special tags) = -->
129 <!-- ============================================================================== -->
130
131 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny">
132     <xsl:apply-templates />
133 </xsl:template>
134
135
136
137 <!-- ==================================================================================== -->
138 <!-- = BLOCK TAGS                                                                       = -->
139 <!-- = (can contain other block tags, paragraph tags, standalone tags and special tags) = -->
140 <!-- ==================================================================================== -->
141 <xsl:template match="nota">
142     <cmd name="par">
143         <parm><xsl:apply-templates /></parm>
144     </cmd>
145 </xsl:template>
146
147 <xsl:template match="lista_osob">
148     <cmd name="par"><parm>
149         <cmd name="textbf">
150             <parm><xsl:value-of select="naglowek_listy" /></parm>
151         </cmd>
152         <env name="itemize">
153             <xsl:apply-templates select="lista_osoba" />
154         </env>
155     </parm></cmd>
156 </xsl:template>
157
158 <xsl:template match="dedykacja">
159     <env name="em">
160         <env name="flushright">
161             <xsl:apply-templates/>
162         </env>
163     </env>
164 </xsl:template>
165
166 <xsl:template match="kwestia">
167     <cmd name="par">
168         <parm><xsl:apply-templates select="strofa|akap|didaskalia" /></parm>
169     </cmd>
170 </xsl:template>
171
172 <xsl:template match="dlugi_cytat">
173     <env name="quotation">
174         <xsl:apply-templates />
175     </env>
176 </xsl:template>
177
178 <xsl:template match="poezja_cyt">
179     <env name="verse">
180         <xsl:apply-templates />
181     </env>
182 </xsl:template>
183
184 <xsl:template match="motto">
185     <env name="em">
186         <xsl:apply-templates mode="inline" />
187     </env>
188 </xsl:template>
189
190
191 <!-- ========================================== -->
192 <!-- = PARAGRAPH TAGS                         = -->
193 <!-- = (can contain inline and special tags)  = -->
194 <!-- ========================================== -->
195
196 <!-- only in root -->
197 <xsl:template match="autor_utworu" mode="title">
198     <cmd name="subsection*"><parm>
199         <xsl:apply-templates mode="inline" />
200     </parm></cmd>
201 </xsl:template>
202
203 <xsl:template match="nazwa_utworu">
204     <cmd name="section*"><parm>
205         <xsl:apply-templates mode="inline" />
206     </parm></cmd>
207 </xsl:template>
208
209 <!-- Section headers (included in index)-->
210 <xsl:template match="naglowek_akt|naglowek_czesc|srodtytul">
211     <cmd name="subsection*">
212         <parm><xsl:apply-templates mode="inline" /></parm>
213     </cmd>
214 </xsl:template>
215
216 <xsl:template match="naglowek_scena|naglowek_rozdzial">
217     <cmd name="subsubsection*">
218         <parm><xsl:apply-templates mode="inline" /></parm>
219     </cmd>
220 </xsl:template>
221
222 <xsl:template match="naglowek_osoba|naglowek_podrozdzial">
223     <cmd name="par">
224         <parm><xsl:apply-templates mode="inline" /></parm>
225     </cmd><cmd name="nopagebreak" />
226 </xsl:template>
227
228 <!-- Other paragraph tags -->
229 <xsl:template match="miejsce_czas">
230     <cmd name="par"><parm>
231         <cmd name="emph">
232             <parm><xsl:apply-templates mode="inline" /></parm>
233         </cmd>
234     </parm></cmd>
235 </xsl:template>
236
237 <xsl:template match="didaskalia">
238     <cmd name="par"><parm>
239         <cmd name="emph">
240             <parm><xsl:apply-templates mode="inline" /></parm>
241         </cmd>
242     </parm></cmd>
243 </xsl:template>
244
245 <xsl:template match="lista_osoba">
246     <cmd name="item"><parm>
247         <xsl:apply-templates mode="inline" />
248     </parm></cmd>
249 </xsl:template>
250
251 <xsl:template match="akap|akap_dialog|akap_cd">
252     <cmd name="par"><parm>
253         <xsl:apply-templates mode="inline" />
254     </parm></cmd>
255 </xsl:template>
256
257 <xsl:template match="strofa">
258 <cmd name="par"><parm><cmd name="noindent"><parm>
259     <xsl:choose>
260         <xsl:when test="count(br) > 0">
261             <xsl:call-template name="verse">
262                 <xsl:with-param name="verse-content" select="br[1]/preceding-sibling::text() | br[1]/preceding-sibling::node()" />
263                 <xsl:with-param name="verse-type" select="br[1]/preceding-sibling::*[name() = 'wers_wciety' or name() = 'wers_akap' or name() = 'wers_cd'][1]" />
264             </xsl:call-template>
265             <xsl:for-each select="br">
266                 <TeXML escape="0">\\{}</TeXML>
267                 <!-- Each BR tag "consumes" text after it -->
268                 <xsl:variable name="lnum" select="count(preceding-sibling::br)" />
269                 <xsl:call-template name="verse">
270                     <xsl:with-param name="verse-content"
271                         select="following-sibling::text()[count(preceding-sibling::br) = $lnum+1] | following-sibling::node()[count(preceding-sibling::br) = $lnum+1]" />
272                     <xsl:with-param name="verse-type" select="following-sibling::*[count(preceding-sibling::br) = $lnum+1 and (name() = 'wers_wciety' or name() = 'wers_akap' or name() = 'wers_cd')][1]" />
273                 </xsl:call-template>
274             </xsl:for-each>
275         </xsl:when>
276         <xsl:otherwise>
277             <xsl:call-template name="verse">
278                 <xsl:with-param name="verse-content" select="text() | node()" />
279                 <xsl:with-param name="verse-type" select="wers_wciety|wers_akap|wers_cd[1]" />
280             </xsl:call-template>
281         </xsl:otherwise>
282     </xsl:choose>
283     <cmd name="vspace"><parm>1em</parm></cmd>
284 </parm></cmd></parm></cmd>
285 </xsl:template>
286
287
288 <xsl:template name="verse">
289     <xsl:param name="verse-content" />
290     <xsl:param name="verse-type" />
291         <xsl:choose><xsl:when test="name($verse-type) = 'wers_akap'"><cmd name="hspace" ><parm>1em</parm></cmd></xsl:when>
292             <xsl:when test="name($verse-type) = 'wers_wciety'">
293                 <xsl:choose>
294                     <xsl:when test="string($verse-content/@typ)">
295                         <cmd name="hspace" ><parm><xsl:value-of select="$verse-content/@typ" />em</parm></cmd>
296                     </xsl:when>
297                     <xsl:otherwise>
298                         <cmd name="hspace" ><parm>1em</parm></cmd>
299                     </xsl:otherwise>
300                 </xsl:choose>
301             </xsl:when>
302             <xsl:when test="name($verse-type) = 'wers_cd'">
303                 <cmd name="hspace" ><parm>8em</parm></cmd>
304             </xsl:when>
305         </xsl:choose>
306         <xsl:apply-templates select="$verse-content" mode="inline" />
307 </xsl:template>
308
309 <xsl:template match="motto_podpis">
310     <env name="em">
311         <env name="flushright">
312             <xsl:apply-templates mode="inline"/>
313         </env>
314     </env>
315 </xsl:template>
316
317 <!-- ================================================ -->
318 <!-- = INLINE TAGS                                  = -->
319 <!-- = (contain other inline tags and special tags) = -->
320 <!-- ================================================ -->
321
322 <!-- Annotations -->
323 <xsl:template match="pa|pe|pr|pt" mode="inline">
324     <cmd name="footnote"><parm><xsl:apply-templates mode="inline" /></parm></cmd>
325 </xsl:template>
326
327 <!-- Other inline tags -->
328 <xsl:template match="mat" mode="inline">
329     <math><xsl:apply-templates mode="inline" /></math>
330 </xsl:template>
331
332 <xsl:template match="didask_tekst" mode="inline">
333     <cmd name="emph"><parm><xsl:apply-templates mode="inline" /></parm></cmd>
334 </xsl:template>
335
336 <xsl:template match="slowo_obce" mode="inline">
337     <cmd name="emph"><parm><xsl:apply-templates mode="inline" /></parm></cmd>
338 </xsl:template>
339
340 <xsl:template match="tytul_dziela" mode="inline">
341     <cmd name="emph"><parm>
342         <xsl:if test="@typ = '1'">„</xsl:if><xsl:apply-templates mode="inline" /><xsl:if test="@typ = '1'">”</xsl:if>
343     </parm></cmd>
344 </xsl:template>
345
346 <xsl:template match="wyroznienie" mode="inline">
347     <cmd name="emph"><parm><xsl:apply-templates mode="inline" /></parm></cmd>
348 </xsl:template>
349
350 <xsl:template match="osoba" mode="inline">
351     <cmd name="textsc"><parm><xsl:apply-templates mode="inline" /></parm></cmd>
352 </xsl:template>
353
354
355 <!-- ============================================== -->
356 <!-- = STANDALONE TAGS                            = -->
357 <!-- = (cannot contain any other tags)            = -->
358 <!-- ============================================== -->
359 <xsl:template match="sekcja_swiatlo">
360     <cmd name="hspace"><parm>30pt</parm></cmd>
361 </xsl:template>
362
363 <xsl:template match="sekcja_asterysk">
364     <!-- TODO: padding? -->
365     <cmd name="par"><parm>*</parm></cmd>
366 </xsl:template>
367
368 <xsl:template match="separator_linia">
369     <cmd name="hrule" />
370 </xsl:template>
371
372
373
374 <!-- ================ -->
375 <!-- = SPECIAL TAGS = -->
376 <!-- ================ -->
377 <!-- Themes -->
378
379
380 <xsl:template match="begin|end|motyw">
381     <xsl:apply-templates select='.' mode="inline" />
382 </xsl:template>
383
384 <xsl:template match="begin" mode="inline" />
385 <xsl:template match="end" mode="inline" />
386
387 <xsl:template match="motyw" mode="inline">
388     <cmd name="mbox" />
389     <cmd name="marginpar">
390         <parm>
391             <cmd name="vspace"><parm>-8pt</parm></cmd>
392             <xsl:if test="@moved">
393                 <cmd name="vspace"><parm>-<xsl:value-of select="@moved" /><cmd name="baselineskip" /></parm></cmd>
394             </xsl:if>
395             <cmd name="raggedright"><parm>
396                 <cmd name="hspace"><parm>0pt</parm></cmd>
397                 <cmd name="footnotesize"><parm>
398                     <cmd name="color"><parm>theme</parm><parm>
399                         <xsl:value-of select="." />
400                     </parm></cmd>
401                 </parm></cmd>
402             </parm></cmd>
403         </parm>
404     </cmd>
405 </xsl:template>
406
407
408 <!-- ============== -->
409 <!-- = ADDED TAGS = -->
410 <!-- ============== -->
411
412
413 <xsl:template match="dywiz" mode="inline">
414     <cmd name="dywiz" />
415 </xsl:template>
416
417 <xsl:template match="nbsp" mode="inline">
418     <spec cat="tilde" />
419 </xsl:template>
420
421 <!-- ================ -->
422 <!-- = IGNORED TAGS = -->
423 <!-- ================ -->
424 <xsl:template match="extra|uwaga" />
425 <xsl:template match="extra|uwaga" mode="inline" />
426
427
428 <!-- ======== -->
429 <!-- = TEXT = -->
430 <!-- ======== -->
431 <xsl:template match="text()" />
432 <xsl:template match="text()" mode="inline">
433     <xsl:if test="preceding-sibling::node() and wl:starts_white(.)">
434       <xsl:text> </xsl:text>
435     </xsl:if>
436
437     <xsl:value-of select="wl:substitute_entities(wl:strip(.))" />
438
439     <xsl:if test="following-sibling::node() and wl:ends_white(.)">
440       <xsl:text> </xsl:text>
441     </xsl:if>
442 </xsl:template>
443
444
445 </xsl:stylesheet>