]> git.stg.codes - stg.git/blob - doc/xslt/website/olink.xsl
Moved common code to a single file.
[stg.git] / doc / xslt / website / olink.xsl
1 <?xml version="1.0"?>
2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3                 version="1.0">
4
5 <xsl:param name="website.database.document"
6            select="'website.database.xml'"/>
7
8 <xsl:template match="olink">
9   <xsl:choose>
10     <xsl:when test="@targetdoc != '' or @targetptr != ''">
11       <xsl:apply-imports/>
12     </xsl:when>
13     <xsl:otherwise>
14       <xsl:call-template name="olink-entity"/>
15     </xsl:otherwise>
16   </xsl:choose>
17 </xsl:template>
18
19 <xsl:template name="olink-entity">
20   <xsl:variable name="xmlfile"
21                 select="document(unparsed-entity-uri(@targetdocent),$autolayout)"/>
22   <xsl:variable name="webpage"
23                 select="$xmlfile/webpage"/>
24   <xsl:variable name="tocentry"
25                 select="$autolayout//*[$webpage/@id=@id]"/>
26
27   <xsl:variable name="dir">
28     <xsl:choose>
29       <xsl:when test="starts-with($tocentry/@dir, '/')">
30         <xsl:value-of select="substring($tocentry/@dir, 2)"/>
31       </xsl:when>
32       <xsl:otherwise>
33         <xsl:value-of select="$tocentry/@dir"/>
34       </xsl:otherwise>
35     </xsl:choose>
36   </xsl:variable>
37
38 <!-- debug
39   <xsl:message>Olink for <xsl:value-of select="unparsed-entity-uri(@targetdocent)"/></xsl:message>
40   <xsl:message>Page id <xsl:value-of select="$webpage/@id"/></xsl:message>
41 -->
42
43   <xsl:choose>
44     <xsl:when test="@type = 'embed'">
45       <xsl:apply-templates select="$xmlfile"/>
46     </xsl:when>
47     <xsl:otherwise>
48       <!-- @type = 'replace' or @type = 'new' -->
49       <a>
50         <xsl:if test="@id">
51           <xsl:attribute name="name">
52             <xsl:value-of select="@id"/>
53           </xsl:attribute>
54         </xsl:if>
55
56 <!-- debug
57         <xsl:message>
58           <xsl:text>href: </xsl:text>
59           <xsl:call-template name="root-rel-path"/>
60           <xsl:text>::</xsl:text>
61           <xsl:value-of select="$dir"/>
62           <xsl:text>::</xsl:text>
63           <xsl:value-of select="$filename-prefix"/>
64           <xsl:text>::</xsl:text>
65           <xsl:value-of select="$tocentry/@filename"/>
66           <xsl:text>::</xsl:text>
67           <xsl:if test="@localinfo">
68             <xsl:text>#</xsl:text>
69             <xsl:value-of select="@localinfo"/>
70           </xsl:if>
71         </xsl:message>
72 -->
73
74         <xsl:attribute name="href">
75           <xsl:call-template name="root-rel-path"/>
76           <xsl:value-of select="$dir"/>
77           <xsl:value-of select="$filename-prefix"/>
78           <xsl:value-of select="$tocentry/@filename"/>
79           <xsl:if test="@localinfo">
80             <xsl:text>#</xsl:text>
81             <xsl:value-of select="@localinfo"/>
82           </xsl:if>
83         </xsl:attribute>
84
85         <xsl:if test="@type = 'new'">
86           <xsl:attribute name="target">_blank</xsl:attribute>
87         </xsl:if>
88
89         <xsl:choose>
90           <xsl:when test="count(node()) = 0">
91             <xsl:apply-templates select="$webpage/head/title"/>
92           </xsl:when>
93           <xsl:otherwise>
94             <xsl:apply-templates/>
95           </xsl:otherwise>
96         </xsl:choose>
97       </a>
98     </xsl:otherwise>
99   </xsl:choose>
100 </xsl:template>
101
102 <!-- Customize the selection to use both website and offsite databases -->
103 <xsl:template name="select.target.database">
104   <xsl:param name="targetdoc.att" select="''"/>
105   <xsl:param name="targetptr.att" select="''"/>
106   <xsl:param name="olink.lang" select="''"/>
107
108   <!-- Is the target in the website database? -->
109   <xsl:variable name="website.olink.key">
110     <xsl:if test="$website.database.document != ''">
111       <xsl:call-template name="select.olink.key">
112         <xsl:with-param name="targetdoc.att" select="$targetdoc.att"/>
113         <xsl:with-param name="targetptr.att" select="$targetptr.att"/>
114         <xsl:with-param name="olink.lang" select="$olink.lang"/>
115         <xsl:with-param name="target.database" 
116                         select="document($website.database.document,/)"/>
117       </xsl:call-template>
118     </xsl:if>
119   </xsl:variable>
120
121   <!-- Is the target in the offsite database? -->
122   <xsl:variable name="offsite.olink.key">
123     <xsl:if test="$target.database.document != ''">
124       <xsl:call-template name="select.olink.key">
125         <xsl:with-param name="targetdoc.att" select="$targetdoc.att"/>
126         <xsl:with-param name="targetptr.att" select="$targetptr.att"/>
127         <xsl:with-param name="olink.lang" select="$olink.lang"/>
128         <xsl:with-param name="target.database" 
129                         select="document($target.database.document,/)"/>
130       </xsl:call-template>
131     </xsl:if>
132   </xsl:variable>
133
134   <xsl:choose>
135     <xsl:when test="$website.olink.key != ''">
136       <xsl:value-of select="$website.database.document"/>
137     </xsl:when>
138     <xsl:when test="$offsite.olink.key != ''">
139       <xsl:value-of select="$target.database.document"/>
140     </xsl:when>
141     <xsl:otherwise>
142       <!-- Return a bogus string and let the olink template deal with it
143       <xsl:text>NOMATCHINANYDATABASEDOCUMENT</xsl:text>
144       -->
145     </xsl:otherwise>
146   </xsl:choose>
147 </xsl:template>
148
149 <!-- Customize this template to use Website root-relative -->
150 <xsl:template name="make.olink.href">
151   <xsl:param name="olink.key" select="''"/>
152   <xsl:param name="target.database"/>
153
154   <xsl:if test="$olink.key != ''">
155
156     <xsl:variable name="targetdoc">
157       <xsl:value-of select="substring-before($olink.key, '/')"/>
158     </xsl:variable>
159   
160     <xsl:variable name="targetptr">
161       <xsl:value-of select="substring-before(substring-after($olink.key, '/'),'/')"/>
162     </xsl:variable>
163   
164     <xsl:variable name="target.href" >
165       <xsl:for-each select="$target.database" >
166         <xsl:value-of select="key('targetptr-key', $olink.key)/@href" />
167       </xsl:for-each>
168     </xsl:variable>
169   
170     <xsl:variable name="target.dir" >
171       <xsl:for-each select="$target.database" >
172         <xsl:value-of select="key('targetdoc-key', $targetdoc)/@dir" />
173       </xsl:for-each>
174     </xsl:variable>
175
176     <xsl:variable name="target.element" >
177       <xsl:for-each select="$target.database" >
178         <xsl:value-of select="key('targetptr-key', $olink.key)/@element" />
179       </xsl:for-each>
180     </xsl:variable>
181
182     <!-- Does the target database use a sitemap? -->
183     <xsl:variable name="use.sitemap">
184       <xsl:choose>
185         <xsl:when test="$target.database//sitemap">1</xsl:when>
186         <xsl:otherwise>0</xsl:otherwise>
187       </xsl:choose>
188     </xsl:variable>
189   
190   
191     <!-- Get the baseuri for this targetptr -->
192     <xsl:variable name="baseuri" >
193       <xsl:choose>
194         <!-- Does the database use a sitemap? -->
195         <xsl:when test="$use.sitemap != 0" >
196           <xsl:choose>
197             <!-- Was current.docid parameter set? -->
198             <xsl:when test="$current.docid != ''">
199               <!-- Was it found in the database? -->
200               <xsl:variable name="currentdoc.key" >
201                 <xsl:for-each select="$target.database" >
202                   <xsl:value-of select="key('targetdoc-key',
203                                         $current.docid)/@targetdoc" />
204                 </xsl:for-each>
205               </xsl:variable>
206               <xsl:choose>
207                 <xsl:when test="$currentdoc.key != ''">
208                   <xsl:for-each select="$target.database" >
209                     <xsl:call-template name="targetpath" >
210                       <xsl:with-param name="dirnode" 
211                           select="key('targetdoc-key', $current.docid)/parent::dir"/>
212                       <xsl:with-param name="targetdoc" select="$targetdoc"/>
213                     </xsl:call-template>
214                   </xsl:for-each >
215                 </xsl:when>
216                 <xsl:otherwise>
217                   <xsl:message>
218                     <xsl:text>Olink error: cannot compute relative </xsl:text>
219                     <xsl:text>sitemap path because $current.docid '</xsl:text>
220                     <xsl:value-of select="$current.docid"/>
221                     <xsl:text>' not found in target database.</xsl:text>
222                   </xsl:message>
223                 </xsl:otherwise>
224               </xsl:choose>
225             </xsl:when>
226             <xsl:otherwise>
227               <xsl:message>
228                 <xsl:text>Olink warning: cannot compute relative </xsl:text>
229                 <xsl:text>sitemap path without $current.docid parameter</xsl:text>
230               </xsl:message>
231             </xsl:otherwise>
232           </xsl:choose> 
233           <!-- In either case, add baseuri from its document entry-->
234           <xsl:variable name="docbaseuri">
235             <xsl:for-each select="$target.database" >
236               <xsl:value-of select="key('targetdoc-key', $targetdoc)/@baseuri" />
237             </xsl:for-each>
238           </xsl:variable>
239           <xsl:if test="$docbaseuri != ''" >
240             <xsl:value-of select="$docbaseuri"/>
241           </xsl:if>
242         </xsl:when>
243         <!-- No database sitemap in use -->
244         <xsl:otherwise>
245           <!-- compute a root-relative path if current page has a @dir -->
246           <xsl:variable name="root-rel">
247             <xsl:call-template name="root-rel-path"/>
248           </xsl:variable>
249           <xsl:if test="$root-rel != ''">
250             <xsl:value-of select="$root-rel"/>
251           </xsl:if>
252           <!-- Add the target's @dir to the path -->
253           <xsl:if test="$target.dir != ''">
254             <xsl:value-of select="$target.dir"/>
255           </xsl:if>
256           <!-- Just use any baseuri from its document entry -->
257           <xsl:variable name="docbaseuri">
258             <xsl:for-each select="$target.database" >
259               <xsl:value-of select="key('targetdoc-key',
260                                         $targetdoc)/@baseuri" />
261             </xsl:for-each>
262           </xsl:variable>
263           <xsl:if test="$docbaseuri != ''" >
264             <xsl:value-of select="$docbaseuri"/>
265           </xsl:if>
266         </xsl:otherwise>
267       </xsl:choose>
268     </xsl:variable>
269   
270     <!-- Form the href information -->
271     <xsl:if test="$baseuri != ''">
272       <xsl:value-of select="$baseuri"/>
273       <xsl:if test="substring($target.href,1,1) != '#'">
274         <!--xsl:text>/</xsl:text-->
275       </xsl:if>
276     </xsl:if>
277     <xsl:choose>
278       <xsl:when test="$target.element = 'webpage' and
279                       $targetdoc = $targetptr">
280         <!-- Don't output #id because not needed -->
281       </xsl:when>
282       <!-- optionally turn off frag for PDF references -->
283       <xsl:when test="not($insert.olink.pdf.frag = 0 and
284             translate(substring($baseuri, string-length($baseuri) - 3),
285                       'PDF', 'pdf') = '.pdf'
286             and starts-with($target.href, '#') )">
287         <xsl:value-of select="$target.href"/>
288       </xsl:when>
289       <xsl:otherwise>
290         <xsl:value-of select="$target.href"/>
291       </xsl:otherwise>
292     </xsl:choose>
293   </xsl:if>
294 </xsl:template>
295
296
297 </xsl:stylesheet>