4

I am reading Think Perl 6 by Laurent Rosenfeld, with Allen B. Downey recently which is a very good reading.

It has its .tex files available in github here.

It has code examples like this: enter image description here

I believe it would be very useful to have the code blocks colored like this: enter image description here

For this to happen we have to batch process all .tex files contained in the repository above. For this to happen we have to convert the latex code:

\begin{verbatim}
        say 42 ==  42;           # True
        say 42 ==  42.0;         # True
        say 42 ===  42;          # True
        say 42 === 42.0;         # False
\end{verbatim}


\begin{verbatim}
$x eq $y            # $x is string-wise equal to $y
$x ne $y            # $x is string-wise not equal to $y
$x gt $y            # $x is greater than $y (alphabetically after)
$x lt $y            # $x is less than $y (alphabetically before)
$x ge $y            # $x is greater than or equal to $y
$x le $y            # $x is less than or equal to $y
$x eqv $y           # $x is truly equivalent to $y
\end{verbatim}

TO

\begin{minted}{perl6}
        say 42 ==  42;           # True
        say 42 ==  42.0;         # True
        say 42 ===  42;          # True
        say 42 === 42.0;         # False
\end{minted}


\begin{minted}{perl6}
$x eq $y            # $x is string-wise equal to $y
$x ne $y            # $x is string-wise not equal to $y
$x gt $y            # $x is greater than $y (alphabetically after)
$x lt $y            # $x is less than $y (alphabetically before)
$x ge $y            # $x is greater than or equal to $y
$x le $y            # $x is less than or equal to $y
$x eqv $y           # $x is truly equivalent to $y
\end{minted}

I want to accomplish this with Perl 6. Here is how I plan to do.

THIS IS DUMMY CODE

# First I want to skim all the .tex files in the cloned repo (with git) 

for dir("ThinkPerl6/book") ->$file {
  say $file if $file~~/\.tex/;
}

# Read each .tex file and modify, replace `\begin{verbatim}` with `\begin{minted}{perl6}`

for "$file.tex".IO.lines -> $line {
  substitute with "\begin{minted}{perl6}" if $line ~~/\\begin\{verbatim\}/;
}

# Read each .tex file and modify, replace `\end{verbatim}` with `\end{minted}`

for "$file.tex".IO.lines -> $line {
  substitute with "\end{minted}" if $line ~~/\\end\{verbatim\}/;
}

I could not go beyond that. Any help? Use of regexp would be very helpful.

Best regards,

Suman

Christopher Bottoms
  • 11,218
  • 8
  • 50
  • 99
Suman Khanal
  • 3,079
  • 1
  • 17
  • 29
  • The basic idea (simple text substitution, you may not even need regex) is sound. Is your program not working? If so, what happens instead? – Thilo Aug 10 '17 at 09:32
  • 1
    Wouldn't simple string replacements suffice? Where is your variable part ? – Jan Aug 10 '17 at 09:32
  • Only the first code works `reads all .tex files` in directory. Rest is my dummy code and how I like to proceed. – Suman Khanal Aug 10 '17 at 09:36
  • 1
    Have you tried moritz's suggestions? Please try them and if you can't figure out how to do them, please do your best and post what you've tried. – Christopher Bottoms Aug 10 '17 at 17:01

2 Answers2

6

You need to do the following steps:

  • create a copy of each line with a substitution applied. You can use the subst method for that
  • write the modified copy to a new file (maybe with extension .new added or so)
  • optionally, move the .new to override the original file. See this example for inspiration.

I hope this helps.

moritz
  • 12,710
  • 1
  • 41
  • 63
  • Alright but I am stucked regarding your first point : creating copy of each line. How to do it? I did this but it didn't work. `for "book/Conditional_and_recu‌​rsion.tex".IO.lines -> $line { $line.subst(/\\begin\{verbatim\}/,"\\begin\{minted\}\{perl6‌​\}") if $line ~~/\\begin\{verbatim\}/; }` – Suman Khanal Aug 11 '17 at 17:53
  • @Suman Add `is copy` after `$line`, thus: `"book/Conditional_and_recu‌​rsion.tex".IO.lines -> $line is copy {`. Otherwise, `$line` is read-only by default. – Christopher Bottoms Aug 11 '17 at 20:25
  • @Suman Please edit your code in the question to update it to what you are currently trying and let us know what errors you are getting. – Christopher Bottoms Aug 12 '17 at 14:28
4

Here is one implementation of moritz's first two bullet points.

my $fh-out = open "$file.new.tex", :w; # Create a new file

# Read in old file, line by line
for "$file.tex".IO.lines -> $line is copy {

    # Make changes, if needed
    $line.=subst('\begin\{verbatim\}','\begin{minted}{perl6}');
    $line.=subst('\end\{verbatim\}','\end{minted}');

    # Print line to new file
    $fh-out.put: $line;
}
Christopher Bottoms
  • 11,218
  • 8
  • 50
  • 99