automatic character comparison

Dear list, I have the following sample: \setuppapersize[A7] \starttext \definecolumnset [paral] [n=2] \definesubcolumnset[paral][1][1] \definesubcolumnset[paral][2][2] \startcolumnset[paral] \startsubcolumnset[1] abce \stopsubcolumnset \startsubcolumnset[2] abcd \stopsubcolumnset \flushsubcolumnsets[spread] \startsubcolumnset[1] abc% \inframed [background=color, backgroundcolor=lightgreen, frame=off] {e} \stopsubcolumnset \startsubcolumnset[2] abc% \inframed [background=color, backgroundcolor=lightred, frame=off] {d} \stopsubcolumnset \flushsubcolumnsets[spread] \stopcolumnset \stoptext What I want to achieve is automatic text comparison between versions of the same text (in different subcolumnsets). The first line shows different versions text. I wonder whether there would be an automatic way to get the \inframed highlighting with any character that differs from the other column (it might be different, or just missing or being added). I think this may be possible with ConTeXt, but I don’t know how to achieve it automagically. Any ideas on how to get this automatic text comparison? Many thanks in advance, Pablo

On 4 Mar 2025, at 18:26, Pablo Rodriguez via ntg-context
wrote: The first line shows different versions text. I wonder whether there would be an automatic way to get the \inframed highlighting with any character that differs from the other column (it might be different, or just missing or being added).
I'm not aware of anything built-in. One way would be to use buffers: you enter your text into two buffers ("left" & "right"); compare them using a script which then modifies the buffers to highlight the changes in red or green; and then \getbuffer the buffers from inside the columnset commands to print them. There's a good discussion of comparison algorithms at the link below, including source code in Javascript (but not Lua, unfortunately). However, Context supports Javascript with \startJScode ... \stopJScode so you could try adapting what's there. https://neil.fraser.name/writing/diff/ Regards, — Bruce Horrocks Hampshire, UK

On 5 Mar 2025, at 01:21, Bruce Horrocks
wrote: On 4 Mar 2025, at 18:26, Pablo Rodriguez via ntg-context
wrote: The first line shows different versions text. I wonder whether there would be an automatic way to get the \inframed highlighting with any character that differs from the other column (it might be different, or just missing or being added).
I'm not aware of anything built-in.
One way would be to use buffers: you enter your text into two buffers ("left" & "right"); compare them using a script which then modifies the buffers to highlight the changes in red or green; and then \getbuffer the buffers from inside the columnset commands to print them.
There's a good discussion of comparison algorithms at the link below, including source code in Javascript (but not Lua, unfortunately). However, Context supports Javascript with \startJScode ... \stopJScode so you could try adapting what's there.
Sorry - ignore the JS bit (that's for embedding into the PDF). You'll need to translate Fraser's example code into Lua. — Bruce Horrocks Hampshire, UK

On 3/5/25 02:27, Bruce Horrocks wrote:
On 5 Mar 2025, at 01:21, Bruce Horrocks
wrote: [...] I'm not aware of anything built-in. One way would be to use buffers: you enter your text into two buffers ("left" & "right"); compare them using a script which then modifies the buffers to highlight the changes in red or green; and then \getbuffer the buffers from inside the columnset commands to print them.
Many thanks for your reply, Bruce. I think this might be a a feasible approach for me.
There's a good discussion of comparison algorithms at the link below, including source code in Javascript (but not Lua, unfortunately).
There might be a Lua version (I think) here: https://github.com/google/diff-match-patch.
However, Context supports Javascript with \startJScode ... \stopJScode so you could try adapting what's there.
https://neil.fraser.name/writing/diff/> Sorry - ignore the JS bit (that's for embedding into the PDF). You'll need to translate Fraser's example code into Lua.
We have https://www.pragma-ade.com/general/manuals/ecmascript-mkiv.pdf, so translating JS to Lua might not be required. Many thanks for your help, Pablo

Hi Pablo, On Tue, 2025-03-04 at 19:26 +0100, Pablo Rodriguez via ntg-context wrote:
What I want to achieve is automatic text comparison between versions of the same text (in different subcolumnsets).
The first line shows different versions text. I wonder whether there would be an automatic way to get the \inframed highlighting with any character that differs from the other column (it might be different, or just missing or being added).
I think this may be possible with ConTeXt, but I don’t know how to achieve it automagically.
Not quite what you're asking for, but the "compare" script does something fairly similar: $ context --extra=compare <filename-1>.pdf <filename-2>.pdf $ context --extra=compare <filename-1>.pdf <filename-2>.pdf --colors=red,blue --result=<output-name>.pdf The source for that script is in tex/context/base/mkiv/mtx-context-compare.tex so maybe you can put together something similar from there? Thanks, -- Max

On 3/5/25 05:23, Max Chernoff via ntg-context wrote:
Hi Pablo,
Hi Max, many thanks for your reply.
Not quite what you're asking for, but the "compare" script does something fairly similar: [...] The source for that script is in
tex/context/base/mkiv/mtx-context-compare.tex
so maybe you can put together something similar from there?
I’m afraid not. I used "compare" in the past, but I need to mark additions and deletions, not to see differences imposing one file over the other one. Sorry, but when I need to see how a document has been modified, diffpdf presents a clearer overview to me. Many thanks for your help, Pablo
participants (3)
-
Bruce Horrocks
-
Max Chernoff
-
Pablo Rodriguez