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