| 1 |
<?xml version='1.0' encoding='UTF-8'?><!-- -*- indent-tabs-mode: nil -*- --> |
| 2 |
<!-- |
| 3 |
This program is free software; you can redistribute it and/or modify it under |
| 4 |
the terms of the GNU Lesser General Public License as published by the Free |
| 5 |
Software Foundation; either version 2 of the License, or (at your option) any |
| 6 |
later version. |
| 7 |
|
| 8 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 10 |
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 11 |
details. |
| 12 |
|
| 13 |
You should have received a copy of the GNU Lesser General Public License |
| 14 |
along with this program; see the file COPYING.LGPL. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
--> |
| 16 |
|
| 17 |
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" |
| 18 |
version="1.0"> |
| 19 |
|
| 20 |
<!--!!========================================================================== |
| 21 |
Utilities |
| 22 |
Common XSLT Utilities. |
| 23 |
@revision[version=1.0 date=2010-05-25 status=final] |
| 24 |
|
| 25 |
This stylesheet contains various useful utilities that are used throughout |
| 26 |
the Yelp stylesheets. |
| 27 |
--> |
| 28 |
|
| 29 |
|
| 30 |
<!--**========================================================================== |
| 31 |
utils.repeat_string |
| 32 |
Repeat a string a given number of times. |
| 33 |
@revision[version=1.0 date=2011-08-24 status=final] |
| 34 |
|
| 35 |
[xsl:params] |
| 36 |
$string: The string to repeat. |
| 37 |
$number: The number of times to repeat $string. |
| 38 |
|
| 39 |
This template repeats the $string argument $number times. |
| 40 |
--> |
| 41 |
<xsl:template name="utils.repeat_string"> |
| 42 |
<xsl:param name="string" select="''"/> |
| 43 |
<xsl:param name="number" select="0"/> |
| 44 |
<xsl:if test="$number > 0"> |
| 45 |
<xsl:value-of select="$string"/> |
| 46 |
<xsl:call-template name="utils.repeat_string"> |
| 47 |
<xsl:with-param name="string" select="$string"/> |
| 48 |
<xsl:with-param name="number" select="$number - 1"/> |
| 49 |
</xsl:call-template> |
| 50 |
</xsl:if> |
| 51 |
</xsl:template> |
| 52 |
|
| 53 |
|
| 54 |
<!--**========================================================================== |
| 55 |
utils.strip_newlines |
| 56 |
Strip leading or trailing newlines from a string. |
| 57 |
@revision[version=1.0 date=2010-05-25 status=final] |
| 58 |
|
| 59 |
[xsl:params] |
| 60 |
$string: The string to strip newlines from. |
| 61 |
$leading: Whether to strip leading newlines. |
| 62 |
$trailing: Whether to strip trailing newlines. |
| 63 |
|
| 64 |
This template strips at most one leading and one trailing newline from |
| 65 |
$string. This is useful for preformatted block elements where leading and |
| 66 |
trailing newlines are ignored to make source formatting easier for authors. |
| 67 |
--> |
| 68 |
<xsl:template name="utils.strip_newlines"> |
| 69 |
<xsl:param name="string"/> |
| 70 |
<xsl:param name="leading" select="false()"/> |
| 71 |
<xsl:param name="trailing" select="false()"/> |
| 72 |
<xsl:choose> |
| 73 |
<xsl:when test="$leading"> |
| 74 |
<xsl:variable name="new"> |
| 75 |
<xsl:choose> |
| 76 |
<xsl:when test="starts-with($string, '
')"> |
| 77 |
<xsl:value-of select="substring($string, 2)"/> |
| 78 |
</xsl:when> |
| 79 |
<xsl:otherwise> |
| 80 |
<xsl:value-of select="$string"/> |
| 81 |
</xsl:otherwise> |
| 82 |
</xsl:choose> |
| 83 |
</xsl:variable> |
| 84 |
<xsl:choose> |
| 85 |
<xsl:when test="$trailing"> |
| 86 |
<xsl:call-template name="utils.strip_newlines"> |
| 87 |
<xsl:with-param name="string" select="$new"/> |
| 88 |
<xsl:with-param name="leading" select="false()"/> |
| 89 |
<xsl:with-param name="trailing" select="true()"/> |
| 90 |
</xsl:call-template> |
| 91 |
</xsl:when> |
| 92 |
<xsl:otherwise> |
| 93 |
<xsl:value-of select="$new"/> |
| 94 |
</xsl:otherwise> |
| 95 |
</xsl:choose> |
| 96 |
</xsl:when> |
| 97 |
<xsl:when test="$trailing"> |
| 98 |
<xsl:choose> |
| 99 |
<xsl:when test="substring($string, string-length($string)) = '
'"> |
| 100 |
<xsl:value-of select="substring($string, 1, string-length($string) - 1 )"/> |
| 101 |
</xsl:when> |
| 102 |
<xsl:otherwise> |
| 103 |
<xsl:value-of select="$string"/> |
| 104 |
</xsl:otherwise> |
| 105 |
</xsl:choose> |
| 106 |
</xsl:when> |
| 107 |
</xsl:choose> |
| 108 |
</xsl:template> |
| 109 |
|
| 110 |
|
| 111 |
<!--**========================================================================== |
| 112 |
utils.linenumbering |
| 113 |
Number each line in a verbatim environment. |
| 114 |
@revision[version=1.0 date=2010-12-03 status=final] |
| 115 |
|
| 116 |
[xsl:params] |
| 117 |
$node: The verbatim element to create the line numbering for. |
| 118 |
$number: The starting line number. |
| 119 |
|
| 120 |
This template outputs a string with line numbers for each line in a verbatim |
| 121 |
elements. Each line number is on its own line, allowing the output string to |
| 122 |
be placed to the side of the verbatim output. |
| 123 |
--> |
| 124 |
<xsl:template name="utils.linenumbering"> |
| 125 |
<xsl:param name="node" select="."/> |
| 126 |
<xsl:param name="number" select="1"/> |
| 127 |
<xsl:param name="string"> |
| 128 |
<xsl:choose> |
| 129 |
<xsl:when test="$node/node()[1]/self::text() and starts-with($node/node()[1], '
')"> |
| 130 |
<xsl:value-of select="substring-after(string($node), '
')"/> |
| 131 |
</xsl:when> |
| 132 |
<xsl:otherwise> |
| 133 |
<xsl:value-of select="string($node)"/> |
| 134 |
</xsl:otherwise> |
| 135 |
</xsl:choose> |
| 136 |
</xsl:param> |
| 137 |
<xsl:choose> |
| 138 |
<xsl:when test="contains($string, '
')"> |
| 139 |
<xsl:number value="$number"/> |
| 140 |
<xsl:text>
</xsl:text> |
| 141 |
<xsl:call-template name="utils.linenumbering"> |
| 142 |
<xsl:with-param name="node" select="$node"/> |
| 143 |
<xsl:with-param name="number" select="$number + 1"/> |
| 144 |
<xsl:with-param name="string" |
| 145 |
select="substring-after($string, '
')"/> |
| 146 |
</xsl:call-template> |
| 147 |
</xsl:when> |
| 148 |
<xsl:when test="string-length($string) != 0"> |
| 149 |
<xsl:number value="$number"/> |
| 150 |
<xsl:text>
</xsl:text> |
| 151 |
</xsl:when> |
| 152 |
</xsl:choose> |
| 153 |
</xsl:template> |
| 154 |
|
| 155 |
|
| 156 |
<!--**========================================================================== |
| 157 |
utils.email_address |
| 158 |
Get an email address from a mailto URL. |
| 159 |
@revision[version=3.4 date=2012-01-18 status=final] |
| 160 |
|
| 161 |
[xsl:params] |
| 162 |
$href: The mailto URL. |
| 163 |
|
| 164 |
This template takes a mailto URL and returns an email address, stripping the |
| 165 |
URL scheme as well as any query string. |
| 166 |
--> |
| 167 |
<xsl:template name="utils.email_address"> |
| 168 |
<xsl:param name="url"/> |
| 169 |
<xsl:variable name="addy"> |
| 170 |
<xsl:value-of select="substring-after($url, 'mailto:')"/> |
| 171 |
</xsl:variable> |
| 172 |
<xsl:choose> |
| 173 |
<xsl:when test="contains($addy, '?')"> |
| 174 |
<xsl:value-of select="substring-before($addy, '?')"/> |
| 175 |
</xsl:when> |
| 176 |
<xsl:otherwise> |
| 177 |
<xsl:value-of select="$addy"/> |
| 178 |
</xsl:otherwise> |
| 179 |
</xsl:choose> |
| 180 |
</xsl:template> |
| 181 |
|
| 182 |
</xsl:stylesheet> |