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