Skip to content

Commit

Permalink
Issue 172, subimport support
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Smith committed May 30, 2019
1 parent 1a3ca7c commit 245d4dd
Showing 1 changed file with 48 additions and 10 deletions.
58 changes: 48 additions & 10 deletions latexdiff
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ my ($algodiffversion)=split(/ /,$Algorithm::Diff::VERSION);


my ($versionstring)=<<EOF ;
This is LATEXDIFF 1.3.0 (Algorithm::Diff $Algorithm::Diff::VERSION, Perl $^V)
This is LATEXDIFF 1.3.1a (Algorithm::Diff $Algorithm::Diff::VERSION, Perl $^V)
(c) 2004-2018 F J Tilmann
EOF

Expand Down Expand Up @@ -885,11 +885,11 @@ exetime(1);
###}

if ($flatten) {
$oldbody=flatten($oldbody,$oldpreamble,$oldfile,$encoding);
$newbody=flatten($newbody,$newpreamble,$newfile,$encoding);
$oldbody=flatten($oldbody,$oldpreamble,File::Spec->rel2abs($oldfile),$encoding);
$newbody=flatten($newbody,$newpreamble,File::Spec->rel2abs($newfile),$encoding);
# flatten preamble
$oldpreamble=flatten($oldpreamble,$oldpreamble,$oldfile,$encoding);
$newpreamble=flatten($newpreamble,$newpreamble,$newfile,$encoding);
$oldpreamble=flatten($oldpreamble,$oldpreamble,File::Spec->rel2abs($oldfile),$encoding);
$newpreamble=flatten($newpreamble,$newpreamble,File::Spec->rel2abs($newfile),$encoding);

### if ( $debug ) {
### open(FILE,">","latexdiff.debug.flatold");
Expand Down Expand Up @@ -1510,11 +1510,13 @@ sub remove_endinput {
# encoding is the encoding
sub flatten {
my ($text,$preamble,$filename,$encoding)=@_;
my ($includeonly,$dirname,$fname,$newpage,$fullfile,$filecontent,$replacement,$begline,$bblfile,$subfile,$command,$verbenv,$verboptions);
my ($includeonly,$dirname,$fname,$newpage,$fullfile,$filecontent,$replacement,$begline,$bblfile,$subfile,$command,$verbenv,$verboptions,$ignore,$fileonly);
my ($subpreamble,$subbody,$subpost);
my ($subdir,$subdirfull,$importfilepath);
require File::Basename ;
require File::Spec ;
$dirname = File::Basename::dirname($filename);
$filename = File::Spec->rel2abs( $filename ) ;
($ignore, $dirname, $fileonly) = File::Spec->splitpath($filename) ;
$bblfile = $filename;
$bblfile=~s/\.tex$//;
$bblfile.=".bbl";
Expand All @@ -1527,6 +1529,40 @@ sub flatten {

print STDERR "DEBUG: includeonly $includeonly\n" if $debug;

# Recursively replace \\import and \\subimport files
$text =~ s/(^(?:[^%\n]|\\%)*)(\\subimport\{(.*?)\}|\\import\{(.*?)\})(?:[\s]*)\{(.*?)\}/{
# $1 is begline
# $3 is directory if subimport
# $4 is directory if import
# $5 is filename
$begline = (defined($1)? $1 : "");
$subdir = $3 if defined($3);
$subdir = $4 if defined($4);
$fname = $5;
$fname .= ".tex" unless $fname =~ m|\.\w{3,4}$|;
print STDERR "DEBUG begline:", $begline, "\n" if $debug;
print STDERR "DEBUG", (defined($3)? "subimport_file:" : "import_file:"), $subdir, "\n" if $debug;
print STDERR "DEBUG file:", $fname, "\n" if $debug;
# subimport appends $subdir to the current $dirname. import replaces it with an absolute path.
$subdirfull = (defined($3) ? File::Spec->catdir($dirname,$subdir) : $subdir);
$importfilepath = File::Spec->catfile($subdirfull, $fname);
print STDERR "importing importfilepath:", $importfilepath,"\n" if $verbose;
if ( -f $importfilepath ) {
# If file exists, replace input or include command with expanded input
$replacement=flatten(read_file_with_encoding($importfilepath, $encoding), $preamble,$importfilepath,$encoding) or die "Could not open file ",$fullfile,": $!";
} else {
# if file does not exist, do not expand include or input command (do not warn if fname contains #[0-9] as it is then likely part of a command definition
# and is not meant to be expanded directly
print STDERR "WARNING: Could not find included file ",$importfilepath,". I will continue but not expand |$2|\n";
$replacement=(defined($3)? "\\subimport" : "\\import");
$replacement .= "{$subdir}{$fname} % Processed";
}
"$begline$replacement";
}/exgm;

# recursively replace \\input and \\include files
$text =~ s/(^(?:[^%\n]|\\%)*)(\\input\{(.*?)\}|\\include\{(${includeonly}(?:\.tex)?)\})/{
$begline=(defined($1)? $1 : "") ;
Expand Down Expand Up @@ -3725,7 +3761,8 @@ Miscelleneous options
be located in the same directories as the old and new master files,
respectively, making it possible to organise files into old and new directories.
--flatten is applied recursively, so inputted files can contain further
\\input statements.
\\input statements. Also handles files included by the import package
(\\import and \\subimport), and \\subfile command.
--help
-h Show this help text.
Expand Down Expand Up @@ -4286,7 +4323,8 @@ included sections are not expanded. The included files are assumed to
be located in the same directories as the old and new master files,
respectively, making it possible to organise files into old and new directories.
--flatten is applied recursively, so inputted files can contain further
C<\input> statements.
C<\input> statements. Also handles files included by the import package
(\\import and \\subimport), and \\subfile command.
Use of this option might result in prohibitive processing times for
larger documents, and the resulting difference document
Expand Down Expand Up @@ -5036,7 +5074,7 @@ verbatim[*]?
%DIF CULINECHBAR PREAMBLE
\RequirePackage[normalem]{ulem}
\RequirePackage[dvips]{changebar}
\RequirePackage{color}
\RequirePackage{color}\definecolor{RED}{rgb}{1,0,0}\definecolor{BLUE}{rgb}{0,0,1}
\providecommand{\DIFadd}[1]{\protect\cbstart{\protect\color{blue}\uwave{#1}}\protect\cbend}
\providecommand{\DIFdel}[1]{\protect\cbdelete{\protect\color{red}\sout{#1}}\protect\cbdelete}
%DIF END CULINECHBAR PREAMBLE
Expand Down

0 comments on commit 245d4dd

Please sign in to comment.