Apache2-Translation

 view release on metacpan or  search on metacpan

script/diffprov  view on Meta::CPAN

   (
    echo echo '<html><body><table>'
    diffprov -o File NotesDir notes ConfigFile trans \
             -n MMapDB FileName trans.mmdb -nonotes
    echo echo '</table></body></html>'
   ) >diff.html

 -nonotes    differences in notes only are disregarded
 -nonumbers  disregard differences in BLOCK or ORDER fields only if the
             resulting block order is the same
 -key        compute the differences only for keys matching that regexp
 -uri        compute the differences only for uris matching that regexp
 -json       JSON output instead of HTML
 -h          print this help
USAGE
	       exit 1;
	     };
}

config;

if( $json ) {
  eval "require JSON::XS";
  die "Need JSON::XS to produce JSON output.\n" if $@;
} else {
  eval "require Algorithm::Diff";
  die "Need Algorithm::Diff to produce HTML output.\n" if $@;
}

my ($old, $new)=map {
  my @param=@$_;
  my $type=shift @param;
  if( eval "require Apache2::Translation::$type" ) {
    $type="Apache2::Translation::$type";
    warn "Using $type (@param)\n";
  } else {
    eval "require $type" and warn "Using $type (@param)\n";
  }
  $type->new(@param) or die "$type->new(@param) ==> undef\n";
} \@old, \@new;

$old->start;
$new->start;
my @diff=$old->sdiff(
		     $new,
		     (defined $key ? (key=>qr/$key/) : ()),
		     (defined $uri ? (uri=>qr/$uri/) : ()),
		     notes=>$notes,
		     numbers=>$numbers,
		    );
$new->stop;
$old->stop;

if( $json ) {
  print JSON::XS::encode_json([map {$_->[0] eq 'u' ? () : [do {
    my $l=$_;
    local $_;
    map {
      ref $_
	? [
	   Encode::decode('utf8', $_->[0]), # key
	   Encode::decode('utf8', $_->[1]), # uri
	   $_->[2],			    # block
	   $_->[3],			    # order
	   Encode::decode('utf8', $_->[4]), # action
	   Encode::decode('utf8', $_->[5]), # note
	  ]
	: ''
    } @{$l}[1,2];
  }]} @diff]);
  exit 0;
}

sub TD {
  if( @_==2 ) {
    "    <td ".$_[0]."><div>".$_[1]."</div></td>\n";
  } else {
    "    <td><div>".$_[0]."</div></td>\n";
  }
}

sub TR {
  if( ref $_[0] ) {
    my $style=${shift()};
    "  <tr $style>\n".join('', @_)."  </tr>\n";
  } else {
    "  <tr>\n".join('', @_)."  </tr>\n";
  }
}

sub blockdiff {
  my @t1=split /\n/, $_[0], -1;
  my @t2=split /\n/, $_[1], -1;
  my @diff=Algorithm::Diff::sdiff(\@t1, \@t2);
  my ($t1, $t2)=('','');

  for my $el (@diff) {
    if( $el->[0] eq 'u' ) {
      $t1.=html_esc($el->[1])."\n";
      $t2.=html_esc($el->[2])."\n";
    } elsif( $el->[0] eq '-' ) {
      $t1.='<span class="plus">'.html_esc($el->[1])."</span>\n";
      $t2.="<span class=\"miss\"> </span>\n";
    } elsif( $el->[0] eq '+' ) {
      $t1.="<span class=\"miss\"> </span>\n";
      $t2.='<span class="plus">'.html_esc($el->[2])."</span>\n";
    } elsif( $el->[0] eq 'c' ) {
      $t1.='<span class="chg">'.html_esc($el->[1])."</span>\n";
      $t2.='<span class="chg">'.html_esc($el->[2])."</span>\n";
    }
  }

  return $t1, $t2;
}

sub key {
  if( 'u' eq shift ) {
    return TR(\'class="firstline equal"',
	      TD('class="col1"', "<b>Key:</b>&nbsp;".html_esc($_[0]->[0])),
	      TD('class="col2"', "<b>Key:</b>&nbsp;".html_esc($_[1]->[0])));
  }
  if( ref $_[0] and ref $_[1] ) {
    if( $_[0]->[0] eq $_[1]->[0] ) {
      return TR(\'class="firstline"',
		TD('class="col1"', "<b>Key:</b>&nbsp;".html_esc($_[0]->[0])),
		TD('class="col2"', "<b>Key:</b>&nbsp;".html_esc($_[1]->[0])));



( run in 0.750 second using v1.01-cache-2.11-cpan-39bf76dae61 )