Skip to content

Commit

Permalink
add VERBATIMENV configuration and set to defaults appropriate for lis…
Browse files Browse the repository at this point in the history
…tings and verbatim packages; partial fix of #38
  • Loading branch information
ftilmann committed Mar 5, 2017
1 parent 22cfe3d commit 9feb46f
Show file tree
Hide file tree
Showing 4 changed files with 262 additions and 13 deletions.
48 changes: 36 additions & 12 deletions latexdiff
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@
### - 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
### - move --show-options so that they are also capable of showing preamble (and commands) after all modificationsbased on source file packages
### - 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.1a
# - treat lstlisting and comment as equivalent to verbatim environment
# make environments that are treated like verbatim environments configurable (config variable VERBATIMENV)
# treat lstinlne as equivalent to verb command
# partially addresses issue #38
# - refactoring: set default configuration variables in a hash, and those that correspond to lists
# - feature: option --add-to-config used to amend configuration variables, which are regex pattern lists
# - bug fix: deleted figures when endfloat package is activated
Expand Down Expand Up @@ -184,6 +188,7 @@ my %CONFIG=(
# for the associated counter such that the overall numbers
# should be the same as in the new file
LISTENV => undef , # list making environments - they will generally be kept
VERBATIMENV => undef, # Environments whose content should be treated as verbatim text
ITEMCMD => 'item' # command marking item in a list environment
);
# Configuration variables: these have to be visible from the subroutines
Expand All @@ -198,7 +203,8 @@ my ($ARRENV,
$MATHREPL,
$MINWORDSBLOCK,
$PICTUREENV,
$SCALEDELGRAPHICS
$SCALEDELGRAPHICS,
$VERBATIMENV
);

# my $MINWORDSBLOCK=3; # minimum number of tokens to form an independent block
Expand Down Expand Up @@ -592,6 +598,7 @@ foreach ( keys(%CONFIG) ) {
elsif ( $_ eq "MATHARRENV" ) { $MATHARRENV = liststringtoregex($CONFIG{$_}) ; }
elsif ( $_ eq "MATHARRREPL" ) { $MATHARRREPL = $CONFIG{$_} ; }
elsif ( $_ eq "ARRENV" ) { $ARRENV = liststringtoregex($CONFIG{$_}) ; }
elsif ( $_ eq "VERBATIMENV" ) { $VERBATIMENV = liststringtoregex($CONFIG{$_}) ; }
elsif ( $_ eq "COUNTERCMD" ) { $COUNTERCMD = liststringtoregex($CONFIG{$_}) ; }
elsif ( $_ eq "SCALEDELGRAPHICS" ) { $SCALEDELGRAPHICS = $CONFIG{$_} ; }
else { die "Unknown configuration variable $_.";}
Expand Down Expand Up @@ -780,6 +787,15 @@ $new=read_file_with_encoding($newfile,$encoding);

# reset time
exetime(1);
### if ( $debug ) {
### open(RAWDIFF,">","latexdiff.debug.oldfile");
### print RAWDIFF $old;
### close(RAWDIFF);
### open(RAWDIFF,">","latexdiff.debug.newfile");
### print RAWDIFF $new;
### close(RAWDIFF);
### }
### print STDERR "DEBUG: before splitting old ",length($old),"\n" if $debug;
($oldpreamble,$oldbody,$oldpost)=splitdoc($old,'\\\\begin\{document\}','\\\\end\{document\}');

###if ( $oldpreamble =~ m/\\usepackage\[(\w*?)\]\{inputenc\}/ ) {
Expand All @@ -793,7 +809,7 @@ exetime(1);
#### Encode::from_to($oldbody,$encoding,"utf8");
#### Encode::from_to($oldpost,$encoding,"utf8");
### }

###print STDERR "DEBUG: before splitting new ",length($old),"\n" if $debug;
($newpreamble,$newbody,$newpost)=splitdoc($new,'\\\\begin\{document\}','\\\\end\{document\}');

###if ( $newpreamble =~ m/\\usepackage\[(\w*?)\]\{inputenc\}/ ) {
Expand Down Expand Up @@ -1142,7 +1158,7 @@ print STDERR "(",exetime()," s)\n","Done.\n" if $verbose;
# matching any of the elements
sub liststringtoregex {
my ($liststring)=@_;
my @elements=split(";",$liststring);
my @elements=grep /\S/,split(";",$liststring);
if ( @elements) {
return('(?:(?:' . join(')|(?:',@elements) .'))');
} else {
Expand Down Expand Up @@ -2237,8 +2253,8 @@ sub take_comments_and_enter_from_frac() {
# 2. mark all first empty line (in block of several) with \PAR tokens
# 3. Convert all '\%' into '\PERCENTAGE ' and all '\$' into \DOLLAR to make parsing regular expressions easier
# 4. Convert all \verb|some verbatim text| commands (where | can be an arbitrary character)
# into \verb{hash}
# 5. Convert \begin{verbatim} some verbatim text \end{verbatim} into \verbatim{hash}
# into \verb{hash} (also lstinline)
# 5. Convert \begin{verbatim} some verbatim text \end{verbatim} into \verbatim{hash} (not only verbatim, all patterns matching VERBATIMENV)
# 6. Convert _n into \SUBSCRIPTNB{n} and _{nnn} into \SUBSCRIPT{nn}
# 7. Convert ^n into \SUPERSCRIPTNB{n} and ^{nnn} into \SUPERSCRIPT{nn}
# 8. a. Convert $$ $$ into \begin{DOLLARDOLLAR} \end{DOLLARDOLLAR}
Expand Down Expand Up @@ -2278,8 +2294,9 @@ sub preprocess {
s/\n(\s*?)\n((?:\s*\n)*)/\n$1\\PAR\n$2/g ;
s/(?<!\\)\\%/\\PERCENTAGE /g ; # (?<! is negative lookbehind assertion to prevent \\% from being converted
s/(?<!\\)\\\$/\\DOLLAR /g ; # (?<! is negative lookbehind assertion to prevent \\$ from being converted
s/(\\verb\*?)(\S)(.*?)\2/"${1}{". tohash(\%verbhash,"${2}${3}${2}") ."}"/esg;
s/\\begin\{(verbatim\*?)\}(.*?)\\end\{\1\}/"\\${1}{". tohash(\%verbhash,"${2}") . "}"/esg;
s/(\\verb\*?|lstinline)(\S)(.*?)\2/"${1}{". tohash(\%verbhash,"${2}${3}${2}") ."}"/esg;
### s/\\begin\{(verbatim\*?)\}(.*?)\\end\{\1\}/"\\${1}{". tohash(\%verbhash,"${2}") . "}"/esg;
s/\\begin\{($VERBATIMENV)\}(.*?)\\end\{\1\}/"\\${1}{". tohash(\%verbhash,"${2}") . "}"/esg;
# Convert _n or _\cmd into \SUBSCRIPTNB{n} or \SUBSCRIPTNB{\cmd} and _{nnn} into \SUBSCRIPT{nn}
1 while s/(?<!\\)_(\s*([^{\\\s]|\\\w+))/\\SUBSCRIPTNB{$1}/g ;
1 while s/(?<!\\)_(\s*{($pat_n)})/\\SUBSCRIPT$1/g ;
Expand Down Expand Up @@ -2723,11 +2740,13 @@ sub postprocess {
s/^\Q${DELCMDOPEN}\E\n//msg;
# Expand hashes of verb and verbatim environments (note negative look behind assertion to not leak out of DIFDELCMD comments
s/(\\verb\*?)\{([-\d]*?)\}/"${1}". fromhash(\%verbhash,$2)/esg;
s/${DELCMDOPEN}\\(verbatim\*?)\{([-\d]*?)\}/"${DELCMDOPEN}\\begin{${1}}".fromhash(\%verbhash,$2,$DELCMDOPEN)."${DELCMDOPEN}\\end{${1}}"/esg;
s/(\\verb\*?|lstinline)\{([-\d]*?)\}/"${1}". fromhash(\%verbhash,$2)/esg;
### s/${DELCMDOPEN}\\(verbatim\*?)\{([-\d]*?)\}/"${DELCMDOPEN}\\begin{${1}}".fromhash(\%verbhash,$2,$DELCMDOPEN)."${DELCMDOPEN}\\end{${1}}"/esg;
s/${DELCMDOPEN}\\($VERBATIMENV)\{([-\d]*?)\}/"${DELCMDOPEN}\\begin{${1}}".fromhash(\%verbhash,$2,$DELCMDOPEN)."${DELCMDOPEN}\\end{${1}}"/esg;
### DEBUG s/${DELCMDOPEN}\\(verbatim\*?)/VERBATIMBLURB/sg;
### s/(?<!%\\DIFCMD < )\\(verbatim\*?)\{([-\d]*?)\}/"\\begin{${1}}".fromhash(\%verbhash,$2)."\\end{${1}}"/esg;
s/\\(verbatim\*?)\{([-\d]*?)\}/"\\begin{${1}}".fromhash(\%verbhash,$2)."\\end{${1}}"/esg;
### s/\\(verbatim\*?)\{([-\d]*?)\}/"\\begin{${1}}".fromhash(\%verbhash,$2)."\\end{${1}}"/esg;
s/\\($VERBATIMENV)\{([-\d]*?)\}/"\\begin{${1}}".fromhash(\%verbhash,$2)."\\end{${1}}"/esg;
# remove all \PAR tokens (taking care to properly keep commented out PAR's
# from introducing uncommented newlines - next line)
s/(%DIF < )([^\n]*?)\\PAR\n/$1$2\n$1\n/sg;
Expand Down Expand Up @@ -3846,7 +3865,7 @@ labels like this C<-L labelold -L labelnew>.
Suppress inclusion of old and new file names as comment in output file
=item B<--visble-label>
=item B<--visible-label>
Include old and new filenames (or labels set with C<--label> option) as
visible output.
Expand Down Expand Up @@ -4492,6 +4511,11 @@ paragraph
subparagraph
%%END COUNTERCMD CONFIG
%%BEGIN VERBATIMENV CONFIG
verbatim[*]?
lstlisting
comment
%%END VERBATIMENV CONFIG
%%% TYPES (Commands for highlighting changed blocks)
Expand Down
111 changes: 111 additions & 0 deletions testsuite/verbatim-new.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
\documentclass{article}
\usepackage{listings}
\usepackage{color}

\begin{document}

We edit to obain revised text, \verb|example-rev.tex|, listed here in
full but also included in the distribution (except that the ``verbatim'' environment had
to be renamed to ``Verbatim'' for the listing).
{\scriptsize
\begin{verbatim}
\documentclass[12pt,a4paper]{article}
\setlength{\topmargin}{-0.2in}
\setlength{\textheight}{9.5in}
\setlength{\oddsidemargin}{0.0in}
\setlength{\textwidth}{6in}
\title{latexdiff Example - Revised version}
\author{F Tilmann}
% Note how in the preamble visual markup is never used (even
% if some preamble might eventually end up as visible text.)
\begin{Document}
\maketitle
\section*{Introduction}
This is an extremely simple document that showcases some of the latexdiff features.
Type
\begin{Verbatim}
latexdiff -t UNDERLINE example-draft.tex example-rev.tex > example-diff.tex
\end{Verbatim}
to create the difference file. You can inspect this file directly. Then run either
\begin{Verbatim}
pdflatex example-diff.tex
xpdf example-diff.pdf
\end{Verbatim}
or
\begin{Verbatim}
latex example-diff.tex
dvips -o example-diff.ps example-diff.dvi
gv example-diff.ps
\end{Verbatim}
to display the markup.
\section*{Yet another section title}
More things could be said were it not for the constraints of time and space.
A paragraph with a line only in the revised document.
More things could be said were it not for the constraints of time and space.
And here is a typo.
Here is a table:
\begin{tabular}{ll}
Name & Description \\
\hline
Gandalf & White \\
Saruman & Evil
\end{tabular}
And now for something completely different, with not a paragraph in sight.
No change,
no markup!
\end{Document}
\end{verbatim}
}
We can now compare
this text to the draft version.
\newpage
A now an example making use of the \lstinline|listings| package.

\definecolor{gray}{rgb}{0.5,0.5,0.5}
We simply take a small subroutine of latexdif as an example:
\lstset{language=perl}
%\lstset{commentstyle=\color{gray}}
\begin{lstlisting}[commentstyle=\color{gray}]
# init_regex_arr_ext(\@array,$arg)
# appends array with regular expressions.
# if arg is a file name, then read in list of regular expressions from that file
# (one expression per line)
# Otherwise treat arg as a comma separated list of regular expressions
sub init_regex_arr_ext {
my ($arr,$arg)=@_;
init_regex_arr_list($arr,$arg);
}

# init_regex_arr_file(\@array,$fname)
# appends array with regular expressions.
# Read in list of regular expressions from $fname
# (one expression per line)
sub init_regex_arr_file {
my ($arr,$fname)=@_;
open(FILE,"$fname") or die ("Couldn't open $fname: $!");
while (<FILE>) {
chomp;
next if /^\s*#/ || /^\s*%/ || /^\s*$/ ;
push (@$arr,qr/^$_$/);
}
close(FILE);
}
\end{lstlisting}



\end{document}

114 changes: 114 additions & 0 deletions testsuite/verbatim-old.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
\documentclass{article}
\usepackage{listings}
\usepackage{color}

\begin{document}

We start with a draft text, \verb|example-draft.tex|, listed here in
full but also included in the distribution (except that the ``verbatim'' environment had
to be renamed to ``Verbatim'' for the listing).
{\scriptsize
\begin{verbatim}
\documentclass[12pt,a4paper]{article}
\setlength{\topmargin}{-0.2in}
\setlength{\textheight}{9.5in}
\setlength{\oddsidemargin}{0.0in}
\setlength{\textwidth}{6.5in}
\title{latexdiff Example - Draft version}
\author{F Tilmann}
\begin{Document}
\maketitle
\section*{Introduction}
This is an extremely simple document that showcases some of latexdiff features.
Type
\begin{Verbatim}
latexdiff -t UNDERLINE example-draft.tex example-rev.tex > example-diff.tex
\end{Verbatim}
to create the difference file. You can inspect this file directly. Then run either
\begin{Verbatim}
pdflatex example-diff.tex
xpdf example-diff.pdf
\end{Verbatim}
or
\begin{Verbatim}
latex example-diff.tex
dvips -o example-diff.ps example-diff.dvi
gv example-diff.ps
\end{Verbatim}
to display the markup. Of course, instead of \verb|xpdf| you can use
\verb|okular, evince, acroread| or any other pdf or postscript viewer.
\section*{Another section title}
A paragraph with a line only in the draft document. More things
could be said were it not for the constraints of time and space.
More things could be said were it not for the constraints of time and space.
And here is a tipo.
Here is a table:
\begin{tabular}{ll}
Name & Description \\
\hline
Gandalf & Grey \\
Saruman & White
\end{tabular}
And sometimes a whole paragraph gets completely rewritten. In this
case latexdiff marks up the whole paragraph even if some words in it
are identical.
No change, no markup!
\end{Document}
\end{verbatim}
}
We can now edit
this text as we would do with any other latex file to create
a new revision of the text, \verb|example-rev.tex|.
\newpage
A now an example making use of the \lstinline|listings| package.

\definecolor{gray}{rgb}{0.5,0.5,0.5}
We simply take a small subroutine of latexdif as an example:
\lstset{language=perl}
%\lstset{commentstyle=\color{gray}}
\begin{lstlisting}[commentstyle=\color{gray}]
# init_regex_arr_ext(\@array,$arg)
# appends array with regular expressions.
# if arg is a file name, then read in list of regular expressions from that file
# (one expression per line)
# Otherwise treat arg as a comma separated list of regular expressions
sub init_regex_arr_ext {
my ($arr,$arg)=@_;
if ( -f $arg ) {
init_regex_arr_file($arr,$arg);
} else {
init_regex_arr_list($arr,$arg);
}
}

# init_regex_arr_file(\@array,$fname)
# appends array with regular expressions.
# Read in list of regular expressions from $fname
# (one expression per line)
sub init_regex_arr_file {
my ($arr,$fname)=@_;
open(FILE,"$fname") or die ("Couldn't open $fname: $!");
while (<FILE>) {
chomp;
next if /^\s*#/ || /^\s*%/ || /^\s*$/ ;
push (@$arr,qr/^$_$/);
}
close(FILE);
}
\end{lstlisting}

\end{document}

2 changes: 1 addition & 1 deletion testsuite/verify
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ set testroots=( test rapine_et_al island_obs2004 texdiffsample gershwin12 "gersh
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 \
"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" \
"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 \
)

#set testroots= ( test subscript )
Expand Down

0 comments on commit 9feb46f

Please sign in to comment.