url.xsl coverage = 95/179 (53.07%)

  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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version='1.0'>

<!--############################################################################
    XSLT Stylesheet DocBook -> LaTeX 
    ############################################################################ -->

<xsl:template name="nolinkurl">
  <xsl:param name="url" select="@url"/>
  <xsl:param name="command" select="'\nolinkurl'"/>
  <xsl:value-of select="$command"/>
  <xsl:text>{</xsl:text>
  <xsl:call-template name="scape-encode">
    <xsl:with-param name="string" select="$url"/>
  </xsl:call-template>
  <xsl:text>}</xsl:text>
</xsl:template>

<!-- The funny thing is that in table cells, it fails if the URL ends with a
     '\'. The workaround is to append a space. Moreover, the problem occurs only
     if the ending '\'s are non a multiple of 2!
-->
<xsl:template name="nolinkurl2">
  <xsl:param name="url" select="@url"/>
  <xsl:param name="command" select="'\nolinkurl'"/>
  <!-- Ignore the URL that contains only a '\n' -->
  <xsl:if test="$url != '&#10;'">
    <xsl:variable name="bscount">
      <xsl:call-template name="bslash-end-count">
        <xsl:with-param name="url" select="$url"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:value-of select="$command"/>
    <xsl:text>{</xsl:text>
    <xsl:call-template name="scape-encode">
      <xsl:with-param name="string" select="$url"/>
    </xsl:call-template>
    <xsl:if test="$bscount mod 2">
      <xsl:text> </xsl:text>
    </xsl:if>
    <xsl:text>}</xsl:text>
  </xsl:if>
</xsl:template>


<!-- Count the number of '\' ending the url (only for entries) -->
<xsl:template name="bslash-end-count">
  <xsl:param name="count" select="0"/>
  <xsl:param name="url"/>

  <xsl:choose>
  <xsl:when test="$url = ''">
    <xsl:value-of select="$count"/>
  </xsl:when>
  <xsl:when test="substring($url,string-length($url))='\'">
    <xsl:call-template name="bslash-end-count">
      <xsl:with-param name="url"
                      select="substring($url,1,string-length($url)-1)"/>
      <xsl:with-param name="count" select="$count + 1"/>
    </xsl:call-template>
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="$count"/>
  </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- Only URLs in table cells must be escaped (mostly because of multicolumn)
     except for spaces that are always skipped by \nolinkurl{}. Note that
     setting the 'obeyspaces' option to the url package doesn't help.
     -->
<xsl:template name="nolinkurl-output">
  <xsl:param name="url" select="@url"/>
  <xsl:param name="command" select="'\nolinkurl'"/>
  <xsl:variable name="url2">
    <xsl:choose>
      <!-- Behaviour depending on the texlive version -->
      <xsl:when test="number($texlive.version) &gt;= 2009">
        <xsl:call-template name="string-replace">
          <xsl:with-param name="string" select="$url"/>
          <xsl:with-param name="from" select="'\'"/>
          <xsl:with-param name="to" select="'\\'"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$url"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:choose>
  <xsl:when test="ancestor::entry or ancestor::revision or ancestor::footnote">
    <xsl:call-template name="nolinkurl-escape">
      <xsl:with-param name="url" select="$url2"/>
      <xsl:with-param name="command" select="$command"/>
    </xsl:call-template>
    <!-- FIXME: do something with '&' and revision if needed -->
  </xsl:when>
  <xsl:otherwise>
    <xsl:call-template name="nolinkurl-escape">
      <xsl:with-param name="url" select="$url2"/>
      <xsl:with-param name="chars" select="' '"/>
      <xsl:with-param name="command" select="$command"/>
    </xsl:call-template>
  </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- Find the first special char position in the string -->
<xsl:template name="find-first">
  <xsl:param name="string"/>
  <xsl:param name="chars" select="'#% '"/>

  <xsl:choose>
  <xsl:when test="$string = ''">
    <xsl:value-of select="0"/>
  </xsl:when>
  <xsl:when test="string-length($chars)=1">
    <xsl:choose>
    <xsl:when test="contains($string, $chars)">
      <xsl:value-of select="string-length(substring-before($string, $chars))"/>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="string-length($string)"/>
    </xsl:otherwise>
    </xsl:choose>
  </xsl:when>
  <xsl:otherwise>
    <xsl:variable name="charset" select="substring($chars, 2)"/>
    <xsl:variable name="char" select="substring($chars, 1, 1)"/>
    <xsl:choose>
    <xsl:when test="contains($string, $char)">
      <xsl:call-template name="find-first">
        <xsl:with-param name="string" select="substring-before($string, $char)"/>
        <xsl:with-param name="chars" select="$charset"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:call-template name="find-first">
        <xsl:with-param name="string" select="$string"/>
        <xsl:with-param name="chars" select="$charset"/>
      </xsl:call-template>
    </xsl:otherwise>
    </xsl:choose>
  </xsl:otherwise>
  </xsl:choose>
</xsl:template>
  
<xsl:template name="nolinkurl-escape">
  <xsl:param name="escchars"/>
  <xsl:param name="url"/>
  <xsl:param name="chars" select="'#% '"/>
  <xsl:param name="command" select="'\nolinkurl'"/>

  <xsl:variable name="len" select="string-length($url)"/>

  <xsl:variable name="pos">
    <xsl:call-template name="find-first">
      <xsl:with-param name="string" select="$url"/>
      <xsl:with-param name="chars" select="$chars"/>
    </xsl:call-template>
  </xsl:variable>

  <xsl:choose>
  <xsl:when test="$pos = $len">
    <xsl:if test="$escchars != ''">
      <xsl:text>\texttt{</xsl:text>
      <xsl:value-of select="$escchars"/>
      <xsl:text>}</xsl:text>
    </xsl:if>
    <xsl:if test="$len != 0">
      <xsl:call-template name="nolinkurl2">
        <xsl:with-param name="url" select="$url"/>
        <xsl:with-param name="command" select="$command"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:when>
  <xsl:when test="$pos = 0">
    <xsl:call-template name="nolinkurl-escape">
      <xsl:with-param name="escchars"
                      select="concat($escchars, '\', substring($url,1,1))"/>
      <xsl:with-param name="url" select="substring($url, 2)"/>
      <xsl:with-param name="chars" select="$chars"/>
      <xsl:with-param name="command" select="$command"/>
    </xsl:call-template>
  </xsl:when>
  <xsl:otherwise>
    <xsl:if test="$escchars != ''">
      <xsl:text>\texttt{</xsl:text>
      <xsl:value-of select="$escchars"/>
      <xsl:text>}</xsl:text>
    </xsl:if>
    <xsl:call-template name="nolinkurl2">
      <xsl:with-param name="url" select="substring($url, 1, $pos)"/>
      <xsl:with-param name="command" select="$command"/>
    </xsl:call-template>
    <xsl:call-template name="nolinkurl-escape">
      <xsl:with-param name="escchars"
                      select="concat('\', substring($url,$pos+1,1))"/>
      <xsl:with-param name="url" select="substring($url, $pos+2)"/>
      <xsl:with-param name="chars" select="$chars"/>
      <xsl:with-param name="command" select="$command"/>
    </xsl:call-template>
  </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!-- Template for a unit-test:
     <u>
       <nolinkurl>ab%cde%#%fg</nolinkurl>
       <nolinkurl>#ab%cde%#%fg###</nolinkurl>
       <nolinkurl>nothing special</nolinkurl>
       <nolinkurl>nothing\special\</nolinkurl>
       <nolinkurl>nothing\special\\</nolinkurl>
       <entry>
         <nolinkurl>ab%cde%#%fg</nolinkurl>
         <nolinkurl>#%#ab%cde%#%fg###</nolinkurl>
         <nolinkurl>nothing special</nolinkurl>
         <nolinkurl>#########</nolinkurl>
         <nolinkurl>%%%%%%%%%</nolinkurl>
         <nolinkurl>#%#%%#%##</nolinkurl>
       </entry>
     </u>

     Expected:
     \nolinkurl{ab%cde%#%fg}
     \nolinkurl{#ab%cde%#%fg###}
     \nolinkurl{nothing}\texttt{\ }\nolinkurl{special}
     \nolinkurl{nothing\special\ }
     \nolinkurl{nothing\special\\}
     <entry>
       \nolinkurl{ab}\texttt{\%}\nolinkurl{cde}\texttt{\%\#\%}\nolinkurl{fg}
       \texttt{\#\%\#}\nolinkurl{ab}\texttt{\%}\nolinkurl{cde}\texttt{\%\#\%}\nolinkurl{fg}\texttt{\#\#\#}
       \nolinkurl{nothing}\texttt{\ }\nolinkurl{special}
       \texttt{\#\#\#\#\#\#\#\#\#}
       \texttt{\%\%\%\%\%\%\%\%\%}
       \texttt{\#\%\#\%\%\#\%\#\#}
     </entry>

-->
<xsl:template match="nolinkurl">
  <xsl:call-template name="nolinkurl-output">
    <xsl:with-param name="url" select="."/>
  </xsl:call-template>
</xsl:template>

</xsl:stylesheet>