From 22787bdda0961e3f136c387b6ed968353d35a5c2 Mon Sep 17 00:00:00 2001 From: Jonas Smedegaard Date: Sun, 21 Apr 2013 22:53:28 +0200 Subject: Rewrite localworddiff as Perl script. --- localworddiff | 75 +++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 23 deletions(-) (limited to 'localworddiff') 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 # 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 . # -# 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 = "
"; +my $d_ = "<\/div>"; +my $i; +my @diffchunk; +foreach ( split /(?:$d_\n)?$d/, $diff ) { + if ($_) { + $diffchunk[$i++] = $d.markdown($_).$d_; + } +} -cat > "$3" < - - - - - -EOF -perl -MText::WordDiff -MText::Markdown=markdown -E '$d="
";$d_="<\/div>";foreach(split /(?:$d_\n)?$d/, word_diff "'"$1"'", "'"$2"'", { STYLE => "HTMLTwoLines" }){say $d.markdown($_).$d_ if ($_)}'>> "$3" -cat >> "$3" < - 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; -- cgit v1.2.3