File Coverage

blib/lib/Pod/Simple/XHTML/BlendedCode.pm
Criterion Covered Total %
statement 89 132 67.4
branch 10 26 38.4
condition 0 6 0.0
subroutine 18 21 85.7
pod 2 10 20.0
total 119 195 61.0


line stmt bran cond sub pod time code
1             package Pod::Simple::XHTML::BlendedCode;
2              
3 3     3   97526 use 5.008001;
  3         10  
  3         134  
4 3     3   14 use strict;
  3         4  
  3         88  
5 3     3   14 use warnings;
  3         3  
  3         124  
6 3     3   500 use parent 0.223 qw(Pod::Simple::XHTML);
  3         345  
  3         18  
7 3     3   103689 use Carp qw(croak);
  3         6  
  3         175  
8 3     3   12 use List::Util qw(first);
  3         5  
  3         251  
9 3     3   1634 use PPI::HTML 1.08 qw();
  3         307782  
  3         2915  
10              
11             our $VERSION = '2.001';
12              
13             sub new {
14 2     2 1 25     my $self = shift;
15 2         21     my $new = $self->SUPER::new(@_);
16 2         294     my $h = PPI::HTML->new();
17 2         54     $new->_accessorize(
18                     'internal_modules_hash', 'internal_url_postfix',
19                     'internal_url_prefix', 'internal_url_divide_slashes',
20                     'tab_to_spaces', '_highlighter',
21                     '_code_stash',
22                 );
23 2         115     $new->internal_url_divide_slashes(1);
24 2         24     $new->internal_modules_hash( {} );
25 2         13     $new->tab_to_spaces(4);
26 2         11     $new->_highlighter($h);
27 2         17     $new->_code_stash(q{});
28 2     244   25     $new->code_handler( sub { $_[2]->_code_handler( $_[0] ); } );
  244         7562  
29 2         13     return $new;
30             } ## end sub new
31              
32             sub _code_handler {
33 244     244   213     my $self = shift;
34 244         180     my $line = shift;
35              
36 244         350     $self->_code_stash( $self->_code_stash . "$line\n" );
37 244         1873     Pod::Simple::DEBUG() > 9 and print "Storing line $line\n";
38 244         322     return;
39             }
40              
41             sub start_Para {
42 35     35 0 15629     my $self = shift;
43              
44 35         77     my $code = $self->_code_stash;
45              
46 35 50       246     if ($code) {
47 0         0         Pod::Simple::DEBUG() > 4
48                       and print "# printing HTML-ized code at start-para event\n";
49 0         0         my $html = $self->_highlighter->html( \$code );
50 0         0         $self->_process_code($code);
51 0         0         $self->emit;
52                 }
53              
54 35         96     $self->SUPER::start_Para;
55 35         114     return;
56             } ## end sub start_Para
57              
58             sub start_Document {
59 2     2 0 276     my $self = shift;
60              
61 2         18     $self->SUPER::start_Document;
62              
63 2         297     my $code = $self->_code_stash;
64              
65 2 50       17     if ($code) {
66 2         2         Pod::Simple::DEBUG() > 4
67                       and print "# printing HTML-ized code at start-document event\n";
68 2         8         $self->_process_code($code);
69 2         11         $self->emit;
70                 }
71              
72 2         160     return;
73             } ## end sub start_Document
74              
75             sub start_head1 {
76 16     16 0 6841     my $self = shift;
77              
78 16         72     $self->SUPER::start_head1;
79              
80 16         78     my $code = $self->_code_stash;
81              
82 16 100       103     if ($code) {
83 1         1         Pod::Simple::DEBUG() > 4
84                       and print "# printing HTML-ized code at start-head1 event\n";
85 1         13         $self->_process_code($code);
86 1         3         $self->emit;
87                 }
88              
89 16         59     return;
90             } ## end sub start_head1
91              
92             sub start_head2 {
93 5     5 0 1947     my $self = shift;
94              
95 5         17     $self->SUPER::start_head2;
96              
97 5         24     my $code = $self->_code_stash;
98              
99 5 50       32     if ($code) {
100 0         0         Pod::Simple::DEBUG() > 4
101                       and print "# printing HTML-ized code at start-head2 event\n";
102 0         0         $self->_process_code($code);
103 0         0         $self->emit;
104                 }
105              
106 5         9     return;
107             } ## end sub start_head2
108              
109             sub start_head3 {
110 0     0 0 0     my $self = shift;
111              
112 0         0     $self->SUPER::start_head2;
113              
114 0         0     my $code = $self->_code_stash;
115              
116 0 0       0     if ($code) {
117 0         0         Pod::Simple::DEBUG() > 4
118                       and print "# printing HTML-ized code at start-head3 event\n";
119 0         0         $self->_process_code($code);
120 0         0         $self->emit;
121                 }
122              
123 0         0     return;
124             } ## end sub start_head3
125              
126             sub start_head4 {
127 0     0 0 0     my $self = shift;
128              
129 0         0     $self->SUPER::start_head2;
130              
131 0         0     my $code = $self->_code_stash;
132              
133 0 0       0     if ($code) {
134 0         0         Pod::Simple::DEBUG() > 4
135                       and print "# printing HTML-ized code at start-head4 event\n";
136 0         0         $self->_process_code($code);
137 0         0         $self->emit;
138                 }
139              
140 0         0     return;
141             } ## end sub start_head4
142              
143             sub start_for {
144 0     0 0 0     my $self = shift;
145              
146 0         0     $self->SUPER::start_head2;
147              
148 0         0     my $code = $self->_code_stash;
149              
150 0 0       0     if ($code) {
151 0         0         Pod::Simple::DEBUG() > 4
152                       and print "# printing HTML-ized code at start-for event\n";
153 0         0         $self->_process_code($code);
154 0         0         $self->emit;
155                 }
156              
157 0         0     return;
158             } ## end sub start_for
159              
160             sub end_Document {
161 2     2 0 179     my $self = shift;
162              
163 2         6     my $code = $self->_code_stash;
164              
165 2 100       15     if ($code) {
166 1         1         Pod::Simple::DEBUG() > 4
167                       and print "# printing HTML-ized code at end-document event\n";
168 1         3         $self->_process_code($code);
169                 }
170              
171 2         16     $self->SUPER::end_Document;
172 2         90     return;
173             } ## end sub end_Document
174              
175             sub _process_code {
176 4     4   5     my $self = shift;
177 4         7     my $code = shift;
178              
179 4         9     my $html = $self->_highlighter->html( \$code );
180 4         256454     $html =~ s{\n\n}{\n}msg;
181 4         32     $html =~ s{\t}{' ' x $self->tab_to_spaces}mesg;
  0         0  
182 4         176     $html =~ s{^(\s+)}{' ' x length($1)}mesg;
  141         327  
183              
184 4         48     $self->{'scratch'} .= $html;
185 4         17     $self->_code_stash(q{});
186 4         37     return;
187             } ## end sub _process_code
188              
189             sub resolve_pod_page_link {
190 11     11 1 839     my ( $self, $to, $section ) = @_;
191              
192 11 50       26     croak
193             q{The parser's internal_modules_hash method is not returning a hashref}
194                   if ( 'HASH' ne ref( $self->internal_modules_hash ) );
195              
196 11         64     my $key;
197 11 50       19     if ( defined $to ) {
198 9     9   109         $key = first { $to =~ m{\A$_\z}ms }
199 11         36         sort { $a cmp $b } keys %{ $self->internal_modules_hash };
  0         0  
  11         18  
200 11 50       166         return $self->SUPER::resolve_pod_page_link( $to, $section )
201                       if not defined $key;
202                 } else {
203 0                   return $self->SUPER::resolve_pod_page_link( $to, $section );
204                 }
205              
206 0               my $processed_to;
207              
208 0 0             if ( $self->internal_url_divide_slashes ) {
209 0                   $processed_to = $to;
210 0                   $processed_to =~ s{::}{/}msg;
211                 } else {
212 0                   $processed_to = encode_entities($to);
213                 }
214              
215 0 0             if ( defined $section ) {
216 0                   $section = q{#} . $self->idify( $section, 1 );
217                 } else {
218 0                   $section = q{};
219                 }
220              
221                 return
222 0   0               ( $self->internal_url_prefix || q{} )
      0        
      0        
223                   . ( $self->internal_modules_hash->{$key} || q{} )
224                   . $processed_to
225                   . $section
226                   . ( $self->internal_url_postfix || q{} );
227             } ## end sub resolve_pod_page_link
228              
229             1;                                     # Magic true value required at end of module
230              
231             __END__
232            
233             =pod
234            
235             =for stopwords XHTML formatter hashref mimimum perl CPAN perlartistic perlgpl MERCHANTABILITY LICENCE
236            
237             =begin readme text
238            
239             Pod::Simple::XHTML::BlendedCode version 2.000
240            
241             =end readme
242            
243             =for readme stop
244            
245             =head1 NAME
246            
247             Pod::Simple::XHTML::BlendedCode - Blends syntax-highlighted code and pod in one XHTML document.
248            
249             =head1 VERSION
250            
251             This document describes Pod::Simple::XHTML::BlendedCode version 2.000
252            
253             =begin readme
254            
255             =head1 INSTALLATION
256            
257             To install this module, run the following commands:
258            
259             perl Makefile.PL
260             make
261             make test
262             make install
263            
264             This method of installation will install a current version of Module::Build
265             if it is not already installed.
266            
267             Alternatively, to install with Module::Build, you can use the following commands:
268            
269             perl Build.PL
270             ./Build
271             ./Build test
272             ./Build install
273            
274             =end readme
275            
276             =for readme stop
277            
278             =head1 SYNOPSIS
279            
280             use Pod::Simple::XHTML::BlendedCode 2.000 qw();
281            
282             my $parser = Pod::Simple::XHTML::BlendedCode->new();
283            
284             # These routines are specific to Pod::Simple::XHTML::BlendedCode.
285             $parser->internal_modules_hash({
286             'Perl::Dist::WiX(.*)?' => 'Perl-Dist-WiX/', # Key can be a regex.
287             'Perl::Dist::VanillaWiX' => 'Perl-Dist-WiX/',
288             'File::List::Object' => 'File-List-Object/',
289             'Alien::WiX' => 'Alien-WiX/',
290             });
291             $parser->internal_url_postfix('.pm.html');
292             $parser->internal_url_prefix('http://csjewell.comyr.com/perl/');
293             $parser->internal_url_divide_slashes(1);
294            
295             # Since this is a subclass of Pod::Simple::XHTML,
296             # you can use all of its routines.
297             $parser->index(1);
298             $parser->html_css('code.css');
299             $parser->parse_file('Perl-Dist-WiX\\lib\\Perl\\Dist\\WiX.pm');
300            
301             =head1 DESCRIPTION
302            
303             This class is a formatter that takes Pod and Perl code and renders it as XHTML
304             validating HTML.
305            
306             This is a subclass of L<Pod::Simple::XHTML|Pod::Simple::XHTML> and inherits all
307             its methods.
308            
309             =head1 METHODS
310            
311             C<Pod::Simple::XHTML::BlendedCode> offers additional methods that modify
312             the format of the HTML output. Call these after creating the parser object,
313             but before the call to C<parse_file> or C<parse_string_document>:
314            
315             my $parser = Pod::Simple::XHTML::BlendedCode->new();
316             $parser->set_optional_param("value");
317             $parser->parse_file($file);
318            
319             =head2 internal_modules_hash
320            
321             This determines which modules are internal to your own web site.
322            
323             The module names in C<< LE<lt>E<gt> >> links are compared against the
324             regular expressions (wrapped in C<< \A >> and C<< \z >>) that are contained
325             in the keys. If no keys match, then normal link processing is used.
326            
327             If a key matches, then it is considered a "site-internal" link and the
328             value is appended to C<internal_url_prefix> for this link, and
329             C<internal_url_divide_slashes> and C<internal_url_postfix> are also used
330             when creating the link.
331            
332             If you are putting all modules in one path (so that there are no
333             per-distribution prefixes), set the values to the empty string.
334            
335             This defaults to an empty hashref, and a hashref must be passed in.
336            
337             =head2 internal_url_divide_slashes
338            
339             If this is set to a true value, then slashes are used to divide the portions
340             of a module name in the URL generated for an internal link.
341            
342             If not, then the module name is left as is.
343            
344             =head2 internal_url_prefix
345            
346             In turning an internal link to L<Foo::Bar|Foo::Bar> into
347             L<http://whatever/Foo%3a%3aBar> or L<http://whatever/Foo/Bar>, what to put
348             before the "Foo%3a%3aBar" or "Foo/Bar". This option is not set by default.
349            
350             =head2 perldoc_url_postfix
351            
352             What to put after "Foo%3a%3aBar" or "Foo/Bar" in the URL for an internal link.
353             This option is not set by default.
354            
355             =head2 tab_to_spaces
356            
357             How many spaces a tab is equivalent to. Defaults to 4.
358            
359             =head1 DIAGNOSTICS
360            
361             "The parser's internal_modules_hash method is not returning a hashref" will
362             be croaked upon processing of the first pod link when the
363             interal_modules_hash method was passed anything but a hashref previously.
364            
365             Also, this module will report any diagnostic
366             L<Pod::Simple::XHTML|Pod::Simple::XHTML> will, as well as any diagnostic
367             that L<Pod::Parser|Pod::Parser> will during the blending process.
368            
369             =head1 CONFIGURATION AND ENVIRONMENT
370            
371             Pod::Simple::XHTML::BlendedCode requires no configuration files or
372             environment variables.
373            
374             =for readme continue
375            
376             =head1 DEPENDENCIES
377            
378             Perl 5.8.1 is the mimimum version of perl that this module will run on.
379            
380             Other modules that this module depends on are
381             L<Pod::Simple::XHTML|Pod::Simple::XHTML>,
382             L<PPI::HTML|PPI::HTML> 1.08, and L<parent|parent> 0.223.
383            
384             =for readme stop
385            
386             =head1 INCOMPATIBILITIES
387            
388             None reported.
389            
390             =head1 BUGS AND LIMITATIONS (SUPPORT)
391            
392             No bugs have been reported.
393            
394             Bugs should be reported via:
395            
396             1) The CPAN bug tracker at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Pod-Simple-XHTML-BlendedCode>
397             if you have an account there.
398            
399             2) Email to E<lt>bug-Pod-Simple-XHTML-BlendedCode@rt.cpan.orgE<gt> if you do not.
400            
401             =head1 AUTHOR
402            
403             Curtis Jewell C<< <csjewell@cpan.org> >>
404            
405             =head1 SEE ALSO
406            
407             L<http://csjewell.comyr.com/perl/> (for examples of the output of this module.)
408            
409             =for readme continue
410            
411             =head1 LICENSE AND COPYRIGHT
412            
413             Copyright (c) 2010, 2014 Curtis Jewell C<< <csjewell@cpan.org> >>. All rights reserved.
414            
415             This module is free software; you can redistribute it and/or
416             modify it under the same terms as Perl itself, either version
417             5.8.1 or any later version. See L<perlartistic|perlartistic>
418             and L<perlgpl|perlgpl>.
419            
420             The full text of the license can be found in the
421             LICENSE file included with this module.
422            
423             =for readme stop
424            
425             =head1 DISCLAIMER OF WARRANTY
426            
427             BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
428             FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
429             OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
430             PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
431             EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
432             WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
433             ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
434             YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
435             NECESSARY SERVICING, REPAIR, OR CORRECTION.
436            
437             IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
438             WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
439             REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
440             LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
441             OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
442             THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
443             RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
444             FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
445             SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
446             SUCH DAMAGES.
447