#!/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 () { printf '\n%s' '*** Close the terminal window or press Return.'; read OK test -z "$OK" && exit $1 } f[0]=$(mktemp -t cdif.XXXXXX) || close_or_exit 1 f[1]=$(mktemp -t cdif.XXXXXX) || close_or_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"; then $HOME/bin/reformat-html $input ${f[$n]} else cp $input ${f[$n]} fi # - Replace chevrons with HTML entities. The page becomes simple text. sed -i "s//\>/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 ${diff_file##*\/}
EOF

# Run wdiff with options to add the proper markup at the beginning and end of
# deletions and insertions.
wdiff --start-delete '' \
      --end-delete '' \
      --start-insert '' \
      --end-insert '' \
      ${f[0]} ${f[1]} >> $diff_file || true

# Add the closing tags.
echo '
' >> ${diff_file} echo -e "\n The diff file is $diff_file." close_term 0