捕获并执行多行代码,并将结果合并到raku中

问题描述 投票:5回答:3

这是降价文件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}
say "this is promising";
say $*CWD;
```



This code will **not** be evaluated.


```{raku evaluate=FALSE}
say "Hello world";
```

我想将其转换为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}
say "this is promising";
say $*CWD;
```

Output:
```
this is promising
"C:\Users\suman".IO
```

This code will **not** be evaluated.

Code:
```{raku evaluate=FALSE}
say "Hello world";
```

我想完成的是:

  • 捕获backticks{raku evaluate}backticks之间的代码
  • 如果评估为真,则执行代码
  • 插入代码并输出回文档中

我尝试做的事情:

  1. 捕获多行代码并评估表达式
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
        };
};
  1. 创建一个temp.p6文件,并从上方将代码块$ 1写入其中
my $fh="temp.p6".IO.spurt: $1;
  1. 如果$ 0为TRUE,则执行块
my $output= q:x/raku temp.p6/ if $0==TRUE
  1. 在创建中间example_new.md时将所有这些都集成到最终的example.md中>
  2. my $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");
    
    

我被困在第一步。有帮助吗?

这是markdown文档example.md,我拥有:##新语言Perl 6是不同于Perl 5的新语言。##它提供了什么+面向对象的编程,包括泛型,角色和...

regex replace perl6 raku capturing-group
3个回答
5
投票

完成“我想完成的事情”的代码]

您可以run this code against your data with glot.io


6
投票

有两种执行代码并捕获输出的方式:


-1
投票

您可以尝试此正则表达式:

© www.soinside.com 2019 - 2024. All rights reserved.