images width in html
[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         \newif\ifflagnotwl
24         <xsl:for-each select="@*[starts-with(name(), 'flag-')]">
25             <cmd>
26                 <xsl:attribute name="name"><xsl:value-of select="wl:texcommand(name())" />true</xsl:attribute>
27             </cmd>
28         </xsl:for-each>
29
30         <xsl:for-each select="@*[starts-with(name(), 'data-')]">
31             <TeXML escape="0">
32                 \def\<xsl:value-of select="wl:texcommand(name())" />{<TeXML escape="1"><xsl:value-of select="."/></TeXML>}
33             </TeXML>
34         </xsl:for-each>
35         </TeXML>
36
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             <xsl:if test="@data-cover-width">
63                 <cmd name="makecover">
64                     <parm><xsl:value-of select="210 * @data-cover-width div @data-cover-height" />mm</parm>
65                     <parm>210mm</parm>
66                 </cmd>
67             </xsl:if>
68             <cmd name="maketitle" />
69
70             <cmd name="tytul"><parm>
71               <xsl:choose>
72                 <xsl:when test="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/nazwa_utworu">
73                     <!-- title in master -->
74                     <xsl:apply-templates select="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/autor_utworu" mode="title" />
75                     <xsl:apply-templates select="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/dzielo_nadrzedne" mode="title" />
76                     <xsl:apply-templates select="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/nazwa_utworu" mode="title" />
77                     <xsl:apply-templates select="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/podtytul" mode="title" />
78                     <!-- dc in master or not -->
79                     <cmd name="translatorsline" />
80                 </xsl:when>
81                 <xsl:otherwise>
82                     <!-- look for author title in dc -->
83                     <xsl:apply-templates select="rdf:RDF" mode="firstdctitle" />
84                     <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode='firstdctitle' />
85                 </xsl:otherwise>
86               </xsl:choose>
87             </parm></cmd>
88             <xsl:apply-templates select="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" />
89             <xsl:apply-templates select="utwor" mode="part" />
90
91             <TeXML escape="0">
92                 \def\coverby{
93                 <xsl:if test="@data-cover-by">Okładka na podstawie: 
94                     <xsl:choose>
95                     <xsl:when test="@data-cover-source">
96                         \href{\datacoversource}{\datacoverby}
97                     </xsl:when>
98                     <xsl:otherwise>
99                         \datacoverby{}
100                     </xsl:otherwise>
101                     </xsl:choose>
102                 </xsl:if>
103                 }
104             </TeXML>
105
106             <cmd name="editorialsection" />
107         </env>
108     </TeXML>
109 </xsl:template>
110
111 <xsl:template match="utwor" mode="part">
112     <cmd name="tytul"><parm>
113       <xsl:choose>
114         <xsl:when test="(powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny)/nazwa_utworu">
115             <!-- title in master -->
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         \def\translatorsline{<xsl:call-template name="translators" />}
146
147         \def\bookurl{<xsl:value-of select=".//dc:identifier.url" />}
148
149         \def\rightsinfo{Ten utwór nie jest chroniony prawem autorskim i~znajduje się w~domenie
150             publicznej, co oznacza że możesz go swobodnie wykorzystywać, publikować
151             i~rozpowszechniać. 
152             %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             }
158         <xsl:if test=".//dc:rights.license">
159             \def\rightsinfo{Ten utwór jest udostepniony na licencji
160             \href{<xsl:value-of select=".//dc:rights.license" />}{<xsl:value-of select=".//dc:rights" />}.}
161         </xsl:if>
162
163         \def\sourceinfo{
164             <xsl:if test=".//dc:source">
165                 Tekst opracowany na podstawie: <xsl:apply-templates select=".//dc:source" mode="inline" />
166                 \vspace{.6em}
167             </xsl:if>}
168         \def\description{<xsl:apply-templates select=".//dc:description" mode="inline" />}
169         \def\editors{<xsl:call-template name="editors" />}
170     </TeXML>
171 </xsl:template>
172
173
174 <!-- ============== -->
175 <!-- = BOOK TITLE = -->
176 <!-- = (from DC)  = -->
177 <!-- ============== -->
178
179 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="dctitle">
180     <xsl:apply-templates select="rdf:RDF" mode="dctitle" />
181 </xsl:template>
182
183 <xsl:template match="rdf:RDF" mode="dctitle">
184     <cmd name="nazwapodutworu"><parm>
185         <xsl:apply-templates select=".//dc:title/node()" mode="inline" />
186     </parm></cmd>
187 </xsl:template>
188
189 <xsl:template match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny" mode="firstdctitle">
190     <xsl:apply-templates select="rdf:RDF" mode="firstdctitle" />
191 </xsl:template>
192
193 <xsl:template match="rdf:RDF" mode="firstdctitle">
194     <cmd name="autorutworu"><parm><cmd name="authors"/></parm></cmd>
195     <cmd name="nazwautworu"><parm>
196         <xsl:apply-templates select=".//dc:title/node()" mode="inline" />
197     </parm></cmd>
198     <cmd name="translatorsline" />
199 </xsl:template>
200
201
202 <!-- ============================================================================== -->
203 <!-- = MASTER TAG                                                                 = -->
204 <!-- = (can contain block tags, paragraph tags, standalone tags and special tags) = -->
205 <!-- ============================================================================== -->
206 <!-- ==================================================================================== -->
207 <!-- = BLOCK TAGS                                                                       = -->
208 <!-- = (can contain other block tags, paragraph tags, standalone tags and special tags) = -->
209 <!-- ==================================================================================== -->
210
211 <xsl:template
212     match="powiesc|opowiadanie|liryka_l|liryka_lp|dramat_wierszowany_l|dramat_wierszowany_lp|dramat_wspolczesny|nota|dedykacja|dlugi_cytat|poezja_cyt|motto">
213     <cmd>
214         <xsl:attribute name="name">
215             <xsl:value-of select="wl:texcommand(name())" />
216         </xsl:attribute>
217         <parm><xsl:apply-templates/></parm>
218     </cmd>
219 </xsl:template>
220
221 <xsl:template match="lista_osob">
222     <cmd name="listaosob">
223         <parm><xsl:apply-templates select="naglowek_listy" /></parm>
224         <parm><xsl:apply-templates select="lista_osoba" /></parm>
225     </cmd>
226 </xsl:template>
227
228 <xsl:template match="kwestia">
229     <cmd name="kwestia">
230         <parm><xsl:apply-templates select="strofa|akap|didaskalia" /></parm>
231     </cmd>
232 </xsl:template>
233
234
235 <!-- ========================================== -->
236 <!-- = PARAGRAPH TAGS                         = -->
237 <!-- = (can contain inline and special tags)  = -->
238 <!-- ========================================== -->
239
240 <!-- only in root -->
241 <xsl:template
242     match="autor_utworu|dzielo_nadrzedne|nazwa_utworu|podtytul"
243     mode="title">
244     <cmd>
245         <xsl:attribute name="name">
246             <xsl:value-of select="wl:texcommand(name())" />
247         </xsl:attribute>
248         <parm><xsl:apply-templates mode="inline"/></parm>
249     </cmd>
250 </xsl:template>
251
252
253 <xsl:template
254     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">
255     <cmd>
256         <xsl:attribute name="name">
257             <xsl:value-of select="wl:texcommand(name())" />
258         </xsl:attribute>
259         <parm><xsl:apply-templates mode="inline"/></parm>
260     </cmd>
261 </xsl:template>
262
263 <xsl:template match="strofa">
264   <cmd name="strofa"><parm>
265     <xsl:choose>
266         <xsl:when test="count(br) > 0">
267             <xsl:call-template name="verse">
268                 <xsl:with-param name="verse-content" select="br[1]/preceding-sibling::text() | br[1]/preceding-sibling::node()" />
269                 <xsl:with-param name="verse-type" select="br[1]/preceding-sibling::*[name() = 'wers_wciety' or name() = 'wers_akap' or name() = 'wers_cd'][1]" />
270             </xsl:call-template>
271             <xsl:for-each select="br">
272                 <TeXML escape="0">\\{}</TeXML>
273                 <!-- Each BR tag "consumes" text after it -->
274                 <xsl:variable name="lnum" select="count(preceding-sibling::br)" />
275                 <xsl:call-template name="verse">
276                     <xsl:with-param name="verse-content"
277                         select="following-sibling::text()[count(preceding-sibling::br) = $lnum+1] | following-sibling::node()[count(preceding-sibling::br) = $lnum+1]" />
278                     <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]" />
279                 </xsl:call-template>
280             </xsl:for-each>
281         </xsl:when>
282         <xsl:otherwise>
283             <xsl:call-template name="verse">
284                 <xsl:with-param name="verse-content" select="text() | node()" />
285                 <xsl:with-param name="verse-type" select="wers_wciety|wers_akap|wers_cd[1]" />
286             </xsl:call-template>
287         </xsl:otherwise>
288     </xsl:choose>
289   </parm></cmd>
290 </xsl:template>
291
292
293 <xsl:template name="verse">
294     <xsl:param name="verse-content" />
295     <xsl:param name="verse-type" />
296
297     <cmd>
298         <xsl:attribute name="name">
299             <xsl:choose>
300                 <xsl:when test="$verse-type != ''">
301                     <xsl:value-of select='wl:texcommand(name($verse-type))' />
302                 </xsl:when>
303                 <xsl:otherwise>wers</xsl:otherwise>
304             </xsl:choose>
305         </xsl:attribute>
306         <xsl:if test="string($verse-content/@typ)">
307             <opt><xsl:value-of select="$verse-content/@typ" />em</opt>
308         </xsl:if>
309         <parm><xsl:apply-templates select="$verse-content" mode="inline"/></parm>
310     </cmd>
311 </xsl:template>
312
313 <!-- ================================================ -->
314 <!-- = INLINE TAGS                                  = -->
315 <!-- = (contain other inline tags and special tags) = -->
316 <!-- ================================================ -->
317
318 <xsl:template mode="inline"
319     match="pa|pe|pr|pt|mat|didask_tekst|slowo_obce|wyroznienie|osoba">
320     <cmd>
321         <xsl:attribute name="name">
322             <xsl:value-of select="wl:texcommand(name())" />
323         </xsl:attribute>
324         <parm><xsl:apply-templates mode="inline"/></parm>
325     </cmd>
326 </xsl:template>
327
328
329
330 <xsl:template match="tytul_dziela" mode="inline">
331     <cmd name="tytuldziela"><parm>
332         <xsl:if test="@typ = '1'">„</xsl:if><xsl:apply-templates mode="inline" /><xsl:if test="@typ = '1'">”</xsl:if>
333     </parm></cmd>
334 </xsl:template>
335
336
337
338 <!-- ============================================== -->
339 <!-- = STANDALONE TAGS                            = -->
340 <!-- = (cannot contain any other tags)            = -->
341 <!-- ============================================== -->
342 <xsl:template
343     match="sekcja_swiatlo|sekcja_asterysk|separator_linia">
344     <cmd>
345         <xsl:attribute name="name">
346             <xsl:value-of select="wl:texcommand(name())" />
347         </xsl:attribute>
348     </cmd>
349 </xsl:template>
350
351 <xsl:template match="ilustr">
352     <cmd>
353         <xsl:attribute name="name">
354             <xsl:value-of select="wl:texcommand(name())" />
355         </xsl:attribute>
356                 <parm><xsl:value-of select="@src" /></parm>
357         <parm><xsl:apply-templates mode="inline" /></parm>
358     </cmd>
359 </xsl:template>
360
361
362 <!-- ================ -->
363 <!-- = SPECIAL TAGS = -->
364 <!-- ================ -->
365 <!-- Themes -->
366
367
368 <xsl:template match="begin|end|motyw">
369     <xsl:apply-templates select='.' mode="inline" />
370 </xsl:template>
371
372 <xsl:template match="begin" mode="inline" />
373 <xsl:template match="end" mode="inline" />
374
375 <xsl:template match="motyw" mode="inline">
376     <cmd name="motyw">
377         <xsl:if test="@moved">
378             <opt name="moved"><xsl:value-of select="@moved" /></opt>
379         </xsl:if>
380         <parm><xsl:apply-templates mode="inline" /></parm>
381     </cmd>
382 </xsl:template>
383
384 <xsl:template name="authors">
385     <xsl:for-each select=".//dc:creator_parsed">
386         <xsl:if test="position() != 1">, </xsl:if>
387         <xsl:apply-templates mode="inline" />
388     </xsl:for-each>
389 </xsl:template>
390
391 <xsl:template name="editors">
392     <xsl:if test="//dc:contributor.editor_parsed|//dc:contributor.technical_editor_parsed">
393         <xsl:text>Opracowanie redakcyjne: </xsl:text>
394         <xsl:for-each select="//dc:contributor.editor_parsed|//dc:contributor.technical_editor_parsed[not(//dc:contributor.editor_parsed/text()=text())]">
395             <xsl:sort select="@sortkey" />
396             <xsl:if test="position() != 1">, </xsl:if>
397             <xsl:apply-templates mode="inline" />
398         </xsl:for-each>.
399     </xsl:if>
400 </xsl:template>
401
402 <xsl:template name="translators">
403     <xsl:if test=".//dc:contributor.translator_parsed">
404         <cmd name='translator'><parm>
405             <xsl:for-each select=".//dc:contributor.translator_parsed">
406                 <xsl:if test="position() != 1">, </xsl:if>
407                 <xsl:apply-templates mode="inline" />
408             </xsl:for-each>
409         </parm></cmd>
410     </xsl:if>
411 </xsl:template>
412
413
414 <!-- ============== -->
415 <!-- = ADDED TAGS = -->
416 <!-- ============== -->
417
418
419 <xsl:template match="dywiz" mode="inline">
420     <cmd name="dywiz" />
421 </xsl:template>
422
423 <xsl:template match="nbsp" mode="inline">
424     <spec cat="tilde" />
425 </xsl:template>
426
427 <xsl:template match="alien" mode="inline">
428     <group>
429         <cmd name="alien" />
430         <xsl:apply-templates mode="inline" />
431     </group>
432 </xsl:template>
433
434 <!-- ================ -->
435 <!-- = IGNORED TAGS = -->
436 <!-- ================ -->
437 <xsl:template match="extra|uwaga" />
438 <xsl:template match="extra|uwaga" mode="inline" />
439
440 <xsl:template match="nota_red" />
441
442 <!-- ======== -->
443 <!-- = TEXT = -->
444 <!-- ======== -->
445 <xsl:template match="text()" />
446 <xsl:template match="text()" mode="inline">
447     <xsl:if test="preceding-sibling::node() and wl:starts_white(.)">
448       <xsl:text> </xsl:text>
449     </xsl:if>
450
451     <xsl:value-of select="wl:substitute_entities(wl:strip(.))" />
452
453     <xsl:if test="following-sibling::node() and wl:ends_white(.)">
454       <xsl:text> </xsl:text>
455     </xsl:if>
456 </xsl:template>
457
458
459 </xsl:stylesheet>