2017-08-10 98 views
3

我正在阅读Think Perl 6 by Laurent Rosenfeld,与Allen B. Downey 最近这是一篇非常不错的阅读。使用Perl处理批处理文本6

它有它的.tex文件可在github here

它的代码示例如下: enter image description here

我相信这将是有代码块有色这样非常有用: enter image description 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} 

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} 

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

我不能超越那个。任何帮助?使用正则表达式会很有帮助。

最好的问候,

网速慢

+0

基本思想(简单的文本替换,你可能甚至不需要正则表达式)是健全的。你的程序不工作?如果是这样,反而会发生什么? – Thilo

+1

不会简单的字符串替换就足够了吗?你的可变部分在哪里? – Jan

+0

只有第一个代码工作在目录中读取所有.tex文件。休息是我的虚拟代码,我喜欢如何继续。 –

回答

4

你需要做以下步骤:

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

我希望这有助于。

+0

好吧,但我对你的第一点感到困惑:创建每一行的副本。怎么做?我做到了,但没有奏效。 'for book/Conditional_and_recu rsion.tex“.IO.lines - > $ line {$ line.subst(/ \\ begin \ {verbatim \} /,”\\ begin \ {minted \} \ {perl6 \ }“)if $ line ~~/\\ begin \ {verbatim \} /; }' –

+0

@Suman在'$ line'后添加'copy',因此:''book/Conditional_and_recu rsion.tex“.IO.lines - > $ line是copy {'。否则,默认情况下'$ line'是只读的。 –

+0

@Suman请在问题中修改您的代码,将其更新为您当前正在尝试的内容,并让我们知道您所得到的错误。 –

1

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

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

# Print out next file, line by line 
for "$file.tex".IO.lines -> $line is copy { 

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

    # Print line 
    $fh-out.put: $line; 
}