Minor fixed to README.
[librarian.git] / librarian / xslt / wl2html_base.xslt
1 <?xml version="1.0" encoding="utf-8"?>
2 <!--
3 #
4 #    This file is part of Librarian.
5 #
6 #    Copyright © 2008,2009,2010 Fundacja Nowoczesna Polska <fundacja@nowoczesnapolska.org.pl>
7 #    
8 #    For full list of contributors see AUTHORS file. 
9 #
10 #    This program is free software: you can redistribute it and/or modify
11 #    it under the terms of the GNU Affero General Public License as published by
12 #    the Free Software Foundation, either version 3 of the License, or
13 #    (at your option) any later version.
14 #
15 #    This program is distributed in the hope that it will be useful,
16 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 #    GNU Affero General Public License for more details.
19 #
20 #    You should have received a copy of the GNU Affero General Public License
21 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 #
23 -->
24 <xsl:stylesheet
25     version="1.0"
26     
27     xmlns="http://www.w3.org/1999/xhtml"
28     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   
29     xmlns:wl="http://wolnelektury.pl/functions"
30
31     exclude-result-prefixes="wl" >
32
33     <xsl:variable name="config" select="document('config.xml')" />
34
35     <xsl:output method="xml"
36         encoding="utf-8"
37         indent="yes"
38         omit-xml-declaration = "yes" />
39
40     <!--     
41         Dokument ten opisuje podstawowe przekształcenia potrzebne
42      do zamiany dokumentu WLML 1.0 na poprawnie sformatowany
43      dokument XHMTL.
44
45     -->
46
47     <xsl:template name="generic-attributes">
48         <xsl:param name="element" />
49         <xsl:param name="mypath" />
50         <xsl:variable name="tag" select="name($element)" />
51
52         <xsl:if test="$with-paths">
53             <xsl:attribute name="x-pointer">
54                 <xsl:value-of select="$mypath" />
55             </xsl:attribute>
56         </xsl:if>
57
58         <xsl:if test="$config//editable/*[name() = $tag]">
59             <xsl:attribute name="x-editable">editable</xsl:attribute>
60         </xsl:if>
61
62         <xsl:attribute name="class">
63             <xsl:value-of select="$tag"/>
64         </xsl:attribute>
65     </xsl:template>
66
67     <xsl:template name="generic-descent">
68         <xsl:param name="element" />
69         <xsl:param name="mypath" />
70         <xsl:param name="mixed" />
71         
72         <xsl:for-each select="child::node()">
73             <xsl:apply-templates select="." mode="element-tag">
74                 <xsl:with-param name="offset" select="count(preceding-sibling::*)" />
75                 <xsl:with-param name="parent-path" select="$mypath" />
76                 <xsl:with-param name="mixed" select="$mixed" />
77             </xsl:apply-templates>
78         </xsl:for-each>
79     </xsl:template>
80     
81     <xsl:template name="generic-content">
82         <xsl:param name="element" />
83         <xsl:param name="mypath" />
84         <xsl:param name="mixed" />
85
86         <xsl:call-template name="generic-attributes">
87             <xsl:with-param name="element" select="$element" />
88             <xsl:with-param name="mypath" select="$mypath" />
89         </xsl:call-template>
90
91         <xsl:call-template name="generic-descent">
92             <xsl:with-param name="element" select="$element" />
93             <xsl:with-param name="mypath" select="$mypath" />
94             <xsl:with-param name="mixed" select="$mixed" />
95         </xsl:call-template>
96     </xsl:template>
97     
98     <!-- Generyczne szablony -->
99     <xsl:template name="generic" >
100         <xsl:param name="element" />
101         <xsl:param name="mypath" />
102         <xsl:param name="offset" />
103
104         <!-- <xsl:param name="parent-type" select="'block'" /> -->
105
106         <xsl:variable name="tag" select="name($element)" />
107         <xsl:variable name="group" select="$config//types/*[@element and child::*[local-name() = $tag]]" />
108                     
109         <xsl:choose>
110             <!-- ignore namespaced elements -->
111             <xsl:when test="namespace-uri()" />
112             <xsl:when test="$group/@element">
113             
114                 <xsl:element name="{$group/@element}" namespace="http://www.w3.org/1999/xhtml">
115                     <xsl:apply-templates select="$element" mode="element-content" >
116                         <xsl:with-param name="mypath" select="$mypath" />
117                         <xsl:with-param name="mixed" select="boolean($group/@mixed)" />
118                     </xsl:apply-templates>
119                 </xsl:element>
120             </xsl:when>
121             <xsl:otherwise />
122         </xsl:choose>
123
124     </xsl:template>
125
126     <!--
127         Tagi niestandardowe
128     -->
129
130     <xsl:template match="pr|pa|pe|pt" mode="element-tag">
131         <a href="#annotation-{generate-id(.)}"><span class="annotation"/></a><a name="anchor-{generate-id(.)}" />
132     </xsl:template>      
133
134     <xsl:template match="dlugi_cytat|poezja_cyt" mode="element-tag">
135         <xsl:param name="offset" />
136         <xsl:param name="parent-path" />
137         <xsl:variable name="mypath"
138             select="concat($parent-path, '/', name(), '[', string($offset),']')" />
139
140         <xsl:element name="blockquote" >
141             <xsl:call-template name="generic-attributes">
142                 <xsl:with-param name="element" select="current()" />
143                 <xsl:with-param name="mypath" select="$mypath" />
144             </xsl:call-template>
145             <xsl:call-template name="generic-descent">
146                 <xsl:with-param name="element" select="current()" />
147                 <xsl:with-param name="mypath" select="$mypath" />
148                 <xsl:with-param name="mixed" select="true" />
149             </xsl:call-template>
150         </xsl:element>
151     </xsl:template>
152
153
154     <xsl:template match="lista_osob" mode="element-tag">
155         <xsl:param name="offset" />
156         <xsl:param name="parent-path" />
157         <xsl:variable name="mypath"
158             select="concat($parent-path, '/', name(), '[', string($offset),']')" />
159
160         <xsl:element name="div" >
161             <xsl:call-template name="generic-attributes">
162                 <xsl:with-param name="element" select="current()" />
163                 <xsl:with-param name="mypath" select="$mypath" />
164             </xsl:call-template>
165
166             <xsl:apply-templates select="./naglowek-listy" mode="element-tag" />
167             <ul>
168                 <xsl:for-each select="./lista_osoba">
169                     <xsl:apply-templates select="." mode="element-tag">
170                         <xsl:with-param name="offset" select="count(preceding-sibling::*)" />
171                         <xsl:with-param name="parent-path" select="$mypath" />
172                         <xsl:with-param name="mixed" select="false" />
173                     </xsl:apply-templates>
174                 </xsl:for-each>
175             </ul>
176         </xsl:element>
177     </xsl:template>
178
179     <xsl:template match="lista_osoba" mode="element-tag">
180         <xsl:param name="offset" />
181         <xsl:param name="parent-path" />
182         <xsl:variable name="mypath"
183             select="concat($parent-path, '/', name(), '[', string($offset),']')" />
184
185         <xsl:element name="li" >
186             <xsl:call-template name="generic-attributes">
187                 <xsl:with-param name="element" select="current()" />
188                 <xsl:with-param name="mypath" select="$mypath" />
189             </xsl:call-template>
190             <xsl:call-template name="generic-descent">
191                 <xsl:with-param name="element" select="current()" />
192                 <xsl:with-param name="mypath" select="$mypath" />
193                 <xsl:with-param name="mixed" select="true" />
194             </xsl:call-template>
195         </xsl:element>
196     </xsl:template>
197
198     <xsl:template match="separator_linia" mode="element-tag">
199         <xsl:param name="offset" />
200         <xsl:param name="parent-path" />
201         <xsl:variable name="mypath"
202             select="concat($parent-path, '/', name(), '[', string($offset),']')" />
203
204         <xsl:element name="hr" >
205             <xsl:call-template name="generic-attributes">
206                 <xsl:with-param name="element" select="current()" />
207                 <xsl:with-param name="mypath" select="$mypath" />
208             </xsl:call-template>
209         </xsl:element>
210     </xsl:template>
211
212     <xsl:template match="sekcja_swiatlo" mode="element-tag">
213         <xsl:param name="offset" />
214         <xsl:param name="parent-path" />
215         <xsl:variable name="mypath"
216             select="concat($parent-path, '/', name(), '[', string($offset),']')" />
217
218         <xsl:element name="br" >
219             <xsl:call-template name="generic-attributes">
220                 <xsl:with-param name="element" select="current()" />
221                 <xsl:with-param name="mypath" select="$mypath" />
222             </xsl:call-template>
223         </xsl:element>
224     </xsl:template>
225
226     <xsl:template match="sekcja_asterysk" mode="element-tag">
227         <xsl:param name="offset" />
228         <xsl:param name="parent-path" />
229         <xsl:variable name="mypath"
230             select="concat($parent-path, '/', name(), '[', string($offset),']')" />
231
232         <xsl:element name="p" >
233             <xsl:call-template name="generic-attributes">
234                 <xsl:with-param name="element" select="current()" />
235                 <xsl:with-param name="mypath" select="$mypath" />
236             </xsl:call-template>
237             *
238         </xsl:element>
239     </xsl:template>
240
241     <xsl:template match="zastepnik_wersu|wers_akap|wers_cd|wers_wciety" mode="element-tag">
242         <xsl:param name="offset" />
243         <xsl:param name="parent-path" />
244
245         <xsl:variable name="mypath"
246             select="concat($parent-path, '/', name(), '[',string($offset),']')" />
247        
248         <xsl:call-template name="generic-descent">
249             <xsl:with-param name="element" select="current()" />
250             <xsl:with-param name="mypath" select="$mypath" />
251             <xsl:with-param name="mixed" select="boolean(1)" />
252         </xsl:call-template>
253     </xsl:template>
254
255     <!-- strofy -->
256     <xsl:template match="strofa" mode="element-tag">
257         <xsl:param name="offset" />
258         <xsl:param name="parent-path" />
259
260         <xsl:variable name="mypath"
261             select="concat($parent-path, '/', name(), '[', string($offset),']')" />
262
263         <xsl:element name="div" >
264             <xsl:call-template name="generic-attributes">
265                 <xsl:with-param name="element" select="current()" />
266                 <xsl:with-param name="mypath" select="$mypath" />
267             </xsl:call-template>
268
269             <xsl:choose>
270                 <xsl:when test="count(br) > 0">
271                     <xsl:call-template name="verse">
272                         <xsl:with-param name="verse-content" select="br[1]/preceding-sibling::text() | br[1]/preceding-sibling::node()" />
273                         <xsl:with-param name="verse-type" select="br[1]/preceding-sibling::*[name() = 'wers_wciety' or name() = 'wers_akap' or name() = 'wers_cd'][1]" />
274                         <xsl:with-param name="mypath" select="$mypath" />
275                     </xsl:call-template>
276                     <xsl:for-each select="br">
277                                 <!-- Each BR tag "consumes" text after it -->
278                         <xsl:variable name="lnum" select="count(preceding-sibling::br)" />
279                         <xsl:call-template name="verse">
280                             <xsl:with-param name="verse-content"
281                                 select="following-sibling::text()[count(preceding-sibling::br) = $lnum+1] | following-sibling::node()[count(preceding-sibling::br) = $lnum+1]" />
282                             <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]" />
283                             <xsl:with-param name="mypath" select="$mypath" />
284                         </xsl:call-template>
285                     </xsl:for-each>
286                 </xsl:when>
287                 <xsl:otherwise>
288                     <xsl:call-template name="verse">
289                         <xsl:with-param name="verse-content" select="child::node()" />
290                         <xsl:with-param name="verse-type" select="wers_wciety|wers_akap|wers_cd[1]" />
291                         <xsl:with-param name="mypath" select="$mypath" />
292                     </xsl:call-template>
293                 </xsl:otherwise>
294             </xsl:choose>
295         </xsl:element>
296     </xsl:template>
297
298     <xsl:template name="verse">
299         <xsl:param name="verse-content" />
300         <xsl:param name="verse-type" />
301         <xsl:param name="mypath" />
302
303         <xsl:element name="p">
304             <xsl:attribute name="class">
305                 <xsl:value-of select="name($verse-type)" />
306             </xsl:attribute>
307             <xsl:for-each select="$verse-content">
308                 <xsl:apply-templates select="." mode="element-tag">
309                     <xsl:with-param name="offset" select="count(preceding-sibling::*)" />
310                     <xsl:with-param name="parent-path" select="$mypath" />
311                     <xsl:with-param name="mixed" select="boolean(1)" />
312                 </xsl:apply-templates>
313             </xsl:for-each>
314         </xsl:element>
315     </xsl:template>
316
317
318 <!-- default content processing -->
319     <xsl:template match="*" mode="element-content">
320         <xsl:param name="mypath" />
321         <xsl:param name="mixed" />
322
323         <xsl:call-template name="generic-content">
324             <xsl:with-param name="element" select="current()"/>
325             <xsl:with-param name="mypath" select="$mypath"/>
326             <xsl:with-param name="mixed" select="$mixed"/>
327         </xsl:call-template>
328     </xsl:template>
329
330     <xsl:template match="*" mode="element-tag" >
331         <xsl:param name="offset" />
332         <xsl:param name="parent-path" />
333         <xsl:param name="mixed" />
334
335         <xsl:variable name="mypath"
336             select="concat($parent-path, '/', name(), '[', string($offset),']')" />
337
338         <xsl:call-template name="generic">
339             <xsl:with-param name="element" select="current()" />
340             <xsl:with-param name="offset" select="$offset" />
341             <xsl:with-param name="mypath" select="$mypath" />
342             <xsl:with-param name="mixed" select="$mixed"/>
343         </xsl:call-template>
344     </xsl:template>
345    
346     <xsl:template match="text()" mode="element-tag">
347         <xsl:param name="mixed" />
348
349         <xsl:choose>
350         
351             <xsl:when test="not($mixed)">
352                 <xsl:choose>
353                     <xsl:when test="not(normalize-space(.))" />
354                     <xsl:when test="normalize-space(.) = '&#xfeff;'"/>
355
356                     <xsl:otherwise>
357                         <div class="parse-warning">
358                             <p class="message">Uwaga! Tekst poza paragrafem</p>
359                             <p>#<xsl:copy-of select="." />#</p>
360                         </div>
361                     </xsl:otherwise>
362                 </xsl:choose>
363             </xsl:when>
364         
365             <xsl:otherwise>
366                 <xsl:value-of select="wl:substitute_entities(.)">
367                     <xsl:fallback>
368                         <xsl:value-of select="." />
369                     </xsl:fallback>
370                 </xsl:value-of>
371             </xsl:otherwise>
372
373         </xsl:choose>
374         
375     </xsl:template>
376
377     <xsl:template match="node()" />
378     
379 </xsl:stylesheet>