Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert diff to markdown with strikeout?

I'd like to convert the output of diff (on a Markdown file) to Markdown with <strike> and <em> tags, so that I can see what has been removed from or added to a new version of a document. (This kind of treatment is very common for legal documents.)

Example of hoped-for output:

Why do weWe study programming languages? notNot in order to ...

One of the many difficulties is that diff's output is line-oriented, where I want to see differences in individual words. Does anyone have suggestions as to what algorithm to use, or what software to build on?

like image 901
Norman Ramsey Avatar asked Mar 05 '10 03:03

Norman Ramsey


2 Answers

Use wdiff. It already does the word-by-word comparison you're looking for; converting its output to markdown should take just a few simple regular expressions.

For example:

$ cat foo
Why do we study programming languages?  Not in order to
$ cat bar
We study programming languages not in order to
$ wdiff foo bar
[-Why do we-]{+We+} study programming [-languages?  Not-] {+languages not+} in order to
$ wdiff foo bar | sed 's|\[-|<em>|g;s|-]|</em>|g;s|{+|<strike>|g;s|+}|</strike>|g'
<em>Why do we</em><strike>We</strike> study programming <em>languages?  Not</em> <strike>languages not</strike> in order to

Edit: Actually, wdiff has some options that make it even easier:

$ wdiff -w '<em>' -x '</em>' -y '<strike>' -z '</strike>' foo bar
<em>Why do we</em><strike>We</strike> study programming <em>languages?  Not</em> <strike>languages not</strike> in order to
like image 89
Adam Rosenfield Avatar answered Nov 12 '22 12:11

Adam Rosenfield


Use Markdown-Diff to have the word diff annotated to your original document. It formats wdiff or git --word-diff's output in Markdown, so you can use your favorite Markdown previewer or compiler to review changes. (Markdown-Diff was written by myself, inspired by Adam Rosenfield's answer.)

like image 7
netj Avatar answered Nov 12 '22 14:11

netj