#950: visibly mark author's annotations,
[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[a4paper, oneside, 11pt]{book}
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
35         \usepackage{wl}
36         </TeXML>
37
38         <xsl:choose>
39             <xsl:when test="@morefloats = 'new'">
40                 <TeXML escape="0">
41                     \usepackage[maxfloats=64]{morefloats}
42                 </TeXML>
43             </xsl:when>
44             <xsl:when test="@morefloats = 'old'">
45                 <TeXML escape="0">
46                     \usepackage{morefloats}
47                 </TeXML>
48             </xsl:when>
49             <xsl:when test="@morefloats = 'none'" />
50             <xsl:otherwise>
51                 <TeXML escape="0">
52                     \IfFileExists{morefloats.sty}{
53                         \usepackage{morefloats}
54                     }{}
55                 </TeXML>
56             </xsl:otherwise>
57         </xsl:choose>
58
59         <xsl:apply-templates select="rdf:RDF" mode="titlepage" />
60         <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode='titlepage' />
61
62         <env name="document">
63             <xsl:if test="@data-cover-width">
64                 <cmd name="makecover">
65                     <parm><xsl:value-of select="210 * @data-cover-width div @data-cover-height" />mm</parm>
66                     <parm>210mm</parm>
67                 </cmd>
68             </xsl:if>
69             <cmd name="maketitle" />
70
71             <cmd name="tytul"><parm>
72               <xsl:choose>
73                 <xsl:when test="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/nazwa_utworu">
74                     <!-- title in master -->
75                     <xsl:apply-templates select="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/autor_utworu" mode="title" />
76                     <xsl:apply-templates select="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/dzielo_nadrzedne" mode="title" />
77                     <xsl:apply-templates select="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/nazwa_utworu" mode="title" />
78                     <xsl:apply-templates select="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/podtytul" mode="title" />
79                     <!-- dc in master or not -->
80                     <cmd name="translatorsline" />
81                 </xsl:when>
82                 <xsl:otherwise>
83                     <!-- look for author title in dc -->
84                     <xsl:apply-templates select="rdf:RDF" mode="firstdctitle" />
85                     <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode='firstdctitle' />
86                 </xsl:otherwise>
87               </xsl:choose>
88             </parm></cmd>
89             <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" />
90             <xsl:apply-templates select="utwor" mode="part" />
91             <cmd name="editorialsection" />
92         </env>
93     </TeXML>
94 </xsl:template>
95
96 <xsl:template match="utwor" mode="part">
97     <cmd name="tytul"><parm>
98       <xsl:choose>
99         <xsl:when test="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/nazwa_utworu">
100             <!-- title in master -->
101             <xsl:apply-templates select="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/nazwa_utworu" mode="title" />
102             <xsl:apply-templates select="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/podtytul" mode="title" />
103         </xsl:when>
104         <xsl:otherwise>
105             <!-- look for title in dc -->
106             <xsl:apply-templates select="rdf:RDF" mode="dctitle" />
107             <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode='dctitle' />
108         </xsl:otherwise>
109       </xsl:choose>
110     </parm></cmd>
111
112     <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" />
113     <xsl:apply-templates select="utwor" mode="part" />
114 </xsl:template>
115
116 <!-- =================== -->
117 <!-- = MAIN TITLE PAGE = -->
118 <!-- = (from DC)       = -->
119 <!-- =================== -->
120
121 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="titlepage">
122     <xsl:apply-templates select="rdf:RDF" mode="titlepage" />
123 </xsl:template>
124
125 <xsl:template match="rdf:RDF" mode="titlepage">
126     <TeXML escape="0">
127         \def\authors{<xsl:call-template name="authors" />}
128         \author{\authors}
129         \title{<xsl:apply-templates select=".//dc:title" />}
130         \def\translatorsline{<xsl:call-template name="translators" />}
131
132         \def\bookurl{<xsl:value-of select=".//dc:identifier.url" />}
133
134         \def\rightsinfo{Ten utwór nie jest chroniony prawem autorskim i~znajduje się w~domenie
135             publicznej, co oznacza że możesz go swobodnie wykorzystywać, publikować
136             i~rozpowszechniać. Jeśli utwór opatrzony jest dodatkowymi materiałami
137             (przypisy, motywy literackie etc.), które podlegają prawu autorskiemu, to
138             te dodatkowe materiały udostępnione są na licencji
139             \href{http://creativecommons.org/licenses/by-sa/3.0/}{Creative Commons
140             Uznanie Autorstwa – Na Tych Samych Warunkach 3.0 PL}.}
141         <xsl:if test=".//dc:rights.license">
142             \def\rightsinfo{Ten utwór jest udostepniony na licencji
143             \href{<xsl:value-of select=".//dc:rights.license" />}{<xsl:value-of select=".//dc:rights" />}.}
144         </xsl:if>
145
146         \def\sourceinfo{
147             <xsl:if test=".//dc:source">
148                 Tekst opracowany na podstawie: <xsl:apply-templates select=".//dc:source" mode="inline" />
149                 \vspace{.6em}
150             </xsl:if>}
151         \def\description{<xsl:apply-templates select=".//dc:description" mode="inline" />}
152         \def\editors{<xsl:call-template name="editors" />}
153     </TeXML>
154 </xsl:template>
155
156
157 <!-- ============== -->
158 <!-- = BOOK TITLE = -->
159 <!-- = (from DC)  = -->
160 <!-- ============== -->
161
162 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="dctitle">
163     <xsl:apply-templates select="rdf:RDF" mode="dctitle" />
164 </xsl:template>
165
166 <xsl:template match="rdf:RDF" mode="dctitle">
167     <cmd name="nazwapodutworu"><parm>
168         <xsl:apply-templates select=".//dc:title/node()" mode="inline" />
169     </parm></cmd>
170 </xsl:template>
171
172 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="firstdctitle">
173     <xsl:apply-templates select="rdf:RDF" mode="firstdctitle" />
174 </xsl:template>
175
176 <xsl:template match="rdf:RDF" mode="firstdctitle">
177     <cmd name="autorutworu"><parm><cmd name="authors"/></parm></cmd>
178     <cmd name="nazwautworu"><parm>
179         <xsl:apply-templates select=".//dc:title/node()" mode="inline" />
180     </parm></cmd>
181     <cmd name="translatorsline" />
182 </xsl:template>
183
184
185 <!-- ============================================================================== -->
186 <!-- = MASTER TAG                                                                 = -->
187 <!-- = (can contain block tags, paragraph tags, standalone tags and special tags) = -->
188 <!-- ============================================================================== -->
189 <!-- ==================================================================================== -->
190 <!-- = BLOCK TAGS                                                                       = -->
191 <!-- = (can contain other block tags, paragraph tags, standalone tags and special tags) = -->
192 <!-- ==================================================================================== -->
193
194 <xsl:template
195     match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny|nota|dedykacja|dlugi_cytat|poezja_cyt|motto">
196     <cmd>
197         <xsl:attribute name="name">
198             <xsl:value-of select="wl:texcommand(name())" />
199         </xsl:attribute>
200         <parm><xsl:apply-templates/></parm>
201     </cmd>
202 </xsl:template>
203
204 <xsl:template match="lista_osob">
205     <cmd name="listaosob">
206         <parm><xsl:apply-templates select="naglowek_listy" /></parm>
207         <parm><xsl:apply-templates select="lista_osoba" /></parm>
208     </cmd>
209 </xsl:template>
210
211 <xsl:template match="kwestia">
212     <cmd name="kwestia">
213         <parm><xsl:apply-templates select="strofa|akap|didaskalia" /></parm>
214     </cmd>
215 </xsl:template>
216
217
218 <!-- ========================================== -->
219 <!-- = PARAGRAPH TAGS                         = -->
220 <!-- = (can contain inline and special tags)  = -->
221 <!-- ========================================== -->
222
223 <!-- only in root -->
224 <xsl:template
225     match="autor_utworu|dzielo_nadrzedne|nazwa_utworu|podtytul"
226     mode="title">
227     <cmd>
228         <xsl:attribute name="name">
229             <xsl:value-of select="wl:texcommand(name())" />
230         </xsl:attribute>
231         <parm><xsl:apply-templates mode="inline"/></parm>
232     </cmd>
233 </xsl:template>
234
235
236 <xsl:template
237     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">
238     <cmd>
239         <xsl:attribute name="name">
240             <xsl:value-of select="wl:texcommand(name())" />
241         </xsl:attribute>
242         <parm><xsl:apply-templates mode="inline"/></parm>
243     </cmd>
244 </xsl:template>
245
246 <xsl:template match="strofa">
247   <cmd name="strofa"><parm>
248     <xsl:choose>
249         <xsl:when test="count(br) > 0">
250             <xsl:call-template name="verse">
251                 <xsl:with-param name="verse-content" select="br[1]/preceding-sibling::text() | br[1]/preceding-sibling::node()" />
252                 <xsl:with-param name="verse-type" select="br[1]/preceding-sibling::*[name() = 'wers_wciety' or name() = 'wers_akap' or name() = 'wers_cd'][1]" />
253             </xsl:call-template>
254             <xsl:for-each select="br">
255                 <TeXML escape="0">\\{}</TeXML>
256                 <!-- Each BR tag "consumes" text after it -->
257                 <xsl:variable name="lnum" select="count(preceding-sibling::br)" />
258                 <xsl:call-template name="verse">
259                     <xsl:with-param name="verse-content"
260                         select="following-sibling::text()[count(preceding-sibling::br) = $lnum+1] | following-sibling::node()[count(preceding-sibling::br) = $lnum+1]" />
261                     <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]" />
262                 </xsl:call-template>
263             </xsl:for-each>
264         </xsl:when>
265         <xsl:otherwise>
266             <xsl:call-template name="verse">
267                 <xsl:with-param name="verse-content" select="text() | node()" />
268                 <xsl:with-param name="verse-type" select="wers_wciety|wers_akap|wers_cd[1]" />
269             </xsl:call-template>
270         </xsl:otherwise>
271     </xsl:choose>
272   </parm></cmd>
273 </xsl:template>
274
275
276 <xsl:template name="verse">
277     <xsl:param name="verse-content" />
278     <xsl:param name="verse-type" />
279
280     <cmd>
281         <xsl:attribute name="name">
282             <xsl:choose>
283                 <xsl:when test="$verse-type != ''">
284                     <xsl:value-of select='wl:texcommand(name($verse-type))' />
285                 </xsl:when>
286                 <xsl:otherwise>wers</xsl:otherwise>
287             </xsl:choose>
288         </xsl:attribute>
289         <xsl:if test="string($verse-content/@typ)">
290             <opt><xsl:value-of select="$verse-content/@typ" />em</opt>
291         </xsl:if>
292         <parm><xsl:apply-templates select="$verse-content" mode="inline"/></parm>
293     </cmd>
294 </xsl:template>
295
296 <!-- ================================================ -->
297 <!-- = INLINE TAGS                                  = -->
298 <!-- = (contain other inline tags and special tags) = -->
299 <!-- ================================================ -->
300
301 <xsl:template mode="inline"
302     match="pa|pe|pr|pt|mat|didask_tekst|slowo_obce|wyroznienie|osoba">
303     <cmd>
304         <xsl:attribute name="name">
305             <xsl:value-of select="wl:texcommand(name())" />
306         </xsl:attribute>
307         <parm><xsl:apply-templates mode="inline"/></parm>
308     </cmd>
309 </xsl:template>
310
311
312
313 <xsl:template match="tytul_dziela" mode="inline">
314     <cmd name="tytuldziela"><parm>
315         <xsl:if test="@typ = '1'">„</xsl:if><xsl:apply-templates mode="inline" /><xsl:if test="@typ = '1'">”</xsl:if>
316     </parm></cmd>
317 </xsl:template>
318
319
320
321 <!-- ============================================== -->
322 <!-- = STANDALONE TAGS                            = -->
323 <!-- = (cannot contain any other tags)            = -->
324 <!-- ============================================== -->
325 <xsl:template
326     match="sekcja_swiatlo|sekcja_asterysk|separator_linia">
327     <cmd>
328         <xsl:attribute name="name">
329             <xsl:value-of select="wl:texcommand(name())" />
330         </xsl:attribute>
331     </cmd>
332 </xsl:template>
333
334
335 <!-- ================ -->
336 <!-- = SPECIAL TAGS = -->
337 <!-- ================ -->
338 <!-- Themes -->
339
340
341 <xsl:template match="begin|end|motyw">
342     <xsl:apply-templates select='.' mode="inline" />
343 </xsl:template>
344
345 <xsl:template match="begin" mode="inline" />
346 <xsl:template match="end" mode="inline" />
347
348 <xsl:template match="motyw" mode="inline">
349     <cmd name="motyw">
350         <xsl:if test="@moved">
351             <opt name="moved"><xsl:value-of select="@moved" /></opt>
352         </xsl:if>
353         <parm><xsl:apply-templates mode="inline" /></parm>
354     </cmd>
355 </xsl:template>
356
357 <xsl:template name="authors">
358     <xsl:for-each select=".//dc:creator_parsed">
359         <xsl:if test="position() != 1">, </xsl:if>
360         <xsl:apply-templates mode="inline" />
361     </xsl:for-each>
362 </xsl:template>
363
364 <xsl:template name="editors">
365     <xsl:if test="//dc:contributor.editor_parsed|//dc:contributor.technical_editor_parsed">
366         <xsl:text>Opracowanie redakcyjne i przypisy: </xsl:text>
367         <xsl:for-each select="//dc:contributor.editor_parsed|//dc:contributor.technical_editor_parsed[not(//dc:contributor.editor_parsed/text()=text())]">
368             <xsl:sort select="@sortkey" />
369             <xsl:if test="position() != 1">, </xsl:if>
370             <xsl:apply-templates mode="inline" />
371         </xsl:for-each>.
372     </xsl:if>
373 </xsl:template>
374
375 <xsl:template name="translators">
376     <xsl:if test=".//dc:contributor.translator_parsed">
377         <cmd name='translator'><parm>
378             <xsl:for-each select=".//dc:contributor.translator_parsed">
379                 <xsl:if test="position() != 1">, </xsl:if>
380                 <xsl:apply-templates mode="inline" />
381             </xsl:for-each>
382         </parm></cmd>
383     </xsl:if>
384 </xsl:template>
385
386
387 <!-- ============== -->
388 <!-- = ADDED TAGS = -->
389 <!-- ============== -->
390
391
392 <xsl:template match="dywiz" mode="inline">
393     <cmd name="dywiz" />
394 </xsl:template>
395
396 <xsl:template match="nbsp" mode="inline">
397     <spec cat="tilde" />
398 </xsl:template>
399
400 <xsl:template match="alien" mode="inline">
401     <group>
402         <cmd name="alien" />
403         <xsl:apply-templates mode="inline" />
404     </group>
405 </xsl:template>
406
407 <!-- ================ -->
408 <!-- = IGNORED TAGS = -->
409 <!-- ================ -->
410 <xsl:template match="extra|uwaga" />
411 <xsl:template match="extra|uwaga" mode="inline" />
412
413 <xsl:template match="nota_red" />
414
415 <!-- ======== -->
416 <!-- = TEXT = -->
417 <!-- ======== -->
418 <xsl:template match="text()" />
419 <xsl:template match="text()" mode="inline">
420     <xsl:if test="preceding-sibling::node() and wl:starts_white(.)">
421       <xsl:text> </xsl:text>
422     </xsl:if>
423
424     <xsl:value-of select="wl:substitute_entities(wl:strip(.))" />
425
426     <xsl:if test="following-sibling::node() and wl:ends_white(.)">
427       <xsl:text> </xsl:text>
428     </xsl:if>
429 </xsl:template>
430
431
432 </xsl:stylesheet>