Skip to content

Commit

Permalink
Merge pull request #213 from jprotze/page-ranges
Browse files Browse the repository at this point in the history
Merge page ranges for minimal diffs
  • Loading branch information
ftilmann authored Oct 3, 2020
2 parents b276cd5 + 07c0f0f commit 1df845f
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions latexdiff-vc
Original file line number Diff line number Diff line change
Expand Up @@ -546,15 +546,15 @@ foreach $diff ( @difffiles ) {
#print STDERR "Generated postscript file $ps\n";
} elsif ( $run ) {
if ( $onlychanges ) {
my @pages=findchangedpages("$diffbase.aux");
my @pages=compresspages(findchangedpages("$diffbase.aux"));
my $gs = `which gs`;
$gs =~ s/^\s+|\s+$//g;
my $qpdf = `which qpdf`;
$qpdf =~ s/^\s+|\s+$//g;
my $pdftk = `which pdftk`;
$pdftk =~ s/^\s+|\s+$//g;
my $command;
if (-x $gs && `gs --version` >= 9.20) {
if (-x $gs && `$gs --version` >= 9.20) {
$command="gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -sPageList=" . join(",", @pages) . " -sOutputFile=\"$diffbase-changedpage.pdf\" \"$diffbase.pdf\"";
} elsif (-x $pdftk) {
$command="pdftk \"$diffbase.pdf\" cat " . join(" ",@pages) . " output \"$diffbase-changedpage.pdf\"";
Expand Down Expand Up @@ -602,13 +602,52 @@ sub findchangedpages {
return(sort {$a <=> $b} keys(%pages));
}

# inspired by a python version posted at https://stackoverflow.com/a/54714846/8136338
# compresspages(@pages)
# @pages: sorted array of unique integers (pages)
# return a list of pages and page ranges. 3 or more consecutive numbers are merged into a "begin-end" string
#
# example: join(",", compresspages([1,3,4,5,7,8])) -> join(",", [1,"3-5",7,8]) -> "1,3-5,7,8"
sub compresspages {
my (@pages) = @_;
my @res;
my $begin=$pages[0];
my $end =$pages[0];
my $page;
foreach $page ( @pages ) {
next if ($page == $begin);
if ($page == $end+1) { # handle continuous pages
$end = $page;
next;
} elsif ($begin == $end) { # push single continuous page
push @res, $begin;
$begin = $end = $page;
} elsif ($begin + 1 == $end) { # push two continuous pages
push @res, $begin, $end;
$begin = $end = $page;
} else { # push multiple continuous pages
push @res, "$begin-$end";
$begin = $end = $page;
}
}
# Finally just check in the same manner for the end element
if ($begin == $end) {
push @res, $begin;
} elsif ($begin + 1 == $end) {
push @res, $begin, $end;
} else {
push @res, "$begin-$end";
}
return @res;
}

# checkout_dir(rev,dirname)
# checks out revision rev and stores it in dirname
# uses global variables: $vc, $rootdir
sub checkout_dir {
my ($rev,$dirname)=@_;

unless (-e $dirname) { mkdir $dirname or die "Cannot mkdir $dirname ." ;}
unless (-e $dirname) { mkpath([ $dirname ]) or die "Cannot mkdir $dirname ." ;}
if ( $vc eq "SVN" ) {
system("svn checkout -r $rev $rootdir $dirname")==0 or die "Something went wrong in executing: svn checkout -r $rev $rootdir $dirname";
} elsif ( $vc eq "GIT" ) {
Expand Down

0 comments on commit 1df845f

Please sign in to comment.