fr: script to visualize differences between 2 pages.
authorThérèse Godefroy <godef.th@free.fr>
Sun, 27 Jul 2014 21:07:29 +0000 (23:07 +0200)
committerThérèse Godefroy <godef.th@free.fr>
Sun, 27 Jul 2014 21:07:29 +0000 (23:07 +0200)
fr/kitchen/color-wdiff [new file with mode: 0755]

diff --git a/fr/kitchen/color-wdiff b/fr/kitchen/color-wdiff
new file mode 100755 (executable)
index 0000000..7648a26
--- /dev/null
@@ -0,0 +1,156 @@
+#!/bin/bash
+
+# color-wdiff visualizes the differences between 2 versions of the same page.
+# This script was extracted from GNUN's GNUmakefile (function "mark-outdated")
+# and adapted. GNUN (http://www.gnu.org/software/gnun/) is under GPLv3.
+
+# Howto:
+# - The script is in [...]/enc/fr/kitchen.
+# - cd to that directory and run:
+#              ./color-wdiff FILE1 FILE2
+#   (FILE1 and FILE2 are the paths to the files to be compared.)
+# - The diff file is created in the directory where FILE1 resides.
+#
+# For example, compare old and new versions of index.t.html:
+#              ./color-diff index.t.html index-new.t.html
+
+# Note: the diff is much easier to use if the HTML is not indented.
+# Reformatting is done by the function "page_reformat", which leaves the
+# original page intact.
+
+
+file=$1
+file1=$2
+diff_file=${file%.html}-diff.html
+
+page_reformat () {
+  #reformats the original pages of emailselfdefense.fsf.org
+
+  cp $1 $1.tmp
+
+  # Remove javascript, which shouldn't be reformatted.
+  sed -i '/jquery-1.11.0.min.js/,$d' $1.tmp
+
+  # Remove leading and trailing spaces/tabs.
+  sed -i 's,\t, ,g' $1.tmp
+  sed -i 's,^ *,,'  $1.tmp
+  sed -i 's, *$,,'  $1.tmp
+
+  # Remove LF after </a>.
+  sed -i '/<\/a>$/ {N; s,<\/a>\n\([^<]\),<\/a>\1,}' $1.tmp
+
+  # One string per paragraph, header or list item.
+  for tag in li p strong a h3; do
+    sed -i "/<$tag[^>]*>$/ {N; s,\\n, ,}" $1.tmp
+  done
+  for tag in a strong; do
+    sed -i "/<\\/$tag>$/ {N; s,\\n, ,}" $1.tmp
+  done
+  # This command may need to be repeated. Adjust the number of repeats. This
+  # could be done by looping back to a sed marker, but a while loop seems
+  # quicker.
+  i=0
+  while (( i < 2 )); do
+    sed -i '/[^<>]$/ {N; s,\([^<>]\)\n,\1 ,}' $1.tmp
+    let i=i+1
+  done
+
+  sed -i '/ \/>$/ {N; s,\( \/>\)\n,\1 ,}' $1.tmp
+  sed -i '/ <a[^>]*>$/ {N; s,\(<a[^>]*>\)\n\([^<]\),\1 \2,}' $1.tmp
+
+  # Make sure there is only one paragraph per string. This command may need to
+  # be repeated. Adjust the number of repeats.
+  i=0
+  while (( i < 2 )); do
+    sed -i 's,</p>\(.\+\)$,</p>\n\1,' $1.tmp
+    let i=i+1
+  done
+
+  # Single out the tags which include p (will also work for pre).
+  sed -i 's,\(.\)<p,\1\n<p,' $1.tmp
+
+  # Single-out input meta and link.
+  for tag in input meta link link; do
+    sed -i "s,> <$tag,>\n<$tag," $1.tmp
+  done
+  # Remove leading and trailing spaces, double spaces and blank lines.
+  sed -i 's,^ *,,' $1.tmp
+  sed -i 's, *$,,' $1.tmp
+  sed -i 's,  , ,g' $1.tmp
+  sed -i '/^$/d' $1.tmp
+
+  # Fuse comment with </p>.
+  sed -i '/<\/p>$/ {N;s,\n\(<!-- [^~]\),\1,}' $1.tmp
+
+  # Separate truncated "~~~" comment from fused tag.
+  sed -i 's,~~~[ ]\?[-]\?[-]\?[ ]\?<,~~~\n<,' $1.tmp
+
+  # Fuse header, section and footer with the corresponding div.
+  for tag in header section footer; do
+    sed -i "/^<$tag/ {N; s,\\(<$tag[^>]*>\\)\\n<div>,\\1<div>,}" $1.tmp
+    sed -i "/^<\\/div>$/ {N; s,<\\/div>\\n\\(<\\/$tag>\\),</div>\\1,}" $1.tmp
+  done
+
+  # Add LF before main sections and commented-out parts.
+  sed -i 's,<!-- ~~,\n<!-- ~~,' $1.tmp
+  sed -i '/COMMENTED OUT/ s,^,\n,' $1.tmp
+
+  # Make the text more readable.
+  for tag in p h1 h2 h3 h4 dl title form; do
+    sed -i "s,<$tag,\\n&," $1.tmp
+  done
+  for tag in p dl ul h1 h2 h3 h4 title head footer form script; do
+    sed -i "/<\\/$tag>/s,$,\\n," $1.tmp
+  done
+  sed -i '/<\/dd>/ {N; s,</dd>\n<dt,</dd>\n\n<dt,}' $1.tmp
+  sed -i '/<\/dt>/ {N; s,</dt>\n<dd,</dt>\n\n<dd,}' $1.tmp
+  sed -i 's,</p></span>$,</p>\n</span>,' $1.tmp
+
+  sed -i 's, alt=,\nalt=,g' $1.tmp
+  sed -i 's, | , |\n,g' $1.tmp
+
+  # Remove extra LFs, if any.
+  sed -i ':a /^$/ {N; s,\n$,,; ba}' $1.tmp
+  sed -i ':a /^\n*$/ {$d; N; ba}' $1.tmp
+
+  # Wrap the text.
+  fmt -s -w 95 $1.tmp > $1.r
+}
+
+
+page_reformat ${file}
+# Replace chevrons with HTML entities; the files are treated as simple text.
+sed "s/</\&lt;/g;s/>/\&gt;/g" ${file}.r  > ${file}.tmp
+page_reformat ${file1}
+sed "s/</\&lt;/g;s/>/\&gt;/g" ${file1}.r > ${file1}.tmp
+
+# Add an HTML header to the wdiff output, with style for visualizing the
+# insertions and deletions, and write the title of the page.
+cat > ${diff_file} << EOF
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<!-- Generated by GNUN -->
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+<meta http-equiv="content-type" content="text/html; charset=utf-8" />
+<title>${diff_file##*\/}</title>
+<style type="text/css">
+span.removed { background-color: #f22; color: #000; }
+span.inserted { background-color: #2f2; color: #000; }
+</style></head>
+<body><pre>
+EOF
+
+# Run wdiff with options to add the proper markup at the beginning and end of
+# deletions and insertions.
+wdiff --start-delete '<span class="removed"><del><strong>' \
+      --end-delete '</strong></del></span>' \
+      --start-insert '<span class="inserted"><ins><em>' \
+      --end-insert '</em></ins></span>' \
+      ${file}.tmp ${file1}.tmp >> $diff_file
+
+# Add the closing tags.
+echo '</pre></body></html>' >> $diff_file
+
+# Clean up.
+rm -f ${file}.tmp ${file1}.tmp ${file}.r ${file1}.r