merge conflicting on stripping spaces
[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|nota_red">
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:strip-space elements="nota|akap|wyroznienie"/>
336
337 <xsl:template mode="inline"
338     match="pa|pe|pr|pt|mat|didask_tekst|slowo_obce|wyroznienie|osoba|punkt|www|nota_red">
339     <cmd>
340         <xsl:attribute name="name">
341             <xsl:value-of select="wl:texcommand(name())" />
342         </xsl:attribute>
343         <parm><xsl:apply-templates mode="inline"/></parm>
344     </cmd>
345 </xsl:template>
346
347 <xsl:template mode="inline"
348     match="wyimek">
349     <env>
350         <xsl:attribute name="name">
351             <xsl:value-of select="wl:texcommand(name())" />
352         </xsl:attribute>
353         <xsl:apply-templates mode="inline"/>
354     </env>
355 </xsl:template>
356 <xsl:template
357     match="wyimek_extra">
358     <env>
359         <xsl:attribute name="name">
360             <xsl:value-of select="wl:texcommand(name())" />
361         </xsl:attribute>
362         <xsl:apply-templates mode="inline"/>
363     </env>
364 </xsl:template>
365
366
367
368
369 <xsl:template match="tytul_dziela" mode="inline">
370     <cmd name="tytuldziela"><parm>
371         <xsl:if test="@typ = '1'">„</xsl:if><xsl:apply-templates mode="inline" /><xsl:if test="@typ = '1'">”</xsl:if>
372     </parm></cmd>
373 </xsl:template>
374
375
376
377 <!-- ============================================== -->
378 <!-- = STANDALONE TAGS                            = -->
379 <!-- = (cannot contain any other tags)            = -->
380 <!-- ============================================== -->
381 <xsl:template
382     match="sekcja_swiatlo|sekcja_asterysk|separator_linia">
383     <cmd>
384         <xsl:attribute name="name">
385             <xsl:value-of select="wl:texcommand(name())" />
386         </xsl:attribute>
387     </cmd>
388 </xsl:template>
389
390 <xsl:strip-space elements="ilustr"/>
391 <xsl:template match="ilustr">
392     <cmd>
393         <xsl:attribute name="name">
394             <xsl:value-of select="wl:texcommand(name())" />
395         </xsl:attribute>
396         <xsl:if test="@opt">
397           <opt><TeXML ecape="0"><xsl:value-of select="normalize-space(@opt)" /></TeXML></opt>
398         </xsl:if>
399         <parm><TeXML escape="0"><xsl:value-of select="normalize-space(@src)" /></TeXML></parm>
400         <parm><xsl:apply-templates mode="inline" /></parm>
401     </cmd>
402 </xsl:template>
403
404 <xsl:template match="@*|node()" mode="identity">
405   <xsl:copy>
406     <xsl:apply-templates select="@*|node()" mode="identity"/>
407   </xsl:copy>
408 </xsl:template>
409
410
411
412 <xsl:template match="dmath">
413   <dmath>
414     <xsl:apply-templates mode="identity"/>
415   </dmath>
416 </xsl:template>
417
418 <xsl:template match="math" mode="inline">
419   <math>
420     <xsl:apply-templates mode="identity"/>
421   </math>
422 </xsl:template>
423
424 <xsl:template match="latex">
425   <TeXML escape="0">
426     <xsl:for-each select="text()">
427       <xsl:value-of select="normalize-space(.)"/>
428     </xsl:for-each>
429   </TeXML>
430 </xsl:template>
431
432 <xsl:template match="latex" mode="inline">
433   <TeXML escape="0">
434     <xsl:for-each select="text()">
435       <xsl:value-of select="normalize-space(.)"/>
436     </xsl:for-each>
437   </TeXML>
438 </xsl:template>
439
440
441 <xsl:template match="tablewrap">
442   <cmd name="begin"><parm>table</parm><opt>h!</opt></cmd>
443   <xsl:apply-templates select="table"/>
444
445   <cmd name="caption*"><parm>
446     <xsl:apply-templates select="akap"/>
447   </parm></cmd>
448   <cmd name="end"><parm>table</parm></cmd>
449 </xsl:template>
450
451 <xsl:template match="table">
452   <xsl:if test="@caption">
453
454       <cmd name="caption*"><parm>
455
456         <cmd name="Large"/><xsl:value-of select="@caption"/>
457       </parm></cmd>
458     </xsl:if>
459     <env name="tabularx">
460       <parm><cmd name="textwidth"/></parm>
461         <parm><xsl:value-of select="@spec"/></parm>
462         <xsl:apply-templates />
463     </env>
464 </xsl:template>
465
466 <xsl:template match="r">
467     <xsl:apply-templates />
468     <spec cat="esc"/><spec cat="esc"/>
469 </xsl:template>
470 <xsl:template match="c">
471     <cmd name="footnotesize"/>
472     <xsl:apply-templates mode="inline"/>
473     <xsl:if test="position() &lt; last()-1">
474     <spec cat="align"/>
475     </xsl:if>
476 </xsl:template>
477 <xsl:template match="hr">
478   <cmd name="hline"/>
479 </xsl:template>
480
481 <!-- ================ -->
482 <!-- = SPECIAL TAGS = -->
483 <!-- ================ -->
484 <!-- Themes -->
485
486
487 <xsl:template match="begin|end|motyw">
488     <xsl:apply-templates select='.' mode="inline" />
489 </xsl:template>
490
491 <xsl:template match="begin" mode="inline" />
492 <xsl:template match="end" mode="inline" />
493
494 <xsl:template match="motyw" mode="inline">
495     <cmd name="motyw">
496         <xsl:if test="@moved">
497             <opt name="moved"><xsl:value-of select="@moved" /></opt>
498         </xsl:if>
499         <parm><xsl:apply-templates mode="inline" /></parm>
500     </cmd>
501 </xsl:template>
502
503 <xsl:template name="authors">
504     <xsl:for-each select=".//dc:creator_parsed">
505         <xsl:if test="position() != 1">, </xsl:if>
506         <xsl:apply-templates mode="inline" />
507     </xsl:for-each>
508 </xsl:template>
509
510 <xsl:template name="editors">
511     <xsl:if test=".//dc:contributor.editor_parsed|.//dc:contributor.technical_editor_parsed">
512         <xsl:text>Redakcja techniczna: </xsl:text>
513         <xsl:for-each select=".//dc:contributor.editor_parsed|.//dc:contributor.technical_editor_parsed[not(.//dc:contributor.editor_parsed/text()=text())]">
514             <!--xsl:sort select="@sortkey" /-->
515             <xsl:if test="position() != 1">, </xsl:if>
516             <xsl:apply-templates mode="inline" />
517         </xsl:for-each>.
518     </xsl:if>
519 </xsl:template>
520
521 <xsl:template name="translators">
522     <xsl:if test=".//dc:contributor.translator_parsed">
523         <cmd name='translator'><parm>
524             <xsl:for-each select=".//dc:contributor.translator_parsed">
525                 <xsl:if test="position() != 1">, </xsl:if>
526                 <xsl:apply-templates mode="inline" />
527             </xsl:for-each>
528         </parm></cmd>
529     </xsl:if>
530 </xsl:template>
531
532
533 <!-- ============== -->
534 <!-- = ADDED TAGS = -->
535 <!-- ============== -->
536
537
538 <xsl:template match="dywiz" mode="inline">
539     <cmd name="dywiz" />
540 </xsl:template>
541
542 <xsl:template match="nbsp" mode="inline">
543     <spec cat="tilde" />
544 </xsl:template>
545
546 <xsl:template match="alien" mode="inline">
547     <group>
548         <cmd name="alien" />
549         <xsl:apply-templates mode="inline" />
550     </group>
551 </xsl:template>
552
553 <!-- ================ -->
554 <!-- = IGNORED TAGS = -->
555 <!-- ================ -->
556 <xsl:template match="extra|uwaga" />
557 <xsl:template match="extra|uwaga" mode="inline" />
558
559 <!-- make it a command: <xsl:template match="nota_red" />-->
560
561
562 <!-- ======== -->
563 <!-- = TEXT = -->
564 <!-- ======== -->
565 <xsl:template match="text()" />
566 <xsl:template match="text()" mode="inline">
567     <xsl:if test="preceding-sibling::node() and wl:starts_white(.)">
568       <xsl:text> </xsl:text>
569     </xsl:if>
570
571     <xsl:value-of select="wl:substitute_entities(wl:strip(.))" />
572
573     <xsl:if test="following-sibling::node() and wl:ends_white(.)">
574       <xsl:text> </xsl:text>
575     </xsl:if>
576 </xsl:template>
577
578
579 </xsl:stylesheet>