这是一个标记文档example.md:
## New language
Raku is a new language different from Perl.
## what does it offer
+ Object-oriented programming including generics, roles and multiple dispatch
+ Functional programming primitives, lazy and eager list evaluation, junctions, autothreading and hyperoperators (vector operators)
+ Parallelism, concurrency, and asynchrony including multi-core support
+ Definable grammars for pattern matching and generalized string processing
+ Optional and gradual typing
This code will be evaluated.
```{raku evaluate=TRUE}4/5
Rakudo is a compiler for raku programming language. Install it and you're all set to run raku programs!
This code will be evaluated.
```{raku evaluate=TRUE}说“这是有希望的”;
说$*CWD;
This code will **not** be evaluated.
```{raku evaluate=FALSE}说“你好世界”;
我想将其转换为example.md,如下所示,其中包含代码和输出。
## New language
Raku is a new language different from Perl.
## what does it offer
+ Object-oriented programming including generics, roles and multiple dispatch
+ Functional programming primitives, lazy and eager list evaluation, junctions, autothreading and hyperoperators (vector operators)
+ Parallelism, concurrency, and asynchrony including multi-core support
+ Definable grammars for pattern matching and generalized string processing
+ Optional and gradual typing
This code will be evaluated.
Code:
```{raku evaluate=TRUE}4/5
Output:0.8
Rakudo is a compiler for raku programming language. Install it and you're all set to run raku programs!
This code will be evaluated.
Code:
```{raku evaluate=TRUE}说“这是有希望的”;
说$*CWD;
Output:这是很有希望的
“C:\User\suman”.IO
This code will **not** be evaluated.
Code:
```{raku evaluate=FALSE}说“你好世界”;
我想要做的是:
backticks{raku evaluate}和backticks之间的代码我想做的是:
my $array= 'example.md'.IO.slurp;
#multiline capture code chunk and evaluate separately
if $array~~/\`\`\`\{raku (.*)\}(.*)\`\`\`/ {
#the first capture $0 will be evaluate
if $0~~"TRUE"{
#execute second capture which is code chunk which is captured in $1
}else {
# don't execute code
};
};my $fh="temp.p6".IO.spurt: $1;my $output= q:x/raku temp.p6/ if $0==TRUEmy $fh-out = open "example_new.md", :w; # Create a new file
# Print out next file, line by line
for "$file.tex".IO.lines -> $line {
# write output of code to example_new.md
}
$fh-out.close;
# copy
my $io = IO::Path.new("example_new.md");
$io.copy("example.md");
# clean up
unlink("example.md");
# move
$io.rename("example.md");我被困在了第一步。有什么帮助吗?
发布于 2019-07-20 19:30:00
执行代码和捕获输出有两种方法:
my $result = qqx{perl6 $filename}生成一个单独的进程。EVAL在同一个解释器中执行代码,并使用IO::Capture::Simple捕获STDOUT:my $re = regex {
^^ # logical newline
'```{perl6 evaluate=' (TRUE|FALSE) '}'
$<code>=(.*?)
'```'
}
for $input.match(:global, $re) -> $match {
if $match[0] eq 'TRUE' {
use IO::Capture::Simple;
my $result = capture_stdout {
use MONKEY-SEE-NO-EVAL;
EVAL $match<code>;
}
# use $result now
}
}现在您只需要从match切换到subst,并从要替换的块返回值,然后就完成了。
我希望这能让你知道该怎么做。
https://stackoverflow.com/questions/57127263
复制相似问题