【问题标题】:Batch text processing with Perl 6使用 Perl 6 进行批量文本处理
【发布时间】:2019-11-21 23:24:41
【问题描述】:

我正在阅读 Laurent Rosenfeld 与 Allen B. Downey 合着的 Think Perl 6 最近是一本非常好的读物。

它的 .tex 文件在 github here 中可用。

它有这样的代码示例:

我相信让代码块像这样着色会非常有用:

为此,我们必须批处理上面存储库中包含的所有 .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}

\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}

我想用 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\}/;
}

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

最好的问候,

苏曼

【问题讨论】:

  • 基本思想(简单的文本替换,你甚至可能不需要正则表达式)是合理的。你的程序不工作吗?如果是这样,会发生什么?
  • 简单的字符串替换还不够吗?你的可变部分在哪里?
  • 只有第一个代码在目录中工作reads all .tex files。休息是我的虚拟代码以及我喜欢如何继续。
  • 你试过莫里茨的建议了吗?请尝试一下,如果您不知道该怎么做,请尽力并发布您尝试过的内容。

标签: regex latex raku rakudo


【解决方案1】:

您需要执行以下步骤:

  • 创建每行的副本并应用替换。您可以为此使用subst method
  • 将修改后的副本写入新文件(可能添加了扩展名.new 左右)
  • (可选)移动.new 以覆盖原始文件。请参阅this example 获取灵感。

我希望这会有所帮助。

【讨论】:

  • 好吧,但我对你的第一点感到困惑:创建每一行的副本。怎么做?我这样做了,但没有用。 for "book/Conditional_and_recu‌​rsion.tex".IO.lines -> $line { $line.subst(/\\begin\{verbatim\}/,"\\begin\{minted\}\{perl6‌​\}") if $line ~~/\\begin\{verbatim\}/; }
  • @Suman 在$line 之后添加is copy,因此:"book/Conditional_and_recu‌​rsion.tex".IO.lines -> $line is copy {。否则,$line 默认为只读。
  • @Suman 请在问题中编辑您的代码,将其更新为您当前正在尝试的内容,并让我们知道您遇到了什么错误。
【解决方案2】:

这是 moritz 前两个要点的一个实现。

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;
}

【讨论】:

    猜你喜欢
    • 2019-05-26
    • 1970-01-01
    • 2014-02-15
    • 2015-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-21
    相关资源
    最近更新 更多