在Perl 6中,如何在仍使用声明符块记录方法/子的同时将模块的pod保持在文件底部?

时间:2018-12-23 19:00:00

标签: perl6 raku

假设我有以下模块:

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

此刻,当我从该模块中提取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

是否可以指示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.

我可能会将所有内容从=begin pod=head1 Subroutines(后跟=end pod)放在文件的顶部,然后将带有声明符块的常规代码放置在文件顶部。但是,如果可能,我想将所有Pod都保留在文件的底部。

1 个答案:

答案 0 :(得分:1)

通过修补Pod::To::Text模块,我提出了一个有点棘手的解决方案 这远非健壮。它仅取决于新的子例程以及对renderpod2textheading2text例程的一些更改:

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

要将Pod呈现在.p6文件中,并将声明符块放在标题为Subroutines/Routines的标题1下,请使用:

 use Pod::To::Textx;

 say Text.new().render($=pod, True);

在文件内部。