| 1 |
<?xml version='1.0'?> |
| 2 |
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version='1.0'> |
| 3 |
|
| 4 |
<!--############################################################################ |
| 5 |
XSLT Stylesheet DocBook -> LaTeX |
| 6 |
############################################################################ --> |
| 7 |
|
| 8 |
<xsl:template name="nolinkurl"> |
| 9 |
<xsl:param name="url" select="@url"/> |
| 10 |
<xsl:param name="command" select="'\nolinkurl'"/> |
| 11 |
<xsl:value-of select="$command"/> |
| 12 |
<xsl:text>{</xsl:text> |
| 13 |
<xsl:call-template name="scape-encode"> |
| 14 |
<xsl:with-param name="string" select="$url"/> |
| 15 |
</xsl:call-template> |
| 16 |
<xsl:text>}</xsl:text> |
| 17 |
</xsl:template> |
| 18 |
|
| 19 |
<!-- The funny thing is that in table cells, it fails if the URL ends with a |
| 20 |
'\'. The workaround is to append a space. Moreover, the problem occurs only |
| 21 |
if the ending '\'s are non a multiple of 2! |
| 22 |
--> |
| 23 |
<xsl:template name="nolinkurl2"> |
| 24 |
<xsl:param name="url" select="@url"/> |
| 25 |
<xsl:param name="command" select="'\nolinkurl'"/> |
| 26 |
<!-- Ignore the URL that contains only a '\n' --> |
| 27 |
<xsl:if test="$url != ' '"> |
| 28 |
<xsl:variable name="bscount"> |
| 29 |
<xsl:call-template name="bslash-end-count"> |
| 30 |
<xsl:with-param name="url" select="$url"/> |
| 31 |
</xsl:call-template> |
| 32 |
</xsl:variable> |
| 33 |
<xsl:value-of select="$command"/> |
| 34 |
<xsl:text>{</xsl:text> |
| 35 |
<xsl:call-template name="scape-encode"> |
| 36 |
<xsl:with-param name="string" select="$url"/> |
| 37 |
</xsl:call-template> |
| 38 |
<xsl:if test="$bscount mod 2"> |
| 39 |
<xsl:text> </xsl:text> |
| 40 |
</xsl:if> |
| 41 |
<xsl:text>}</xsl:text> |
| 42 |
</xsl:if> |
| 43 |
</xsl:template> |
| 44 |
|
| 45 |
|
| 46 |
<!-- Count the number of '\' ending the url (only for entries) --> |
| 47 |
<xsl:template name="bslash-end-count"> |
| 48 |
<xsl:param name="count" select="0"/> |
| 49 |
<xsl:param name="url"/> |
| 50 |
|
| 51 |
<xsl:choose> |
| 52 |
<xsl:when test="$url = ''"> |
| 53 |
<xsl:value-of select="$count"/> |
| 54 |
</xsl:when> |
| 55 |
<xsl:when test="substring($url,string-length($url))='\'"> |
| 56 |
<xsl:call-template name="bslash-end-count"> |
| 57 |
<xsl:with-param name="url" |
| 58 |
select="substring($url,1,string-length($url)-1)"/> |
| 59 |
<xsl:with-param name="count" select="$count + 1"/> |
| 60 |
</xsl:call-template> |
| 61 |
</xsl:when> |
| 62 |
<xsl:otherwise> |
| 63 |
<xsl:value-of select="$count"/> |
| 64 |
</xsl:otherwise> |
| 65 |
</xsl:choose> |
| 66 |
</xsl:template> |
| 67 |
|
| 68 |
|
| 69 |
<!-- Only URLs in table cells must be escaped (mostly because of multicolumn) |
| 70 |
except for spaces that are always skipped by \nolinkurl{}. Note that |
| 71 |
setting the 'obeyspaces' option to the url package doesn't help. |
| 72 |
--> |
| 73 |
<xsl:template name="nolinkurl-output"> |
| 74 |
<xsl:param name="url" select="@url"/> |
| 75 |
<xsl:param name="command" select="'\nolinkurl'"/> |
| 76 |
<xsl:variable name="url2"> |
| 77 |
<xsl:choose> |
| 78 |
<!-- Behaviour depending on the texlive version --> |
| 79 |
<xsl:when test="number($texlive.version) >= 2009"> |
| 80 |
<xsl:call-template name="string-replace"> |
| 81 |
<xsl:with-param name="string" select="$url"/> |
| 82 |
<xsl:with-param name="from" select="'\'"/> |
| 83 |
<xsl:with-param name="to" select="'\\'"/> |
| 84 |
</xsl:call-template> |
| 85 |
</xsl:when> |
| 86 |
<xsl:otherwise> |
| 87 |
<xsl:value-of select="$url"/> |
| 88 |
</xsl:otherwise> |
| 89 |
</xsl:choose> |
| 90 |
</xsl:variable> |
| 91 |
|
| 92 |
<xsl:choose> |
| 93 |
<xsl:when test="ancestor::entry or ancestor::revision or ancestor::footnote"> |
| 94 |
<xsl:call-template name="nolinkurl-escape"> |
| 95 |
<xsl:with-param name="url" select="$url2"/> |
| 96 |
<xsl:with-param name="command" select="$command"/> |
| 97 |
</xsl:call-template> |
| 98 |
<!-- FIXME: do something with '&' and revision if needed --> |
| 99 |
</xsl:when> |
| 100 |
<xsl:otherwise> |
| 101 |
<xsl:call-template name="nolinkurl-escape"> |
| 102 |
<xsl:with-param name="url" select="$url2"/> |
| 103 |
<xsl:with-param name="chars" select="' '"/> |
| 104 |
<xsl:with-param name="command" select="$command"/> |
| 105 |
</xsl:call-template> |
| 106 |
</xsl:otherwise> |
| 107 |
</xsl:choose> |
| 108 |
</xsl:template> |
| 109 |
|
| 110 |
|
| 111 |
<!-- Find the first special char position in the string --> |
| 112 |
<xsl:template name="find-first"> |
| 113 |
<xsl:param name="string"/> |
| 114 |
<xsl:param name="chars" select="'#% '"/> |
| 115 |
|
| 116 |
<xsl:choose> |
| 117 |
<xsl:when test="$string = ''"> |
| 118 |
<xsl:value-of select="0"/> |
| 119 |
</xsl:when> |
| 120 |
<xsl:when test="string-length($chars)=1"> |
| 121 |
<xsl:choose> |
| 122 |
<xsl:when test="contains($string, $chars)"> |
| 123 |
<xsl:value-of select="string-length(substring-before($string, $chars))"/> |
| 124 |
</xsl:when> |
| 125 |
<xsl:otherwise> |
| 126 |
<xsl:value-of select="string-length($string)"/> |
| 127 |
</xsl:otherwise> |
| 128 |
</xsl:choose> |
| 129 |
</xsl:when> |
| 130 |
<xsl:otherwise> |
| 131 |
<xsl:variable name="charset" select="substring($chars, 2)"/> |
| 132 |
<xsl:variable name="char" select="substring($chars, 1, 1)"/> |
| 133 |
<xsl:choose> |
| 134 |
<xsl:when test="contains($string, $char)"> |
| 135 |
<xsl:call-template name="find-first"> |
| 136 |
<xsl:with-param name="string" select="substring-before($string, $char)"/> |
| 137 |
<xsl:with-param name="chars" select="$charset"/> |
| 138 |
</xsl:call-template> |
| 139 |
</xsl:when> |
| 140 |
<xsl:otherwise> |
| 141 |
<xsl:call-template name="find-first"> |
| 142 |
<xsl:with-param name="string" select="$string"/> |
| 143 |
<xsl:with-param name="chars" select="$charset"/> |
| 144 |
</xsl:call-template> |
| 145 |
</xsl:otherwise> |
| 146 |
</xsl:choose> |
| 147 |
</xsl:otherwise> |
| 148 |
</xsl:choose> |
| 149 |
</xsl:template> |
| 150 |
|
| 151 |
<xsl:template name="nolinkurl-escape"> |
| 152 |
<xsl:param name="escchars"/> |
| 153 |
<xsl:param name="url"/> |
| 154 |
<xsl:param name="chars" select="'#% '"/> |
| 155 |
<xsl:param name="command" select="'\nolinkurl'"/> |
| 156 |
|
| 157 |
<xsl:variable name="len" select="string-length($url)"/> |
| 158 |
|
| 159 |
<xsl:variable name="pos"> |
| 160 |
<xsl:call-template name="find-first"> |
| 161 |
<xsl:with-param name="string" select="$url"/> |
| 162 |
<xsl:with-param name="chars" select="$chars"/> |
| 163 |
</xsl:call-template> |
| 164 |
</xsl:variable> |
| 165 |
|
| 166 |
<xsl:choose> |
| 167 |
<xsl:when test="$pos = $len"> |
| 168 |
<xsl:if test="$escchars != ''"> |
| 169 |
<xsl:text>\texttt{</xsl:text> |
| 170 |
<xsl:value-of select="$escchars"/> |
| 171 |
<xsl:text>}</xsl:text> |
| 172 |
</xsl:if> |
| 173 |
<xsl:if test="$len != 0"> |
| 174 |
<xsl:call-template name="nolinkurl2"> |
| 175 |
<xsl:with-param name="url" select="$url"/> |
| 176 |
<xsl:with-param name="command" select="$command"/> |
| 177 |
</xsl:call-template> |
| 178 |
</xsl:if> |
| 179 |
</xsl:when> |
| 180 |
<xsl:when test="$pos = 0"> |
| 181 |
<xsl:call-template name="nolinkurl-escape"> |
| 182 |
<xsl:with-param name="escchars" |
| 183 |
select="concat($escchars, '\', substring($url,1,1))"/> |
| 184 |
<xsl:with-param name="url" select="substring($url, 2)"/> |
| 185 |
<xsl:with-param name="chars" select="$chars"/> |
| 186 |
<xsl:with-param name="command" select="$command"/> |
| 187 |
</xsl:call-template> |
| 188 |
</xsl:when> |
| 189 |
<xsl:otherwise> |
| 190 |
<xsl:if test="$escchars != ''"> |
| 191 |
<xsl:text>\texttt{</xsl:text> |
| 192 |
<xsl:value-of select="$escchars"/> |
| 193 |
<xsl:text>}</xsl:text> |
| 194 |
</xsl:if> |
| 195 |
<xsl:call-template name="nolinkurl2"> |
| 196 |
<xsl:with-param name="url" select="substring($url, 1, $pos)"/> |
| 197 |
<xsl:with-param name="command" select="$command"/> |
| 198 |
</xsl:call-template> |
| 199 |
<xsl:call-template name="nolinkurl-escape"> |
| 200 |
<xsl:with-param name="escchars" |
| 201 |
select="concat('\', substring($url,$pos+1,1))"/> |
| 202 |
<xsl:with-param name="url" select="substring($url, $pos+2)"/> |
| 203 |
<xsl:with-param name="chars" select="$chars"/> |
| 204 |
<xsl:with-param name="command" select="$command"/> |
| 205 |
</xsl:call-template> |
| 206 |
</xsl:otherwise> |
| 207 |
</xsl:choose> |
| 208 |
</xsl:template> |
| 209 |
|
| 210 |
|
| 211 |
<!-- Template for a unit-test: |
| 212 |
<u> |
| 213 |
<nolinkurl>ab%cde%#%fg</nolinkurl> |
| 214 |
<nolinkurl>#ab%cde%#%fg###</nolinkurl> |
| 215 |
<nolinkurl>nothing special</nolinkurl> |
| 216 |
<nolinkurl>nothing\special\</nolinkurl> |
| 217 |
<nolinkurl>nothing\special\\</nolinkurl> |
| 218 |
<entry> |
| 219 |
<nolinkurl>ab%cde%#%fg</nolinkurl> |
| 220 |
<nolinkurl>#%#ab%cde%#%fg###</nolinkurl> |
| 221 |
<nolinkurl>nothing special</nolinkurl> |
| 222 |
<nolinkurl>#########</nolinkurl> |
| 223 |
<nolinkurl>%%%%%%%%%</nolinkurl> |
| 224 |
<nolinkurl>#%#%%#%##</nolinkurl> |
| 225 |
</entry> |
| 226 |
</u> |
| 227 |
|
| 228 |
Expected: |
| 229 |
\nolinkurl{ab%cde%#%fg} |
| 230 |
\nolinkurl{#ab%cde%#%fg###} |
| 231 |
\nolinkurl{nothing}\texttt{\ }\nolinkurl{special} |
| 232 |
\nolinkurl{nothing\special\ } |
| 233 |
\nolinkurl{nothing\special\\} |
| 234 |
<entry> |
| 235 |
\nolinkurl{ab}\texttt{\%}\nolinkurl{cde}\texttt{\%\#\%}\nolinkurl{fg} |
| 236 |
\texttt{\#\%\#}\nolinkurl{ab}\texttt{\%}\nolinkurl{cde}\texttt{\%\#\%}\nolinkurl{fg}\texttt{\#\#\#} |
| 237 |
\nolinkurl{nothing}\texttt{\ }\nolinkurl{special} |
| 238 |
\texttt{\#\#\#\#\#\#\#\#\#} |
| 239 |
\texttt{\%\%\%\%\%\%\%\%\%} |
| 240 |
\texttt{\#\%\#\%\%\#\%\#\#} |
| 241 |
</entry> |
| 242 |
|
| 243 |
--> |
| 244 |
<xsl:template match="nolinkurl"> |
| 245 |
<xsl:call-template name="nolinkurl-output"> |
| 246 |
<xsl:with-param name="url" select="."/> |
| 247 |
</xsl:call-template> |
| 248 |
</xsl:template> |
| 249 |
|
| 250 |
</xsl:stylesheet> |