<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
<xsl:stylesheet 
	version="1.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:msxml="urn:schemas-microsoft-com:xslt"
	xmlns:htmlDecode="urn:htmlDecode"
	xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" 
	exclude-result-prefixes="htmlDecode msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">

	

<xsl:output method="xml" omit-xml-declaration="yes"/>

<!--

This document deals with how to edit localLinks before the are parsed by the XML engine, into links which are site relative. 

In our case this was useful as we wanted to use the localLink ID to pass into a query string for the page. This query
string controlled the media which played back via a FlowPlayer control. 

By outputting the value of each variable its easy to see how they link together.

lau@voodoodog.com
20th October 2009

	Reference

	http://our.umbraco.org/wiki/reference/xslt/extend-your-xslt-with-custom-functions
	http://www.exslt.org/regexp/functions/replace/regexp.replace.html (quite limited)
	http://www.exslt.org/regexp/functions/match/index.html (more useful)
	http://our.umbraco.org/wiki/reference/umbracolibrary (very useful)
	
	Layout
	
	1. CUSTOM XSLT FUNCTION
	2. GET THE CURRENT PAGES URL (variable)
	3. GET THE CURRENT PAGES CONTENT and ENCODE (variable)
	4. SET THE REGEX STRING REPLACEMENT
	5. RUN REGEX REPLACE
	6. GET REPLACE OUTPUT, DECODE & SPIT OUT. 

-->


<!--

CUSTOM XSLT FUNCTION - htmlDecode

This code has been referenced from the Umbraco Wiki, and reference can be found at...

http://our.umbraco.org/wiki/reference/xslt/extend-your-xslt-with-custom-functions
	
-->

<msxml:script implements-prefix="htmlDecode" language="C#">
<msxml:assembly name="System.Web"/>
<msxml:using namespace="System.Web"/>
<![CDATA[
public string convertText(string text)
{
    string decodedStr = HttpUtility.HtmlDecode(text);
    return decodedStr;
}
]]>
</msxml:script>

<xsl:param name="currentPage"/>

<xsl:template match="/">


<!--

CURRENT PAGE URL	
	
We need the URL for the page we want to make the links reference too.
Using the 'umbraco.library:NiceURL(string)' function.

This URL is assigned the variable '$niceURL'.	
-->
	<xsl:variable name="niceURL">
		<xsl:value-of select="umbraco.library:NiceUrl($currentPage/@id)"/>
	</xsl:variable>


<!--

CONTENT CONTAINING LINKS	
	
Next we grab the content which contains our links. In this case it was a richtext editor.
To be able to process this with the eXSLT:Regex extension we need to encode the characters in the HTML.

This is really easy to do using the 'umbraco.library:HtmlEncode(string)' function.

-->
	<xsl:variable name="assetTeaser">
		<xsl:value-of select="umbraco.library:HtmlEncode(data [@alias = 'assetTeaser'])"/>
	</xsl:variable>


<!--

REGEX STRING REPLACEMENT	
	
For some reason I couldn't write the whole replacement string in the regex replace function. So I pull in
the string I wanted by setting it as a variable. If anyone knows how to avoid this please let me know.	
-->
	<xsl:variable name="niceURLregex">
	..<xsl:value-of select="$niceURL"/>?clip=$1
	</xsl:variable>

	
<!--

THE SEXY PART, REGEX REPLACE.	
	
Okay, so we use an eXSLT function called :replace. 
1. Firstly we feed it an 'input', in this case the variable $assetTeaser (which contains an htmlEncoded version of [@alias = 'assetTeaser'])
2. Now we need to give a Regular Expression.
	in this case, we are looking for localLink, and a 4 digit number. N.b. the number could actually be 4+ digits long depending on the size of your site.
3. Now we inform the process that its a multiline input (m), a ungreedy pattern (U) and to treat each string as a single line (s); giving us 'mUs'.
4. Next we give the replacement information.
	As noted above I couldn't work out how to make this work without using a variable for it all. ?clip=$1 worked, as did $niceURL, but I could not
	get any combination of the above, using brackets, etc to work. I'm sure someone knows though!
	
	So it grabs the 'String Replacement' from a variable we've already set called '$niceURLregex'.
	
	This string tells is to;
		.. 			-	move to the root.
		$niceURL	-	get the niceURL variable, defined in the top of the document. This is just the URL for the current page.
		?clip=		- 	add some static text
		$1			- 	add the string, e.g "1234" in /{localLink:(1234)}
		
5. Next we disable-output-escaping. (not needed, but fails if enabled)
	
-->
	<xsl:variable name="assetTeaserReplace">
		<xsl:value-of select="Exslt.ExsltRegularExpressions:replace($assetTeaser, '/{localLink:([0-9][0-9][0-9][0-9])}', 'mUs', $niceURLregex )" disable-output-escaping="no" />	
	</xsl:variable>

	
<!--

SPIT THE CONTENT OUT, and REMEMBER TO DECODE THE HTML
Okay so we've taken $assetTeaser, and using some regex we've updated the links. Now we need to Decode it. (we encoded it to start with).

1. We simply use the custom function we setup in the top of the document which decodes the HTML! Disable-Output-Escaping, and voila! 
	
-->
	
	<xsl:value-of select="htmlDecode:convertText(string($assetTeaserReplace))" disable-output-escaping="yes" />
	

</xsl:template>

</xsl:stylesheet>