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