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