使用Perl 6进行批处理文本

Sum*_*nal 3 regex latex rakudo perl6 raku

我正在与Laurent Rosenfeld一起阅读Think Perl 6, 最近与Allen B. Downey一起读这本书非常不错。

它的.tex文件在github 此处提供

它具有如下代码示例: 在此处输入图片说明

我相信将代码块设置如下颜色将非常有用: 在此处输入图片说明

为此,我们必须对上述存储库中包含的所有.tex文件进行批处理。为此,我们必须转换乳胶代码:

\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}
Run Code Online (Sandbox Code Playgroud)

\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}
Run Code Online (Sandbox Code Playgroud)

我想用Perl 6完成此任务。这是我打算做的事情。

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\}/;
}
Run Code Online (Sandbox Code Playgroud)

我无法超越。有什么帮助吗?使用正则表达式将非常有帮助。

最好的祝福,

苏曼

mor*_*itz 5

您需要执行以下步骤:

  • 创建每行的副本并应用替换。您可以使用SUBST方法
  • 将修改后的副本写入新文件(可能.new添加了扩展名等)
  • (可选)移动.new来覆盖原始文件。请参阅此示例以获取灵感。

我希望这有帮助。