File Coverage

blib/lib/Perl6/Pod/Utl.pm
Criterion Covered Total %
statement 57 61 93.4
branch 9 14 64.2
condition 2 5 40.0
subroutine 12 12 100.0
pod 3 3 100.0
total 83 95 87.3


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   86698 use strict;
  6         12  
  6         171  
9 6     6   32 use warnings;
  6         12  
  6         165  
10 6     6   5752 use utf8;
  6         69  
  6         31  
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 783 my ( $src, %args ) = @_;
25 5         10 my $r = do {
26 6     6   10005 use Regexp::Grammars;
  6         103589  
  6         57  
  5         12  
27 6     6   24560 use Perl6::Pod::Grammars;
  6         21  
  6         181  
28              
29 5         69 qr{
30            
31            
32             \A \Z
33             }xms;
34             };
35 5         11 my $tree;
36 5 100       218 if ( $src =~ $r ) {
37 6     6   4140 use Perl6::Pod::Lex;
  6         17  
  6         1510  
38 4         58 $tree = Perl6::Pod::Lex->new(%args)->make_tree( $/{File} );
39             }
40             else {
41 1         17 return undef;
42             }
43 4         51 $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 37 my ( $vmargin, $content ) = @_;
57              
58             #get min margin of text
59 20         27 my $min = $vmargin;
60 20         93 foreach ( split( /[\n\r]/, $content ) ) {
61 23 100       90 if (m/(\s+)/) {
62 10         20 my $length = length($1);
63 10 50       36 $min = $length if $length < $min;
64             }
65             }
66              
67             #remove only if $min > 0
68 20 100       51 if ( $min > 0 ) {
69 4         7 my $new_content = '';
70 4         15 foreach ( split( /[\n\r]/, $content ) ) {
71 4         11 $new_content .= substr( $_, $min ) . "\n";
72             }
73 4         10 $content = $new_content;
74             }
75 20         53 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   2685 use Perl6::Pod::Codeactions;
  6         15  
  6         678  
91 13   50 13 1 8473 my $text = shift || return [];
92 13         68 my %delim = ( '<' => '>', '«' => '»', '<<' => '>>' );
93 13         33 my %allow = ( '*' => 1 );
94              
95 13         21 my %args = @_;
96 13 50       56 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         24 my $DEFER_REGEX_COMPILATION = "";
104 13   33     43 my $r = $args{reg} || do {
105 6     6   42 use Regexp::Grammars;
  6         10  
  6         45  
106 6     6   769 use Perl6::Pod::Grammars;
  6         10  
  6         383  
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       427 if ( $text =~ $r->with_actions( Perl6::Pod::Codeactions->new ) ) {
187 13         95 return $/{Text};
188             }
189             else {
190 0           return undef;
191             }
192              
193             }
194              
195             1;
196