Don't like ads? PRO users don't see any ads ;-)
Guest

Convert diff output to colorized HTML.

By: a guest on May 30th, 2012  |  syntax: Bash  |  size: 2.59 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/bin/bash
  2. #
  3. # Convert diff output to colorized HTML.
  4.  
  5. cat <<XX
  6. <html>
  7. <head>
  8. <title>Colorized Diff</title>
  9. </head>
  10. <style>
  11. .diffdiv  { border: solid 1px black;           }
  12. .comment  { color: gray;                       }
  13. .diff     { color: #8A2BE2;                    }
  14. .minus3   { color: blue;                       }
  15. .plus3    { color: maroon;                     }
  16. .at2      { color: lime;                       }
  17. .plus     { color: green; background: #E7E7E7; }
  18. .minus    { color: red;   background: #D7D7D7; }
  19. .only     { color: purple;                     }
  20. </style>
  21. <body>
  22. <pre>
  23. XX
  24.  
  25. echo -n '<span class="comment">'
  26.  
  27. first=1
  28. diffseen=0
  29. lastonly=0
  30.  
  31. OIFS=$IFS
  32. IFS='
  33. '
  34.  
  35. # The -r option keeps the backslash from being an escape char.
  36. read -r s
  37.  
  38. while [[ $? -eq 0 ]]
  39. do
  40.     # Get beginning of line to determine what type
  41.     # of diff line it is.
  42.     t1=${s:0:1}
  43.     t2=${s:0:2}
  44.     t3=${s:0:3}
  45.     t4=${s:0:4}
  46.     t7=${s:0:7}
  47.  
  48.     # Determine HTML class to use.
  49.     if    [[ "$t7" == 'Only in' ]]; then
  50.         cls='only'
  51.         if [[ $diffseen -eq 0 ]]; then
  52.             diffseen=1
  53.             echo -n '</span>'
  54.         else
  55.             if [[ $lastonly -eq 0 ]]; then
  56.                 echo "</div>"
  57.             fi
  58.         fi
  59.         if [[ $lastonly -eq 0 ]]; then
  60.             echo "<div class='diffdiv'>"
  61.         fi
  62.         lastonly=1
  63.     elif [[ "$t4" == 'diff' ]]; then
  64.         cls='diff'
  65.         if [[ $diffseen -eq 0 ]]; then
  66.             diffseen=1
  67.             echo -n '</span>'
  68.         else
  69.             echo "</div>"
  70.         fi
  71.         echo "<div class='diffdiv'>"
  72.         lastonly=0
  73.     elif  [[ "$t3" == '+++'  ]]; then
  74.         cls='plus3'
  75.         lastonly=0
  76.     elif  [[ "$t3" == '---'  ]]; then
  77.         cls='minus3'
  78.         lastonly=0
  79.     elif  [[ "$t2" == '@@'   ]]; then
  80.         cls='at2'
  81.         lastonly=0
  82.     elif  [[ "$t1" == '+'    ]]; then
  83.         cls='plus'
  84.         lastonly=0
  85.     elif  [[ "$t1" == '-'    ]]; then
  86.         cls='minus'
  87.         lastonly=0
  88.     else
  89.         cls=
  90.         lastonly=0
  91.     fi
  92.  
  93.     # Convert &, <, > to HTML entities.
  94.     s=$(sed -e 's/\&/\&amp;/g' -e 's/</\&lt;/g' -e 's/>/\&gt;/g' <<<"$s")
  95.     if [[ $first -eq 1 ]]; then
  96.         first=0
  97.     else
  98.         echo
  99.     fi
  100.  
  101.     # Output the line.
  102.     if [[ "$cls" ]]; then
  103.         echo -n '<span class="'${cls}'">'${s}'</span>'
  104.     else
  105.         echo -n ${s}
  106.     fi
  107.     read -r s
  108. done
  109. IFS=$OIFS
  110.  
  111. if [[ $diffseen -eq 0  &&  $onlyseen -eq 0 ]]; then
  112.     echo -n '</span>'
  113. else
  114.     echo "</div>"
  115. fi
  116. echo
  117.  
  118. cat <<XX
  119. </pre>
  120. </body>
  121. </html>
  122. XX