Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge page ranges for minimal diffs #213

Merged
merged 4 commits into from
Oct 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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