Skip to content

Commit

Permalink
--flatten: if file is not found, do not fail, simply warn and leave c…
Browse files Browse the repository at this point in the history
…ommand unexpanded -- inspired by issue #112
  • Loading branch information
ftilmann committed Dec 29, 2017
1 parent 6bf2a5e commit 4c20a8b
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 19 deletions.
56 changes: 38 additions & 18 deletions latexdiff
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@
### - use kdiff3 as a merge tool
### - make style that allows (forward and backjumping with hyperref)
### - --flatten option only expands first including command per line if there is more than one
### --flatten if file not found, don't fail, simply warn and leave input (or equivalent command)
### - move --show-xxx options so that they are also capable of showing preamble (and commands) after all modificationsbased on source file packages
#
# Version 1.2.2a:
# - improved pattern matching: now allows nested angular brackets, and is no longer confused by escaped curly braces
# - improved pattern matching in COARSE mode: occasionally, the closing bracket or some other elements would be matched in an 'unnatural' way due to another sequence being more minimal in the computational sense, sometimes even causing errors due to tokens moving in or out of the scope of math environments. This is now discouraged by adding internal \DIFANCHOR commands (which are removed again in post-processing) (fixes issues reported via email by li_ruomeng .
# - verbatim and lstlisting environments are marked-up with line-by-line in a similar style to non-verbatim text (requires the listing package to be installed)
# (see new configuration variable VERBATIMLINEENV)
# - --flatten now supports \verbatiminput and \lstlistinput
# - --flatten: now supports \verbatiminput and \lstlistinput
- --flatten: if file is not found, do not fail, simply warn and leave command unexpanded (inspired by issue #112). Don't warn if file name contains #[0-9] as it is then most likely an argument within a command definition rather than an actual file (applies to \input, \subfile, \include commands)
# Bug fixes:
# - pattern matching of \verb and \lstinline commands had an error which meant they would trigger on commands beginning with \verb.
#
Expand Down Expand Up @@ -1483,7 +1483,7 @@ sub remove_endinput {
# encoding is the encoding
sub flatten {
my ($text,$preamble,$filename,$encoding)=@_;
my ($includeonly,$dirname,$fname,$newpage,$replacement,$begline,$bblfile,$subfile,$command,$verbenv,$verboptions);
my ($includeonly,$dirname,$fname,$newpage,$fullfile,$filecontent,$replacement,$begline,$bblfile,$subfile,$command,$verbenv,$verboptions);
my ($subpreamble,$subbody,$subpost);
require File::Basename ;
require File::Spec ;
Expand All @@ -1501,21 +1501,30 @@ sub flatten {
print STDERR "DEBUG: includeonly $includeonly\n" if $debug;

# recursively replace \\input and \\include files
1 while $text=~s/(^(?:[^%\n]|\\%)*)(?:\\input\{(.*?)\}|\\include\{(${includeonly}(?:\.tex)?)\})/{
$text =~ s/(^(?:[^%\n]|\\%)*)(\\input\{(.*?)\}|\\include\{(${includeonly}(?:\.tex)?)\})/{
$begline=(defined($1)? $1 : "") ;
$fname = $2 if defined($2) ;
$fname = $3 if defined($3) ;
$fname = $4 if defined($4) ;
# # add tex extension unless there is a three or four letter extension already
$fname .= ".tex" unless $fname =~ m|\.\w{3,4}$|;
$fullfile = File::Spec->catfile($dirname,$fname);
print STDERR "DEBUG Beg of line match |$1|\n" if defined($1) && $debug ;
print STDERR "Include file $fname\n" if $verbose;
print STDERR "DEBUG looking for file ",File::Spec->catfile($dirname,$fname), "\n" if $debug;
print STDERR "DEBUG looking for file ",$fullfile, "\n" if $debug;
# content of file becomes replacement value (use recursion), add \newpage if the command was include
###$replacement=read_file_with_encoding(File::Spec->catfile($dirname,$fname), $encoding) or die "Couldn't find file ",File::Spec->catfile($dirname,$fname),": $!";
$replacement=flatten(read_file_with_encoding(File::Spec->catfile($dirname,$fname), $encoding), $preamble,$filename,$encoding) or die "Couldn't find file ",File::Spec->catfile($dirname,$fname),": $!";
$replacement = remove_endinput($replacement);
# \include always starts a new page; use explicit \newpage command to simulate this
$newpage=(defined($3)? " \\newpage " : "") ;
if ( -f $fullfile ) {
# If file exists, replace input or include command with expanded input
$replacement=flatten(read_file_with_encoding($fullfile, $encoding), $preamble,$filename,$encoding) or die "Could not open file ",$fullfile,": $!";
$replacement = remove_endinput($replacement);
# \include always starts a new page; use explicit \newpage command to simulate this
$newpage=(defined($4)? " \\newpage " : "") ;
} 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 ",$fullfile,". I will continue but not expand |$2|\n" unless $fname =~ m(#[0-9]) ;
$replacement = $2 ; # i.e. just the original command again -> make no change file does not exist
$newpage="";
}
"$begline$newpage$replacement$newpage";
}/exgm;

Expand All @@ -1539,16 +1548,25 @@ sub flatten {
$fname .= ".tex" unless $fname =~ m|\.\w{3,4}|;
### print STDERR "DEBUG Beg of line match |$1|\n" if defined($1) && $debug ;
print STDERR "Include file as subfile $fname\n" if $verbose;
### print STDERR "DEBUG looking for file ",File::Spec->catfile($dirname,$fname), "\n" if $debug;
### print STDERR "DEBUG looking for file |",File::Spec->catfile($dirname,$fname), "|\n" if $debug;
# content of file becomes replacement value (use recursion)
# now strip away everything outside and including \begin{document} and \end{document} pair#
# # note: no checking for comments is made
$subfile=read_file_with_encoding(File::Spec->catfile($dirname,$fname), $encoding) or die "Couldn't find file ",File::Spec->catfile($dirname,$fname),": $!";
($subpreamble,$subbody,$subpost)=splitdoc($subfile,'\\\\begin\{document\}','\\\\end\{document\}');
### $subfile=~s|^.*\\begin{document}||s;
### $subfile=~s|\\end{document}.*$||s;
$replacement=flatten($subbody, $preamble,$filename,$encoding);
$replacement = remove_endinput($replacement);
$fullfile=File::Spec->catfile($dirname,$fname);
if ( -f $fullfile) {
# if file exists, expand \subfile command by contents of file
$subfile=read_file_with_encoding($fullfile,$encoding) or die "Could not open included subfile ",$fullfile,": $!";
($subpreamble,$subbody,$subpost)=splitdoc($subfile,'\\\\begin\{document\}','\\\\end\{document\}');
### $subfile=~s|^.*\\begin{document}||s;
### $subfile=~s|\\end{document}.*$||s;
$replacement=flatten($subbody, $preamble,$filename,$encoding);
### $replacement = remove_endinput($replacement);
} else {
# if file does not exist, do not expand subfile
print STDERR "WARNING: Could not find subfile ",$fullfile,". I will continue but not expand |$2|\n" unless $fname =~ m(#[0-9]) ;
$replacement = "\\subfile\{$2\}" ; # i.e. just the original command again -> make no change file does not exist
}
"$begline$replacement";
}/exgm;

Expand Down Expand Up @@ -3054,6 +3072,8 @@ sub postprocess {
### # disabled as this turned out to be a bad idea
### 1 while s/(%.*)\\CLEFTBRACED (.*)$/$1\{$2/mg ;
### 1 while s/(%.*)\\CRIGHTBRACED (.*)$/$1\}$2/mg ;

### 1 while s/(%.*)DOLLARDIF/$1\$/mg ;
# although we only renamed $ in comments to DOLLARDIFF, we might have lost the % in unchanged verbatim blocks, so rename all
s/DOLLARDIF/\$/g;
### # undo renaming of the \cite.. commands in comments
Expand Down
9 changes: 9 additions & 0 deletions testsuite/master-new.tex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
\includeonly{delequ-slave-new}

\begin{document}

\newcommand{\includesvg}[1]{%
\executeiffilenewer{./pics/#1.svg}{./pics/#1.pdf}%
{inkscape -z -D --file=./pics/#1.svg %
--export-pdf=./pics/#1.pdf --export-latex}%
\input{./pics/#1.pdf_tex}%
}


\textbf{delequ}
\include{delequ-slave-new}

Expand Down
5 changes: 5 additions & 0 deletions testsuite/subfile-includeme.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
\documentclass[main.tex]{subfiles}
\begin{document}
Text!
\end{document}

12 changes: 12 additions & 0 deletions testsuite/test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdio.h>
main()
{
char string[30];
while(1){
printf("enter:");
scanf("%[+-01-9]",string);
while(getchar()!=10);
printf("...%s..%d..\n",string,atoi(string));
string[0]='\0';
}
}
12 changes: 12 additions & 0 deletions testsuite/testg.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <stdio.h>
main()
{
char string[30];
while(1){
printf("Eingabe:");
scanf("%[+-01-9]",string);
while(getchar()!=10);
printf("...%s..%d..\n",string,atoi(string));
string[0]='\0';
}
}
2 changes: 1 addition & 1 deletion testsuite/verify
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ set bodydiff=0
set testroots=( test rapine_et_al island_obs2004 texdiffsample gershwin12 "gershwin34 --graphics-markup=none" example utf \
delequ latin9 pollack move-equation nomarkup subscript doubledollar \
DIFDELCMDBUG eqnarray subscriptm delequ2 schneider gennady umesh underwood endfloat endfloat2 outerrise \
delequ3 delequ4 "simplefrac --allow-spaces" "master --flatten" titlediffTest2 exampleDiff bornd2 \
delequ3 delequ4 "simplefrac --allow-spaces" "master --flatten" "subfile --flatten" titlediffTest2 exampleDiff bornd2 \
"rolla --math-markup=2" mini "complex-maths --math-markup=1" margalit circonflex mwe-comment "apacite-test --flatten" quoteddollarunderscore units complicated-math move-equation2 lists "figures --graphics-markup=both" alignat "safecmd --append-safecmd=remark" verbatim embedded-math-array "verbatim-input --flatten" anchordemo \
)

Expand Down

0 comments on commit 4c20a8b

Please sign in to comment.