Skip to content

Instantly share code, notes, and snippets.

@gimsieke
Created May 1, 2019 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gimsieke/e7a10d254e0aa6697898217cdb14a7db to your computer and use it in GitHub Desktop.
Save gimsieke/e7a10d254e0aa6697898217cdb14a7db to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<p:declare-step xmlns:p="http://www.w3.org/ns/xproc" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:c="http://www.w3.org/ns/xproc-step" xmlns:cx="http://xmlcalabash.com/ns/extensions"
xmlns:dc="http://www.delightfulcomputing.com/ns/" version="1.0" name="textdiff-with-options" type="dc:textdiff-with-options">
<p:documentation/>
<p:option name="whitespace" select="'observe'">
<p:documentation>Alternatives: ignore, normalize. Whether inline whitespace is ignored or contracted to a single space
before comparison.</p:documentation>
</p:option>
<p:option name="original-uri" select="''"/>
<p:option name="original-text" select="''"/>
<p:option name="revised-uri" select="''"/>
<p:option name="revised-text" select="''"/>
<p:option name="encoding" select="''"/>
<p:output port="result" primary="true"/>
<p:serialization port="result" omit-xml-declaration="false" indent="true"/>
<p:output port="space" sequence="true">
<p:pipe port="space" step="space-modified"/>
</p:output>
<p:serialization port="space" omit-xml-declaration="false" indent="true"/>
<p:import href="http://xmlcalabash.com/extension/steps/library-1.0.xpl"/>
<p:import href="textdiff.xpl"/>
<p:variable name="original-string"
select="if ($original-uri)
then if ($encoding)
then unparsed-text($original-uri, $encoding)
else unparsed-text($original-uri)
else $original-text"/>
<p:variable name="revised-string"
select="if ($revised-uri)
then if ($encoding)
then unparsed-text($revised-uri, $encoding)
else unparsed-text($revised-uri)
else $revised-text"/>
<dc:textdiff name="diff">
<p:input port="original"><p:empty/></p:input>
<p:input port="revised"><p:empty/></p:input>
<p:with-option name="original-text" select="$original-string"/>
<p:with-option name="revised-text" select="$revised-string"/>
</dc:textdiff>
<p:choose name="space-modified">
<p:when test="$whitespace = ('ignore', 'normalize')">
<p:output port="result" primary="true"/>
<p:output port="space" sequence="true">
<p:pipe port="result" step="space-modified-diff"/>
</p:output>
<p:variable name="space-modified-original"
select="if ($whitespace = 'ignore')
then replace($original-string, '[ \t]+', '', 'm')
else replace($original-string, '[ \t]+', ' ', 'm')"/>
<p:variable name="space-modified-revised"
select="if ($whitespace = 'ignore')
then replace($revised-string, '[ \t]+', '', 'm')
else replace($revised-string, '[ \t]+', ' ', 'm')"/>
<dc:textdiff name="space-modified-diff">
<p:input port="original"><p:empty/></p:input>
<p:input port="revised"><p:empty/></p:input>
<p:with-option name="original-text" select="$space-modified-original"/>
<p:with-option name="revised-text" select="$space-modified-revised"/>
</dc:textdiff>
<p:sink/>
<p:xslt name="consolidate">
<p:input port="source">
<p:pipe port="result" step="diff"/>
<p:pipe port="result" step="space-modified-diff"/>
</p:input>
<p:input port="stylesheet">
<p:inline>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:variable name="diff" as="document-node(element(dc:textdiff))" select="collection()[1]"/>
<xsl:variable name="space-modified-diff" as="document-node(element(dc:textdiff))" select="collection()[2]"/>
<xsl:template match="* | @*">
<xsl:copy>
<xsl:apply-templates select="@*, node()"/>
</xsl:copy>
</xsl:template>
<xsl:key name="change-by-orig" match="dc:delta[@type = 'change']"
use="string-join(dc:original/(@position, @size), '_')"/>
<xsl:template match="dc:delta[@type = 'change']
[empty(key('change-by-orig',
string-join(dc:original/(@position, @size), '_'),
$space-modified-diff))]">
<xsl:variable name="next-pos" as="xs:integer?"
select="following-sibling::dc:delta[dc:original][1]/dc:original/@position"/>
<xsl:apply-templates
select="$space-modified-diff/dc:textdiff/dc:delta[dc:original/@position >= current()/dc:original/@position]
[not(dc:original/@position >= $next-pos)]"/>
</xsl:template>
</xsl:stylesheet>
</p:inline>
</p:input>
<p:input port="parameters"><p:empty/></p:input>
</p:xslt>
</p:when>
<p:otherwise>
<p:output port="result" primary="true"/>
<p:output port="space" sequence="true">
<p:empty/>
</p:output>
<p:identity/>
</p:otherwise>
</p:choose>
</p:declare-step>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment