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