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