Skip to content

Instantly share code, notes, and snippets.

@terrywbrady
Created December 12, 2017 16:58
Show Gist options
  • Save terrywbrady/c022e2cb3ec64c34b3b613b6fffb59bc to your computer and use it in GitHub Desktop.
Save terrywbrady/c022e2cb3ec64c34b3b613b6fffb59bc to your computer and use it in GitHub Desktop.
DSpace XMLUI XSLT to convert URL's to links and Markdown to Html
<xsl:template name="summaryHeaderMarkdown">
<xsl:apply-templates select="dim:field[@element='description' and not(@qualifier)][starts-with(.,'[MD]')]" mode="markdown"/>
</xsl:template>
<xsl:template match="dim:field[@element='description' and not(@qualifier)][starts-with(.,'[MD]')]" mode="markdown">
<xsl:call-template name="markdown-text">
<xsl:with-param name="val" select="substring-after(text(),'[MD]')"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="dim:field" mode="markdown"/>
<xsl:template name="markdown-text">
<xsl:param name="val"/>
<xsl:choose>
<xsl:when test="starts-with($val, '*')">
<xsl:variable name="first" select="substring-before($val, '&#13;&#10;&#13;&#10;')"/>
<xsl:variable name="rest" select="substring-after($val, '&#13;&#10;&#13;&#10;')"/>
<xsl:choose>
<xsl:when test="string-length($val)=0"/>
<xsl:when test="string-length($first)=0">
<ul class="markdown">
<xsl:call-template name="markdown-list">
<xsl:with-param name="val" select="$val"/>
</xsl:call-template>
</ul>
</xsl:when>
<xsl:when test="string-length($rest)=0">
<ul class="markdown">
<xsl:call-template name="markdown-list">
<xsl:with-param name="val" select="$first"/>
</xsl:call-template>
</ul>
</xsl:when>
<xsl:otherwise>
<ul class="markdown">
<xsl:call-template name="markdown-list">
<xsl:with-param name="val" select="$first"/>
</xsl:call-template>
</ul>
<xsl:call-template name="markdown-text">
<xsl:with-param name="val" select="$rest"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="first" select="substring-before($val, '&#10;')"/>
<xsl:variable name="rest" select="substring-after($val, '&#10;')"/>
<xsl:choose>
<xsl:when test="string-length($val)=0"/>
<xsl:when test="string-length($first)=0">
<xsl:call-template name="markdown-line">
<xsl:with-param name="val" select="$val"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="string-length($rest)=0">
<xsl:call-template name="markdown-line">
<xsl:with-param name="val" select="$first"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="markdown-line">
<xsl:with-param name="val" select="$first"/>
<xsl:with-param name="rest" select="$rest"/>
</xsl:call-template>
<xsl:call-template name="markdown-text">
<xsl:with-param name="val" select="$rest"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="markdown-line">
<xsl:param name="val"/>
<xsl:param name="rest"/>
<xsl:choose>
<xsl:when test="starts-with($val, '####')">
<h4 class="ds-div-head">
<xsl:call-template name="markdown-phrase">
<xsl:with-param name="val" select="substring-after($val, '####')"/>
</xsl:call-template>
</h4>
</xsl:when>
<xsl:when test="starts-with($val, '###')">
<h3 class="ds-div-head">
<xsl:call-template name="markdown-phrase">
<xsl:with-param name="val" select="substring-after($val, '###')"/>
</xsl:call-template>
</h3>
</xsl:when>
<xsl:when test="starts-with($val, '##')">
<h2 class="ds-div-head">
<xsl:call-template name="markdown-phrase">
<xsl:with-param name="val" select="substring-after($val, '##')"/>
</xsl:call-template>
</h2>
</xsl:when>
<xsl:when test="starts-with($val, '#')">
<h1 class="ds-div-head">
<xsl:call-template name="markdown-phrase">
<xsl:with-param name="val" select="substring-after($val, '#')"/>
</xsl:call-template>
</h1>
</xsl:when>
<xsl:when test="starts-with($rest, '&#13;&#10;')">
<p class="ds-paragraph">
<xsl:call-template name="markdown-phrase">
<xsl:with-param name="val" select="$val"/>
</xsl:call-template>
</p>
</xsl:when>
<xsl:otherwise>
<p class="ds-paragraph markdown">
<xsl:call-template name="markdown-phrase">
<xsl:with-param name="val" select="$val"/>
</xsl:call-template>
</p>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="markdown-list">
<xsl:param name="val"/>
<xsl:variable name="first" select="substring-before($val, '&#10;')"/>
<xsl:variable name="rest" select="substring-after($val, '&#10;')"/>
<xsl:choose>
<xsl:when test="string-length($val)=0"/>
<xsl:when test="string-length($first)=0">
<xsl:call-template name="markdown-li">
<xsl:with-param name="val" select="$val"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="string-length($rest)=0">
<xsl:call-template name="markdown-li">
<xsl:with-param name="val" select="$first"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="markdown-li">
<xsl:with-param name="val" select="$first"/>
</xsl:call-template>
<xsl:call-template name="markdown-list">
<xsl:with-param name="val" select="$rest"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="markdown-li">
<xsl:param name="val"/>
<li>
<xsl:call-template name="markdown-phrase">
<xsl:with-param name="val">
<xsl:choose>
<xsl:when test="starts-with($val,'*')">
<xsl:value-of select="substring-after($val, '*')"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$val"/>
</xsl:otherwise>
</xsl:choose>
</xsl:with-param>
</xsl:call-template>
</li>
</xsl:template>
<xsl:template name="markdown-phrase">
<xsl:param name="val"/>
<xsl:variable name="t1">
<xsl:call-template name="markdown-phrase-test">
<xsl:with-param name="val" select="$val"/>
<xsl:with-param name="sep">*</xsl:with-param>
<xsl:with-param name="elem">b</xsl:with-param>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="t2">
<xsl:call-template name="markdown-phrase-test">
<xsl:with-param name="node" select="$t1"/>
<xsl:with-param name="val" select="$val"/>
<xsl:with-param name="sep">_</xsl:with-param>
<xsl:with-param name="elem">em</xsl:with-param>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="string-length($t2) > 0">
<xsl:copy-of select="$t2"/>
</xsl:when>
<xsl:when test="string-length($val)=0"/>
<xsl:otherwise>
<xsl:apply-templates select="text()" mode="urltext">
<xsl:with-param name="val" select="$val"/>
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="markdown-phrase-test">
<xsl:param name="node"/>
<xsl:param name="val"/>
<xsl:param name="sep"/>
<xsl:param name="elem"/>
<xsl:choose>
<xsl:when test="string-length($node) > 0">
<xsl:copy-of select="$node"/>
</xsl:when>
<xsl:otherwise>
<xsl:if test="contains($val, concat(' ',$sep))">
<xsl:variable name="first" select="substring-before($val, concat(' ',$sep))"/>
<xsl:variable name="rest" select="substring-after($val, concat(' ',$sep))"/>
<xsl:if test="contains($rest, $sep)">
<xsl:variable name="middle" select="substring-before($rest, $sep)"/>
<xsl:variable name="last" select="substring-after($rest, $sep)"/>
<xsl:call-template name="markdown-phrase">
<xsl:with-param name="val" select="$first"/>
</xsl:call-template>
<xsl:text> </xsl:text>
<xsl:element name="{$elem}">
<xsl:call-template name="markdown-phrase">
<xsl:with-param name="val" select="$middle"/>
</xsl:call-template>
</xsl:element>
<xsl:text> </xsl:text>
<xsl:call-template name="markdown-phrase">
<xsl:with-param name="val" select="$last"/>
</xsl:call-template>
</xsl:if>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="dim:field" mode="urltext">
<xsl:param name="title-hover"/>
<xsl:apply-templates select="*|text()" mode="urltext">
<xsl:with-param name="title-hover" select="$title-hover"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="*|@*" mode="urltext">
<xsl:param name="title-hover"/>
<xsl:param name="val" select="."/>
<xsl:copy>
<xsl:apply-templates select="*|@*|text()" mode="urltext">
<xsl:with-param name="title-hover" select="$title-hover"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="text()" mode="urltext">
<xsl:param name="val" select="."/>
<xsl:param name="title-hover"/>
<xsl:variable name="valnorm" select="normalize-space(translate($val,'&#10;&#9;&#13;',' '))" />
<xsl:variable name="pre">
<xsl:choose>
<xsl:when test="string-length(substring-before($valnorm,' ')) = 0">
<xsl:value-of select="$valnorm"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-before($valnorm,' ')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="hover">
<xsl:choose>
<xsl:when test="string-length($title-hover) &gt; 0">
<xsl:value-of select="$title-hover"/>
</xsl:when>
<xsl:when test="starts-with($pre, 'http://hdl.handle.net')">
<xsl:text>Permanent link to this item. Use this URL to cite this page.</xsl:text>
</xsl:when>
</xsl:choose>
</xsl:variable>
<xsl:variable name="suff" select="substring($valnorm, string-length($pre) + 1)"/>
<xsl:choose>
<xsl:when test="starts-with($pre,'http://') or starts-with($pre,'https://') or starts-with($pre,'mailto:')">
<a>
<xsl:if test="$hover">
<xsl:attribute name="title">
<xsl:value-of select="$hover"/>
</xsl:attribute>
</xsl:if>
<xsl:attribute name="href">
<xsl:value-of select="$pre"/>
</xsl:attribute>
<xsl:choose>
<xsl:when test="contains($pre,'search.serialssolutions.com')">
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:text>Find Full Text at Georgetown University Library</xsl:text>
</xsl:when>
<xsl:when test="starts-with($pre,'http://dx.doi.org')">
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:text>Full Text from Publisher</xsl:text>
</xsl:when>
<xsl:when test="starts-with($pre,'http://worldcatlibraries.org')">
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:text>Find in a Library</xsl:text>
</xsl:when>
<xsl:when test="starts-with($pre,'http://worldcat.org')">
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:text>Find in a Library</xsl:text>
</xsl:when>
<xsl:when test="starts-with($pre,'http://www.worldcat.org')">
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:text>Find in a Library</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$pre"/>
</xsl:otherwise>
</xsl:choose>
</a>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$pre"/>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="string-length($suff) &gt; 0">
<xsl:text> </xsl:text>
<xsl:apply-templates select="." mode="urltext">
<xsl:with-param name="title-hover" select="$title-hover"/>
<xsl:with-param name="val" select="$suff"/>
</xsl:apply-templates>
</xsl:if>
</xsl:template>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment