inprogress
[jalview.git] / forester / phyloxml / accessories / xmlverbatim.xsl
1 <?xml version="1.0" encoding="ISO-8859-1"?>
2
3 <!--
4     XML to HTML Verbatim Formatter with Syntax Highlighting
5     Version 1.1
6     Contributors: Doug Dicks, added auto-indent (parameter indent-elements)
7                   for pretty-print
8
9     Copyright 2002 Oliver Becker
10     ob@obqo.de
11  
12     Licensed under the Apache License, Version 2.0 (the "License"); 
13     you may not use this file except in compliance with the License.
14     You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
15     Unless required by applicable law or agreed to in writing, software distributed
16     under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
17     CONDITIONS OF ANY KIND, either express or implied. See the License for the
18     specific language governing permissions and limitations under the License.
19
20     Alternatively, this software may be used under the terms of the 
21     GNU Lesser General Public License (LGPL).
22 -->
23
24 <xsl:stylesheet version="1.0"
25                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
26                 xmlns:verb="http://informatik.hu-berlin.de/xmlverbatim"
27                 exclude-result-prefixes="verb">
28
29    <xsl:output method="html" omit-xml-declaration="yes" indent="no"/>
30
31    <xsl:param name="indent-elements" select="false()" />
32
33    <xsl:template match="/">
34       <xsl:apply-templates select="." mode="xmlverb" />
35    </xsl:template>
36
37    <!-- root -->
38    <xsl:template match="/" mode="xmlverb">
39       <xsl:text>&#xA;</xsl:text>
40       <xsl:comment>
41          <xsl:text> converted by xmlverbatim.xsl 1.1, (c) O. Becker </xsl:text>
42       </xsl:comment>
43       <xsl:text>&#xA;</xsl:text>
44       <div class="xmlverb-default">
45          <xsl:apply-templates mode="xmlverb">
46             <xsl:with-param name="indent-elements" select="$indent-elements" />
47          </xsl:apply-templates>
48       </div>
49       <xsl:text>&#xA;</xsl:text>
50    </xsl:template>
51
52    <!-- wrapper -->
53    <xsl:template match="verb:wrapper">
54       <xsl:apply-templates mode="xmlverb">
55          <xsl:with-param name="indent-elements" select="$indent-elements" />
56       </xsl:apply-templates>
57    </xsl:template>
58
59    <xsl:template match="verb:wrapper" mode="xmlverb">
60       <xsl:apply-templates mode="xmlverb">
61          <xsl:with-param name="indent-elements" select="$indent-elements" />
62       </xsl:apply-templates>
63    </xsl:template>
64
65    <!-- element nodes -->
66    <xsl:template match="*" mode="xmlverb">
67       <xsl:param name="indent-elements" select="false()" />
68       <xsl:param name="indent" select="''" />
69       <xsl:param name="indent-increment" select="'&#xA0;&#xA0;&#xA0;'" />
70       <xsl:if test="$indent-elements">
71          <br/>
72          <xsl:value-of select="$indent" />
73       </xsl:if>
74       <xsl:text>&lt;</xsl:text>
75       <xsl:variable name="ns-prefix"
76                     select="substring-before(name(),':')" />
77       <xsl:if test="$ns-prefix != ''">
78          <span class="xmlverb-element-nsprefix">
79             <xsl:value-of select="$ns-prefix"/>
80          </span>
81          <xsl:text>:</xsl:text>
82       </xsl:if>
83       <span class="xmlverb-element-name">
84          <xsl:value-of select="local-name()"/>
85       </span>
86       <xsl:variable name="pns" select="../namespace::*"/>
87       <xsl:if test="$pns[name()=''] and not(namespace::*[name()=''])">
88          <span class="xmlverb-ns-name">
89             <xsl:text> xmlns</xsl:text>
90          </span>
91          <xsl:text>=&quot;&quot;</xsl:text>
92       </xsl:if>
93       <xsl:for-each select="namespace::*">
94          <xsl:if test="not($pns[name()=name(current()) and 
95                            .=current()])">
96             <xsl:call-template name="xmlverb-ns" />
97          </xsl:if>
98       </xsl:for-each>
99       <xsl:for-each select="@*">
100          <xsl:call-template name="xmlverb-attrs" />
101       </xsl:for-each>
102       <xsl:choose>
103          <xsl:when test="node()">
104             <xsl:text>&gt;</xsl:text>
105             <xsl:apply-templates mode="xmlverb">
106               <xsl:with-param name="indent-elements"
107                               select="$indent-elements"/>
108               <xsl:with-param name="indent"
109                               select="concat($indent, $indent-increment)"/>
110               <xsl:with-param name="indent-increment"
111                               select="$indent-increment"/>
112             </xsl:apply-templates>
113             <xsl:if test="* and $indent-elements">
114                <br/>
115                <xsl:value-of select="$indent" />
116             </xsl:if>
117             <xsl:text>&lt;/</xsl:text>
118             <xsl:if test="$ns-prefix != ''">
119                <span class="xmlverb-element-nsprefix">
120                   <xsl:value-of select="$ns-prefix"/>
121                </span>
122                <xsl:text>:</xsl:text>
123             </xsl:if>
124             <span class="xmlverb-element-name">
125                <xsl:value-of select="local-name()"/>
126             </span>
127             <xsl:text>&gt;</xsl:text>
128          </xsl:when>
129          <xsl:otherwise>
130             <xsl:text> /&gt;</xsl:text>
131          </xsl:otherwise>
132       </xsl:choose>
133       <xsl:if test="not(parent::*)"><br /><xsl:text>&#xA;</xsl:text></xsl:if>
134    </xsl:template>
135
136    <!-- attribute nodes -->
137    <xsl:template name="xmlverb-attrs">
138       <xsl:text> </xsl:text>
139       <span class="xmlverb-attr-name">
140          <xsl:value-of select="name()"/>
141       </span>
142       <xsl:text>=&quot;</xsl:text>
143       <span class="xmlverb-attr-content">
144          <xsl:call-template name="html-replace-entities">
145             <xsl:with-param name="text" select="normalize-space(.)" />
146             <xsl:with-param name="attrs" select="true()" />
147          </xsl:call-template>
148       </span>
149       <xsl:text>&quot;</xsl:text>
150    </xsl:template>
151
152    <!-- namespace nodes -->
153    <xsl:template name="xmlverb-ns">
154       <xsl:if test="name()!='xml'">
155          <span class="xmlverb-ns-name">
156             <xsl:text> xmlns</xsl:text>
157             <xsl:if test="name()!=''">
158                <xsl:text>:</xsl:text>
159             </xsl:if>
160             <xsl:value-of select="name()"/>
161          </span>
162          <xsl:text>=&quot;</xsl:text>
163          <span class="xmlverb-ns-uri">
164             <xsl:value-of select="."/>
165          </span>
166          <xsl:text>&quot;</xsl:text>
167       </xsl:if>
168    </xsl:template>
169
170    <!-- text nodes -->
171    <xsl:template match="text()" mode="xmlverb">
172       <span class="xmlverb-text">
173          <xsl:call-template name="preformatted-output">
174             <xsl:with-param name="text">
175                <xsl:call-template name="html-replace-entities">
176                   <xsl:with-param name="text" select="." />
177                </xsl:call-template>
178             </xsl:with-param>
179          </xsl:call-template>
180       </span>
181    </xsl:template>
182
183    <!-- comments -->
184    <xsl:template match="comment()" mode="xmlverb">
185       <xsl:text>&lt;!--</xsl:text>
186       <span class="xmlverb-comment">
187          <xsl:call-template name="preformatted-output">
188             <xsl:with-param name="text" select="." />
189          </xsl:call-template>
190       </span>
191       <xsl:text>--&gt;</xsl:text>
192       <xsl:if test="not(parent::*)"><br /><xsl:text>&#xA;</xsl:text></xsl:if>
193    </xsl:template>
194
195    <!-- processing instructions -->
196    <xsl:template match="processing-instruction()" mode="xmlverb">
197       <xsl:text>&lt;?</xsl:text>
198       <span class="xmlverb-pi-name">
199          <xsl:value-of select="name()"/>
200       </span>
201       <xsl:if test=".!=''">
202          <xsl:text> </xsl:text>
203          <span class="xmlverb-pi-content">
204             <xsl:value-of select="."/>
205          </span>
206       </xsl:if>
207       <xsl:text>?&gt;</xsl:text>
208       <xsl:if test="not(parent::*)"><br /><xsl:text>&#xA;</xsl:text></xsl:if>
209    </xsl:template>
210
211
212    <!-- =========================================================== -->
213    <!--                    Procedures / Functions                   -->
214    <!-- =========================================================== -->
215
216    <!-- generate entities by replacing &, ", < and > in $text -->
217    <xsl:template name="html-replace-entities">
218       <xsl:param name="text" />
219       <xsl:param name="attrs" />
220       <xsl:variable name="tmp">
221          <xsl:call-template name="replace-substring">
222             <xsl:with-param name="from" select="'&gt;'" />
223             <xsl:with-param name="to" select="'&amp;gt;'" />
224             <xsl:with-param name="value">
225                <xsl:call-template name="replace-substring">
226                   <xsl:with-param name="from" select="'&lt;'" />
227                   <xsl:with-param name="to" select="'&amp;lt;'" />
228                   <xsl:with-param name="value">
229                      <xsl:call-template name="replace-substring">
230                         <xsl:with-param name="from" 
231                                         select="'&amp;'" />
232                         <xsl:with-param name="to" 
233                                         select="'&amp;amp;'" />
234                         <xsl:with-param name="value" 
235                                         select="$text" />
236                      </xsl:call-template>
237                   </xsl:with-param>
238                </xsl:call-template>
239             </xsl:with-param>
240          </xsl:call-template>
241       </xsl:variable>
242       <xsl:choose>
243          <!-- $text is an attribute value -->
244          <xsl:when test="$attrs">
245             <xsl:call-template name="replace-substring">
246                <xsl:with-param name="from" select="'&#xA;'" />
247                <xsl:with-param name="to" select="'&amp;#xA;'" />
248                <xsl:with-param name="value">
249                   <xsl:call-template name="replace-substring">
250                      <xsl:with-param name="from" 
251                                      select="'&quot;'" />
252                      <xsl:with-param name="to" 
253                                      select="'&amp;quot;'" />
254                      <xsl:with-param name="value" select="$tmp" />
255                   </xsl:call-template>
256                </xsl:with-param>
257             </xsl:call-template>
258          </xsl:when>
259          <xsl:otherwise>
260             <xsl:value-of select="$tmp" />
261          </xsl:otherwise>
262       </xsl:choose>
263    </xsl:template>
264
265    <!-- replace in $value substring $from with $to -->
266    <xsl:template name="replace-substring">
267       <xsl:param name="value" />
268       <xsl:param name="from" />
269       <xsl:param name="to" />
270       <xsl:choose>
271          <xsl:when test="contains($value,$from)">
272             <xsl:value-of select="substring-before($value,$from)" />
273             <xsl:value-of select="$to" />
274             <xsl:call-template name="replace-substring">
275                <xsl:with-param name="value" 
276                                select="substring-after($value,$from)" />
277                <xsl:with-param name="from" select="$from" />
278                <xsl:with-param name="to" select="$to" />
279             </xsl:call-template>
280          </xsl:when>
281          <xsl:otherwise>
282             <xsl:value-of select="$value" />
283          </xsl:otherwise>
284       </xsl:choose>
285    </xsl:template>
286
287    <!-- preformatted output: space as &nbsp;, tab as 8 &nbsp;
288                              nl as <br> -->
289    <xsl:template name="preformatted-output">
290       <xsl:param name="text" />
291       <xsl:call-template name="output-nl">
292          <xsl:with-param name="text">
293             <xsl:call-template name="replace-substring">
294                <xsl:with-param name="value"
295                                select="translate($text,' ','&#xA0;')" />
296                <xsl:with-param name="from" select="'&#9;'" />
297                <xsl:with-param name="to" 
298                                select="'&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;'" />
299             </xsl:call-template>
300          </xsl:with-param>
301       </xsl:call-template>
302    </xsl:template>
303
304    <!-- output nl as <br> -->
305    <xsl:template name="output-nl">
306       <xsl:param name="text" />
307       <xsl:choose>
308          <xsl:when test="contains($text,'&#xA;')">
309             <xsl:value-of select="substring-before($text,'&#xA;')" />
310             <br />
311             <xsl:text>&#xA;</xsl:text>
312             <xsl:call-template name="output-nl">
313                <xsl:with-param name="text" 
314                                select="substring-after($text,'&#xA;')" />
315             </xsl:call-template>
316          </xsl:when>
317          <xsl:otherwise>
318             <xsl:value-of select="$text" />
319          </xsl:otherwise>
320       </xsl:choose>
321    </xsl:template>
322
323 </xsl:stylesheet>