From 9feb46f26c0dd2e3e1804676bfc2fd16f42f0d06 Mon Sep 17 00:00:00 2001 From: Frederik Tilmann Date: Sun, 5 Mar 2017 16:22:15 +0100 Subject: [PATCH] add VERBATIMENV configuration and set to defaults appropriate for listings and verbatim packages; partial fix of #38 --- latexdiff | 48 ++++++++++++---- testsuite/verbatim-new.tex | 111 ++++++++++++++++++++++++++++++++++++ testsuite/verbatim-old.tex | 114 +++++++++++++++++++++++++++++++++++++ testsuite/verify | 2 +- 4 files changed, 262 insertions(+), 13 deletions(-) create mode 100644 testsuite/verbatim-new.tex create mode 100644 testsuite/verbatim-old.tex diff --git a/latexdiff b/latexdiff index 22a3bf1..e471a88 100755 --- a/latexdiff +++ b/latexdiff @@ -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 @@ -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 @@ -198,7 +203,8 @@ my ($ARRENV, $MATHREPL, $MINWORDSBLOCK, $PICTUREENV, - $SCALEDELGRAPHICS + $SCALEDELGRAPHICS, + $VERBATIMENV ); # my $MINWORDSBLOCK=3; # minimum number of tokens to form an independent block @@ -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 $_.";} @@ -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\}/ ) { @@ -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\}/ ) { @@ -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 { @@ -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} @@ -2278,8 +2294,9 @@ sub preprocess { s/\n(\s*?)\n((?:\s*\n)*)/\n$1\\PAR\n$2/g ; s/(?. 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. @@ -4492,6 +4511,11 @@ paragraph subparagraph %%END COUNTERCMD CONFIG +%%BEGIN VERBATIMENV CONFIG +verbatim[*]? +lstlisting +comment +%%END VERBATIMENV CONFIG %%% TYPES (Commands for highlighting changed blocks) diff --git a/testsuite/verbatim-new.tex b/testsuite/verbatim-new.tex new file mode 100644 index 0000000..08d8402 --- /dev/null +++ b/testsuite/verbatim-new.tex @@ -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 () { + chomp; + next if /^\s*#/ || /^\s*%/ || /^\s*$/ ; + push (@$arr,qr/^$_$/); + } + close(FILE); +} +\end{lstlisting} + + + +\end{document} + diff --git a/testsuite/verbatim-old.tex b/testsuite/verbatim-old.tex new file mode 100644 index 0000000..8680bd6 --- /dev/null +++ b/testsuite/verbatim-old.tex @@ -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 () { + chomp; + next if /^\s*#/ || /^\s*%/ || /^\s*$/ ; + push (@$arr,qr/^$_$/); + } + close(FILE); +} +\end{lstlisting} + +\end{document} + diff --git a/testsuite/verify b/testsuite/verify index 5cb523e..684996f 100755 --- a/testsuite/verify +++ b/testsuite/verify @@ -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 )