-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtransclusion-fixup.xsl
185 lines (166 loc) · 6.43 KB
/
transclusion-fixup.xsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:db="http://docbook.org/ns/docbook"
xmlns:f="http://nwalsh.com/ns/xslt/functions"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="db f xlink xs"
version="2.0">
<xsl:template match="element()">
<xsl:copy>
<xsl:apply-templates select="@*,node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@xml:id" priority="100">
<xsl:variable name="id" select="f:trans-id(.)"/>
<xsl:if test="exists($id)">
<xsl:attribute name="xml:id" select="$id"/>
</xsl:if>
</xsl:template>
<xsl:template match="@linkend|@xlink:href[starts-with(.,'#')]" priority="100">
<xsl:variable name="hash" select="if (starts-with(.,'#')) then '#' else ''"/>
<xsl:variable name="linkend"
select="if ($hash='#') then substring-after(., '#') else ."/>
<xsl:variable name="xiroot" select="(ancestor-or-self::*[@db:idfixup])[last()]"/>
<xsl:variable name="fixup" select="$xiroot/@db:idfixup"/>
<xsl:variable name="linkscope" select="$xiroot/@db:linkscope"/>
<xsl:choose>
<xsl:when test="empty($fixup) or $linkscope='user'">
<xsl:copy/>
</xsl:when>
<xsl:when test="($fixup='prefix' or $fixup='auto')
and ($linkscope='local' or empty($linkscope))">
<xsl:choose>
<xsl:when test="$fixup='auto'">
<xsl:variable name="id" select="concat('idf-', generate-id($xiroot), '-', .)"/>
<xsl:attribute name="{node-name(.)}" select="concat($hash, $id)"/>
</xsl:when>
<xsl:when test="$fixup='prefix'">
<xsl:variable name="id" select="concat($xiroot/@db:idprefix, .)"/>
<xsl:attribute name="{node-name(.)}" select="concat($hash, $id)"/>
</xsl:when>
<xsl:otherwise>
<xsl:message>
<xsl:text>Unrecognized db:idfixup value: </xsl:text>
<xsl:value-of select="$fixup"/>
</xsl:message>
<xsl:copy/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:when test="$linkscope='local'">
<xsl:message>
<xsl:text>Error: linkscope 'local' cannot be used with fixup '</xsl:text>
<xsl:value-of select="$fixup"/>
<xsl:text>'. Link ignored.</xsl:text>
</xsl:message>
<xsl:copy/>
</xsl:when>
<xsl:when test="$linkscope='near'">
<xsl:variable name="link" as="xs:string?">
<xsl:call-template name="f:trans-link">
<xsl:with-param name="id" select="."/>
<xsl:with-param name="targets"
select="reverse(preceding::*[@xml:id=$linkend])"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="exists($link)">
<xsl:attribute name="{node-name(.)}" select="concat($hash, $link)"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="link" as="xs:string?">
<xsl:call-template name="f:trans-link">
<xsl:with-param name="id" select="."/>
<xsl:with-param name="targets"
select="following::*[@xml:id=$linkend]"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="exists($link)">
<xsl:attribute name="{node-name(.)}" select="concat($hash, $link)"/>
</xsl:when>
<xsl:otherwise>
<xsl:message>
<xsl:text>Failed to find near target for link: </xsl:text>
<xsl:value-of select="$linkend"/>
</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<!-- assume $linkscope='global' -->
<xsl:variable name="link" as="xs:string?">
<xsl:call-template name="f:trans-link">
<xsl:with-param name="id" select="."/>
<xsl:with-param name="targets" select="//*[@xml:id=$linkend]"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="exists($link)">
<xsl:attribute name="{node-name(.)}" select="concat($hash, $link)"/>
</xsl:when>
<xsl:otherwise>
<xsl:message>
<xsl:text>Failed to find global target for link: </xsl:text>
<xsl:value-of select="$linkend"/>
</xsl:message>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="@db:idfixup|@db:idprefix|@db:linkscope"/>
<xsl:template match="attribute()|text()|comment()|processing-instruction()">
<xsl:copy/>
</xsl:template>
<!-- ============================================================ -->
<xsl:function name="f:trans-id" as="xs:string?">
<xsl:param name="id" as="attribute(xml:id)"/>
<xsl:variable name="xiroot" select="($id/ancestor-or-self::*[@db:idfixup])[last()]"/>
<xsl:variable name="fixup" select="$xiroot/@db:idfixup"/>
<xsl:choose>
<xsl:when test="empty($fixup) or $fixup='none'">
<xsl:value-of select="$id"/>
</xsl:when>
<xsl:when test="$fixup='strip'">
<xsl:if test="$xiroot is $id/parent::*">
<xsl:value-of select="$id"/>
</xsl:if>
</xsl:when>
<xsl:when test="$fixup='auto'">
<xsl:value-of select="concat('idf-', generate-id($xiroot), '-', $id)"/>
</xsl:when>
<xsl:when test="$fixup='prefix'">
<xsl:value-of select="concat($xiroot/@db:idprefix, $id)"/>
</xsl:when>
<xsl:otherwise>
<xsl:message>
<xsl:text>Unrecognized db:idfixup value: </xsl:text>
<xsl:value-of select="$fixup"/>
</xsl:message>
<xsl:value-of select="$id"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:template name="f:trans-link" as="xs:string?">
<xsl:param name="id" required="yes" as="xs:string"/>
<xsl:param name="targets" required="yes" as="element()*"/>
<xsl:if test="exists($targets)">
<xsl:variable name="link" select="f:trans-id($targets[1]/@xml:id)"/>
<xsl:choose>
<xsl:when test="empty($link)">
<xsl:call-template name="f:trans-link">
<xsl:with-param name="id" select="$id"/>
<xsl:with-param name="targets" select="$targets[position() > 1]"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$link"/>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
</xsl:stylesheet>