File Coverage

blib/lib/Pod/Example.pm
Criterion Covered Total %
statement 82 82 100.0
branch 24 24 100.0
condition 3 3 100.0
subroutine 14 14 100.0
pod 2 2 100.0
total 125 125 100.0


line stmt bran cond sub pod time code
1             package Pod::Example;
2              
3 4     4   230282 use base qw(Exporter);
  4         35  
  4         511  
4 4     4   29 use strict;
  4         7  
  4         85  
5 4     4   18 use warnings;
  4         8  
  4         138  
6              
7 4     4   1008 use Error::Pure qw(err);
  4         24119  
  4         134  
8 4     4   2064 use Pod::Abstract;
  4         114287  
  4         152  
9 4     4   33 use Pod::Find qw(pod_where);
  4         11  
  4         264  
10 4     4   33 use Readonly;
  4         9  
  4         3579  
11              
12             # Constants.
13             Readonly::Array our @EXPORT_OK => qw(get sections);
14             Readonly::Scalar my $EMPTY_STR => q{};
15              
16             our $VERSION = 0.11;
17              
18             # Get content for file or module.
19             sub get {
20 15     15 1 12437 my ($file_or_module, $section, $number_of_example) = @_;
21              
22             # Get Pod::Abstract object.
23 15         50 my $pod_abstract = _pod_abstract($file_or_module);
24              
25             # Get section pod.
26 14         63653 my ($code) = _get_content($pod_abstract, $section, $number_of_example);
27              
28 14         240 return $code;
29             }
30              
31             # Get example sections.
32             sub sections {
33 2     2 1 962 my ($file_or_module, $section) = @_;
34              
35             # Get Pod::Abstract object.
36 2         7 my $pod_abstract = _pod_abstract($file_or_module);
37              
38             # Get first section.
39 2         8551 my @pod_sections = _get_sections($pod_abstract, $section);
40              
41             # Get section names.
42 2         1498 my @sections = map { _get_section_name($_) } @pod_sections;
  2         6  
43              
44 2         32 return @sections;
45             }
46              
47             # Get content in Pod::Abstract object.
48             sub _get_content {
49 14     14   32 my ($pod_abstract, $section, $number_of_example) = @_;
50              
51             # Get first section.
52 14         42 my ($pod_section) = _get_sections($pod_abstract, $section,
53             $number_of_example);
54              
55             # No section.
56 14 100       10635 if (! defined $pod_section) {
57 1         3 return;
58             }
59              
60             # Remove #cut.
61 13         32 my @cut = $pod_section->select("//#cut");
62 13         3472 foreach my $cut (@cut) {
63 10         23 $cut->detach;
64             }
65              
66             # Get pod.
67 13         735 my $child_pod = $EMPTY_STR;
68 13         27 foreach my $child ($pod_section->children) {
69 16 100       321 if ($child->type eq 'begin') {
70              
71             # =begin text as commented text.
72 2 100       16 if ($child->body =~ m/^text/ms) {
73             $child_pod .= join "\n",
74 1         13 map { ' #'.$_ }
  6         73  
75             split m/\n/ms,
76             ($child->children)[0]->pod;
77              
78             # Skip =begin html and other unsupported sections.
79             } else {
80 1         12 next;
81             }
82             } else {
83 14         98 $child_pod .= $child->pod;
84             }
85             }
86              
87             # Remove spaces and return.
88 13         645 return _remove_spaces($child_pod);
89             }
90              
91             # Get section name.
92             # XXX Hack to structure.
93             sub _get_section_name {
94 2     2   3 my $pod_abstract_node = shift;
95             return $pod_abstract_node->{'params'}->{'heading'}->{'tree'}
96 2         8 ->{'nodes'}->[0]->{'body'};
97             }
98              
99             # Get sections.
100             sub _get_sections {
101 16     16   48 my ($pod_abstract, $section, $number_of_example) = @_;
102              
103             # Default section.
104 16 100       65 if (! $section) {
105 11         21 $section = 'EXAMPLE';
106             }
107              
108             # Concerete number of example.
109 16 100       62 if ($number_of_example) {
110 2         5 $section .= $number_of_example;
111              
112             # Number of example as potential number.
113             } else {
114 14         28 $section .= '\d*';
115             }
116              
117             # Get and return sections.
118 16         65 return $pod_abstract->select('/head1[@heading =~ {'.$section.'}]');
119             }
120              
121             # Get pod abstract for module.
122             sub _pod_abstract {
123 17     17   28 my $file_or_module = shift;
124              
125             # Module file.
126 17         25 my $file;
127 17 100       396 if (-r $file_or_module) {
128 13         38 $file = $file_or_module;
129              
130             # Module.
131             } else {
132 4         2338 $file = pod_where({ -inc => 1 }, $file_or_module);
133 4 100       30 if (! $file) {
134 1         10 err 'Cannot open pod file or Perl module.';
135             }
136             }
137              
138             # Get and return pod.
139 16         115 return Pod::Abstract->load_file($file);
140             }
141              
142             # Remove spaces from example.
143             sub _remove_spaces {
144 13     13   22 my $string = shift;
145 13         52 my @lines = split /\n/, $string;
146              
147             # Get number of spaces in begin.
148 13         21 my $max = 0;
149 13         21 foreach my $line (@lines) {
150 72 100       132 if (! length $line) {
151 14         22 next;
152             }
153 58         142 $line =~ m/^(\ +)/ms;
154 58         89 my $spaces = $EMPTY_STR;
155 58 100       119 if ($1) {
156 54         72 $spaces = $1;
157             }
158 58 100 100     185 if ($max == 0 || length $spaces < $max) {
159 17         26 $max = length $spaces;
160             }
161             }
162              
163             # Remove spaces.
164 13 100       30 if ($max > 0) {
165 12         22 foreach my $line (@lines) {
166 67 100       104 if (! length $line) {
167 13         18 next;
168             }
169 54         88 $line = substr $line, $max;
170             }
171             }
172              
173             # Return string.
174 13         82 return join "\n", @lines;
175             }
176              
177             1;
178              
179              
180             __END__