假设我有以下模块:
module Simple-Mod;
#| Calculate the nth fibonacci number.
multi fib( 0 ) { 1 }
multi fib( 1 ) { 1 }
multi fib( Int $n where * > 1 ) {
fib($n - 2 ) + fib($n - 1);
}
#| Say hello to a person.
sub hello( $person ) { say "Hello, $person!" }
=begin pod
=head1 SYNOPSIS
A really simple module.
=head1 Example
=begin code
use Simple-Mod;
say fib(3); #=> 2
hello("Gina"); #=> Hello, Gina!
=end code
=head1 Subroutines
=end pod
Run Code Online (Sandbox Code Playgroud)
目前,当我从这个模块中提取Pod时,我得到了这个:
sub fib(
Int $ where { ... },
)
Calculate the nth fibonacci number.
sub hello(
$person,
)
Say hello to a person.
SYNOPSIS
A really simple module.
Example
use Simple-Mod;
say fib(3); #=> 2
hello("Gina"); #=> Hello, Gina!
Subroutines
Run Code Online (Sandbox Code Playgroud)
是否可以指示Pod解析过程将子例程定义和注释放在Subroutines标题之后?像这样:
SYNOPSIS
A really simple module.
Example
use Simple-Mod;
say fib(3); #=> 2
hello("Gina"); #=> Hello, Gina!
Subroutines
sub fib(
Int $ where { ... },
)
Calculate the nth fibonacci number.
sub hello(
$person,
)
Say hello to a person.
Run Code Online (Sandbox Code Playgroud)
我大概可以把一切从=begin pod到=head1 Subroutines(随后=end pod在文件的顶部)指令,然后通常的代码与声明符块.但是,如果可能的话,我想将所有Pod保留在文件的底部.
通过修改该Pod::To::Text模块,我想出了一个有点黑客的解决方案,远非健壮。它仅取决于新的子例程以及对render,pod2text和heading2text例程的一些更改:
unit class Pod::To::Textx;
my $top-pod = Any;
method render($pod, Bool $declarator-displacement = False) {
$top-pod = $pod if $declarator-displacement;
pod2text($pod)
}
sub pod2text($pod) is export {
# other code
when Pod::Block::Declarator { if $top-pod { succeed }
else { declarator2text($pod) }
}
# remaining code
}
sub add-code-info($pod) {
return pod2text($pod.contents) unless $top-pod;
if $pod.contents.head.contents.lc.contains("routines") {
pod2text($pod.contents) ~
@($top-pod).grep({ $_ ~~ Pod::Block::Declarator })
.map({ "\n\n" ~ declarator2text($_) })
}
}
sub heading2text($pod) {
given $pod.level {
when 1 { add-code-info($pod) }
when 2 { ' ' ~ pod2text($pod.contents) }
default { ' ' ~ pod2text($pod.contents) }
}
}
# rest of code
Run Code Online (Sandbox Code Playgroud)
要在文件中渲染 Pod.p6并将声明器块放置在标题为 1 的标题级别下方Subroutines/Routines,请使用:
use Pod::To::Textx;
say Text.new().render($=pod, True);
Run Code Online (Sandbox Code Playgroud)
文件内。
| 归档时间: |
|
| 查看次数: |
131 次 |
| 最近记录: |