Advertisement
goodwid

diff2html

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