X-Git-Url: http://git.projectaon.org/?p=project-aon.git;a=blobdiff_plain;f=common%2Fscripts%2Fdiff2html.sh;fp=common%2Fscripts%2Fdiff2html.sh;h=8e0c0a74abf5f802d4fdc3d8afcca4538b2c9394;hp=0000000000000000000000000000000000000000;hb=f4bdee5083ca9a72713637e1e979aa183e06faea;hpb=97545603aea8298f1eceb604ff65085cc7adfced diff --git a/common/scripts/diff2html.sh b/common/scripts/diff2html.sh new file mode 100755 index 0000000..8e0c0a7 --- /dev/null +++ b/common/scripts/diff2html.sh @@ -0,0 +1,124 @@ +#!/bin/bash +# +# Convert diff output to colorized HTML. +# +# (http://www.linuxjournal.com/content/convert-diff-output-colorized-html) + +cat < + +Colorized Diff + + + +
+XX
+
+echo -n ''
+
+first=1
+diffseen=0
+lastonly=0
+
+OIFS=$IFS
+IFS='
+'
+
+# The -r option keeps the backslash from being an escape char.
+read -r s
+
+while [[ $? -eq 0 ]]
+do
+    # Get beginning of line to determine what type
+    # of diff line it is.
+    t1=${s:0:1}
+    t2=${s:0:2}
+    t3=${s:0:3}
+    t4=${s:0:4}
+    t7=${s:0:7}
+
+    # Determine HTML class to use.
+    if    [[ "$t7" == 'Only in' ]]; then
+        cls='only'
+        if [[ $diffseen -eq 0 ]]; then
+            diffseen=1
+            echo -n ''
+        else
+            if [[ $lastonly -eq 0 ]]; then
+                echo ""
+            fi
+        fi
+        if [[ $lastonly -eq 0 ]]; then
+            echo "
" + fi + lastonly=1 + elif [[ "$t4" == 'diff' ]]; then + cls='diff' + if [[ $diffseen -eq 0 ]]; then + diffseen=1 + echo -n '' + else + echo "
" + fi + echo "
" + lastonly=0 + elif [[ "$t3" == '+++' ]]; then + cls='plus3' + lastonly=0 + elif [[ "$t3" == '---' ]]; then + cls='minus3' + lastonly=0 + elif [[ "$t2" == '@@' ]]; then + cls='at2' + lastonly=0 + elif [[ "$t1" == '+' ]]; then + cls='plus' + lastonly=0 + elif [[ "$t1" == '-' ]]; then + cls='minus' + lastonly=0 + else + cls= + lastonly=0 + fi + + # Convert &, <, > to HTML entities. + s=$(sed -e 's/\&/\&/g' -e 's//\>/g' <<<"$s") + if [[ $first -eq 1 ]]; then + first=0 + else + echo + fi + + # Output the line. + if [[ "$cls" ]]; then + echo -n ''${s}'' + else + echo -n ${s} + fi + read -r s +done +IFS=$OIFS + +if [[ $diffseen -eq 0 && $onlyseen -eq 0 ]]; then + echo -n '' +else + echo "
" +fi +echo + +cat < + + +XX