Preliminary math and tables support.
[librarian.git] / librarian / pdf / 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[<xsl:value-of select="@customizations"/>]{wl}
19
20         <!-- flags and values set on root -->
21
22         \newif\ifflaglessadvertising
23         <xsl:for-each select="@*[starts-with(name(), 'flag-')]">
24             <cmd>
25                 <xsl:attribute name="name"><xsl:value-of select="wl:texcommand(name())" />true</xsl:attribute>
26             </cmd>
27         </xsl:for-each>
28
29         <xsl:for-each select="@*[starts-with(name(), 'data-')]">
30             <TeXML escape="0">
31                 \def\<xsl:value-of select="wl:texcommand(name())" />{<TeXML escape="1"><xsl:value-of select="."/></TeXML>}
32             </TeXML>
33         </xsl:for-each>
34         </TeXML>
35
36         <xsl:choose>
37             <xsl:when test="@morefloats = 'new'">
38                 <TeXML escape="0">
39                     \usepackage[maxfloats=64]{morefloats}
40                 </TeXML>
41             </xsl:when>
42             <xsl:when test="@morefloats = 'old'">
43                 <TeXML escape="0">
44                     \usepackage{morefloats}
45                 </TeXML>
46             </xsl:when>
47             <xsl:when test="@morefloats = 'none'" />
48             <xsl:otherwise>
49                 <TeXML escape="0">
50                     \IfFileExists{morefloats.sty}{
51                         \usepackage{morefloats}
52                     }{}
53                 </TeXML>
54             </xsl:otherwise>
55         </xsl:choose>
56
57         <xsl:apply-templates select="rdf:RDF" mode="titlepage" />
58         <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode='titlepage' />
59
60         <env name="document">
61             <xsl:if test="@data-cover-width">
62                 <cmd name="makecover">
63                     <parm>210mm</parm>
64                     <parm><xsl:value-of select="210 * @data-cover-height div @data-cover-width" />mm</parm>
65                 </cmd>
66             </xsl:if>
67                 <xsl:if test="@thanks">
68                     <TeXML escape="0">
69                         \def\thanknote{<TeXML escape="1"><xsl:value-of select="@thanks" /></TeXML>}
70                     </TeXML>
71                 </xsl:if>
72             <cmd name="maketitle" />
73
74             <cmd name="tytul"><parm>
75               <xsl:choose>
76                 <xsl:when test="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/nazwa_utworu">
77                     <!-- title in master -->
78                     <xsl:apply-templates select="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/autor_utworu" mode="title" />
79                     <xsl:apply-templates select="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/dzielo_nadrzedne" mode="title" />
80                     <xsl:apply-templates select="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/nazwa_utworu" mode="title" />
81                     <xsl:apply-templates select="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/podtytul" mode="title" />
82                     <!-- dc in master or not -->
83                     <cmd name="translatorsline" />
84                 </xsl:when>
85                 <xsl:otherwise>
86                     <!-- look for author title in dc -->
87                     <xsl:apply-templates select="rdf:RDF" mode="firstdctitle" />
88                     <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode='firstdctitle' />
89                 </xsl:otherwise>
90               </xsl:choose>
91             </parm></cmd>
92             <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" />
93             <xsl:apply-templates select="utwor" mode="part" />
94
95             <TeXML escape="0">
96                 <xsl:if test="@data-cover-by">
97                 \def\coverby{Okładka na podstawie:
98                     <xsl:choose>
99                     <xsl:when test="@data-cover-source">
100                         \href{\datacoversource}{\datacoverby}
101                     </xsl:when>
102                     <xsl:otherwise>
103                         \datacoverby{}
104                     </xsl:otherwise>
105                     </xsl:choose>
106                 }
107                 </xsl:if>
108                 \def\editors{<TeXML escape="1"><xsl:call-template name="editors" /></TeXML>}
109                 <xsl:if test="@funders">
110                     \def\funders{Publikację ufundowali i ufundowały:
111                         <TeXML escape="1"><xsl:value-of select="@funders" /></TeXML>.}
112                 </xsl:if>
113
114                 <xsl:if test="@sponsor-note|data-sponsor">
115                     \def\sponsors{
116                         \scriptsize
117                         <xsl:choose>
118                             <xsl:when test="@sponsor-note">
119                                 <TeXML escape="1"><xsl:value-of select="@sponsor-note" /></TeXML>
120                             </xsl:when>
121                             <xsl:otherwise>
122                                 Sfinansowano ze~środków:
123                             </xsl:otherwise>
124                         </xsl:choose>
125
126                         \vspace{1em}
127
128                         <xsl:apply-templates select="data-sponsor" mode="sponsor" />
129                     }
130                 </xsl:if>
131             </TeXML>
132
133             <cmd name="editorialsection" />
134
135         </env>
136     </TeXML>
137 </xsl:template>
138
139 <xsl:template match="utwor" mode="part">
140     <cmd name="tytul"><parm>
141       <xsl:choose>
142         <xsl:when test="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/nazwa_utworu">
143             <!-- title in master -->
144             <xsl:apply-templates select="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/nazwa_utworu" mode="title" />
145             <xsl:apply-templates select="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/podtytul" mode="title" />
146         </xsl:when>
147         <xsl:otherwise>
148             <!-- look for title in dc -->
149             <xsl:apply-templates select="rdf:RDF" mode="dctitle" />
150             <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode='dctitle' />
151         </xsl:otherwise>
152       </xsl:choose>
153     </parm></cmd>
154
155     <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" />
156     <xsl:apply-templates select="utwor" mode="part" />
157 </xsl:template>
158
159 <!-- =================== -->
160 <!-- = MAIN TITLE PAGE = -->
161 <!-- = (from DC)       = -->
162 <!-- =================== -->
163
164 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="titlepage">
165     <xsl:apply-templates select="rdf:RDF" mode="titlepage" />
166 </xsl:template>
167
168 <xsl:template match="rdf:RDF" mode="titlepage">
169     <TeXML escape="0">
170         \def\authors{<TeXML escape="1"><xsl:call-template name="authors" /></TeXML>}
171         \author{\authors}
172         \title{<TeXML escape="1"><xsl:apply-templates select=".//dc:title" mode="inline" /></TeXML>}
173         \def\translatorsline{<TeXML escape="1"><xsl:call-template name="translators" /></TeXML>}
174
175         \def\bookurl{<TeXML escape="1"><xsl:value-of select=".//dc:identifier.url" /></TeXML>}
176
177         \def\rightsinfo{Ten utwór nie jest objęty majątkowym prawem autorskim i~znajduje się w~domenie
178             publicznej, co oznacza że możesz go swobodnie wykorzystywać, publikować
179             i~rozpowszechniać. Jeśli utwór opatrzony jest dodatkowymi materiałami
180             (przypisy, motywy literackie etc.), które podlegają prawu autorskiemu, to
181             te dodatkowe materiały udostępnione są na licencji
182             \href{http://creativecommons.org/licenses/by-sa/3.0/}{Creative Commons
183             Uznanie Autorstwa – Na Tych Samych Warunkach 3.0 PL}.}
184         <xsl:if test=".//dc:rights.license">
185             \def\rightsinfo{Ten utwór jest udostepniony na licencji
186             \href{<xsl:value-of select=".//dc:rights.license" />}{<TeXML escape="1"><xsl:apply-templates select=".//dc:rights" mode="inline" /></TeXML>}.}
187         </xsl:if>
188
189         \def\sourceinfo{
190             <xsl:if test=".//dc:source">
191                 Tekst opracowany na podstawie: <TeXML escape="1"><xsl:apply-templates select=".//dc:source" mode="inline" /></TeXML>
192                 \vspace{.6em}
193             </xsl:if>}
194         \def\description{<TeXML escape="1"><xsl:apply-templates select=".//dc:description" mode="inline" /></TeXML>}
195     </TeXML>
196 </xsl:template>
197
198
199 <!-- ============== -->
200 <!-- = BOOK TITLE = -->
201 <!-- = (from DC)  = -->
202 <!-- ============== -->
203
204 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="dctitle">
205     <xsl:apply-templates select="rdf:RDF" mode="dctitle" />
206 </xsl:template>
207
208 <xsl:template match="rdf:RDF" mode="dctitle">
209     <cmd name="nazwapodutworu"><parm>
210         <xsl:apply-templates select=".//dc:title/node()" mode="inline" />
211     </parm></cmd>
212 </xsl:template>
213
214 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="firstdctitle">
215     <xsl:apply-templates select="rdf:RDF" mode="firstdctitle" />
216 </xsl:template>
217
218 <xsl:template match="rdf:RDF" mode="firstdctitle">
219     <cmd name="autorutworu"><parm><cmd name="authors"/></parm></cmd>
220     <cmd name="nazwautworu"><parm>
221         <xsl:apply-templates select=".//dc:title/node()" mode="inline" />
222     </parm></cmd>
223     <cmd name="translatorsline" />
224 </xsl:template>
225
226
227 <!-- ============================================================================== -->
228 <!-- = MASTER TAG                                                                 = -->
229 <!-- = (can contain block tags, paragraph tags, standalone tags and special tags) = -->
230 <!-- ============================================================================== -->
231 <!-- ==================================================================================== -->
232 <!-- = BLOCK TAGS                                                                       = -->
233 <!-- = (can contain other block tags, paragraph tags, standalone tags and special tags) = -->
234 <!-- ==================================================================================== -->
235
236 <xsl:template
237     match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny|nota|dedykacja|dlugi_cytat|poezja_cyt|motto">
238     <cmd>
239         <xsl:attribute name="name">
240             <xsl:value-of select="wl:texcommand(name())" />
241         </xsl:attribute>
242         <parm><xsl:apply-templates/></parm>
243     </cmd>
244 </xsl:template>
245
246 <xsl:template match="lista_osob">
247     <cmd name="listaosob">
248         <parm><xsl:apply-templates select="naglowek_listy" /></parm>
249         <parm><xsl:apply-templates select="lista_osoba" /></parm>
250     </cmd>
251 </xsl:template>
252
253 <xsl:template match="kwestia">
254     <cmd name="kwestia">
255         <parm><xsl:apply-templates select="strofa|akap|didaskalia" /></parm>
256     </cmd>
257 </xsl:template>
258
259
260 <!-- ========================================== -->
261 <!-- = PARAGRAPH TAGS                         = -->
262 <!-- = (can contain inline and special tags)  = -->
263 <!-- ========================================== -->
264
265 <!-- only in root -->
266 <xsl:template
267     match="autor_utworu|dzielo_nadrzedne|nazwa_utworu|podtytul"
268     mode="title">
269     <cmd>
270         <xsl:attribute name="name">
271             <xsl:value-of select="wl:texcommand(name())" />
272         </xsl:attribute>
273         <parm><xsl:apply-templates mode="inline"/></parm>
274     </cmd>
275 </xsl:template>
276
277
278 <xsl:template
279     match="naglowek_akt|naglowek_czesc|srodtytul|naglowek_osoba|naglowek_podrozdzial|naglowek_scena|naglowek_rozdzial|miejsce_czas|didaskalia|lista_osoba|akap|akap_dialog|akap_cd|motto_podpis|naglowek_listy">
280     <cmd>
281         <xsl:attribute name="name">
282             <xsl:value-of select="wl:texcommand(name())" />
283         </xsl:attribute>
284         <parm><xsl:apply-templates mode="inline"/></parm>
285     </cmd>
286 </xsl:template>
287
288 <xsl:template match="strofa">
289   <cmd name="strofa"><parm>
290     <xsl:choose>
291         <xsl:when test="count(br) > 0">
292             <xsl:call-template name="verse">
293                 <xsl:with-param name="verse-content" select="br[1]/preceding-sibling::text() | br[1]/preceding-sibling::node()" />
294                 <xsl:with-param name="verse-type" select="br[1]/preceding-sibling::*[name() = 'wers_wciety' or name() = 'wers_akap' or name() = 'wers_cd'][1]" />
295             </xsl:call-template>
296             <xsl:for-each select="br">
297                 <TeXML escape="0">\\{}</TeXML>
298                 <!-- Each BR tag "consumes" text after it -->
299                 <xsl:variable name="lnum" select="count(preceding-sibling::br)" />
300                 <xsl:call-template name="verse">
301                     <xsl:with-param name="verse-content"
302                         select="following-sibling::text()[count(preceding-sibling::br) = $lnum+1] | following-sibling::node()[count(preceding-sibling::br) = $lnum+1]" />
303                     <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]" />
304                 </xsl:call-template>
305             </xsl:for-each>
306         </xsl:when>
307         <xsl:otherwise>
308             <xsl:call-template name="verse">
309                 <xsl:with-param name="verse-content" select="text() | node()" />
310                 <xsl:with-param name="verse-type" select="wers_wciety|wers_akap|wers_cd[1]" />
311             </xsl:call-template>
312         </xsl:otherwise>
313     </xsl:choose>
314   </parm></cmd>
315 </xsl:template>
316
317
318 <xsl:template name="verse">
319     <xsl:param name="verse-content" />
320     <xsl:param name="verse-type" />
321
322     <cmd>
323         <xsl:attribute name="name">
324             <xsl:choose>
325                 <xsl:when test="$verse-type != ''">
326                     <xsl:value-of select='wl:texcommand(name($verse-type))' />
327                 </xsl:when>
328                 <xsl:otherwise>wers</xsl:otherwise>
329             </xsl:choose>
330         </xsl:attribute>
331         <xsl:if test="string($verse-content/@typ)">
332             <opt><xsl:value-of select="$verse-content/@typ" />em</opt>
333         </xsl:if>
334         <parm><xsl:apply-templates select="$verse-content" mode="inline"/></parm>
335     </cmd>
336 </xsl:template>
337
338 <!-- ================================================ -->
339 <!-- = INLINE TAGS                                  = -->
340 <!-- = (contain other inline tags and special tags) = -->
341 <!-- ================================================ -->
342
343 <xsl:template mode="inline"
344     match="pa|pe|pr|pt|mat|didask_tekst|slowo_obce|wyroznienie|osoba">
345     <cmd>
346         <xsl:attribute name="name">
347             <xsl:value-of select="wl:texcommand(name())" />
348         </xsl:attribute>
349         <parm><xsl:apply-templates mode="inline"/></parm>
350     </cmd>
351 </xsl:template>
352
353
354
355 <xsl:template match="tytul_dziela" mode="inline">
356     <cmd name="tytuldziela"><parm>
357         <xsl:if test="@typ = '1'">„</xsl:if><xsl:apply-templates mode="inline" /><xsl:if test="@typ = '1'">”</xsl:if>
358     </parm></cmd>
359 </xsl:template>
360
361
362
363 <!-- ============================================== -->
364 <!-- = STANDALONE TAGS                            = -->
365 <!-- = (cannot contain any other tags)            = -->
366 <!-- ============================================== -->
367 <xsl:template
368     match="sekcja_swiatlo|sekcja_asterysk|separator_linia">
369     <cmd>
370         <xsl:attribute name="name">
371             <xsl:value-of select="wl:texcommand(name())" />
372         </xsl:attribute>
373     </cmd>
374 </xsl:template>
375
376
377 <!-- ================ -->
378 <!-- = SPECIAL TAGS = -->
379 <!-- ================ -->
380 <!-- Themes -->
381
382
383 <xsl:template match="begin|end|motyw">
384     <xsl:apply-templates select='.' mode="inline" />
385 </xsl:template>
386
387 <xsl:template match="begin" mode="inline" />
388 <xsl:template match="end" mode="inline" />
389
390 <xsl:template match="motyw" mode="inline">
391     <cmd name="motyw">
392         <xsl:if test="@moved">
393             <opt name="moved"><xsl:value-of select="@moved" /></opt>
394         </xsl:if>
395         <parm><xsl:apply-templates mode="inline" /></parm>
396     </cmd>
397 </xsl:template>
398
399 <xsl:template name="authors">
400     <xsl:for-each select=".//dc:creator_parsed">
401         <xsl:if test="position() != 1">, </xsl:if>
402         <xsl:apply-templates mode="inline" />
403     </xsl:for-each>
404 </xsl:template>
405
406 <xsl:template name="editors">
407     <xsl:if test="@editors">
408         <xsl:text>Opracowanie redakcyjne i przypisy: </xsl:text>
409         <xsl:value-of select="@editors" />
410         <xsl:text>.</xsl:text>
411     </xsl:if>
412 </xsl:template>
413
414 <xsl:template name="translators">
415     <xsl:if test=".//dc:contributor.translator_parsed">
416         <cmd name='translator'><parm>
417             <xsl:for-each select=".//dc:contributor.translator_parsed">
418                 <xsl:if test="position() != 1">, </xsl:if>
419                 <xsl:apply-templates mode="inline" />
420             </xsl:for-each>
421         </parm></cmd>
422     </xsl:if>
423 </xsl:template>
424
425 <xsl:template match="data-sponsor" mode="sponsor">
426     <cmd name="par"><parm>
427     <xsl:choose>
428         <xsl:when test="@src">
429             \includegraphics[height=0.25\textwidth,width=0.25\textwidth,keepaspectratio]{<xsl:value-of select="@src" />}
430         </xsl:when>
431         <xsl:otherwise>
432             <xsl:value-of select="@name" />
433         </xsl:otherwise>
434     </xsl:choose>
435     </parm></cmd>
436 </xsl:template>
437
438 <xsl:template match="mat" mode="inline">
439     <TeXML escape="0">
440         <xsl:text>$</xsl:text>
441         <xsl:value-of select="wl:mathml_latex(.)" />
442         <xsl:text>$</xsl:text>
443     </TeXML>
444 </xsl:template>
445
446 <xsl:template match="mat">
447     <TeXML escape="0">
448         <xsl:text>$$</xsl:text>
449         <xsl:value-of select="wl:mathml_latex(.)" />
450         <xsl:text>$$</xsl:text>
451     </TeXML>
452 </xsl:template>
453
454 <xsl:template match="tabela|tabelka">
455     <cmd name="par" />
456     <cmd name="vspace"><parm>1em</parm></cmd>
457     <group><cmd name="raggedright" />
458     <env name="longtabu"> to <TeXML escape="0">\textwidth </TeXML>
459       <!--parm><cmd name="textwidth"/></parm-->
460       <parm><TeXML escape="0"><xsl:value-of select="@_format" /></TeXML></parm>
461         <xsl:choose>
462         <xsl:when test="@ramka='1' or @ramki='1'">
463           <cmd name="hline" />
464           <xsl:apply-templates mode="wiersze-ramki"/>
465         </xsl:when>
466         <xsl:otherwise>
467           <xsl:apply-templates/>
468         </xsl:otherwise>
469         </xsl:choose>
470     </env>
471     </group>
472     <cmd name="vspace"><parm>1em</parm></cmd>
473 </xsl:template>
474 <xsl:template match="wiersz" mode="wiersze-ramki">
475     <xsl:apply-templates />
476     <spec cat="esc"/><spec cat="esc"/>
477     <cmd name="hline" gr="0" />
478 </xsl:template>
479 <xsl:template match="wiersz">
480     <xsl:apply-templates />
481     <spec cat="esc"/><spec cat="esc"/>
482 </xsl:template>
483 <xsl:template match="kol">
484     <xsl:apply-templates mode="inline"/>
485     <xsl:if test="position() &lt; last()">
486     <spec cat="align"/>
487     </xsl:if>
488 </xsl:template>
489
490
491 <!-- ============== -->
492 <!-- = ADDED TAGS = -->
493 <!-- ============== -->
494
495
496 <xsl:template match="dywiz" mode="inline">
497     <cmd name="dywiz" />
498 </xsl:template>
499
500 <xsl:template match="nbsp" mode="inline">
501     <spec cat="tilde" />
502 </xsl:template>
503
504 <xsl:template match="alien" mode="inline">
505     <group>
506         <cmd name="alien" />
507         <xsl:apply-templates mode="inline" />
508     </group>
509 </xsl:template>
510
511 <!-- ================ -->
512 <!-- = IGNORED TAGS = -->
513 <!-- ================ -->
514 <xsl:template match="extra|uwaga" />
515 <xsl:template match="extra|uwaga" mode="inline" />
516
517 <xsl:template match="nota_red" />
518
519 <!-- ======== -->
520 <!-- = TEXT = -->
521 <!-- ======== -->
522 <xsl:template match="text()" />
523 <xsl:template match="text()" mode="inline">
524     <xsl:if test="preceding-sibling::node() and wl:starts_white(.)">
525       <xsl:text> </xsl:text>
526     </xsl:if>
527
528     <xsl:value-of select="wl:substitute_entities(wl:strip(.))" />
529
530     <xsl:if test="following-sibling::node() and wl:ends_white(.)">
531       <xsl:text> </xsl:text>
532     </xsl:if>
533 </xsl:template>
534
535
536 </xsl:stylesheet>