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