eventual.pl untested

filter: Statementstmt 0.0% 0 / 126 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 199 CC 11 SCAR 28.2
CC
11
Cov
0%
parse_pod
56.0
node_level
20.0
aggregate
6.0
CRAP 16.8
1 use Pod::Eventual::Simple;
2  
3 package Foo {
4 use Pod::Simple::PullParser;
5  
6 use Moo;
7  
8 extends 'Pod::Simple::PullParser';
9  
10 use experimental 'signatures';
11  
12 my @levels = (
13 [],
14 [ qw/ B L / ],
15 [qw/ Para Verbatim /],
16 [qw/ item-text /],
17 [qw/ over-text /],
18 ( map { ["head$_"] } reverse 1..4),
19 [ 'Document' ],
20 );
21  
22 my %commands = (
23 'Document' => { },
24 head1 => { section => 1, },
25 'item-text' => { section => 1, },
26 );
27  
28 use experimental qw/ postderef /;
29  
30 sub add_directive($new, $alias) {
31 $commands{$new} = { $commands{$alias}->%*, alias => $alias };
32  
33 use List::AllUtils qw/ first /;
34  
35 push( (first {
36 $alias ~~ @$_
37 } @levels)->@*, $new );
38  
39 }
40  
41 add_directive(synopsis => 'head1');
42  
43 has xml => (
44 is => 'ro',
45 lazy => 1,
46 default => sub {
47 use XML::Writer;
48 use XML::Writer::Simpler;
49 XML::Writer::Simpler->new( OUTPUT => 'self' );
50 },
51 );
52  
53 sub run($self) {
54 $self->parse_pod;
55 $self->xml->to_string;
56 }
57  
58 sub node_level($token) {
59 use List::AllUtils qw/ first_index /;
60 use experimental 'smartmatch';
61  
62 my $i = first_index { $token ~~ @$_ } @levels;
63 warn $i;
64 return $i;
65 }
66  
67 sub parse_pod($self, $end_cond = undef ) {
68 while( my $token = $self->get_token ) {
69 if( $end_cond and $end_cond->($token) ) {
70 $self->unget_token($token);
71 return;
72 }
73  
74 if( $token->is_text) {
75 $self->xml->characters( $token->text );
76 next;
77 }
78  
79 my $tag = $token->tagname;
80
81 next if $token->is_end;
82  
83 use experimental 'postderef';
84  
85 my $normalized = $tag;
86 if( my $alias = $commands{$tag}{alias} ) {
87 $normalized = $alias;
88 $token->attr( class => $tag );
89 }
90  
91 $self->xml->tag( $commands{$tag}{section} ? 'section' : $normalized, [ map { s/~//gr } $token->attr_hash->%* ], sub {
92 if( $commands{$tag}{section} ) {
93 $self->xml->tag( $normalized, sub {
94 $self->parse_pod( sub($tag) { $tag->is_end and $tag->is_tag(
95 $token->tagname
96 ) } );
97 });
98  
99  
100 my $level = node_level($tag);
101  
102 $self->parse_pod( sub($tag) {
103 $tag->is_start and
104 $level <= node_level( $tag->tagname )
105 } );
106  
107 }
108 else {
109 $self->parse_pod( sub($tag) { $tag->is_end and $tag->is_tag(
110 $token->tagname
111 ) } );
112 }
113 });
114  
115  
116 }
117 }
118  
119 }
120  
121 my $parser = Foo->new;
122 my $document_source = join '', <DATA>;
123 $parser->accept_directive_as_processed(qw/ synopsis /);
124 $parser->set_source( \$document_source );
125 print $parser->run;
126 exit;
127  
128 my $data = Pod::Eventual::Simple->read_string(
129 join '', <DATA>
130 );
131  
132 use DDP;
133  
134  
135  
136  
137 my %command_alias = (
138 synopsis => 'head1',
139 );
140  
141 $data = aggregate($data);
142 p $data;
143  
144 use 5.20.0;
145 use experimental 'signatures';
146  
147 sub node_level($node) {
148 if( $node->{command} =~ /head(\d+)/ ) {
149 return 6 - $1;
150 }
151 return 1 if $node->{command} eq 'over';
152 return 1 if $node->{command} eq 'back';
153 return 0;
154 }
155  
156 sub aggregate($data,$processed=undef) {
157 $processed ||= [];
158  
159 my @data = @$data;
160 my $first = shift @data or return $processed;
161  
162 my $level = node_level($first);
163  
164 use List::MoreUtils qw/ before /;
165  
166 my @inner = before { node_level($_) >= $level } @data;
167  
168 $first->{inner} = __SUB__->(\@inner) if @inner;
169  
170 splice @data, 0, scalar @inner;
171  
172 @_ = ( \@data, [ @$processed, $first ] );
173  
174 goto __SUB__;
175 }
176  
177  
178 __DATA__