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