File Coverage

blib/lib/Perl6/Pod/Lex.pm
Criterion Covered Total %
statement 68 70 97.1
branch 22 24 91.6
condition 15 18 83.3
subroutine 10 10 100.0
pod 0 4 0.0
total 115 126 91.2


line stmt bran cond sub pod time code
1             #===============================================================================
2             #
3             # DESCRIPTION: Make objectx from syntax tree
4             #
5             # AUTHOR: Aliaksandr P. Zahatski,
6             #===============================================================================
7             package Perl6::Pod::Lex::RawText;
8 6     6   34 use base 'Perl6::Pod::Lex::Block';
  6         15  
  6         1677  
9             our $VERSION = '0.01';
10              
11             package Perl6::Pod::Lex::Text;
12 6     6   32 use base 'Perl6::Pod::Lex::RawText';
  6         11  
  6         3261  
13             our $VERSION = '0.01';
14              
15             package Perl6::Pod::Lex;
16             our $VERSION = '0.01';
17 6     6   34 use strict;
  6         14  
  6         156  
18 6     6   31 use warnings;
  6         11  
  6         547  
19             sub new {
20 17     17 0 279859 my $class = shift;
21 17 50 33     176 my $self = bless( ( $#_ == 0 ) ? shift : {@_}, ref($class) || $class );
22 17         99 $self;
23             }
24 6     6   166 use v5.10;
  6         21  
25 6     6   31 use Perl6::Pod::Utl;
  6         13  
  6         4497  
26              
27             sub make_block {
28 53     53 0 81 my $self = shift;
29 53         226 my %ref = @_;
30             #save original bloack name
31 53         132 my $name = $ref{src_name} = $ref{name};
32             #for items1,2 and heads
33 53 50       148 if ($name =~ /(item|head)(\d+)/ ) {
34 0         0 $name = $ref{name} = $1;
35 0         0 $ref{level} = $2
36             }
37              
38 53   100     161 my $childs = $ref{content} || [];
39 53   100     208 my $vmargin = length( $ref{spaces} // '' );
40              
41             #is first para if item|defn ?
42 53         104 my $is_first = 1;
43              
44 53         113 foreach my $node (@$childs) {
45 56 100       273 if ( $node->{type} eq 'block' ) {
    100          
46 29         130 $node = $self->make_block(%$node);
47              
48             }
49             elsif ( $node->{type} =~ /text|raw/ ) {
50 20         36 my $type = $node->{type};
51 20 100       51 if ( $type eq 'text' ) {
52 15         120 $node = Perl6::Pod::Lex::Text->new(%$node);
53             }
54             else {
55 5         56 $node = Perl6::Pod::Lex::RawText->new(%$node);
56             }
57              
58             }
59             else {
60            
61 7         36 $node = $self->make_block(%$node);
62            
63             }
64             next
65 56 100 100     625 unless UNIVERSAL::isa( $node, 'Perl6::Pod::Lex::Text' )
66             || UNIVERSAL::isa( $node, 'Perl6::Pod::Lex::RawText' );
67 20         64 my $content = delete $node->{''};
68            
69             #remove virual margin
70 20         68 $content = Perl6::Pod::Utl::strip_vmargin( $vmargin, $content );
71              
72             #skip first text block for item| defn
73 20 100 66     128 if ( $name =~ 'item|defn' and $is_first ) {
74              
75             #always ordinary text
76 1         4 $content =~ s/^\s+//;
77 1         2 $node = $content;
78 1         9 $is_first=0;
79 1         3 next;
80              
81             }
82              
83             #convert paragraph's to blocks
84             my $is_implicit_code_and_para_blocks =
85             $ref{force_implicit_code_and_para_blocks}
86 19   100     662 || $name =~ /(pod|item|defn|nested|finish|\U $name\E )/x;
87 19 100       64 if ($is_implicit_code_and_para_blocks) {
88 11 100       38 my $block_name = $content =~ /^\s+/ ? 'code' : 'para';
89 11         75 $node = Perl6::Pod::Lex::Block->new(
90             %$node,
91             name => $block_name,
92             srctype => 'implicit',
93             content => [$content]
94             );
95              
96             }
97             else {
98 8 100       28 if ( $name eq 'para' ) {
99              
100             #para blocks always
101             # ordinary text
102 3         8 $content =~ s/^\s+//;
103             }
104 8         55 $node = $content;
105             }
106             }
107 53         273 return Perl6::Pod::Lex::Block->new(%ref);
108              
109             }
110              
111             sub process_file {
112 17     17 0 27 my $self = shift;
113 17         31 my $ref = shift;
114 17         42 $ref->{force_implicit_code_and_para_blocks} = 1;
115 17         92 my $block = $self->make_block( %$ref, name => 'File' );
116 17 100       77 unless ($self->{default_pod} ) {
117             # filter all implicit blocks
118 12         25 my @result = ();
119 12         17 foreach my $node (@ { $block->childs}) {
  12         43  
120 13   100     52 my $type = $node->{srctype} || 'UNKNOWN';
121 13 100       51 push @result, $node unless $type eq 'implicit';
122             }
123 12         37 $block->childs(\@result)
124             }
125 17         54 return $block->childs;
126             }
127              
128             sub make_tree {
129 17     17 0 36 my $self = shift;
130 17         38 my $tree = shift;
131 17         37 my $type = $tree->{type};
132 17         45 my $method = "process_" . $type;
133 17         76 return $self->$method($tree);
134             }
135             1;
136