<?xml version="1.0" encoding="UTF-8" ?>
<!--
	string-tools
	Created by Mehdi Kabab on 2010-03-25.
	Copyright (c) 2010 Mehdi Kabab (http://pioupioum.fr). All rights reserved.
-->
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output encoding="UTF-8" indent="yes" method="xml" />

	<!--
	 | Pad a string to a certain length with another string.
	 |
	 | @param string string The input string.
	 | @param int    length If the value of pad_length is negative, less than, or equal 
	 |                      to the length of the input string, no padding takes place.
	 | @param string pad    The padding characters.
	 | @param mixed  type   Optionnal. The padding direction. Uses 4 or 'LEFT' for padding
	 |                      before the string. Default to 6 or 'RIGHT'.
	 +-->
	<xsl:template name="str_pad">
		<xsl:param name="string"/>
		<xsl:param name="length"/>
		<xsl:param name="pad" select="' '"/>
		<xsl:param name="type" select="6"/>
		<!-- [Internal] Do not used directly this parameters -->
		<xsl:param name="_stack"/>
		<xsl:variable name="_length">
			<xsl:choose>
				<xsl:when test="number($length)">
					<xsl:value-of select="number($length)"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="0"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:choose>
			<xsl:when test="$_length &lt; string-length($string)">
				<xsl:value-of select="$string"/>
			</xsl:when>
			<xsl:when test="$_length &gt; string-length($string) + string-length($_stack)">
				<xsl:call-template name="str_pad">
					<xsl:with-param name="string" select="$string"/>
					<xsl:with-param name="length" select="$length"/>
					<xsl:with-param name="pad"    select="$pad"/>
					<xsl:with-param name="type"   select="$type"/>
					<xsl:with-param name="_stack" select="concat($_stack, $pad)"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:choose>
					<xsl:when test="0 &gt; $_length">
						<xsl:value-of select="$string"/>
					</xsl:when>
					<xsl:when test="4 = $type or $type = 'LEFT'">
						<xsl:variable name="start">
							<xsl:choose>
								<xsl:when test="0 &gt;= string-length(concat($_stack, $string)) - $_length">
									<xsl:value-of select="1"/>
								</xsl:when>
								<xsl:when test="1 = string-length(concat($_stack, $string)) - $_length">
									<xsl:value-of select="2"/>
								</xsl:when>
								<xsl:otherwise>
									<xsl:value-of select="string-length(concat($_stack, $string)) - $_length + 1"/>
								</xsl:otherwise>
							</xsl:choose>
						</xsl:variable>
						<xsl:value-of select="substring(concat($_stack, $string), $start)"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="substring(concat($string, $_stack), 1, $_length)"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<!--
	 | Repeat a string.
	 |
	 | @param string string     The string to be repeated.
	 | @param int    multiplier Number of time the input string should be repeated.
	 |                          multiplier has to be greater than or equal to 0.
	 |                          If the multiplier is set to 0, the function will
	 +-->
	<xsl:template name="str_repeat">
		<xsl:param name="string"/>
		<xsl:param name="multiplier"/>
		<!-- [Internal] Do not used directly this parameter -->
		<xsl:param name="_stack"/>
		<xsl:choose>
			<xsl:when test="0 &gt; $multiplier"/>
			<xsl:when test="0 &lt; $multiplier">
				<xsl:call-template name="str_repeat">
					<xsl:with-param name="string"     select="$string"/>
					<xsl:with-param name="multiplier" select="$multiplier - 1"/>
					<xsl:with-param name="_stack"     select="concat($_stack, $string)"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$_stack"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<!--
	 | Replace all occurrences of the search string with the replacement string.
	 |
	 | @param string search  The value being searched for, otherwise known as the needle.
	 | @param string replace The replacement value that replaces found search values.
	 | @param string subject The string being searched and replaced on.
	 +-->
	<xsl:template name="str_replace">
		<xsl:param name="search"/>
		<xsl:param name="replace"/>
		<xsl:param name="subject"/>
		<xsl:choose>
			<xsl:when test="contains($subject, $search)">
				<xsl:value-of select="substring-before($subject, $search)"/>
				<xsl:value-of select="$replace"/>
				<!-- recursion, we must replace all occurences -->
				<xsl:call-template name="str_replace">
					<xsl:with-param name="search"  select="$search"/>
					<xsl:with-param name="replace" select="$replace"/>
					<xsl:with-param name="subject" select="substring-after($subject, $search)"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$subject"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<!--
	 | Returns the rest of a given string after the last given needle.
	 |
	 | @param string string The string from which to extract a substring.
	 | @param string needle The string containing characters to find.
	 +-->
	<xsl:template name="substring-after-last">
		<xsl:param name="string"/>
		<xsl:param name="needle"/>
		<xsl:choose>
			<xsl:when test="contains($string, $needle)">
				<xsl:call-template name="substring-after-last">
					<xsl:with-param name="string"
					                select="substring-after($string, $needle)"/>
					<xsl:with-param name="needle" select="$needle"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$string"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<!--
	 | Returns the contents of a given string before the last given needle.
	 |
	 | @param string string The string from which to extract a substring.
	 | @param string needle The string containing characters to find.
	 +-->
	<xsl:template name="substring-before-last">
		<xsl:param name="string"/>
		<xsl:param name="needle"/>
		<!-- [Internal] Do not used directly this parameter -->
		<xsl:param name="_stack"/>
		<xsl:choose>
			<xsl:when test="contains($string, $needle)">
				<xsl:call-template name="substring-before-last">
					<xsl:with-param name="string"
					                select="substring-after($string, $needle)"/>
					<xsl:with-param name="needle" select="$needle"/>
					<xsl:with-param name="_stack" select="concat($_stack, substring-before($string, $needle), $needle)"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="substring($_stack, 1, string-length($_stack) - string-length($needle))"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<!--
	 | Truncate the given URL.
	 |
	 | http://pioupioum.fr/developpement/javascript-detecter-support-rgba.html (71 chars)
	 | with a parameter max_length = 60 becomes
	 | pioupioum.fr/.../javascript-detecter-support-rgba.html (54 chars)
	 |
	 | @param string url The URL to truncate.
	 | @param int max_length The limit length for the truncated URL.
	 | @param boolean force
	 +-->
	<xsl:template name="truncate_url">
		<xsl:param name="url"/>
		<xsl:param name="max_length" select="72"/>
		<xsl:param name="force" select="0"/>
		<xsl:choose>
			<xsl:when test="string-length($url) &lt; $max_length">
				<xsl:value-of select="$url"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:variable name="protocol">
					<xsl:if test="contains($url, '://')">
						<xsl:value-of select="concat(substring-before($url, '://'), '://')"/>
					</xsl:if>
				</xsl:variable>
				<xsl:variable name="t_url">
					<xsl:choose>
						<!-- Excludes URL parameters -->
						<xsl:when test="number($force) = 2 and contains($url, '?')">
							<xsl:value-of select="substring-before(substring-after($url, $protocol), '?')"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="substring-after($url, $protocol)"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:variable>
				<xsl:choose>
					<xsl:when test="string-length($t_url) &lt;= $max_length">
						<xsl:value-of select="$t_url"/>
					</xsl:when>
					<xsl:otherwise>
						<xsl:variable name="first">
							<xsl:value-of select="substring-before($t_url, '/')"/>
						</xsl:variable>
						<xsl:variable name="last">
							<xsl:call-template name="substring-after-last">
								<xsl:with-param name="string" select="$t_url"/>
								<xsl:with-param name="needle" select="'/'"/>
							</xsl:call-template>
						</xsl:variable>
						<xsl:choose>
							<xsl:when test="$force &gt;= 1 and ($max_length &lt;= 1 + string-length(concat($first, $last)))">
								<xsl:variable name="remainder">
									<xsl:call-template name="chop_path">
										<xsl:with-param name="path"     select="$last"/>
										<xsl:with-param name="max_size" select="$max_length - string-length($first) - 5"/>
										<xsl:with-param name="beacon"   select="'-'"/>
										<xsl:with-param name="position" select="'START'"/>
									</xsl:call-template>
								</xsl:variable>
								<xsl:value-of select="$first"/>
								<xsl:text>/...</xsl:text>
								<xsl:choose>
									<xsl:when test="0 &lt; string-length($remainder)">
										<xsl:value-of select="$remainder"/>
									</xsl:when>
									<xsl:otherwise>
										<xsl:value-of select="substring($last, string-length(concat($first, $last)) - $max_length + 5)"/>
									</xsl:otherwise>
								</xsl:choose>
							</xsl:when>
							<xsl:otherwise>
								<xsl:variable name="remainder">
									<xsl:text>/</xsl:text>
									<xsl:value-of select="substring($t_url, string-length($first) + 2, string-length($t_url) - string-length($last) - string-length($first))"/>
								</xsl:variable>
								<xsl:variable name="chunk_max_sise" select="$max_length - string-length(concat($first, '/', $last))"/>
								<xsl:choose>
									<xsl:when test="$chunk_max_sise &lt; 1">
										<xsl:value-of select="concat($first, '/.../', $last)"/>
									</xsl:when>
									<xsl:otherwise>
										<xsl:value-of select="$first"/>
										<xsl:call-template name="chop_path">
											<xsl:with-param name="path"     select="$remainder"/>
											<xsl:with-param name="max_size" select="$chunk_max_sise"/>
										</xsl:call-template>
										<xsl:value-of select="concat('/.../', $last)"/>
									</xsl:otherwise>
								</xsl:choose>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<xsl:template name="chop_path">
		<xsl:param name="path"/>
		<xsl:param name="max_size"/>
		<xsl:param name="beacon"   select="'/'"/>
		<xsl:param name="position" select="'END'"/>
		<xsl:choose>
			<xsl:when test="string-length($path) &lt;= $max_size">
				<xsl:value-of select="$path"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:variable name="t_path">
					<xsl:choose>
						<xsl:when test="'START' = $position">
							<xsl:value-of select="substring-after($path, $beacon)"/>
						</xsl:when>
						<xsl:otherwise>
							<xsl:variable name="chunk">
								<xsl:call-template name="substring-after-last">
									<xsl:with-param name="string" select="$path"/>
									<xsl:with-param name="needle" select="$beacon"/>
								</xsl:call-template>
							</xsl:variable>
							<xsl:value-of select="substring($path, 0, string-length($path) - string-length($chunk))"/>
						</xsl:otherwise>
					</xsl:choose>
				</xsl:variable>
				<xsl:call-template name="chop_path">
					<xsl:with-param name="path"     select="$t_path"/>
					<xsl:with-param name="max_size" select="$max_size"/>
					<xsl:with-param name="beacon"   select="$beacon"/>
					<xsl:with-param name="position" select="$position"/>
				</xsl:call-template>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

	<!--
	 | Wraps a string to a given number of characters.
	 +-->
	<xsl:template name="wordwrap">
		<xsl:param name="string"/>
		<xsl:param name="width" select="75"/>
		<xsl:param name="cut"   select="0"/>
		<xsl:param name="break">
			<xsl:text>
</xsl:text>
		</xsl:param>
		<!-- [Internal] Do not used directly this parameter -->
		<xsl:param name="_len" select="0"/>

		<xsl:variable name="n_string" select="normalize-space($string)"/>
		<xsl:choose>
			<xsl:when test="contains($n_string, ' ')">
				<xsl:variable name="before" select="substring-before($n_string, ' ')"/>
				<xsl:choose>
					<xsl:when test="$_len + string-length($before) &lt;= $width">
						<xsl:if test="$_len != 0">
							<xsl:text> </xsl:text>
						</xsl:if>
						<xsl:value-of select="$before"/>
						<xsl:call-template name="wordwrap">
							<xsl:with-param name="string" select="substring-after($n_string, ' ')"/>
							<xsl:with-param name="width"  select="$width"/>
							<xsl:with-param name="break"  select="$break"/>
							<xsl:with-param name="cut"    select="$cut"/>
							<xsl:with-param name="_len"   select="$_len + string-length($before) + 1"/>
						</xsl:call-template>
					</xsl:when>
					<xsl:when test="$width &lt; string-length($before)">
						<xsl:choose>
							<xsl:when test="boolean($cut)">
								<xsl:if test="$width &lt; $_len + string-length(substring(concat(' ', $before), $width))">
									<xsl:copy-of select="$break"/>
								</xsl:if>
								<xsl:value-of select="concat(substring($before, 0, $width), '-')"/>
								<xsl:copy-of select="$break"/>
								<xsl:call-template name="wordwrap">
									<xsl:with-param name="string" select="concat(substring($before, $width), ' ', substring-after($n_string, ' '))"/>
									<xsl:with-param name="width" select="$width"/>
									<xsl:with-param name="break" select="$break"/>
									<xsl:with-param name="cut"   select="$cut"/>
									<xsl:with-param name="_len"  select="0"/>
								</xsl:call-template>
							</xsl:when>
							<xsl:otherwise>
								<xsl:copy-of select="$break"/>
								<xsl:value-of select="$before"/>
								<xsl:copy-of select="$break"/>
								<xsl:call-template name="wordwrap">
									<xsl:with-param name="string" select="substring-after($n_string, ' ')"/>
									<xsl:with-param name="width"  select="$width"/>
									<xsl:with-param name="break"  select="$break"/>
									<xsl:with-param name="cut"    select="$cut"/>
									<xsl:with-param name="_len"   select="0"/>
								</xsl:call-template>
							</xsl:otherwise>
						</xsl:choose>
					</xsl:when>
					<xsl:otherwise>
						<xsl:copy-of select="$break"/>
						<xsl:call-template name="wordwrap">
							<xsl:with-param name="string" select="$n_string"/>
							<xsl:with-param name="width"  select="$width"/>
							<xsl:with-param name="break"  select="$break"/>
							<xsl:with-param name="cut"    select="$cut"/>
							<xsl:with-param name="_len"   select="0"/>
						</xsl:call-template>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise>
				<xsl:if test="$_len != 0">
					<xsl:text> </xsl:text>
				</xsl:if>
				<xsl:value-of select="$n_string"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>

</xsl:stylesheet>