-
-
Save DavidFichtmueller/69065633fee55101c338a3688daf353c 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 | |
into HTML formatted hyperlinks. | |
Example on how to call the template: | |
<xsl:template match="content"> | |
<xsl:call-template name="hyperlink"> | |
<xsl:with-param name="string" select="."/> | |
</xsl:call-template> | |
</xsl:template> | |
--> | |
<xsl:output method="html" /> | |
<xsl:template name="hyperlink"> | |
<!-- Input --> | |
<xsl:param name="string" select="string()" /> | |
<!-- adding a space at the end, so urls which are at the end of the string are also detected, the space will be removed afterwards --> | |
<xsl:variable name="string" select="concat($string,' ')" /> | |
<xsl:variable name="http"> | |
<xsl:choose> | |
<!-- check if the text contains http links --> | |
<xsl:when test="contains($string, 'http://')"> | |
<xsl:choose> | |
<!-- next check if the text contains https links --> | |
<xsl:when test="contains($string, 'https://')"> | |
<xsl:choose> | |
<!-- the text contains both http and http links, therefore check which one comes first --> | |
<xsl:when test="string-length(substring-before($string, 'http://')) < string-length(substring-before($string, 'https://'))">http://</xsl:when> | |
<xsl:otherwise>https://</xsl:otherwise> | |
</xsl:choose> | |
</xsl:when> | |
<!-- the text only contains http links --> | |
<xsl:otherwise>http://</xsl:otherwise> | |
</xsl:choose> | |
</xsl:when> | |
<xsl:otherwise> | |
<!-- there are no http links in the entire text, next check if there are any https links --> | |
<xsl:choose> | |
<xsl:when test="contains($string, 'https://')">https://</xsl:when> | |
</xsl:choose> | |
</xsl:otherwise> | |
</xsl:choose> | |
</xsl:variable> | |
<!-- Vars --> | |
<xsl:variable name="before" select="substring-before($string, $http)" /> | |
<xsl:variable name="after" select="substring-after($string, $http)" /> | |
<xsl:variable name="url"> | |
<xsl:choose> | |
<!-- check which comes first, a line break ( ) or a space --> | |
<xsl:when test="contains($after, ' ') and string-length(substring-before($after, ' ')) < string-length(substring-before($after, ' '))"> | |
<xsl:value-of select="concat($http, substring-before($after, ' '))" /> | |
</xsl:when> | |
<xsl:otherwise> | |
<xsl:value-of select="concat($http, substring-before($after, ' '))" /> | |
</xsl:otherwise> | |
</xsl:choose> | |
</xsl:variable> | |
<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="substring($rest,0,string-length($rest))" /> <!-- remove the padded space from the end, the next interation will add its own --> | |
</xsl:call-template> | |
</xsl:when> | |
<xsl:otherwise> | |
<xsl:value-of select="substring($after,0,string-length($after))" /> <!-- remove the padded space from the end --> | |
</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