Debian Codegraph @

xslt/mallard/common/mal-sort.xsl

raw
1 <?xml version='1.0' encoding='UTF-8'?><!-- -*- indent-tabs-mode: nil -*- -->
2 <!--
3 This program is free software; you can redistribute it and/or modify it under
4 the terms of the GNU Lesser General Public License as published by the Free
5 Software Foundation; either version 2 of the License, or (at your option) any
6 later version.
7
8 This program is distributed in the hope that it will be useful, but WITHOUT
9 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11 details.
12
13 You should have received a copy of the GNU Lesser General Public License
14 along with this program; see the file COPYING.LGPL. If not, see <http://www.gnu.org/licenses/>.
15 -->
16
17 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
18 xmlns:cache="http://projectmallard.org/cache/1.0/"
19 xmlns:mal="http://projectmallard.org/1.0/"
20 xmlns:exsl="http://exslt.org/common"
21 extension-element-prefixes="exsl"
22 exclude-result-prefixes="mal"
23 version="1.0">
24
25 <!--!!==========================================================================
26 Mallard Topological Sort
27 Sort a Mallard document.
28 @revision[version=1.0 date=2010-07-08]
29
30 This stylesheet contains utilities for sorting the pages in a Mallard
31 document based on their informational links.
32 -->
33
34
35 <!--**==========================================================================
36 mal.sort.tsort
37 Sort pages based on topic and next links.
38 @revision[version=1.0 date=2010-07-08]
39
40 [xsl:params]
41 $node: The current `page` in the Mallard cache file.
42
43 This template outputs links to pages sorted according to their topic and
44 next links. Pages occur after the first guide that references them, in
45 their sort order for that guide. Page series constructed with next links
46 always appear in order at the sort position of their first page.
47
48 This template outputs `link` elements with `xref` attributes pointing to
49 the target page. The output is a result tree fragment. To use these results,
50 call `exsl:node-set` on them.
51
52 You can specify a starting node with the $node parameter. By default, it
53 uses the node pointed to by {mal.link.default_root}.
54
55 This template does not include any nodes that are not reachable through
56 topic or next links.
57 -->
58 <xsl:template name="mal.sort.tsort">
59 <xsl:param name="node" select="key('mal.cache.key', $mal.link.default_root)"/>
60 <xsl:variable name="sorted">
61 <xsl:call-template name="_mal.sort.tsort.node">
62 <xsl:with-param name="node" select="$node"/>
63 </xsl:call-template>
64 </xsl:variable>
65 <xsl:variable name="nodes" select="exsl:node-set($sorted)/mal:link"/>
66 <xsl:for-each select="$nodes">
67 <xsl:variable name="xref" select="@xref"/>
68 <xsl:if test="not(preceding::*[string(@xref) = $xref]) and not(contains($xref, '#'))">
69 <xsl:copy-of select="."/>
70 </xsl:if>
71 </xsl:for-each>
72 </xsl:template>
73
74 <!--#* _mal.sort.tsort.node -->
75 <xsl:template name="_mal.sort.tsort.node">
76 <xsl:param name="node" select="key('mal.cache.key', $mal.link.default_root)"/>
77 <xsl:param name="done" select="''"/>
78 <xsl:variable name="linkid">
79 <xsl:call-template name="mal.link.linkid">
80 <xsl:with-param name="node" select="$node"/>
81 </xsl:call-template>
82 </xsl:variable>
83
84 <mal:link xref="{$linkid}"/>
85
86 <xsl:variable name="next" select="$node/mal:info/mal:link[@type = 'next']"/>
87 <xsl:if test="$next">
88 <xsl:variable name="linklinkid">
89 <xsl:call-template name="mal.link.xref.linkid">
90 <xsl:with-param name="node" select="$next"/>
91 </xsl:call-template>
92 </xsl:variable>
93 <xsl:if test="$linklinkid != '' and not(contains($done, concat(' ', $linklinkid, ' ')))">
94 <xsl:variable name="nextnode" select="key('mal.cache.key', $linklinkid)"/>
95 <xsl:call-template name="_mal.sort.tsort.node">
96 <xsl:with-param name="node" select="$nextnode"/>
97 <xsl:with-param name="done" select="concat($done, ' ', $linkid, ' ')"/>
98 </xsl:call-template>
99 </xsl:if>
100 </xsl:if>
101
102 <xsl:variable name="page" select="document($node/@cache:href)"/>
103
104 <xsl:variable name="topics">
105 <xsl:for-each select="$node | $node//mal:section">
106 <xsl:variable name="positionsort" select="position()"/>
107 <xsl:variable name="groups">
108 <xsl:choose>
109 <xsl:when test="self::mal:page">
110 <xsl:call-template name="_mal.sort.getgroups">
111 <xsl:with-param name="node" select="$page/mal:page"/>
112 </xsl:call-template>
113 </xsl:when>
114 <xsl:otherwise>
115 <xsl:variable name="sectid">
116 <xsl:value-of select="substring-after(@id, '#')"/>
117 </xsl:variable>
118 <xsl:call-template name="_mal.sort.getgroups">
119 <xsl:with-param name="node" select="$page//mal:section[@id = $sectid]"/>
120 </xsl:call-template>
121 </xsl:otherwise>
122 </xsl:choose>
123 </xsl:variable>
124 <xsl:variable name="subtopics">
125 <xsl:call-template name="mal.link.topiclinks">
126 <xsl:with-param name="node" select="."/>
127 <xsl:with-param name="groups" select="$groups"/>
128 </xsl:call-template>
129 </xsl:variable>
130 <xsl:for-each select="exsl:node-set($subtopics)/*">
131 <xsl:copy>
132 <xsl:attribute name="positionsort">
133 <xsl:value-of select="$positionsort"/>
134 </xsl:attribute>
135 <xsl:for-each select="@* | node()">
136 <xsl:copy-of select="."/>
137 </xsl:for-each>
138 </xsl:copy>
139 </xsl:for-each>
140 </xsl:for-each>
141 </xsl:variable>
142 <xsl:variable name="topicnodes" select="exsl:node-set($topics)/*"/>
143 <xsl:variable name="newdone">
144 <xsl:value-of select="$done"/>
145 <xsl:for-each select="$topicnodes">
146 <xsl:variable name="linklinkid">
147 <xsl:call-template name="mal.link.xref.linkid"/>
148 </xsl:variable>
149 <xsl:if test="$linklinkid != ''">
150 <xsl:value-of select="concat(' ', $linklinkid)"/>
151 </xsl:if>
152 </xsl:for-each>
153 <xsl:text> </xsl:text>
154 </xsl:variable>
155 <xsl:for-each select="$topicnodes">
156 <xsl:sort data-type="number" select="@positionsort"/>
157 <xsl:sort data-type="number" select="@groupsort"/>
158 <xsl:sort select="mal:title[@type = 'sort']"/>
159 <xsl:variable name="linklinkid">
160 <xsl:call-template name="mal.link.xref.linkid"/>
161 </xsl:variable>
162 <xsl:if test="$linklinkid != '' and not(contains($done, concat(' ', $linklinkid, ' ')))">
163 <xsl:for-each select="$mal.cache">
164 <xsl:variable name="topic" select="key('mal.cache.key', $linklinkid)"/>
165 <xsl:if test="$topic">
166 <xsl:call-template name="_mal.sort.tsort.node">
167 <xsl:with-param name="node" select="$topic"/>
168 <xsl:with-param name="done" select="$newdone"/>
169 </xsl:call-template>
170 </xsl:if>
171 </xsl:for-each>
172 </xsl:if>
173 </xsl:for-each>
174 </xsl:template>
175
176 <!--#* _mal.sort.getgroups -->
177 <xsl:template name="_mal.sort.getgroups">
178 <xsl:param name="node" select="."/>
179 <xsl:variable name="groups">
180 <xsl:text> </xsl:text>
181 <xsl:choose>
182 <xsl:when test="$node/mal:links[@type = 'topic']">
183 <xsl:for-each select="$node/mal:links[@type = 'topic']">
184 <xsl:text> </xsl:text>
185 <xsl:value-of select="@groups"/>
186 </xsl:for-each>
187 </xsl:when>
188 <xsl:otherwise>
189 <xsl:value-of select="$node/@groups"/>
190 </xsl:otherwise>
191 </xsl:choose>
192 <xsl:text> </xsl:text>
193 </xsl:variable>
194 <xsl:if test="not(contains($groups, ' #first '))">
195 <xsl:text> #first </xsl:text>
196 </xsl:if>
197 <xsl:value-of select="$groups"/>
198 <xsl:if test="not(contains($groups, ' #default '))">
199 <xsl:text> #default </xsl:text>
200 </xsl:if>
201 <xsl:if test="not(contains($groups, ' #last '))">
202 <xsl:text> #last </xsl:text>
203 </xsl:if>
204 </xsl:template>
205
206 </xsl:stylesheet>
207