Created
January 12, 2014 22:11
-
-
Save ijy/8391348 to your computer and use it in GitHub Desktop.
Converts any plain text URL strings into HTML formatted hyperlinks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<xsl:stylesheet version="1.0" | |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> | |
<!-- | |
Converts any plain text URL strings from a passage of text | |
inot HTML formatted hyperlinks. | |
--> | |
<xsl:template name="hyperlink"> | |
<!-- Input --> | |
<xsl:param name="string" select="string()" /> | |
<xsl:choose> | |
<xsl:when test="contains($string, 'http://')"> | |
<xsl:variable name="http" select="http://" /> | |
</xsl:when> | |
<xsl:when test="contains($string, 'https://')"> | |
<xsl:variable name="http" select="https://" /> | |
</xsl:when> | |
</xsl:choose> | |
<!-- Vars --> | |
<xsl:variable name="before" select="substring-before($string, $http)" /> | |
<xsl:variable name="after" select="substring-after($string, 'http://')" /> | |
<xsl:variable name="url" select="concat($http, substring-before($after, ' '))"/> | |
<xsl:variable name="rest" select="substring-after($string, $url)" /> | |
<!-- Output --> | |
<xsl:value-of select="$before"/> | |
<xsl:choose> | |
<!-- If the url is at then end, $rest doesn't work --> | |
<xsl:when test="substring-after($url, $http) != ''"> | |
<a href="{$url}"> | |
<xsl:value-of select="$url" /> | |
</a> | |
<xsl:call-template name="hyperlink"> | |
<xsl:with-param name="string" select="$rest"/> | |
</xsl:call-template> | |
</xsl:when> | |
<xsl:otherwise> | |
<a href="{$url}{$after}"> | |
<xsl:value-of select="$after" /> | |
</a> | |
</xsl:otherwise> | |
</xsl:choose> | |
</xsl:template> | |
</xsl:stylesheet> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment