summaryrefslogtreecommitdiff
path: root/localworddiff
blob: 63a67fa978ddd10d106f89ef47311fdef6cee7e6 (plain)
  1. #!/usr/bin/perl
  2. #
  3. # Copyright © 2013 Jonas Smedegaard <dr@jones.dk>
  4. # Description: Generate word-based diff for console or web
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 3, or (at your option)
  9. # any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful, but
  12. # WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. # General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. # Depends: libtext-markdown-perl libhtml-html5-builder libcss-perl
  20. # Depends: libtext-worddiff-perl (>= 0.08)
  21. #
  22. # TODO: html-escape non-ASCII characters
  23. use Text::WordDiff;
  24. use Text::Markdown qw[markdown];
  25. use CSS::Tiny;
  26. use HTML::HTML5::Builder qw[:standard];
  27. use File::Slurp;
  28. use strictures 1;
  29. use autodie;
  30. my ( $infile1, $infile2, $outfile ) = @ARGV;
  31. die 'Missing input file arguments'
  32. unless ( $infile1 and $infile2 );
  33. # read infiles using File::Slurp (Text::WordDiff don't handle UTF-8)
  34. my $text1 = read_file( $infile1, binmode => ':utf8' );
  35. my $text2 = read_file( $infile2, binmode => ':utf8' );
  36. # use console if no output file provided as third argument
  37. unless ($outfile) {
  38. print word_diff \$text1, \$text2, { STYLE => 'ANSIColor' };
  39. exit 0;
  40. }
  41. # resolve diff
  42. my $diff = word_diff \$text1, \$text2, { STYLE => 'HTMLTwoLines' };
  43. # apply markup to each file div of resolved diff
  44. my $d = "<div class=\"file\">";
  45. my $d_ = "<\/div>";
  46. my $i;
  47. my @diffchunk;
  48. foreach ( split /(?:$d_\n)?$d/, $diff ) {
  49. if ($_) {
  50. $diffchunk[ $i++ ] = $d . markdown($_) . $d_;
  51. }
  52. }
  53. # parse styling
  54. my $css = CSS::Tiny->new();
  55. $css->read_string(<<'EOF');
  56. .fileheader {
  57. display: none;
  58. visibility: hidden;
  59. }
  60. .file {
  61. float: left;
  62. width: 49%;
  63. }
  64. .file .hunk del,
  65. .file .hunk ins {
  66. font-weight: bold;
  67. text-decoration: inherit;
  68. }
  69. .file .hunk del {
  70. color: darkred;
  71. }
  72. .file .hunk ins {
  73. color: darkgreen;
  74. }
  75. EOF
  76. # compose and save web page
  77. my $page = html(
  78. head(
  79. XML_CHUNK( $css->html ),
  80. ),
  81. body(
  82. CHUNK( $diffchunk[0] ),
  83. CHUNK( $diffchunk[1] ),
  84. ),
  85. );
  86. write_file( $outfile, { binmode => ':utf8' }, $page );
  87. 1;