Regex Perl 6的批文本处理
我正在阅读Laurent Rosenfeld和Allen B.Downey合著的Think Perl 6 最近这是一本很好的读物 它的.tex文件在github中可用 它有如下代码示例: 我相信将代码块着色将非常有用,如下所示: 要实现这一点,我们必须批处理上述存储库中包含的所有.tex文件。 为此,我们必须转换latex代码:Regex Perl 6的批文本处理,regex,latex,raku,rakudo,Regex,Latex,Raku,Rakudo,我正在阅读Laurent Rosenfeld和Allen B.Downey合著的Think Perl 6 最近这是一本很好的读物 它的.tex文件在github中可用 它有如下代码示例: 我相信将代码块着色将非常有用,如下所示: 要实现这一点,我们必须批处理上述存储库中包含的所有.tex文件。 为此,我们必须转换latex代码: \begin{verbatim} say 42 == 42; # True say 42 == 42.0;
\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}
到
我想用Perl6来实现这一点。
下面是我的计划
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\}/;
}
我无法超越这一点。有什么帮助吗?使用regexp将非常有用
致以最良好的祝愿
Suman您需要执行以下步骤:
- 创建每行的副本,并应用替换。你可以用这个
- 将修改后的副本写入新文件(可能添加了扩展名
左右).new
- (可选)移动
以覆盖原始文件。看看有没有灵感.new
我希望这能有所帮助。这里是莫里茨前两个要点的一个实现
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;
}
基本思想(简单的文本替换,您甚至可能不需要正则表达式)是合理的。你的程序不起作用吗?如果是这样的话,会发生什么呢?简单的字符串替换就足够了吗?变量部分在哪里?只有第一个代码可以工作
读取目录中的所有.tex文件
。Rest是我的虚拟代码,我喜欢这样做。你试过莫里茨的建议吗?请尝试一下,如果你不知道怎么做,请尽你最大的努力把你尝试过的东西贴出来。好吧,但我对你的第一点感到困惑:创建每一行的副本。怎么做?我这么做了,但没用<代码>用于“书本/条件”和“重复”rsion.tex“.IO.lines->$line{$line.subst(/\\begin\{verbatim\}/,“\\begin\{minted\}{perl6\}") 如果$line~~/\\begin\{verbatim\}/;}@Suman Add在$line
之后是copy
,那么:“book/Conditional”和rsion.tex“.IO.lines->$line为copy{
。否则,$line
默认为只读。@Suman请编辑问题中的代码,将其更新为您当前正在尝试的代码,并告知我们您遇到的错误。
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;
}