Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/tools/boostbook/xsl/source-highlight.xsl @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 6.5 KB
Line 
1<?xml version="1.0" encoding="utf-8"?>
2<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3                version="1.0">
4  <xsl:param name="boost.syntax.highlight">1</xsl:param>
5
6  <xsl:template name="source-highlight">
7    <xsl:param name="text" select="."/>
8    <xsl:choose>
9      <xsl:when test="$boost.syntax.highlight='1'">
10        <xsl:call-template name="highlight-text">
11          <xsl:with-param name="text" select="$text"/>
12        </xsl:call-template>
13      </xsl:when>
14      <xsl:otherwise>
15        <xsl:value-of select="$text"/>
16      </xsl:otherwise>
17    </xsl:choose>
18  </xsl:template>
19
20  <!-- Perform C++ keyword highlighting on the given text -->
21  <xsl:template name="highlight-text">
22    <xsl:param name="text" select="."/>
23    <xsl:param name="keywords"
24      select="'asm auto bool break case catch char class const const_cast continue default delete do double dynamic_cast else enum explicit export extern false float for friend goto if inline int long mutable namespace new operator private protected public register reinterpret_cast return short signed sizeof static static_cast struct switch template this throw true try typedef typeid typename union unsigned using virtual void volatile wchar_t while'"/>
25    <xsl:param name="best-match" select="''"/>
26   
27    <!-- Determine the current keyword -->
28    <xsl:variable name="keyword">
29      <xsl:choose>
30        <xsl:when test="contains($keywords, ' ')">
31          <xsl:value-of select="substring-before($keywords, ' ')"/>
32        </xsl:when>
33        <xsl:otherwise>
34          <xsl:value-of select="$keywords"/>
35        </xsl:otherwise>
36      </xsl:choose>
37    </xsl:variable>
38
39    <!-- Determine the set of keywords that are left -->
40    <xsl:variable name="keywords-left">
41      <xsl:if test="contains($keywords, ' ')">
42        <xsl:value-of select="substring-after($keywords, ' ')"/>
43      </xsl:if>
44    </xsl:variable>
45
46    <!-- The set of characters that can be identifiers -->
47    <xsl:variable name="id-chars" select="'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'"/>
48
49    <xsl:variable name="X" select="'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'"/>
50
51    <xsl:choose>
52      <!-- Have we exhausted all keywords without finding any to highlight? -->
53      <xsl:when test="$keyword='' and $best-match=''">
54        <!-- Just copy the text -->
55        <xsl:copy-of select="$text"/>
56      </xsl:when>
57
58      <!-- Have we exhausted all keywords, but have one to highlight? If so,
59           make sure we didn't just find part of an identifier. -->
60      <xsl:when 
61        test="$keyword='' and
62              not (starts-with(translate(substring-after($text, $best-match),
63                                         $id-chars, $X), 'X')) and
64              not (substring(translate(substring-before($text, $best-match),
65                                       $id-chars, $X),
66                             string-length(substring-before($text,
67                                                            $best-match)),
68                             1) = 'X')">
69        <!-- Copy text before this keyword -->
70        <xsl:value-of select="substring-before($text, $best-match)"/>
71
72        <!-- Highlight the keyword -->
73        <xsl:call-template name="highlight-keyword">
74          <xsl:with-param name="keyword" select="$best-match"/>
75        </xsl:call-template>
76
77        <!-- Recurse on the rest of the text -->
78        <xsl:call-template name="highlight-text">
79          <xsl:with-param name="text" 
80            select="substring-after($text, $best-match)"/>
81        </xsl:call-template>
82      </xsl:when>
83
84      <!-- We thought we had a keyword to highlight, but it was part of an
85           identifier. So output all of the text up to (but not including!)
86           the last letter of the identifier, and try again to
87           highlight. -->
88      <xsl:when test="$keyword=''">
89        <xsl:value-of select="substring-before($text, $best-match)"/>
90        <xsl:value-of 
91          select="substring($best-match, 1, string-length($best-match)-1)"/>
92        <xsl:call-template name="highlight-text">
93          <xsl:with-param name="text"
94            select="concat(substring($best-match, string-length($best-match),
95                           1), substring-after($text, $best-match))"/>
96        </xsl:call-template>
97      </xsl:when>
98     
99      <!-- Does the text contain this keyword with a better match than we
100           previously had? -->
101      <xsl:when 
102        test="contains($text, $keyword) and
103              (($best-match = '') or
104               (string-length(substring-before($text, $keyword)) &lt;
105                string-length(substring-before($text, $best-match))))">
106        <!-- Recurse with the current keyword as the new best match -->
107        <xsl:call-template name="highlight-text">
108          <xsl:with-param name="text" select="$text"/>
109          <xsl:with-param name="keywords" select="$keywords-left"/>
110          <xsl:with-param name="best-match" select="$keyword"/>
111        </xsl:call-template>
112      </xsl:when>
113
114      <!-- Text does not contain this keyword. Just recurse normally -->
115      <xsl:otherwise>
116        <xsl:call-template name="highlight-text">
117          <xsl:with-param name="text" select="$text"/>
118          <xsl:with-param name="keywords" select="$keywords-left"/>
119          <xsl:with-param name="best-match" select="$best-match"/>
120        </xsl:call-template>
121      </xsl:otherwise>
122    </xsl:choose>
123  </xsl:template>
124
125  <xsl:template match="*" mode="highlight">
126    <xsl:element name="{name(.)}">
127      <xsl:for-each select="./@*">
128        <xsl:choose>
129          <xsl:when test="local-name(.)='last-revision'">
130            <xsl:attribute
131              name="rev:last-revision"
132              namespace="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision"
133>
134              <xsl:value-of select="."/>
135            </xsl:attribute>
136          </xsl:when>
137          <xsl:otherwise>
138            <xsl:attribute name="{name(.)}">
139              <xsl:value-of select="."/>
140            </xsl:attribute>
141          </xsl:otherwise>
142        </xsl:choose>
143      </xsl:for-each>
144      <xsl:apply-templates mode="highlight"/>
145    </xsl:element>
146  </xsl:template>
147
148  <xsl:template match="text()" mode="highlight">
149    <xsl:call-template name="source-highlight">
150      <xsl:with-param name="text" select="."/>
151    </xsl:call-template>
152  </xsl:template>
153
154  <xsl:template match="classname|methodname|functionname|libraryname|enumname|
155                       conceptname|macroname" mode="highlight">
156    <xsl:apply-templates select="." mode="annotation"/>
157  </xsl:template>
158</xsl:stylesheet>
Note: See TracBrowser for help on using the repository browser.