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   58 use base 'Perl6::Pod::Lex::Block';
  6         12  
  6         1589  
9             our $VERSION = '0.01';
10              
11             package Perl6::Pod::Lex::Text;
12 6     6   34 use base 'Perl6::Pod::Lex::RawText';
  6         11  
  6         3119  
13             our $VERSION = '0.01';
14              
15             package Perl6::Pod::Lex;
16             our $VERSION = '0.01';
17 6     6   38 use strict;
  6         14  
  6         149  
18 6     6   30 use warnings;
  6         12  
  6         535  
19             sub new {
20 17     17 0 278495 my $class = shift;
21 17 50 33     169 my $self = bless( ( $#_ == 0 ) ? shift : {@_}, ref($class) || $class );
22 17         82 $self;
23             }
24 6     6   86 use v5.10;
  6         21  
25 6     6   30 use Perl6::Pod::Utl;
  6         11  
  6         4357  
26              
27             sub make_block {
28 53     53 0 70 my $self = shift;
29 53         217 my %ref = @_;
30             #save original bloack name
31 53         120 my $name = $ref{src_name} = $ref{name};
32             #for items1,2 and heads
33 53 50       147 if ($name =~ /(item|head)(\d+)/ ) {
34 0         0 $name = $ref{name} = $1;
35 0         0 $ref{level} = $2
36             }
37              
38 53   100     155 my $childs = $ref{content} || [];
39 53   100     195 my $vmargin = length( $ref{spaces} // '' );
40              
41             #is first para if item|defn ?
42 53         68 my $is_first = 1;
43              
44 53         106 foreach my $node (@$childs) {
45 56 100       260 if ( $node->{type} eq 'block' ) {
    100          
46 29         122 $node = $self->make_block(%$node);
47              
48             }
49             elsif ( $node->{type} =~ /text|raw/ ) {
50 20         37 my $type = $node->{type};
51 20 100       105 if ( $type eq 'text' ) {
52 15         112 $node = Perl6::Pod::Lex::Text->new(%$node);
53             }
54             else {
55 5         52 $node = Perl6::Pod::Lex::RawText->new(%$node);
56             }
57              
58             }
59             else {
60            
61 7         33 $node = $self->make_block(%$node);
62            
63             }
64             next
65 56 100 100     599 unless UNIVERSAL::isa( $node, 'Perl6::Pod::Lex::Text' )
66             || UNIVERSAL::isa( $node, 'Perl6::Pod::Lex::RawText' );
67 20         59 my $content = delete $node->{''};
68            
69             #remove virual margin
70 20         67 $content = Perl6::Pod::Utl::strip_vmargin( $vmargin, $content );
71              
72             #skip first text block for item| defn
73 20 100 66     85 if ( $name =~ 'item|defn' and $is_first ) {
74              
75             #always ordinary text
76 1         4 $content =~ s/^\s+//;
77 1         3 $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     628 || $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         73 $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       22 if ( $name eq 'para' ) {
99              
100             #para blocks always
101             # ordinary text
102 3         9 $content =~ s/^\s+//;
103             }
104 8         48 $node = $content;
105             }
106             }
107 53         266 return Perl6::Pod::Lex::Block->new(%ref);
108              
109             }
110              
111             sub process_file {
112 17     17 0 27 my $self = shift;
113 17         30 my $ref = shift;
114 17         36 $ref->{force_implicit_code_and_para_blocks} = 1;
115 17         81 my $block = $self->make_block( %$ref, name => 'File' );
116 17 100       75 unless ($self->{default_pod} ) {
117             # filter all implicit blocks
118 12         25 my @result = ();
119 12         18 foreach my $node (@ { $block->childs}) {
  12         43  
120 13   100     52 my $type = $node->{srctype} || 'UNKNOWN';
121 13 100       53 push @result, $node unless $type eq 'implicit';
122             }
123 12         39 $block->childs(\@result)
124             }
125 17         50 return $block->childs;
126             }
127              
128             sub make_tree {
129 17     17 0 30 my $self = shift;
130 17         25 my $tree = shift;
131 17         36 my $type = $tree->{type};
132 17         44 my $method = "process_" . $type;
133 17         65 return $self->$method($tree);
134             }
135             1;
136