Simpel
April 19, 2023, 8:01pm
1
Hi.
I want to show the diff of a part from a xml file. So far I have a velocity script like this:
{{velocity}}
{{html clean="false"}}
#set ($discard = $xwiki.ssfx.use('uicomponents/viewers/diff.css', true))
#set ($discard = $xwiki.jsfx.use('uicomponents/viewers/diff.js'))
#set ($config = $services.diff.html.defaultConfiguration)
## Change the splitter used for text nodes from 'character' to 'word'.
#set ($discard = $config.setSplitterForNodeType(3, 'word'))
#set ($previousXML = '<setting name="Log_Delete" serializeAs="String">
<value>False</value>
</setting>')
#set ($nextXML = '<setting name="Log_Delete" serializeAs="String">
<value>True</value>
</setting>')
<div class="html-diff">
#set ($htmlDiff = $services.diff.html.unified($previousXML, $nextXML, $config))
#if ($htmlDiff == '')
No changes.
#elseif ("$!htmlDiff" == '')
Failed to compute the changes.
#else
$htmlDiff
#end
</div>
{{/html}}
{{/velocity}}
What I miss are the unchanged lines before and after the difference. Is there a config parameter to achieve this? What config paramter are existing? Couldn’t find more help then this: https://extensions.xwiki.org/xwiki/bin/view/Extension/Diff%20Module .
Regards, Simpel
surli
April 21, 2023, 12:30pm
2
So the problem here is that you’re using the service for displaying HTML diff: the main purpose of this service is to actually visualize the changes between two HTML. In your case, you cannot see the root node context, since it’s an invisible node containing only attributes.
You can see that if you use:
#set ($previousXML = '<setting name="Log_Delete" serializeAs="String">
<value>Anything</value>
<value>False</value>
</setting>')
#set ($nextXML = '<setting name="Log_Delete" serializeAs="String">
<value>Anything</value>
<value>True</value>
</setting>')
you will see the node Anything
.
Now if you want to actually see the diff of the code you can use another API such as:
{{velocity}}
{{html clean="false"}}
#set ($discard = $xwiki.ssfx.use('uicomponents/viewers/diff.css', true))
#set ($discard = $xwiki.jsfx.use('uicomponents/viewers/diff.js'))
#set ($config = $services.diff.html.defaultConfiguration)
## Change the splitter used for text nodes from 'character' to 'word'.
#set ($discard = $config.setSplitterForNodeType(3, 'word'))
#set ($previousXML = '<setting name="Log_Delete" serializeAs="String">
<value>Anything</value>
<value>False</value>
</setting>')
#set ($nextXML = '<setting name="Log_Delete" serializeAs="String">
<value>Anything</value>
<value>True</value>
</setting>')
<div class="html-diff">
## Display the differences between two texts in the unified format.
#foreach ($block in $services.diff.display.unified($previousXML, $nextXML))
$escapetool.xml($block)
#end
</div>
{{/html}}
{{/velocity}}
Simpel
April 22, 2023, 12:48pm
3
Thanks @surli your code is nearly like my second approach (I dismissed the escapetool - otherwise all newlines were gone):
{{velocity}}
#set ($previousXML = '<setting name="Log_Delete" serializeAs="String">
<value>False</value>
<value>Anything</value>
</setting>')
#set ($nextXML = '<setting name="Log_Delete" serializeAs="String">
<value>True</value>
<value>Anything</value>
</setting>')
#set ($discard = $xwiki.ssfx.use('uicomponents/viewers/diff.css', true))
#set ($discard = $xwiki.jsfx.use('uicomponents/viewers/diff.js'))
#set ($config = $services.diff.html.defaultConfiguration)
## Change the splitter used for text nodes from 'character' to 'word'.
#set ($discard = $config.setSplitterForNodeType(3, 'word'))
## Display the differences between two texts in the unified format.
#foreach ($block in $services.diff.display.unified($previousXML, $nextXML))
$block
#end
{{/velocity}}
But that way I miss the green and red colors to see the differences much better. The mix of both worlds would be best.