]> Lady’s Gitweb - Shushe/blob - lib/catalog2transform.xslt
Allow including entire directories
[Shushe] / lib / catalog2transform.xslt
1 <?xml version="1.0"?>
2 <!--
3 SPDX-FileCopyrightText: 2023, 2024 Lady <https://www.ladys.computer/about/#lady>
4 SPDX-License-Identifier: MPL-2.0
5 -->
6 <!--
7 ⁌ ⛩📰 书社 ∷ lib/catalog2transform.xslt
8
9 © 2023–2024 Lady [@ Ladys Computer].
10
11 This Source Code Form is subject to the terms of the Mozilla Public License, v 2.0.
12 If a copy of the M·P·L was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0/>.
13 -->
14 <!DOCTYPE transform [
15 <!ENTITY 书社 "urn:fdc:ladys.computer:20231231:Shu1She4">
16 <!ENTITY xml "http://www.w3.org/XML/1998/namespace">
17 ]>
18 <transform
19 xmlns="http://www.w3.org/1999/XSL/Transform"
20 xmlns:dct="http://purl.org/dc/terms/"
21 xmlns:catalog="urn:oasis:names:tc:entity:xmlns:xml:catalog"
22 xmlns:exsl="http://exslt.org/common"
23 xmlns:exslstr="http://exslt.org/strings"
24 xmlns:html="http://www.w3.org/1999/xhtml"
25 xmlns:nie="http://www.semanticdesktop.org/ontologies/2007/01/19/nie#"
26 xmlns:nfo="http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#"
27 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
28 xmlns:svg="http://www.w3.org/2000/svg"
29 xmlns:xlink="http://www.w3.org/1999/xlink"
30 xmlns:xslt="http://www.w3.org/1999/XSL/TransformAlias"
31 xmlns:书社="urn:fdc:ladys.computer:20231231:Shu1She4"
32 xmlns:书社vocab="urn:fdc:ladys.computer:20231231:Shu1She4:vocab:"
33 exclude-result-prefixes="catalog"
34 version="1.0"
35 >
36 <include href="literally.xslt"/>
37 <namespace-alias stylesheet-prefix="xslt" result-prefix="#default"/>
38 <param name="METADATA" select="'about:blank'"/>
39 <variable name="builddir" select="document($METADATA)//书社vocab:BuildDirectory/@nfo:fileUrl"/>
40 <template match="/">
41 <variable name="ids">
42 <for-each select="//catalog:uri[@name]">
43 <书社:id>
44 <value-of select="@name"/>
45 </书社:id>
46 </for-each>
47 </variable>
48 <variable name="unique-ids">
49 <for-each select="exsl:node-set($ids)/*">
50 <if test="not(preceding-sibling::*[string(.)=string(current())])">
51 <copy-of select="."/>
52 </if>
53 </for-each>
54 </variable>
55 <xslt:transform exclude-result-prefixes="nie nfo" extension-element-prefixes="exsl exslstr" version="1.0">
56 <xslt:param name="BUILDTIME" select="'1972-12-31T00:00:00Z'"/>
57 <xslt:param name="IDENTIFIER" select="false()"/>
58 <xslt:param name="SRCREV" select="false()"/>
59 <xslt:param name="THISREV" select="false()"/>
60 <xslt:variable name="书社:source" select="/"/>
61 <xslt:variable name="书社:about-fragment">
62 <apply-templates select="document($METADATA)" mode="书社:literally">
63 <with-param name="extension-element-namespaces" select="'http://exslt.org/common http://exslt.org/strings'"/>
64 </apply-templates>
65 </xslt:variable>
66 <xslt:variable name="书社:about" select="exsl:node-set($书社:about-fragment)"/>
67 <xslt:variable name="书社:expansion-fragment">
68 <xslt:apply-templates select="$书社:source/node()" mode="书社:expand"/>
69 </xslt:variable>
70 <xslt:variable name="书社:expansion" select="exsl:node-set($书社:expansion-fragment)"/>
71 <xslt:variable name="书社:result-fragment">
72 <xslt:apply-templates select="$书社:expansion/node()"/>
73 </xslt:variable>
74 <xslt:variable name="书社:result" select="exsl:node-set($书社:result-fragment)"/>
75 <xslt:variable name="书社:destination" select="string($书社:about//*[@rdf:about=$IDENTIFIER]/@书社vocab:destination)"/>
76 <for-each select="//catalog:uri">
77 <xslt:include href="{@uri}">
78 <attribute name="书社:id">
79 <value-of select="@name"/>
80 </attribute>
81 </xslt:include>
82 </for-each>
83 <xslt:template name="书社:apply-attributes">
84 <xslt:param name="and-version" select="false()"/>
85 <xslt:param name="context-nodes" select="/.."/>
86 <xslt:param name="destination-nodes" select="/.."/>
87 <xslt:variable name="additional-attributes" select="$context-nodes/@*"/>
88 <xslt:for-each select="$destination-nodes">
89 <xslt:variable name="existing-attributes" select="@*"/>
90 <xslt:choose>
91 <xslt:when test="self::*">
92 <xslt:variable name="context" select="."/>
93 <xslt:variable name="attribute-names">
94 <xslt:for-each select="$existing-attributes|$additional-attributes">
95 <书社:attribute>
96 <attribute name="local-name">
97 <text>{local-name()}</text>
98 </attribute>
99 <attribute name="name">
100 <text>{name()}</text>
101 </attribute>
102 <attribute name="namespace-uri">
103 <text>{namespace-uri()}</text>
104 </attribute>
105 </书社:attribute>
106 </xslt:for-each>
107 </xslt:variable>
108 <xslt:variable name="lang">
109 <xslt:choose>
110 <xslt:when test="(self::html:*|self::svg:*)/@lang">
111 <xslt:value-of select="@lang"/>
112 </xslt:when>
113 <xslt:when test="@xml:lang">
114 <xslt:value-of select="@xml:lang"/>
115 </xslt:when>
116 <xslt:when test="$additional-attributes[(parent::html:* or parent::svg:*) and namespace-uri()='' and local-name()='lang']">
117 <xslt:value-of select="$additional-attributes[(parent::html:* or parent::svg:*) and namespace-uri()='' and local-name()='lang']"/>
118 </xslt:when>
119 <xslt:when test="(self::html:* or self::svg:*) and $additional-attributes[(parent::书社:apply-attributes or parent::书社:apply-attributes-to-root) and namespace-uri()='' and local-name()='lang']">
120 <xslt:value-of select="$additional-attributes[(parent::书社:apply-attributes or parent::书社:apply-attributes-to-root) and namespace-uri()='' and local-name()='lang']"/>
121 </xslt:when>
122 <xslt:when test="$additional-attributes[namespace-uri()='&xml;' and local-name()='lang']">
123 <xslt:value-of select="$additional-attributes[namespace-uri()='&xml;' and local-name()='lang']"/>
124 </xslt:when>
125 </xslt:choose>
126 </xslt:variable>
127 <xslt:copy>
128 <xslt:if test="$and-version and $THISREV">
129 <xslt:attribute name="书社:version">
130 <xslt:value-of select="$THISREV"/>
131 </xslt:attribute>
132 </xslt:if>
133 <xslt:if test="string($lang)!=''">
134 <xslt:if test="self::html:* or self::svg:*">
135 <xslt:attribute name="lang">
136 <xslt:value-of select="$lang"/>
137 </xslt:attribute>
138 </xslt:if>
139 <xslt:attribute name="xml:lang">
140 <xslt:value-of select="$lang"/>
141 </xslt:attribute>
142 </xslt:if>
143 <xslt:for-each select="exsl:node-set($attribute-names)/*">
144 <xslt:choose>
145 <xslt:when test="@namespace-uri='&xml;'"/>
146 <xslt:when test="$context[self::html:* or self::svg:*] and @namespace-uri='' and @local-name='lang'"/>
147 <xslt:when test="@namespace-uri='&书社;' and @local-name='archived-as' and not($context-nodes/ancestor::*[not(self::书社:apply-attributes-to-root or self::书社:apply-attributes)][1][self::书社:archive])"/>
148 <xslt:when test="@namespace-uri='&书社;' and (@local-name='destination' or @local-name='disable-output-wrapping' or @local-name='version')"/>
149 <xslt:when test="preceding-sibling::*[@local-name=current()/@local-name and @namespace-uri=current()/@namespace-uri]"/>
150 <xslt:otherwise>
151 <xslt:attribute>
152 <attribute name="name">
153 <text>{@name}</text>
154 </attribute>
155 <attribute name="namespace">
156 <text>{@namespace-uri}</text>
157 </attribute>
158 <xslt:for-each select="$existing-attributes[local-name()=current()/@local-name and namespace-uri()=current()/@namespace-uri]">
159 <xslt:value-of select="."/>
160 <xslt:if test="position()!=last()">
161 <xslt:text>
162 <text> </text>
163 </xslt:text>
164 </xslt:if>
165 </xslt:for-each>
166 <xslt:if test="$existing-attributes[local-name()=current()/@local-name and namespace-uri()=current()/@namespace-uri] and $additional-attributes[local-name()=current()/@local-name and namespace-uri()=current()/@namespace-uri]">
167 <xslt:text>
168 <text> </text>
169 </xslt:text>
170 </xslt:if>
171 <xslt:for-each select="$additional-attributes[local-name()=current()/@local-name and namespace-uri()=current()/@namespace-uri]">
172 <xslt:value-of select="."/>
173 <xslt:if test="position()!=last()">
174 <xslt:text>
175 <text> </text>
176 </xslt:text>
177 </xslt:if>
178 </xslt:for-each>
179 </xslt:attribute>
180 </xslt:otherwise>
181 </xslt:choose>
182 </xslt:for-each>
183 <xslt:copy-of select="node()"/>
184 </xslt:copy>
185 </xslt:when>
186 <xslt:otherwise>
187 <xslt:copy-of select="."/>
188 </xslt:otherwise>
189 </xslt:choose>
190 </xslt:for-each>
191 </xslt:template>
192 <xslt:template name="书社:wrap">
193 <xslt:param name="nodes" select="/.."/>
194 <xslt:variable name="modalinput-fragment">
195 <xslt:copy-of select="$nodes"/>
196 <apply-templates select="exsl:node-set($unique-ids)" mode="书社:literally"/>
197 </xslt:variable>
198 <xslt:variable name="modalinput" select="exsl:node-set($modalinput-fragment)"/>
199 <xslt:variable name="metadata-fragment">
200 <xslt:copy-of select="$nodes[self::html:html]/html:head/node()|$nodes[self::html:head]/node()"/>
201 <xslt:apply-templates select="$modalinput/node()" mode="书社:metadata"/>
202 </xslt:variable>
203 <xslt:variable name="metadata" select="exsl:node-set($metadata-fragment)"/>
204 <html:html>
205 <xslt:copy-of select="$nodes[self::html:html]/@*"/>
206 <xslt:if test="not($nodes[self::html:html]/@书社:archived-as) and $nodes/@书社:archived-as">
207 <xslt:attribute name="书社:archived-as">
208 <xslt:value-of select="$nodes/@书社:archived-as"/>
209 </xslt:attribute>
210 </xslt:if>
211 <xslt:if test="not($nodes[self::html:html]/@lang) and $nodes[self::html:*]/@lang|$nodes[self::svg:*]/@lang|$nodes/@xml:lang">
212 <xslt:attribute name="lang">
213 <xslt:value-of select="$nodes[self::html:*]/@lang|$nodes[self::svg:*]/@lang|$nodes/@xml:lang"/>
214 </xslt:attribute>
215 </xslt:if>
216 <xslt:if test="not($nodes[self::html:html]/@xml:lang) and $nodes[self::html:*]/@lang|$nodes[self::svg:*]/@lang|$nodes/@xml:lang">
217 <xslt:attribute name="xml:lang">
218 <xslt:value-of select="$nodes[self::html:*]/@lang|$nodes[self::svg:*]/@lang|$nodes/@xml:lang"/>
219 </xslt:attribute>
220 </xslt:if>
221 <html:head>
222 <xslt:copy-of select="$nodes[self::html:html]/html:head/@*|$nodes[self::html:head]/@*"/>
223 <html:title>
224 <xslt:for-each select="$metadata/html:title">
225 <xslt:value-of select="."/>
226 <xslt:if test="position()!=last()">
227 <xslt:text>
228 <text> </text>
229 </xslt:text>
230 </xslt:if>
231 </xslt:for-each>
232 </html:title>
233 <xslt:copy-of select="$metadata/node()[not(self::html:title or self::html:meta and @name='generator')]"/>
234 <html:meta name="generator">
235 <xslt:attribute name="content">
236 <xslt:for-each select="$metadata/html:meta[@name='generator']">
237 <xslt:value-of select="@content"/>
238 <xslt:text>
239 <text>, </text>
240 </xslt:text>
241 </xslt:for-each>
242 <xslt:text>
243 <text>⛩📰 书社</text>
244 </xslt:text>
245 <xslt:if test="$THISREV">
246 <xslt:text>
247 <text> (</text>
248 </xslt:text>
249 <xslt:value-of select="$THISREV"/>
250 <xslt:text>
251 <text>)</text>
252 </xslt:text>
253 </xslt:if>
254 </xslt:attribute>
255 </html:meta>
256 </html:head>
257 <html:body>
258 <xslt:copy-of select="$nodes[self::html:html]/html:body/@*|$nodes[self::html:body]/@*"/>
259 <xslt:apply-templates select="$modalinput/node()" mode="书社:header"/>
260 <xslt:copy-of select="$nodes[not(self::html:html or self::html:head or self::html:body)]|$nodes[self::html:html]/node()[not(self::html:head or self::html:body)]|$nodes[self::html:html]/html:body/node()|$nodes[self::html:body]/node()"/>
261 <xslt:apply-templates select="$modalinput/node()" mode="书社:footer"/>
262 </html:body>
263 </html:html>
264 </xslt:template>
265 <xslt:template match="/" priority="1">
266 <xslt:variable name="result-nodes" select="$书社:result/node()[not(self::书社:apply-attributes-to-root)]|$书社:result/书社:apply-attributes-to-root/descendant::node()[not(self::书社:apply-attributes-to-root) and not(ancestor::*[not(self::书社:apply-attributes-to-root)])]"/>
267 <xslt:variable name="root-with-attributes">
268 <xslt:choose>
269 <xslt:when test="not($result-nodes/self::html:*) or $result-nodes/self::书社:*[local-name()='raw-text' or local-name()='base64-binary' or local-name()='archive'] or $result-nodes/@书社:disable-output-wrapping or $书社:result//书社:apply-attributes-to-root/@书社:disable-output-wrapping">
270 <xslt:call-template name="书社:apply-attributes">
271 <xslt:with-param name="and-version" select="true()"/>
272 <xslt:with-param name="context-nodes" select="$书社:result//书社:apply-attributes-to-root"/>
273 <xslt:with-param name="destination-nodes" select="$result-nodes"/>
274 </xslt:call-template>
275 </xslt:when>
276 <xslt:otherwise>
277 <xslt:variable name="wrapped-result">
278 <xslt:call-template name="书社:wrap">
279 <xslt:with-param name="nodes" select="$result-nodes"/>
280 </xslt:call-template>
281 </xslt:variable>
282 <xslt:call-template name="书社:apply-attributes">
283 <xslt:with-param name="and-version" select="true()"/>
284 <xslt:with-param name="context-nodes" select="$书社:result//书社:apply-attributes-to-root"/>
285 <xslt:with-param name="destination-nodes" select="exsl:node-set($wrapped-result)/node()"/>
286 </xslt:call-template>
287 </xslt:otherwise>
288 </xslt:choose>
289 </xslt:variable>
290 <xslt:apply-templates select="exsl:node-set($root-with-attributes)/node()" mode="书社:apply"/>
291 </xslt:template>
292 <xslt:template match="@*|node()" priority="-1">
293 <xslt:copy>
294 <xslt:apply-templates select="@*|node()"/>
295 </xslt:copy>
296 </xslt:template>
297 <xslt:template match="@书社:destination|@书社:disable-output-wrapping|@书社:archived-as[../ancestor::*[not(self::书社:apply-attributes-to-root or self::书社:apply-attributes)]]" mode="书社:apply" priority="1"/>
298 <xslt:template match="书社:archive" mode="书社:apply" priority="1">
299 <xslt:copy>
300 <xslt:for-each select="@*">
301 <xslt:choose>
302 <xslt:when test="local-name()='archived-as' and namespace-uri()='&书社;' and ../ancestor::*[not(self::书社:apply-attributes-to-root or self::书社:apply-attributes)]"/>
303 <xslt:otherwise>
304 <xslt:apply-templates select="." mode="书社:apply"/>
305 </xslt:otherwise>
306 </xslt:choose>
307 </xslt:for-each>
308 <xslt:for-each select="node()">
309 <xslt:choose>
310 <xslt:when test="self::*">
311 <xslt:variable name="component">
312 <xslt:choose>
313 <xslt:when test="self::书社:*[local-name()='raw-text' or local-name()='base64-binary' or local-name()='archive']|@书社:disable-output-wrapping">
314 <xslt:copy-of select="."/>
315 </xslt:when>
316 <xslt:otherwise>
317 <xslt:call-template name="书社:wrap">
318 <xslt:with-param name="nodes" select="."/>
319 </xslt:call-template>
320 </xslt:otherwise>
321 </xslt:choose>
322 </xslt:variable>
323 <xslt:apply-templates select="exsl:node-set($component)/node()" mode="书社:apply"/>
324 </xslt:when>
325 <xslt:otherwise>
326 <xslt:apply-templates select="." mode="书社:apply"/>
327 </xslt:otherwise>
328 </xslt:choose>
329 </xslt:for-each>
330 </xslt:copy>
331 </xslt:template>
332 <xslt:template match="书社:apply-attributes" mode="书社:apply" priority="1">
333 <xslt:variable name="children">
334 <xslt:apply-templates mode="书社:apply"/>
335 </xslt:variable>
336 <xslt:call-template name="书社:apply-attributes">
337 <xslt:with-param name="context-nodes" select="."/>
338 <xslt:with-param name="destination-nodes" select="exsl:node-set($children)/node()"/>
339 </xslt:call-template>
340 </xslt:template>
341 <xslt:template match="书社:apply-attributes-to-root" mode="书社:apply" priority="1">
342 <xslt:apply-templates mode="书社:apply"/>
343 </xslt:template>
344 <xslt:template match="书社:link[@xlink:show='embed' and (not(@xlink:actuate) or @xlink:actuate='none')]" mode="书社:expand" priority="1">
345 <xslt:variable name="identifier" select="string(@xlink:href)"/>
346 <xslt:variable name="is-dir" select="substring($identifier, string-length($identifier))='/'"/>
347 <xslt:variable name="included" select="$书社:about//*[@rdf:about=$identifier or $is-dir and starts-with(@rdf:about, $identifier)]"/>
348 <xslt:choose>
349 <xslt:when test="$included">
350 <xslt:for-each select="$included">
351 <xslt:sort select="@rdf:about" data-type="text" lang="zxx" case-order="upper-first"/>
352 <xslt:variable name="uri" select="书社vocab:hasParsedFile/@nfo:fileUrl"/>
353 <xslt:variable name="expanded">
354 <xslt:apply-templates select="document($uri)" mode="书社:expand">
355 <xslt:with-param name="identifier" select="string(@rdf:about)"/>
356 </xslt:apply-templates>
357 </xslt:variable>
358 <xslt:for-each select="exsl:node-set($expanded)/node()">
359 <xslt:copy>
360 <xslt:choose>
361 <xslt:when test="self::html:*">
362 <xslt:attribute name="itemscope">itemscope</xslt:attribute>
363 <xslt:attribute name="itemtype">
364 <xslt:text>
365 <text>&书社;:embed</text>
366 </xslt:text>
367 <xslt:for-each select="exslstr:tokenize(@itemtype)/token[string()!='&书社;:document']">
368 <xslt:text>
369 <text> </text>
370 </xslt:text>
371 <xslt:value-of select="."/>
372 </xslt:for-each>
373 </xslt:attribute>
374 <xslt:copy-of select="@*[not(namespace-uri()='' and (local-name()='itemscope' or local-name()='itemtype'))]|node()"/>
375 </xslt:when>
376 <xslt:otherwise>
377 <xslt:apply-templates select="@*|node()" mode="书社:expand"/>
378 </xslt:otherwise>
379 </xslt:choose>
380 </xslt:copy>
381 </xslt:for-each>
382 </xslt:for-each>
383 </xslt:when>
384 <xslt:otherwise>
385 <xslt:copy>
386 <xslt:apply-templates select="@*|node()" mode="书社:expand"/>
387 </xslt:copy>
388 </xslt:otherwise>
389 </xslt:choose>
390 </xslt:template>
391 <xslt:template match="@*|node()" mode="书社:apply" priority="-1">
392 <xslt:copy>
393 <xslt:apply-templates select="@*|node()" mode="书社:apply"/>
394 </xslt:copy>
395 </xslt:template>
396 <xslt:template match="/node()" mode="书社:expand" priority="0">
397 <xslt:param name="identifier" select="$IDENTIFIER"/>
398 <xslt:copy>
399 <xslt:if test="self::*">
400 <xslt:attribute name="书社:cksum">
401 <xslt:value-of select="$书社:about//*[@rdf:about=$identifier]/nfo:hasHash[@nfo:hashAlgorithm='CRC32']/@nfo:hashValue"/>
402 </xslt:attribute>
403 <xslt:attribute name="书社:identifier">
404 <xslt:value-of select="$identifier"/>
405 </xslt:attribute>
406 <xslt:attribute name="书社:mtime">
407 <xslt:value-of select="$书社:about//*[@rdf:about=$identifier]/nfo:fileLastModified"/>
408 </xslt:attribute>
409 </xslt:if>
410 <xslt:choose>
411 <xslt:when test="self::html:*">
412 <xslt:attribute name="itemscope">itemscope</xslt:attribute>
413 <xslt:attribute name="itemtype">
414 <xslt:text>
415 <text>&书社;:document</text>
416 </xslt:text>
417 <xslt:for-each select="exslstr:tokenize(@itemtype)/token">
418 <xslt:text>
419 <text> </text>
420 </xslt:text>
421 <xslt:value-of select="."/>
422 </xslt:for-each>
423 </xslt:attribute>
424 <xslt:apply-templates select="@*[not(namespace-uri()='' and (local-name()='itemscope' or local-name()='itemtype') or namespace-uri()='&书社;' and (local-name()='cksum' or local-name()='identifier' or local-name()='mtime'))]|node()" mode="书社:expand"/>
425 </xslt:when>
426 <xslt:otherwise>
427 <xslt:apply-templates select="@*[not(namespace-uri()='&书社;' and (local-name()='cksum' or local-name()='identifier' or local-name()='mtime'))]|node()" mode="书社:expand"/>
428 </xslt:otherwise>
429 </xslt:choose>
430 </xslt:copy>
431 </xslt:template>
432 <xslt:template match="@*|node()" mode="书社:expand" priority="-1">
433 <xslt:copy>
434 <xslt:apply-templates select="@*|node()" mode="书社:expand"/>
435 </xslt:copy>
436 </xslt:template>
437 <xslt:template match="text()" mode="书社:header"/>
438 <xslt:template match="text()" mode="书社:footer"/>
439 <xslt:template match="text()" mode="书社:metadata"/>
440 <xslt:output method="xml" encoding="UTF-8" cdata-section-elements="html:script html:style html:textarea 书社:raw-output 书社:raw-text"/>
441 </xslt:transform>
442 </template>
443 <output method="xml" encoding="UTF-8"/>
444 </transform>
This page took 0.069205 seconds and 5 git commands to generate.