summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xlocalworddiff75
1 files changed, 52 insertions, 23 deletions
diff --git a/localworddiff b/localworddiff
index ba177b1..e26e50c 100755
--- a/localworddiff
+++ b/localworddiff
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/usr/bin/perl
#
# Copyright © 2013 Jonas Smedegaard <dr@jones.dk>
# Description: Generate word-based diff for console or web
@@ -16,22 +16,46 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
-# Depends: libtext-worddiff-perl (>= 0.08), libtext-markdown-perl
-#
-# TODO: rewrite as Perl script, using HTML::HTML5::Builder
+# Depends: libtext-markdown-perl libhtml-html5-builder libcss-perl
+# Depends: libtext-worddiff-perl (>= 0.08)
+
+use Text::WordDiff;
+use Text::Markdown qw[markdown];
+use CSS::Tiny;
+use HTML::HTML5::Builder qw[:standard];;
+use File::Slurp;
+
+use strictures 1;
+use autodie;
+
+my ($infile1, $infile2, $outfile) = @ARGV;
+
+die 'Missing input file arguments'
+ unless ($infile1 and $infile2);
+
+# use console if no output file provided as third argument
+unless ($outfile) {
+ print word_diff $infile1, $infile2, { STYLE => 'ANSIColor' };
+ exit 0;
+}
-set -e
+# resolve diff
+my $diff = word_diff $infile1, $infile2, { STYLE => 'HTMLTwoLines' };
-if [ -z "$3" ]; then
- perl -MText::WordDiff -E 'say word_diff "'"$1"'", "'"$2"'", { STYLE => 'ANSIColor' }'
- exit
-fi
+# apply markup to each file div of resolved diff
+my $d = "<div class=\"file\">";
+my $d_ = "<\/div>";
+my $i;
+my @diffchunk;
+foreach ( split /(?:$d_\n)?$d/, $diff ) {
+ if ($_) {
+ $diffchunk[$i++] = $d.markdown($_).$d_;
+ }
+}
-cat > "$3" <<EOF
-<!DOCTYPE html>
-<html>
-<head>
-<style>
+# parse styling
+my $css = CSS::Tiny->new();
+$css->read_string (<<'EOF');
.fileheader {
display: none;
visibility: hidden;
@@ -51,13 +75,18 @@ cat > "$3" <<EOF
.file .hunk ins {
color: darkgreen;
}
-
-</style>
-</head>
-<body>
-EOF
-perl -MText::WordDiff -MText::Markdown=markdown -E '$d="<div class=\"file\">";$d_="<\/div>";foreach(split /(?:$d_\n)?$d/, word_diff "'"$1"'", "'"$2"'", { STYLE => "HTMLTwoLines" }){say $d.markdown($_).$d_ if ($_)}'>> "$3"
-cat >> "$3" <<EOF
-</body>
-</html>
EOF
+
+# compose and save web page
+my $page = html(
+ head(
+ XML_CHUNK($css->html),
+ ),
+ body(
+ CHUNK($diffchunk[0]),
+ CHUNK($diffchunk[1]),
+ ),
+);
+write_file( $outfile, $page );
+
+1;