almos done
[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             <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:template match="ilustr">
366     <cmd>
367         <xsl:attribute name="name">
368             <xsl:value-of select="wl:texcommand(name())" />
369         </xsl:attribute>
370                 <parm><xsl:value-of select="@src" /></parm>
371         <parm><xsl:apply-templates mode="inline" /></parm>
372     </cmd>
373 </xsl:template>
374
375 <xsl:template match="@*|node()" mode="identity">
376   <xsl:copy>
377     <xsl:apply-templates select="@*|node()" mode="identity"/>
378   </xsl:copy>
379 </xsl:template>
380
381
382
383 <xsl:template match="dmath">
384   <dmath>
385     <xsl:apply-templates mode="identity"/>
386   </dmath>
387 </xsl:template>
388
389 <xsl:template match="math" mode="inline">
390   <math>
391     <xsl:apply-templates mode="identity"/>
392   </math>
393 </xsl:template>
394
395 <xsl:template match="tablewrap">
396   <cmd name="begin"><parm>table</parm><opt>h!</opt></cmd>
397   <xsl:apply-templates select="table"/>
398
399   <cmd name="caption*"><parm>
400     <xsl:apply-templates select="akap"/>
401   </parm></cmd>
402   <cmd name="end"><parm>table</parm></cmd>
403 </xsl:template>
404
405 <xsl:template match="table">
406   <xsl:if test="@caption">
407
408       <cmd name="caption*"><parm>
409
410         <cmd name="Large"/><xsl:value-of select="@caption"/>
411       </parm></cmd>
412     </xsl:if>
413     <env name="tabularx">
414       <parm><cmd name="textwidth"/></parm>
415         <parm><xsl:value-of select="@spec"/></parm>
416         <xsl:apply-templates />
417     </env>
418 </xsl:template>
419
420 <xsl:template match="r">
421     <xsl:apply-templates />
422     <spec cat="esc"/><spec cat="esc"/>
423 </xsl:template>
424 <xsl:template match="c">
425     <cmd name="footnotesize"/>
426     <xsl:apply-templates mode="inline"/>
427     <xsl:if test="position() &lt; last()-1">
428     <spec cat="align"/>
429     </xsl:if>
430 </xsl:template>
431 <xsl:template match="hr">
432   <cmd name="hline"/>
433 </xsl:template>
434
435 <!-- ================ -->
436 <!-- = SPECIAL TAGS = -->
437 <!-- ================ -->
438 <!-- Themes -->
439
440
441 <xsl:template match="begin|end|motyw">
442     <xsl:apply-templates select='.' mode="inline" />
443 </xsl:template>
444
445 <xsl:template match="begin" mode="inline" />
446 <xsl:template match="end" mode="inline" />
447
448 <xsl:template match="motyw" mode="inline">
449     <cmd name="motyw">
450         <xsl:if test="@moved">
451             <opt name="moved"><xsl:value-of select="@moved" /></opt>
452         </xsl:if>
453         <parm><xsl:apply-templates mode="inline" /></parm>
454     </cmd>
455 </xsl:template>
456
457 <xsl:template name="authors">
458     <xsl:for-each select=".//dc:creator_parsed">
459         <xsl:if test="position() != 1">, </xsl:if>
460         <xsl:apply-templates mode="inline" />
461     </xsl:for-each>
462 </xsl:template>
463
464 <xsl:template name="editors">
465     <xsl:if test=".//dc:contributor.editor_parsed|.//dc:contributor.technical_editor_parsed">
466         <xsl:text>Redakcja techniczna: </xsl:text>
467         <xsl:for-each select=".//dc:contributor.editor_parsed|.//dc:contributor.technical_editor_parsed[not(.//dc:contributor.editor_parsed/text()=text())]">
468             <!--xsl:sort select="@sortkey" /-->
469             <xsl:if test="position() != 1">, </xsl:if>
470             <xsl:apply-templates mode="inline" />
471         </xsl:for-each>.
472     </xsl:if>
473 </xsl:template>
474
475 <xsl:template name="translators">
476     <xsl:if test=".//dc:contributor.translator_parsed">
477         <cmd name='translator'><parm>
478             <xsl:for-each select=".//dc:contributor.translator_parsed">
479                 <xsl:if test="position() != 1">, </xsl:if>
480                 <xsl:apply-templates mode="inline" />
481             </xsl:for-each>
482         </parm></cmd>
483     </xsl:if>
484 </xsl:template>
485
486
487 <!-- ============== -->
488 <!-- = ADDED TAGS = -->
489 <!-- ============== -->
490
491
492 <xsl:template match="dywiz" mode="inline">
493     <cmd name="dywiz" />
494 </xsl:template>
495
496 <xsl:template match="nbsp" mode="inline">
497     <spec cat="tilde" />
498 </xsl:template>
499
500 <xsl:template match="alien" mode="inline">
501     <group>
502         <cmd name="alien" />
503         <xsl:apply-templates mode="inline" />
504     </group>
505 </xsl:template>
506
507 <!-- ================ -->
508 <!-- = IGNORED TAGS = -->
509 <!-- ================ -->
510 <xsl:template match="extra|uwaga" />
511 <xsl:template match="extra|uwaga" mode="inline" />
512
513 <xsl:template match="nota_red" />
514
515 <!-- ======== -->
516 <!-- = TEXT = -->
517 <!-- ======== -->
518 <xsl:template match="text()" />
519 <xsl:template match="text()" mode="inline">
520     <xsl:if test="preceding-sibling::node() and wl:starts_white(.)">
521       <xsl:text> </xsl:text>
522     </xsl:if>
523
524     <xsl:value-of select="wl:substitute_entities(wl:strip(.))" />
525
526     <xsl:if test="following-sibling::node() and wl:ends_white(.)">
527       <xsl:text> </xsl:text>
528     </xsl:if>
529 </xsl:template>
530
531
532 </xsl:stylesheet>