File Coverage

blib/lib/Perl6/Pod/Utl.pm
Criterion Covered Total %
statement 56 60 93.3
branch 9 14 64.2
condition 2 5 40.0
subroutine 12 12 100.0
pod 3 3 100.0
total 82 94 87.2


line stmt bran cond sub pod time code
1             #===============================================================================
2             #
3             # DESCRIPTION: Utils for Perl6 Pod
4             #
5             # AUTHOR: Aliaksandr P. Zahatski,
6             #===============================================================================
7             package Perl6::Pod::Utl;
8 6     6   87063 use strict;
  6         14  
  6         172  
9 6     6   32 use warnings;
  6         13  
  6         192  
10 6     6   5453 use utf8;
  6         64  
  6         34  
11             our $VERSION = '0.01';
12              
13              
14             =head2 parse_pod [default_pod => 0 ]
15              
16             =item * default_pod => 0/1 ,
17              
18             switch on/off ambient mode for para out of =pod blocks. Default 0 (ambient mode)
19             return ref to tree
20              
21             =cut
22              
23             sub parse_pod {
24 5     5 1 814 my ( $src, %args ) = @_;
25 5         10 my $r = do {
26 6     6   10373 use Regexp::Grammars;
  6         390922  
  6         70  
27 6     6   6649 use Perl6::Pod::Grammars;
  6         17  
  6         189  
28              
29 5         85 qr{
30            
31            
32             \A \Z
33             }xms;
34             };
35 5         12 my $tree;
36 5 100       264 if ( $src =~ $r ) {
37 6     6   4324 use Perl6::Pod::Lex;
  6         16  
  6         1680  
38 4         55 $tree = Perl6::Pod::Lex->new(%args)->make_tree( $/{File} );
39             }
40             else {
41 1         18 return undef;
42             }
43 4         46 $tree;
44             }
45              
46             =head2 strip_vmargin $vmargin, $txt
47              
48             =begin pod
49             =para
50             text
51             =end pod
52              
53             =cut
54              
55             sub strip_vmargin {
56 20     20 1 35 my ( $vmargin, $content ) = @_;
57              
58             #get min margin of text
59 20         26 my $min = $vmargin;
60 20         83 foreach ( split( /[\n\r]/, $content ) ) {
61 23 100       99 if (m/(\s+)/) {
62 10         23 my $length = length($1);
63 10 50       46 $min = $length if $length < $min;
64             }
65             }
66              
67             #remove only if $min > 0
68 20 100       58 if ( $min > 0 ) {
69 4         7 my $new_content = '';
70 4         17 foreach ( split( /[\n\r]/, $content ) ) {
71 4         14 $new_content .= substr( $_, $min ) . "\n";
72             }
73 4         10 $content = $new_content;
74             }
75 20         55 return $content;
76             }
77              
78             =head2 parse_para $text
79              
80             parse formatting codes
81              
82             Optrions:
83              
84             =item * allow=>[ 'A', 'B']
85              
86              
87             =cut
88              
89             sub parse_para {
90 6     6   2824 use Perl6::Pod::Codeactions;
  6         15  
  6         754  
91 13   50 13 1 6888 my $text = shift || return [];
92 13         65 my %delim = ( '<' => '>', '«' => '»', '<<' => '>>' );
93 13         33 my %allow = ( '*' => 1 );
94              
95 13         26 my %args = @_;
96 13 50       48 if ( my $allow = $args{allow} ) {
97 0 0       0 my @list = ref($allow) ? @$allow : ($allow);
98 0         0 %allow = ();
99              
100             #fill allowed fcodes
101 0         0 @allow{@list} = ();
102             }
103 13         22 my $DEFER_REGEX_COMPILATION = "";
104 13   33     46 my $r = $args{reg} || do {
105 6     6   34 use Regexp::Grammars;
  6         12  
  6         50  
106 6     6   1047 use Perl6::Pod::Grammars;
  6         15  
  6         362  
107             qr{
108              
109            
110            
111            
112             \A \Z
113             <[content]>+
114             .+?
115             [ \t]*
116             \#
117            
118             |
119             |
120             |
121             |
122             |
123             | <.text>
124             <%delim>
125             (??{ quotemeta $delim{$ARG{ldelim}} })
126            
127            
128             ( $ARG{name} && ( $ARG{name} eq uc($ARG{name} ) ) )
129             &&
130             ( exists $allow{'*'} || exists $allow{$ARG{name}} )
131            
132             })>
133             (?! \s+)
134            
135            
136             (?! \s+)
137            
138             (?: \| <[syns=(\S+)]>+ % ;)?
139             (?! \s+)
140            
141            
142              
143             (?! \s+)
144            
145            
146             #alternate presentation
147             (?: \| )? #(.*) \| not work for
148             #L< http://cpan.org > B L< haname | http:perl.html >
149             # '' => 'L< http://cpan.org > B L< haname | http:perl.html >'
150              
151             # (?:)? #hack
152              
153             ? #scheme specifier
154              
155             (?: )?
156             ?
157             (?: )? #internal addresses
158            
159             <[entry=([^,\;]+?)]>* % (\s*,\s*)
160             (?! \s+)
161            
162            
163             # X
164             ( (?{$MATCH{entry}=$MATCH{text}; $MATCH{form} = 1 })
165             |
166             ? \| <[entries=X_code_entry]>* % (\s*\;\s*)
167             (?{$MATCH{form} = 2})
168             )
169            
170              
171            
172            
173             <.hs>
174             ? #scheme specifier
175             (?: )?
176            
177             <.hs>
178              
179            
180            
181             <.hs> <[content]>*? <.hs>
182             $DEFER_REGEX_COMPILATION
183             }xms;
184             };
185              
186 13 50       426 if ( $text =~ $r->with_actions( Perl6::Pod::Codeactions->new ) ) {
187 13         105 return $/{Text};
188             }
189             else {
190 0           return undef;
191             }
192              
193             }
194              
195             1;
196