]> Lady’s Gitweb - Shushe/blob - lib/expandmetadata.xslt
Fix/improve recursive dependency detection
[Shushe] / lib / expandmetadata.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/expandmetadata.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 <transform
15 xmlns="http://www.w3.org/1999/XSL/Transform"
16 xmlns:exsl="http://exslt.org/common"
17 xmlns:nie="http://www.semanticdesktop.org/ontologies/2007/01/19/nie#"
18 xmlns:nfo="http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#"
19 xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
20 xmlns:xlink="http://www.w3.org/1999/xlink"
21 xmlns:书社="urn:fdc:ladys.computer:20231231:Shu1She4"
22 xmlns:书社vocab="urn:fdc:ladys.computer:20231231:Shu1She4:vocab:"
23 extension-element-prefixes="exsl"
24 version="1.0"
25 >
26 <variable name="builddir" select="//书社vocab:BuildDirectory/@nfo:fileUrl"/>
27 <variable name="files" select="//书社vocab:SourceFile|//书社vocab:IncludeFile"/>
28 <template name="书社:collect-dependency-paths">
29 <param name="path" select="/.."/>
30 <variable name="parent" select="string($path/*[last()])"/>
31 <variable name="children-fragment">
32 <for-each select="$files[@rdf:about=$parent and nie:interpretedAs/nfo:PlainTextDocument]">
33 <variable name="uri" select="书社vocab:hasParsedFile/@nfo:fileUrl"/>
34 <for-each select="document($uri)//书社:link[@xlink:show='embed']">
35 <variable name="identifier" select="string(@xlink:href)"/>
36 <variable name="is-dir" select="substring($identifier, string-length($identifier))='/'"/>
37 <for-each select="$files/@rdf:about[string(.)=$identifier or $is-dir and starts-with(., $identifier)]">
38 <sort select="." data-type="text" lang="zxx" case-order="upper-first"/>
39 <choose>
40 <when test="$path/*[string(.)=string(current())]">
41 <书社:recursive-dependency>
42 <value-of select="."/>
43 </书社:recursive-dependency>
44 </when>
45 <otherwise>
46 <书社:dependency>
47 <value-of select="."/>
48 </书社:dependency>
49 </otherwise>
50 </choose>
51 </for-each>
52 </for-each>
53 </for-each>
54 </variable>
55 <variable name="children" select="exsl:node-set($children-fragment)"/>
56 <choose>
57 <when test="$children/*">
58 <for-each select="$children/书社:recursive-dependency">
59 <书社:dependency-path>
60 <copy-of select="$path[string(.)=string(current())]/preceding-sibling::*"/>
61 <copy-of select="."/>
62 </书社:dependency-path>
63 </for-each>
64 <for-each select="$children/书社:dependency">
65 <variable name="path-fragment">
66 <copy-of select="$path"/>
67 <copy-of select="."/>
68 </variable>
69 <call-template name="书社:collect-dependency-paths">
70 <with-param name="path" select="exsl:node-set($path-fragment)"/>
71 </call-template>
72 </for-each>
73 </when>
74 <otherwise>
75 <书社:dependency-path>
76 <copy-of select="$path"/>
77 </书社:dependency-path>
78 </otherwise>
79 </choose>
80 </template>
81 <template match="//书社vocab:SourceFile[not(nie:interpretedAs/nfo:PlainTextDocument)]" priority="1">
82 <copy>
83 <apply-templates select="@*"/>
84 <attribute name="书社vocab:destination">
85 <value-of select="@书社vocab:path"/>
86 </attribute>
87 <apply-templates select="node()"/>
88 </copy>
89 </template>
90 <template match="//书社vocab:SourceFile[nie:interpretedAs/nfo:PlainTextDocument]" priority="1">
91 <variable name="parsed" select="document(书社vocab:hasParsedFile/@nfo:fileUrl)"/>
92 <variable name="destination-delim" select="concat('/', $parsed/*/@书社:destination, '/')"/>
93 <variable name="destination">
94 <choose>
95 <when test="not(contains($destination-delim, '//') or contains($destination-delim, '/./') or contains($destination-delim, '/../'))">
96 <value-of select="$parsed/*/@书社:destination"/>
97 </when>
98 <otherwise>
99 <value-of select="@书社vocab:path"/>
100 </otherwise>
101 </choose>
102 </variable>
103 <variable name="dependencies-fragment">
104 <apply-templates select="." mode="书社:dependencies"/>
105 </variable>
106 <variable name="dependencies" select="exsl:node-set($dependencies-fragment)"/>
107 <copy>
108 <apply-templates select="@*"/>
109 <attribute name="书社vocab:destination">
110 <value-of select="$destination"/>
111 </attribute>
112 <apply-templates select="node()"/>
113 <for-each select="$dependencies/书社:dependency">
114 <书社vocab:hasDependencyOn rdf:resource="{.}"/>
115 </for-each>
116 <for-each select="$dependencies/书社:recursive-dependency">
117 <书社vocab:hasRecursiveDependencyOn rdf:resource="{.}"/>
118 </for-each>
119 </copy>
120 </template>
121 <template match="//书社vocab:IncludeFile[nie:interpretedAs/nfo:PlainTextDocument]" priority="1">
122 <variable name="dependencies-fragment">
123 <apply-templates select="." mode="书社:dependencies"/>
124 </variable>
125 <variable name="dependencies" select="exsl:node-set($dependencies-fragment)"/>
126 <copy>
127 <apply-templates select="@*|node()"/>
128 <for-each select="$dependencies/书社:dependency">
129 <书社vocab:hasDependencyOn rdf:resource="{.}"/>
130 </for-each>
131 <for-each select="$dependencies/书社:recursive-dependency">
132 <书社vocab:hasRecursiveDependencyOn rdf:resource="{.}"/>
133 </for-each>
134 </copy>
135 </template>
136 <template match="/">
137 <variable name="result-fragment">
138 <apply-templates/>
139 </variable>
140 <variable name="result" select="exsl:node-set($result-fragment)"/>
141 <copy-of select="$result/*"/>
142 <exsl:document href="dependencies" method="text" encoding="UTF-8">
143 <for-each select="$result/rdf:RDF/*[nie:interpretedAs/nfo:PlainTextDocument]">
144 <value-of select="@rdf:about"/>
145 <text>&#xA;</text>
146 <for-each select="书社vocab:hasRecursiveDependencyOn/@rdf:resource">
147 <text>&#x9;</text>
148 <text>-</text>
149 <value-of select="."/>
150 <text>&#xA;</text>
151 </for-each>
152 <for-each select="书社vocab:hasDependencyOn/@rdf:resource">
153 <text>&#x9;</text>
154 <value-of select="."/>
155 <text>&#xA;</text>
156 </for-each>
157 </for-each>
158 </exsl:document>
159 <exsl:document href="destinations" method="text" encoding="UTF-8">
160 <for-each select="$result//书社vocab:SourceFile">
161 <value-of select="@rdf:about"/>
162 <text>|</text>
163 <value-of select="@书社vocab:destination"/>
164 <text>&#xA;</text>
165 </for-each>
166 </exsl:document>
167 </template>
168 <template match="@*|node()">
169 <copy>
170 <apply-templates select="@*|node()"/>
171 </copy>
172 </template>
173 <template match="书社vocab:SourceFile|书社vocab:IncludeFile" mode="书社:dependencies">
174 <if test="nie:interpretedAs/nfo:PlainTextDocument">
175 <variable name="path-fragment">
176 <书社:dependency-root>
177 <value-of select="@rdf:about"/>
178 </书社:dependency-root>
179 </variable>
180 <variable name="paths-fragment">
181 <call-template name="书社:collect-dependency-paths">
182 <with-param name="path" select="exsl:node-set($path-fragment)"/>
183 </call-template>
184 </variable>
185 <for-each select="exsl:node-set($paths-fragment)/*/*[not(self::书社:dependency-root)]">
186 <if test="not(preceding::*[string(.)=string(current())])">
187 <copy-of select="."/>
188 </if>
189 </for-each>
190 </if>
191 </template>
192 <output method="xml" encoding="UTF-8"/>
193 </transform>
This page took 0.057135 seconds and 5 git commands to generate.