archived old versions of the web pages
[enc.git] / old / en / kitchen / color-wdiff.1
diff --git a/old/en/kitchen/color-wdiff.1 b/old/en/kitchen/color-wdiff.1
new file mode 100755 (executable)
index 0000000..86dbdc2
--- /dev/null
@@ -0,0 +1,106 @@
+#!/bin/bash
+
+# NAME
+#    color-wdiff - visualize differences between text files
+
+# SYNOPSIS
+#    color-wdiff FILE0 FILE1
+
+# DEPENDENCIES
+#    wdiff, reformat-html in $HOME/bin.
+
+# DESCRIPTION
+#    1. If the input files are HTML, they are reformatted, to remove
+#       indentation among other things.
+#    2. The markup is inactivated by replacing angle brackets with the
+#       corresponding entities.
+#    3. The files are compared with wdiff, using options which label
+#       insertions and deletions. The labels are HTML tags with specific
+#       classes.
+#    4. The diff is turned into a valid HTML page by adding the required
+#       markup, plus CSS style for the insertion and deletion classes.
+#
+#    The diff file is created in the directory where FILE0 resides.
+
+# ORIGIN OF THE SCRIPT
+#    This script was extracted from GNUN's GNUmakefile (function
+#    "mark-outdated"), and adapted.
+#    GNUN (http://www.gnu.org/software/gnun/) is under GPLv3.
+
+# =============================================================================
+
+# Command-line arguments
+arg=($1 $2)
+
+set -e
+
+close_term () {
+  sleep 3
+  exit $1
+}
+
+f[0]=$(mktemp -t cdif.XXXXXX) || exit 1
+f[1]=$(mktemp -t cdif.XXXXXX) || exit 1
+trap 'rm -f "${f[0]}" "${f[1]}"' EXIT
+
+## Prepare the pages to be compared.
+
+for n in 0 1; do
+  # Input a valid file.
+  input=${arg[$n]}
+  if test ! -f "$input" -o ! -s "$input"; then
+    echo "*** color-wdiff - Please enter file $n."; read input
+    input=${input%\'}; input=${input#\'}
+    test -f "$input" -a -s "$input" \
+    || (echo 1>&2 "!!! This file doesn't exist or is empty."; close_term 1)
+  fi
+
+  # Name the diff after file 0.
+  test "$n" == "0" && diff_file=${input%.html}-diff.html
+
+  # If the file is an HTML but not a diff, process it:
+  if test "${input%.html}" != "$input" -a "${input%-diff.html}" == "$input";
+  then
+    # - Standardize the format for easier reading of the diff.
+    if test -f "$HOME/bin/reformat-html.1"; then
+      $HOME/bin/reformat-html.1 $input ${f[$n]}
+    else
+      cp $input ${f[$n]}
+    fi
+    # - Replace chevrons with HTML entities. The page becomes simple text.
+    sed -i "s/</\&lt;/g;s/>/\&gt;/g" ${f[$n]}
+  fi
+done
+
+## Build the diff page.
+
+# 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>' \
+      ${f[0]} ${f[1]} >> $diff_file || true
+
+# Add the closing tags.
+echo '</pre></body></html>' >> ${diff_file}
+
+echo -e "\n    The diff file is $diff_file."
+close_term 0