Debian Codegraph @

xsl/common/related-links.xsl

raw symbols SCIP
1 <?xml version="1.0" encoding="UTF-8"?>
2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
3 xmlns:related-links="http://dita-ot.sourceforge.net/ns/200709/related-links"
4 exclude-result-prefixes="related-links">
5
6 <!-- Ungrouped links have a priority of zero. (Can be overridden.) -->
7 <xsl:template match="*[contains(@class, ' topic/link ')]" mode="related-links:get-group-priority"
8 name="related-links:group-priority.">
9 <xsl:value-of select="0"/>
10 </xsl:template>
11
12 <!-- Ungrouped links belong to the no-name group. (Can be overridden.) -->
13 <xsl:template match="*[contains(@class, ' topic/link ')]" mode="related-links:get-group" name="related-links:group.">
14 <xsl:text/>
15 </xsl:template>
16
17 <!-- Without a group, links are emitted as-is. (Can be overridden.) -->
18 <xsl:template match="*[contains(@class, ' topic/link ')]" mode="related-links:result-group" name="related-links:group-result.">
19 <xsl:param name="links"/>
20 <xsl:copy-of select="$links"/>
21 </xsl:template>
22
23 <!-- Ungrouped links have the default-mode template applied to them. (Can be overridden.) -->
24 <xsl:template match="*[contains(@class, ' topic/link ')]" mode="related-links:link" name="related-links:link.">
25 <xsl:apply-templates select="."/>
26 </xsl:template>
27
28 <!-- Main entry point. -->
29 <xsl:template match="*[contains(@class, ' topic/related-links ')]" mode="related-links:group-unordered-links">
30 <!-- Node set. The set of nodes to group. -->
31 <xsl:param name="nodes"/>
32 <!-- Sent back to all callback templates as a parameter.-->
33 <xsl:param name="tunnel"/>
34
35 <!-- Query all links for their group and priority. -->
36 <xsl:variable name="group-priorities">
37 <xsl:call-template name="related-links:get-priorities">
38 <xsl:with-param name="nodes" select="$nodes"/>
39 <xsl:with-param name="tunnel" select="$tunnel"/>
40 </xsl:call-template>
41 </xsl:variable>
42
43 <!-- Get order of groups based on priorities. -->
44 <xsl:variable name="group-sequence">
45 <xsl:call-template name="related-links:get-sequence-from-priorities">
46 <xsl:with-param name="priorities" select="$group-priorities"/>
47 <xsl:with-param name="tunnel" select="$tunnel"/>
48 </xsl:call-template>
49 </xsl:variable>
50
51 <!-- Process the links in each group in order. -->
52 <xsl:call-template name="related-links:walk-groups">
53 <xsl:with-param name="nodes" select="$nodes"/>
54 <xsl:with-param name="group-sequence" select="$group-sequence"/>
55 <xsl:with-param name="tunnel" select="$tunnel"/>
56 </xsl:call-template>
57 </xsl:template>
58
59 <!-- Get the priorities and groups of every link. -->
60 <!-- Produces a string like "2 task task ;3 concept concept ;1 reference reference ;0 topic ;",
61 where the numbers are priorities of each group, and the space-delimited words
62 are the groups and link types (link/@type) which belong to that group. -->
63 <xsl:template name="related-links:get-priorities">
64 <xsl:param name="nodes"/>
65 <xsl:param name="tunnel"/>
66 <xsl:param name="partial-result" select="''"/>
67
68 <xsl:choose>
69 <xsl:when test="count($nodes)">
70 <!-- Process each node one at a time. -->
71 <xsl:variable name="node" select="$nodes[1]"/>
72 <xsl:variable name="node-group">
73 <xsl:apply-templates select="$node" mode="related-links:get-group">
74 <xsl:with-param name="tunnel" select="$tunnel"/>
75 </xsl:apply-templates>
76 </xsl:variable>
77 <xsl:variable name="node-priorty">
78 <xsl:apply-templates select="$node" mode="related-links:get-group-priority">
79 <xsl:with-param name="tunnel" select="$tunnel"/>
80 </xsl:apply-templates>
81 </xsl:variable>
82 <xsl:call-template name="related-links:get-priorities">
83 <xsl:with-param name="nodes" select="$nodes[position() != 1]"/>
84 <xsl:with-param name="tunnel" select="$tunnel"/>
85 <xsl:with-param name="partial-result">
86 <xsl:choose>
87 <!-- This type has already been seen. -->
88 <xsl:when
89 test="contains($partial-result, concat(' ', $node-group, ' '))
90 and contains($partial-result, concat(' ', $node/@type, ' '))">
91 <xsl:value-of select="$partial-result"/>
92 </xsl:when>
93 <!-- This type has not been seen, but the base group has. -->
94 <xsl:when test="contains($partial-result, concat(' ', $node-group, ' '))">
95 <xsl:value-of select="substring-before($partial-result, concat(' ', $node-group, ' '))"/>
96 <xsl:text> </xsl:text>
97 <xsl:value-of select="$node-group"/>
98 <xsl:text> </xsl:text>
99 <xsl:value-of select="$node/@type"/>
100 <xsl:text> </xsl:text>
101 <xsl:value-of select="substring-after($partial-result, concat(' ', $node-group, ' '))"/>
102 </xsl:when>
103 <!-- Never seen this base group before (nor the type). -->
104 <xsl:otherwise>
105 <xsl:value-of select="$partial-result"/>
106 <xsl:value-of select="$node-priorty"/>
107 <xsl:text> </xsl:text>
108 <xsl:value-of select="$node-group"/>
109 <xsl:if test="$node-group != $node/@type">
110 <xsl:text> </xsl:text>
111 <xsl:value-of select="$node/@type"/>
112 </xsl:if>
113 <xsl:text> </xsl:text>
114 <xsl:text>;</xsl:text>
115 </xsl:otherwise>
116 </xsl:choose>
117 </xsl:with-param>
118 </xsl:call-template>
119 </xsl:when>
120 <xsl:otherwise>
121 <xsl:value-of select="$partial-result"/>
122 </xsl:otherwise>
123 </xsl:choose>
124 </xsl:template>
125
126 <!-- Sort groups according to their priorities, removing duplicates. -->
127 <!-- Takes a string returned by related-links:get-priorities and returns
128 the groups and link types in decreasing order of priority
129 (e.g., "concept concept;task task;reference reference; topic;"). -->
130 <xsl:template name="related-links:get-sequence-from-priorities">
131 <xsl:param name="priorities"/>
132 <xsl:param name="tunnel"/>
133 <xsl:param name="partial-result"/>
134
135 <xsl:choose>
136 <xsl:when test="contains($priorities, ';')">
137 <xsl:call-template name="related-links:get-best-priority-in-sequence">
138 <xsl:with-param name="priorities" select="$priorities"/>
139 <xsl:with-param name="tunnel" select="$tunnel"/>
140 <xsl:with-param name="partial-result" select="$partial-result"/>
141 </xsl:call-template>
142 </xsl:when>
143 <xsl:otherwise>
144 <xsl:value-of select="$partial-result"/>
145 </xsl:otherwise>
146 </xsl:choose>
147 </xsl:template>
148
149 <!-- Find the highest-priority group remaining in the list of priorities. -->
150 <xsl:template name="related-links:get-best-priority-in-sequence">
151 <xsl:param name="priorities"/>
152 <xsl:param name="tunnel"/>
153 <xsl:param name="partial-result"/>
154 <xsl:param name="best-group" select="'#none#'"/>
155 <xsl:param name="best-priority" select="-1"/>
156 <xsl:param name="lesser-priorities"/>
157
158 <xsl:choose>
159 <xsl:when test="contains($priorities, ';')">
160 <xsl:choose>
161 <!-- First group always wins. -->
162 <xsl:when test="$best-group = '#none#'">
163 <xsl:call-template name="related-links:get-best-priority-in-sequence">
164 <xsl:with-param name="priorities" select="substring-after($priorities, ';')"/>
165 <xsl:with-param name="tunnel" select="$tunnel"/>
166 <xsl:with-param name="partial-result" select="$partial-result"/>
167 <xsl:with-param name="best-priority" select="substring-before(substring-before($priorities, ';'), ' ')"/>
168 <xsl:with-param name="best-group" select="substring-after(substring-before($priorities, ';'), ' ')"/>
169 <xsl:with-param name="lesser-priorities" select="$lesser-priorities"/>
170 </xsl:call-template>
171 </xsl:when>
172 <!-- Higher-priority group found; shunt best-so-far to lesser priorities and continue. -->
173 <xsl:when test="substring-before(substring-before($priorities, ';'), ' ') > $best-priority">
174 <xsl:call-template name="related-links:get-best-priority-in-sequence">
175 <xsl:with-param name="priorities" select="substring-after($priorities, ';')"/>
176 <xsl:with-param name="tunnel" select="$tunnel"/>
177 <xsl:with-param name="partial-result" select="$partial-result"/>
178 <xsl:with-param name="best-priority" select="substring-before(substring-before($priorities, ';'), ' ')"/>
179 <xsl:with-param name="best-group" select="substring-after(substring-before($priorities, ';'), ' ')"/>
180 <xsl:with-param name="lesser-priorities"
181 select="concat($lesser-priorities, $best-priority, ' ', $best-group, ';')"/>
182 </xsl:call-template>
183 </xsl:when>
184 <!-- Best-so-far priority is still supreme. -->
185 <xsl:otherwise>
186 <xsl:call-template name="related-links:get-best-priority-in-sequence">
187 <xsl:with-param name="priorities" select="substring-after($priorities, ';')"/>
188 <xsl:with-param name="tunnel" select="$tunnel"/>
189 <xsl:with-param name="partial-result" select="$partial-result"/>
190 <xsl:with-param name="best-priority" select="$best-priority"/>
191 <xsl:with-param name="best-group" select="$best-group"/>
192 <xsl:with-param name="lesser-priorities"
193 select="concat($lesser-priorities, substring-before($priorities, ';'), ';')"/>
194 </xsl:call-template>
195 </xsl:otherwise>
196 </xsl:choose>
197 </xsl:when>
198 <!-- Best priority found. -->
199 <xsl:otherwise>
200 <xsl:call-template name="related-links:get-sequence-from-priorities">
201 <xsl:with-param name="priorities" select="$lesser-priorities"/>
202 <xsl:with-param name="tunnel" select="$tunnel"/>
203 <xsl:with-param name="partial-result">
204 <xsl:choose>
205 <!-- Duplicate; just move on. (Should not happen.) -->
206 <xsl:when test="contains(concat(';', $partial-result), concat(';', $best-group, ';'))">
207 <xsl:value-of select="$partial-result"/>
208 </xsl:when>
209 <!-- Add group to list and move on. -->
210 <xsl:otherwise>
211 <xsl:value-of select="concat($partial-result, $best-group, ';')"/>
212 </xsl:otherwise>
213 </xsl:choose>
214 </xsl:with-param>
215 </xsl:call-template>
216 </xsl:otherwise>
217 </xsl:choose>
218 </xsl:template>
219
220 <!-- Process each group in turn. -->
221 <xsl:template name="related-links:walk-groups">
222 <xsl:param name="nodes" select="/.."/>
223 <xsl:param name="tunnel"/>
224 <xsl:param name="group-sequence" select="''"/>
225
226 <xsl:choose>
227 <xsl:when test="contains($group-sequence, ';')">
228 <xsl:call-template name="related-links:do-group">
229 <xsl:with-param name="nodes" select="$nodes"/>
230 <xsl:with-param name="tunnel" select="$tunnel"/>
231 <xsl:with-param name="group" select="substring-before($group-sequence, ';')"/>
232 </xsl:call-template>
233 <xsl:call-template name="related-links:walk-groups">
234 <xsl:with-param name="nodes" select="$nodes"/>
235 <xsl:with-param name="tunnel" select="$tunnel"/>
236 <xsl:with-param name="group-sequence" select="substring-after($group-sequence, ';')"/>
237 </xsl:call-template>
238 </xsl:when>
239 </xsl:choose>
240 </xsl:template>
241
242 <!-- Process each group. -->
243 <xsl:template name="related-links:do-group">
244 <xsl:param name="nodes" select="/.."/>
245 <xsl:param name="tunnel"/>
246 <xsl:param name="group"/>
247
248 <!-- Process the links belonging to that group. -->
249 <xsl:variable name="group-nodes" select="$nodes[contains(concat(' ', $group), concat(' ', @type, ' '))]"/>
250 <!-- Let the group wrap all its links in additional elements. -->
251 <xsl:apply-templates select="$group-nodes[1]" mode="related-links:result-group">
252 <xsl:with-param name="links">
253 <!-- Pass links as result-tree fragments, ordered according to closeness of role. -->
254 <xsl:apply-templates select="$group-nodes" mode="related-links:link">
255 <xsl:sort
256 select="
257 10 * number(@role = 'parent') +
258 9 * number(@role = 'ancestor') +
259 8 * number(@role = 'child') +
260 7 * number(@role = 'descendant') +
261 6 * number(@role = 'next') +
262 5 * number(@role = 'previous') +
263 4 * number(@role = 'sibling') +
264 3 * number(@role = 'cousin') +
265 2 * number(@role = 'friend') +
266 1 * number(@role = 'other')"
267 data-type="number" order="descending"/>
268 <!-- All @role='other' have to go together, darn. -->
269 <xsl:sort select="@otherrole" data-type="text"/>
270 <xsl:with-param name="tunnel" select="$tunnel"/>
271 </xsl:apply-templates>
272 </xsl:with-param>
273 <xsl:with-param name="tunnel" select="$tunnel"/>
274 </xsl:apply-templates>
275
276 </xsl:template>
277
278 </xsl:stylesheet>