lib/Pod/PP.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« lib/Pod/PP/State/Info.pm ↑ Summary
1 #
2 # $Id: PP.pm,v 0.1.1.2 2001/12/01 14:03:47 ram Exp $
3 #
4 # Copyright (c) 2000-2001, Raphael Manfredi
5 #
6 # You may redistribute only under the terms of the Artistic License,
7 # as specified in the README file that comes with the distribution.
8 #
9 # HISTORY
10 # $Log: PP.pm,v $
11 # Revision 0.1.1.2 2001/12/01 14:03:47 ram
12 # patch2: noted that this software is now unmaintained
13 #
14 # Revision 0.1.1.1 2001/04/25 12:18:11 ram
15 # patch1: forgot to expand copyright notice in files
16 # patch1: updated version number
17 #
18 # Revision 0.1 2001/04/25 10:41:48 ram
19 # Baseline for first Alpha release.
20 #
21 # $EndLog$
22 #
23  
24 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 package Pod::PP;
27  
28 require Pod::Parser;
29 10 ▶use vars qw(@ISA $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 @ISA = qw(Pod::Parser);
31  
32 $VERSION = '0.102';
33  
34 0 ▶use Carp::Datum;
Subroutine BEGIN: called CC=1 SCAR=2.6
35 use Log::Agent;
36 use Getargs::Long;
37 use File::Basename;
38  
39 require Pod::PP::Env;
40 require Pod::PP::State;
41  
42 use Pod::PP::State::Info;
43  
44 my $MAX_EXPANSION = 100; # Maximum levels of recursive P<> expansion
45  
46 #
47 # ->make
48 #
49 # Creation routine
50 #
51 sub make {
52 DFEATURE my $f_;
53 my $pkg = shift;
54 my ($incpath, $symbols) = getargs(@_,
55 qw(incpath=ARRAY symbols=HASH));
56  
57 my $self = bless Pod::Parser->new(), $pkg;
58 $self->{file_env} = Pod::PP::Env->make($incpath, $symbols);
59  
60 $self->init_parser();
61  
62 #
63 # Don't initialize env, it will be done in parse_from_*() routines.
64 #
65  
66 return DVAL $self;
67 }
68  
69 #
70 # Attribute access
71 #
72  
73 sub env { $_[0]->{env} }
74 sub ifstate { $_[0]->{ifstate} }
75 sub file_env { $_[0]->{file_env} }
76 sub exp_level { $_[0]->{exp_level} }
77 sub exp_symbol { $_[0]->{exp_symbol} }
78 sub exp_podinfo { $_[0]->{exp_podinfo} }
79 sub begin_start { $_[0]->{begin_start} }
80 sub begin_data { $_[0]->{begin_data} }
81  
82 ###
83 ### Pod::PP interface
84 ###
85  
86 #
87 # ->parse_from_filehandle -- redefined
88 #
89 # Intitial pre-processing.
90 # Resets the environment at the start of a new file.
91 #
92 sub parse_from_filehandle {
93 DFEATURE my $f_;
94 my $self = shift;
95  
96 #
97 # This feature may be called either directly, or via
98 # SUPER::parse_from_file(), which call parse_from_filehandle()
99 # on the opened file.
100 #
101 # Therefore, we must lazily initialize the environment, because
102 # we could come from subparse_from_file(), where the environment is
103 # already there.
104 #
105  
106 $self->init_env() unless $self->env;
107 $self->SUPER::parse_from_filehandle(@_);
108  
109 return DVOID;
110 }
111  
112 #
113 # ->subparse_from_file
114 #
115 # This is used to pre-process another file using the same environment,
116 # so that any define/undef in there impacts us also. Typically for
117 # "include" statements.
118 #
119 sub subparse_from_file {
120 DFEATURE my $f_;
121 my $self = shift;
122 my ($file) = @_;
123  
124 my $clone = bless Pod::Parser->new(), ref $self;
125 $clone->init_parser();
126 $clone->{env} = $self->env; # Share environment object
127 $clone->SUPER::parse_from_file($file); # Hence we don't want init_env()!
128  
129 return DVOID;
130 }
131  
132 ###
133 ### Pod::Parser callbacks
134 ###
135  
136 #
137 # ->begin_input
138 #
139 # Pod::Parser callback at the beginning of each file.
140 #
141 sub begin_pod {
142 DFEATURE my $f_;
143 my $self = shift;
144  
145 #
146 # We can't recurse via an "=for pp include" whilst we're in a begin.
147 # Assert that.
148 #
149  
150 DASSERT !$self->begin_start;
151 DASSERT $self->begin_data eq '';
152  
153 return DVOID;
154 }
155  
156 #
157 # ->end_input
158 #
159 # Pod::Parser callback at the end of each file.
160 #
161 sub end_input {
162 DFEATURE my $f_;
163 my $self = shift;
164  
165 #
166 # Reset the if/elif/else/endif stack. Will warn on pending "if".
167 #
168  
169 $self->ifstate->reset();
170  
171 #
172 # Make sure that we're not in the middle of an "=begin pp" directive...
173 # If we are, warn them and discard collected data.
174 #
175  
176 my $bstart = $self->begin_start;
177 return DVOID unless $bstart;
178  
179 my ($which, $podinfo) = @{$bstart};
180 my ($file, $line) = $podinfo->file_line();
181  
182 logwarn "no =end for \"=begin pp $which\" started at \"%s\", line %d",
183 $file, $line;
184  
185 #
186 # Discard, discard!
187 #
188  
189 $self->{begin_start} = undef; # Start of last =begin pp
190 $self->{begin_data} = ''; # Stuff seen in begin block
191  
192 return DVOID;
193 }
194  
195 #
196 # Flow control commands -- must always be interpreted, whatever the state
197 #
198 my %PP_flow = (
199 'ifdef' => 'pp_ifdef',
200 'ifndef' => 'pp_ifndef',
201 'if' => 'pp_if',
202 'elif' => 'pp_elif',
203 'else' => 'pp_else',
204 'endif' => 'pp_endif',
205 );
206  
207 #
208 # Symbol manipulation commands -- don't go through interpolate()
209 #
210 my %PP_sym = (
211 %PP_flow,
212 'define' => 'pp_define',
213 'undef' => 'pp_undef',
214 );
215  
216 #
217 # Supported Pod::PP directives:
218 #
219 my %PP = (
220 %PP_sym,
221 'require' => 'pp_require',
222 'include' => 'pp_include',
223 'image' => 'pp_image',
224 'comment' => 'pp_comment',
225 );
226  
227 #
228 # ->command
229 #
230 # Pod::Parser callback on "=" or "==" commands.
231 #
232 # We're only interested in "for/begin/end pp" and "pp" commands, and leave
233 # the others intact.
234 #
235 sub command {
236 DFEATURE my $f_;
237 my $self = shift;
238 my ($command, $paragraph, $line, $podinfo) = @_;
239  
240 my $ok = $self->ifstate->state == POD_PP_STATE_OK;
241  
242 #
243 # Buffer input if we're within a begin
244 #
245  
246 my $begin = $self->begin_start;
247 if ($begin) {
248 my $pp = '';
249 $pp = $1 if $paragraph =~ s/^(pp\s+)//;
250 (my $now = $paragraph) =~ s/(?:\r?\n)?\r?\n$//s;
251  
252 if ($command eq 'end' && length $pp) {
253  
254 #
255 # Finishing already started command?
256 #
257  
258 my ($which, $podinfo, $ppcmd, $options) = @$begin;
259  
260 if ($which eq $now) {
261 #
262 # We found the end of your "=begin pp" command.
263 #
264 # Fine, but execute it only if we're in the OK state
265 # or if it's a control flow command (if/else/endif).
266 #
267  
268 $self->pp($ppcmd, $options, $self->begin_data, $podinfo)
269 if $ok || exists $PP_flow{$ppcmd};
270  
271 $self->{begin_start} = undef;
272 $self->{begin_data} = '';
273  
274 return DVOID;
275 }
276  
277 # Fall through
278 }
279  
280 #
281 # Still within begin... buffer data.
282 #
283  
284 $self->{begin_data} .= "=$command $pp$now\n\n";
285  
286 return DVOID;
287 }
288  
289 if (
290 $command =~ /^pp$/ ||
291 ($command =~ /^(?:for|begin)$/ && $paragraph =~ s/^pp\s+//)
292 ) {
293  
294 #
295 # A "=begin pp" is deferred up to the point where we reach the
296 # matching =end.
297 #
298  
299 if ($command eq 'begin') {
300 DASSERT !$self->begin_start, "nested =begin are not processed";
301  
302 (my $now = $paragraph) =~ s/(?:\r?\n)?\r?\n$//s;
303 $begin = $self->{begin_start} = [$now, $podinfo];
304 }
305  
306 #
307 # Proper format is "=for pp command" or "=pp command"
308 #
309 # An interpolation pass is done on the remaining of the line to
310 # expand interior sequences, unless command is flagged for no
311 # interpolation (symbol manipulation commands).
312 #
313 # Each command may be followed by options, held within <> and
314 # immediately following the command name, as in:
315 #
316 # =pp image <center> "file.png"
317 #
318 # The options are defined on a command basis. Various options are
319 # separated via spaces, just like within SGML tags.
320 #
321  
322 if ($paragraph =~ s/^(\w+)\s*//) {
323 my $ppcmd = $1;
324 $paragraph = $self->interpolate($paragraph, $line)
325 unless exists $PP_sym{$ppcmd};
326  
327 my $options = '';
328 $options = $1 if $paragraph =~ s/^<(.*?)>\s*//;
329  
330 #
331 # An =begin is not executed right now...
332 # The command parameters will be what follows till the =end.
333 #
334  
335 if ($begin) {
336 my ($file, $line) = $podinfo->file_line;
337 $paragraph =~ s/(?:\r?\n)+$//s;
338 $self->{begin_data} = $paragraph . " ";
339 push @$begin, ($ppcmd, $options);
340 return DVOID;
341 }
342  
343 #
344 # Commands are only executed if we're in the OK state, or when
345 # it's a control flow one (if/else/endif).
346 #
347  
348 $self->pp($ppcmd, $options, $paragraph, $podinfo)
349 if $ok || exists $PP_flow{$ppcmd};
350  
351 return DVOID;
352 }
353  
354 my ($file, $line) = $podinfo->file_line;
355 logwarn "ignoring badly formed Pod::PP directive at \"%s\", line %d",
356 $file, $line;
357  
358 # Fall through...
359 }
360  
361 return DVOID unless $ok;
362
363 #
364 # Since we pre-process, normalize output with a single "="
365 # and add trailing "\n" if necessary (command started with "==")
366 #
367  
368 my $out = $self->output_handle();
369 print $out "=$command";
370 print $out length $paragraph ?
371 (" " . $self->interpolate($paragraph, $line)) : "\n";
372 print $out "\n" unless $paragraph =~ /\r?\n\r?\n/s;
373  
374 return DVOID;
375 }
376  
377 #
378 # ->interior_sequence
379 #
380 # Pod::Parser callback on [A-Z]<> commands
381 # We're only interested in processing P<> sequences and leave the others as-is.
382 #
383 sub interior_sequence {
384 DFEATURE my $f_;
385 my $self = shift;
386 my ($seq_command, $seq_argument, $podinfo) = @_;
387  
388 return DVOID unless $self->ifstate->state == POD_PP_STATE_OK;
389  
390 return DVAL $podinfo->raw_text() unless $seq_command eq 'P';
391 return DVAL $self->recursive_expansion($seq_argument, $podinfo);
392 }
393  
394 #
395 # ->verbatim
396 #
397 # Pod::Parser callback on verbatim paragraph
398 # Stuff if in begin_data if we're within a begin.
399 #
400 sub verbatim {
401 DFEATURE my $f_;
402 my $self = shift;
403 my ($paragraph, $line, $podinfo) = @_;
404  
405 return DVOID unless $self->ifstate->state == POD_PP_STATE_OK;
406  
407 if ($self->begin_start) {
408 $self->{begin_data} .= $paragraph;
409 } else {
410 my $out = $self->output_handle();
411 print $out $paragraph;
412 }
413  
414 return DVOID;
415 }
416  
417 #
418 # ->textblock
419 #
420 # Pod::Parser callback on text block
421 # Stuff if in begin_data if we're within a begin.
422 #
423 sub textblock {
424 DFEATURE my $f_;
425 my $self = shift;
426 my ($paragraph, $line, $podinfo) = @_;
427  
428 return DVOID unless $self->ifstate->state == POD_PP_STATE_OK;
429  
430 if ($self->begin_start) {
431 $self->{begin_data} .= $paragraph;
432 } else {
433 my $out = $self->output_handle();
434 print $out $self->interpolate($paragraph, $line);
435 }
436  
437 return DVOID;
438 }
439  
440  
441 ###
442 ### Pre-processing callbacks
443 ###
444  
445 #
446 # ->pp
447 #
448 # A Pod pre-processor command.
449 # Dispatch to relevant routine.
450 #
451 sub pp {
452 DFEATURE my $f_;
453 my $self = shift;
454 my ($command, $options, $paragraph, $podinfo) = @_;
455  
456 DREQUIRE defined $self->{env},
457 "has called preprocess_from_*() to start with on $self";
458  
459 my $routine = $PP{$command};
460 my $what;
461 my $error;
462 chomp $paragraph; # Strip trailing end-paragraph marker
463  
464 #
465 # Each processing routine is given the following arguments:
466 #
467 # $opts a list ref, containing all the options
468 # $paragraph the text containg the command arguments
469 # $podinfo the Pod::Parser location object
470 #
471 # Routines are expected to return an empty string if OK, or an error
472 # message if something goes wrong.
473 #
474  
475 if (defined $routine) {
476 my @opts = split(/\s+/, $options);
477 $error = $self->$routine(\@opts, $paragraph, $podinfo);
478 return DVOID unless $error;
479 $what = "failed: $error";
480 } else {
481 $what = "ignored";
482 }
483  
484 #
485 # Flag unknown/failed command, and let it go through as-is.
486 #
487  
488 my ($file, $line) = $podinfo->file_line;
489 if (defined $error) {
490 logerr "error in Pod::PP directive '%s' at \"%s\", line %d: %s",
491 $command, $file, $line, $error;
492 } else {
493 logwarn "unknown Pod::PP directive '%s' at \"%s\", line %d",
494 $command, $file, $line;
495 }
496  
497 my $out = $self->output_handle();
498 my $cmd = $podinfo->cmd_name();
499 $paragraph =~ s/^/\t/gm; # Indent whole paragrah
500 $paragraph =~ s/^\t//s; # But remove initial tab
501  
502 printf $out "=for pp comment (at \"%s\", line %d):\n", $file, $line;
503 printf $out "\tFollowing \"=$cmd\" directive $what\n";
504 printf $out "\t=%s $command", $cmd eq 'pp' ? $cmd : "$cmd pp";
505 print $out " <$options>" if $options ne '';
506 print $out length $paragraph ? " $paragraph" : "\n";
507 print $out "\n" unless $paragraph =~ /\r?\n\r?\n$/s;
508  
509 return DVOID;
510 }
511  
512 #
513 # ->pp_comment
514 #
515 # Process a "comment" directive.
516 #
517 sub pp_comment {
518 DFEATURE my $f_;
519 my $self = shift;
520 my ($opts, $paragraph, $podinfo) = @_;
521  
522 return DVAL ''; # Comment ignored
523 }
524  
525 #
526 # ->pp_include
527 #
528 # Process an "include" directive.
529 #
530 sub pp_include {
531 DFEATURE my $f_;
532 my $self = shift;
533 my ($opts, $paragraph, $podinfo) = @_;
534  
535 my ($file, $line) = $podinfo->file_line;
536 return DVAL "no filename found" unless $paragraph =~ /^"(.*)"/;
537  
538 my $incfile = $self->env->lookup_file($file, $1);
539 return DVAL "cannot find \"$1\"" unless $incfile;
540  
541 $self->subparse_from_file($incfile);
542  
543 return DVAL ''; # OK
544 }
545  
546 #
547 # ->pp_require
548 #
549 # Process a "require" directive.
550 # Like "include", but done once per file.
551 #
552 sub pp_require {
553 DFEATURE my $f_;
554 my $self = shift;
555 my ($opts, $paragraph, $podinfo) = @_;
556  
557 my ($file, $line) = $podinfo->file_line;
558 return DVAL "no filename found" unless $paragraph =~ /^"(.*)"/;
559  
560 my $env = $self->env;
561 my $incfile = $env->lookup_file($file, $1);
562 return DVAL "cannot find \"$1\"" unless $incfile;
563  
564 return DVAL '' if $env->is_parsed($incfile);
565 $env->set_is_parsed($incfile);
566  
567 $self->subparse_from_file($incfile);
568  
569 return DVAL ''; # OK
570 }
571  
572 #
573 # ->pp_define
574 #
575 # Process a "define" directive.
576 #
577 sub pp_define {
578 DFEATURE my $f_;
579 my $self = shift;
580 my ($opts, $paragraph, $podinfo) = @_;
581  
582 return DVAL "no symbol found" unless $paragraph =~ s/^(\w+)\s*//;
583  
584 my $symbol = $1;
585 my $env = $self->env;
586  
587 if ($env->is_defined($symbol)) {
588 my ($file, $line) = $podinfo->file_line;
589 logwarn "redefining Pod::PP symbol '%s' at \"%s\", line %d",
590 $symbol, $file, $line;
591 }
592  
593 chomp $paragraph;
594 $env->define($symbol, $paragraph);
595  
596 return DVAL ''; # OK
597 }
598  
599 #
600 # ->pp_undef
601 #
602 # Process an "undef" directive.
603 #
604 sub pp_undef {
605 DFEATURE my $f_;
606 my $self = shift;
607 my ($opts, $paragraph, $podinfo) = @_;
608  
609 return DVAL "no symbol found" unless $paragraph =~ /^(\w+)\s*/;
610  
611 $self->env->undefine($1);
612 return DVAL ''; # OK
613 }
614  
615 #
616 # ->pp_image
617 #
618 # Process an "image" directive.
619 #
620 sub pp_image {
621 DFEATURE my $f_;
622 my $self = shift;
623 my ($opts, $paragraph, $podinfo) = @_;
624  
625 my ($file, $line) = $podinfo->file_line;
626 return DVAL "no filename found" unless $paragraph =~ /^"(.*)"/;
627  
628 my $src = $1;
629 my $alt = basename $src;
630 $alt =~ s/\.\w+$//;
631  
632 my $align = "";
633 $align = ' ALIGN="right"' if grep($_ eq "right", @$opts); # XXX use hash
634 $align = ' ALIGN="center"' if grep($_ eq "center", @$opts); # XXX use hash
635  
636 my $out = $self->output_handle();
637 my $str = <<EOS;
638 :=for html <P$align><IMG SRC="$src" ALT="$alt"></P>
639  
640 :=begin text
641  
642 [image "$src" not rendered]
643  
644 :=end text
645  
646 EOS
647 $str =~ s/^://gm; # POD directives protected for trivial parsers
648 print $out $str;
649  
650 return DVAL '';
651 }
652  
653 ###
654 ### State machine for if/elif/else/endif
655 ###
656  
657 #
658 # _pp_test_symbol
659 #
660 # Handle "if(n)def SYMBOL" condition.
661 #
662 sub _pp_test_symbol {
663 DFEATURE my $f_;
664 my $self = shift;
665 my ($def, $paragraph, $podinfo) = @_;
666  
667 my $ifstate = $self->ifstate;
668 my $cmd = $def ? "ifdef" : "ifndef";
669  
670 if ($ifstate->state != POD_PP_STATE_OK) {
671 # Skip till matching "endif"
672 $ifstate->push($cmd, POD_PP_STATE_ENDIF, $podinfo);
673 return DVOID;
674 }
675  
676 unless ($paragraph =~ s/^(\w+)\s*//) {
677 my ($file, $line) = $podinfo->file_line();
678 logwarn "no Pod::PP symbol found for 'if%sdef' at \"%s\", line %d",
679 $def ? "" : "n", $file, $line;
680 # Skip till matching "else"/"endif"
681 $ifstate->push($cmd, POD_PP_STATE_ALT, $podinfo);
682 return DVOID;
683 }
684  
685 my $symbol = $1;
686 my $test = $self->env->is_defined($symbol);
687 $test = !$test unless $def;
688 $ifstate->push($cmd, $test ? POD_PP_STATE_OK : POD_PP_STATE_ALT, $podinfo);
689  
690 return DVOID;
691 }
692  
693 #
694 # pp_ifdef
695 #
696 # Handle "ifdef SYMBOL" condition.
697 #
698 sub pp_ifdef {
699 DFEATURE my $f_;
700 my $self = shift;
701 my ($opts, $paragraph, $podinfo) = @_;
702  
703 return DVAL $self->_pp_test_symbol(1, $paragraph, $podinfo);
704 }
705  
706 #
707 # pp_ifndef
708 #
709 # Handle "ifndef SYMBOL" condition.
710 #
711 sub pp_ifndef {
712 DFEATURE my $f_;
713 my $self = shift;
714 my ($opts, $paragraph, $podinfo) = @_;
715  
716 return DVAL $self->_pp_test_symbol(0, $paragraph, $podinfo);
717 }
718  
719 #
720 # pp_if
721 #
722 # Handle "if expr" condition
723 #
724 sub pp_if {
725 DFEATURE my $f_;
726 my $self = shift;
727 my ($opts, $paragraph, $podinfo) = @_;
728  
729 my $ifstate = $self->ifstate;
730 if ($ifstate->state != POD_PP_STATE_OK) {
731 # Skip till matching "endif"
732 $ifstate->push(POD_PP_STATE_ENDIF, $podinfo);
733 return DVOID;
734 }
735  
736 my $test = $self->env->evaluate($paragraph, $podinfo);
737 $ifstate->push("if",
738 $test ? POD_PP_STATE_OK : POD_PP_STATE_ALT, $podinfo);
739  
740 return DVOID;
741 }
742  
743 #
744 # pp_elif
745 #
746 # Handle "elif expr" condition
747 #
748 sub pp_elif {
749 DFEATURE my $f_;
750 my $self = shift;
751 my ($opts, $paragraph, $podinfo) = @_;
752  
753 my $ifstate = $self->ifstate;
754 my $state = $ifstate->state;
755  
756 if (
757 $state == POD_PP_STATE_OK || # We kept last "if"/"elif"
758 $state == POD_PP_STATE_ENDIF # Skip anyway
759 ) {
760 $ifstate->replace(POD_PP_STATE_ENDIF, $podinfo);
761 return DVOID;
762 }
763 my $test = $self->env->evaluate($paragraph, $podinfo);
764 $ifstate->replace("elif",
765 $test ? POD_PP_STATE_OK : POD_PP_STATE_ALT, $podinfo);
766  
767 return DVOID;
768 }
769  
770 #
771 # pp_else
772 #
773 # Handle "else" instruction
774 #
775 sub pp_else {
776 DFEATURE my $f_;
777 my $self = shift;
778 my ($opts, $paragraph, $podinfo) = @_;
779  
780 my $ifstate = $self->ifstate;
781 my $test = $ifstate->state == POD_PP_STATE_ALT;
782 $ifstate->replace("else",
783 $test ? POD_PP_STATE_OK : POD_PP_STATE_ENDIF, $podinfo);
784 }
785  
786 #
787 # pp_endif
788 #
789 # Handle "endif" instruction
790 #
791 sub pp_endif {
792 DFEATURE my $f_;
793 my $self = shift;
794 my ($opts, $paragraph, $podinfo) = @_;
795  
796 my $ifstate = $self->ifstate;
797 $ifstate->pop($podinfo);
798 }
799  
800 ###
801 ### Internal calls
802 ###
803  
804 #
805 # init_parser
806 #
807 # Initialize per-parser data.
808 #
809 sub init_parser {
810 DFEATURE my $f_;
811 my $self = shift;
812  
813 $self->{begin_start} = undef; # Start of last =begin pp
814 $self->{begin_data} = ''; # Stuff seen in begin block
815  
816 $self->{ifstate} = Pod::PP::State->make();
817  
818 return DVOID;
819 }
820  
821 #
822 # ->init_env
823 #
824 # Initialize pre-processing environment.
825 #
826 sub init_env {
827 DFEATURE my $f_;
828 my $self = shift;
829  
830 $self->{env} = $self->file_env->clone();
831 $self->{exp_level} = 0;
832  
833 return DVOID;
834 }
835  
836 #
837 # ->recursive_expansion
838 #
839 # Expand `sym' as a symbol then call interpolate on the result.
840 # We limit the maximum recursion to $MAX_EXPANSION to be able to flag loops.
841 #
842 sub recursive_expansion {
843 DFEATURE my $f_;
844 my $self = shift;
845 my ($sym, $podinfo) = @_;
846  
847 my $env = $self->env;
848 my $level = $self->{exp_level};
849 unless ($level) {
850 $self->{exp_symbol} = $sym;
851 $self->{exp_podinfo} = $podinfo;
852 }
853 my ($file, $line) = $self->exp_podinfo->file_line();
854  
855 unless ($env->is_defined($sym)) {
856 logwarn "using undefined Pod::PP symbol '%s' at \"%s\", line %d",
857 $sym, $file, $line;
858 return DVAL '';
859 }
860  
861 my $text = $env->symbol_value($sym);
862  
863 if ($level > $MAX_EXPANSION) {
864 logwarn "endless expansion for Pod::PP symbol '%s' at \"%s\", line %d",
865 $self->exp_symbol, $file, $line;
866 return DVAL $text;
867 }
868  
869 $self->{exp_level}++;
870 $text = $self->interpolate($text, $line);
871 $self->{exp_level}--;
872  
873 return DVAL $text;
874 }
875  
876 1;
877  
878 =head1 NAME
879  
880 Pod::PP - POD pre-processor
881  
882 =head1 SYNOPSIS
883  
884 # normally used via the podpp script
885  
886 require Pod::PP;
887  
888 my $pp = Pod::PP->make(
889 -incpath => ['h', 'h/sys'],
890 -symbols => { DIR => "/var/www", TMPDIR => "/var/tmp" },
891 );
892  
893 $pp->parse_from_filehandle(\*STDIN);
894 $pp->parse_from_file("file.pp");
895  
896 =head1 DESCRIPTION
897  
898 The C<Pod::PP> module is a POD pre-processor built on top of C<Pod::Parser>.
899 The helper script B<podpp> provides a pre-processor command for POD,
900 whose interface is very much like B<cpp>, the C pre-processor.
901 However, unlike C, the C<Pod::PP> processing is not normally invoked when
902 parsing POD.
903  
904 If you wish to automate the pre-processing for every POD file, you need to
905 write C<.pp> files (say) instead of C<.pod> files, and add the following
906 B<make> rules to your C<Makefile>:
907  
908 PODPP = podpp
909 PP_FLAGS =
910  
911 .SUFFIXES: .pp .pod
912  
913 .pp.pod:
914 $(PODPP) $(PP_FLAGS) $< >$*.pod
915  
916 Those teach B<make> how to derive a C<.pod> from a C<.pp> file using
917 the B<podpp> pre-processor.
918  
919 C<Pod::PP> uses the C<PE<lt>E<gt>> notation to request symbol expansion.
920 Since it processes text, you need to tag the symbols to be expanded
921 explicitely. Expansion is done recursively, until there is no more
922 expansion possible.
923  
924 If you are familiar with B<cpp>, most directives will be easy to grasp.
925 For instance, using the C<==> prefix to make shorter commands:
926  
927 ==pp include "common.pp"
928  
929 ==pp define DIR /var/www
930 ==pp define TMP /tmp
931  
932 ==pp ifdef SOME_COMMON_SYMBOL
933 ==pp define FOO common foo
934 ==pp else
935 ==pp define FOO P<DIR>
936 ==pp endif
937  
938 The C<==> notation is not standard POD, but it is understood by C<Pod::Parser>
939 and very convenient when it comes to writing things like the above block,
940 because there's no need to separate commands by blank lines.
941 Since the code is going to be processed by B<podpp> anyway, there's no problem,
942 and B<podpp> will always emit legitimate POD. That is, given the following:
943  
944 ==head1 NAME
945 Some data
946  
947 it will re-emit:
948  
949 =head1 NAME
950  
951 Some data
952  
953 thereby normalizing the output. It is guaranteed that after a B<podpp> pass,
954 the output is regular POD. If you make errors in writing the C<Pod::PP>
955 directives, you will not get the expected output, but it will be regular POD.
956  
957 =head1 DIRECTIVES
958  
959 The pre-processing directives can be given in two forms, depending on
960 whether you wish to process your POD files containing C<Pod::PP> directives
961 with the usual POD tools before or after having run B<podpp> on them:
962  
963 =over 4
964  
965 =item *
966  
967 By using the C<=for pp> form before all commands, you ensure that regular
968 POD tools will simply ignore those. This might result in incorrect
969 processing though, if you depend on the definition of some symbols to
970 produce different outputs (i.e. you would need a B<podpp> pass anyway).
971  
972 =item *
973  
974 By using the C<=pp> form before all commands, you require that B<podpp>
975 be run on your file to produce regular POD that can be then processed
976 via regular POD tools.
977  
978 =back
979  
980 Here are the supported directives, in alphabetical order:
981  
982 =over 4
983  
984 =item C<=pp comment> I<comment>
985  
986 A comment. Will be stripped out upon reading.
987  
988 When C<Pod::PP> encounters an error whilst processing a directive, e.g.
989 an include with a file not found, it will leave a comment in the output,
990 albeit using the C<=for pp> form so that it is properly ignored by standard
991 POD tools.
992  
993 =item C<=pp define> I<symbol> [I<value>]
994  
995 Defines I<symbol> to be I<value>. If there's no I<value>, the symbol is
996 simply defined to an empty value. There may be an arbitrary amount of
997 spaces or tabs between I<symbol> and I<value>.
998  
999 A symbol can be tested for defined-ness via C<=pp ifdef>, used in expressions
1000 via C<=pp if>, or expanded via C<PE<lt>I<sym>E<gt>>.
1001  
1002 =item C<=pp elif> I<expr>
1003  
1004 Alternate condition. There may be as many C<=pp elif> as needed, but they
1005 must precede any C<=pp else> directive, and follow a leading C<=pp if> test.
1006 See C<=pp if> below for the I<expr> definiton.
1007  
1008 Naturally, within an C<=pp if> test, the expression I<expr> is evaluated only
1009 if the C<if> condition was false.
1010  
1011 =item C<=pp else>
1012  
1013 The I<else> clause of the C<=pp if> or C<=pp ifdef> test.
1014  
1015 =item C<=pp endif>
1016  
1017 Closes the testing sequence opened by last C<=pp if> or C<=pp ifdef>.
1018  
1019 =item C<=pp if> I<expr>
1020  
1021 Starts a conditional text sequence. If I<expr> evaluates to I<true>, the
1022 remaining up to the matching C<=pp elif> or C<=pp else> or C<=pp endif>
1023 is included in the output, otherwise it is stripped.
1024  
1025 Within an expression, you may include any symbol, verbatim, and form any legal
1026 Perl expression. For instance:
1027  
1028 ==pp define X 4
1029 ==pp define LIMIT 100
1030  
1031 =pp if X*X < LIMIT
1032  
1033 Include this portion if X*X < LIMIT
1034  
1035 =pp else
1036  
1037 Include this portion if X*X >= LIMIT
1038  
1039 =pp endif
1040  
1041 would yield, when processed by B<podpp>:
1042  
1043 Include this portion if X*X < LIMIT
1044  
1045 since the condition is true with the current symbol values.
1046  
1047 You may also use the C<defined()> operator in tests, as in B<cpp>:
1048  
1049 =pp if defined(X) || !defined(LIMIT)
1050  
1051 A bad expression will result in an error message, but you must know that
1052 your expressions are converted into Perl, and then are evaluated within
1053 a C<Safe> compartment: the errors will be reported relative to the translated
1054 Perl expressions, not to your original expressions.
1055  
1056 =item C<=pp ifdef> I<symbol>
1057  
1058 Tests whether a symbol is defined. This is equivalent to:
1059  
1060 =pp if defined(symbol)
1061  
1062 only it is shorter to say.
1063  
1064 =item C<=pp ifndef> I<symbol>
1065  
1066 Tests whether a symbol is not defined. This is equivalent to:
1067  
1068 =pp if !defined(symbol)
1069  
1070 but it is shorter to say.
1071  
1072 =item C<=pp image> [I<E<lt>centerE<gt>>] "I<path>"
1073  
1074 This directive is not a regular pre-processing directive in that it is
1075 highly specialized. It's there because I historically implemented C<Pod::PP>
1076 to pre-process that command in my PODs.
1077  
1078 It's a high-level macro, that is hardwired because C<Pod::PP> is not rich
1079 enough yet to be able to support the definition of that kind of macro.
1080  
1081 It is expanded into two POD directives: one for HTML, one for text.
1082 An example will be better than a lengthy description:
1083  
1084 =pp image <center> "logo.png"
1085  
1086 will expand into:
1087  
1088 =for html <P ALIGN="center"><IMG SRC="logo.png" ALT="logo"></P>
1089  
1090 =begin text
1091  
1092 [image "logo.png" not rendered]
1093  
1094 =end text
1095  
1096 The I<E<lt>centerE<gt>> tag is optional, and you may use I<E<lt>rightE<gt>>
1097 instead to right-justify your image in HTML.
1098  
1099 =item C<=pp include> "I<file>"
1100  
1101 Includes C<"file"> at the present location, through C<Pod::PP>. That is,
1102 the included file may itself use C<Pod::PP> directives.
1103  
1104 The algorithm to find the I<file> is as follows:
1105  
1106 1. The file is first looked for from the location of the current file being
1107 processed.
1108  
1109 2. If not found there, the search path is traversed. You may supply a search
1110 path with the B<-I> flag in B<podpp> or via the C<-incpath> of the
1111 creation routine for C<Pod::PP>.
1112  
1113 3. If still not found, an error is reported.
1114  
1115 =item C<=pp require> "I<file>"
1116  
1117 Same as an C<=pp include> directive, but the C<"file"> is included only
1118 B<once>. The absolute path of the file is used to determine whether it
1119 has already been included. For example, assuming we're in file C<dir/foo>,
1120 and that C<dir/foo/file.pp> exists, the following:
1121  
1122 ==pp require "file.pp"
1123 ==pp require "../dir/file.pp"
1124  
1125 will result in only B<one> inclusion of C<"file.pp">, since both I<require>
1126 statements end up requesting the inclusion of the same path.
1127  
1128 =item C<=pp undef> I<symbol>
1129  
1130 Undefines the target I<symbol>.
1131  
1132 =back
1133  
1134 =head1 DIAGNOSTICS
1135  
1136 C<Pod::PP> uses C<Log::Agent> to emit its diagnostics. The B<podpp> script
1137 leaves C<Log::Agent> in its default configuration, thereby redirecting all
1138 the errors to STDERR. If you use the C<Pod::PP> interface directly in a
1139 script, you can look at configuring alternatives for the logs in L<Log::Agent>.
1140  
1141 Whenever possible, C<Pod::PP> leaves a trail in the output marking the error.
1142 For instance, feeding the following to B<podpp>:
1143  
1144 =pp include "no-such-file"
1145  
1146 would print the following error to STDERR:
1147  
1148 podpp: error in Pod::PP directive 'include' at "example", line 17:
1149 cannot find "no-such-file"
1150  
1151 and leave the following trail:
1152  
1153 =for pp comment (at "example", line 17):
1154 Following "=pp" directive failed: cannot find "no-such-file"
1155 =pp include "no-such-file"
1156  
1157 which will be ignored by all POD tools.
1158  
1159 =head1 INTERFACE
1160  
1161 You will normally don't care, since you will be mostly interfacing with
1162 C<Pod::PP> via the B<podpp> script. This section is therefore only useful
1163 for people wishing to use C<Pod::PP> from within a program.
1164  
1165 Since C<Pod::PP> inherits from C<Pod::Parser>, it conforms to its
1166 interface, in particular for the C<parse_from_filehandle()> and
1167 C<parse_from_file()> routines. See L<Pod::Parser> for more information.
1168  
1169 The creation routine C<make()> takes the following mandatory arguments:
1170  
1171 =over 4
1172  
1173 =item C<-incpath> => I<array_ref>
1174  
1175 The additional include search path (C<"."> is always part of the search path,
1176 and always the first thing looked at). The I<array_ref> provides a list
1177 of directories to look. For instance:
1178  
1179 -incpath => ["h", "/home/ram/usr/podpp"]
1180  
1181 would add the two directories, in the order given.
1182  
1183 =item C<-symbols> => I<hash_ref>
1184  
1185 Provides the intial set of defined symbols. Each key from the I<hash_ref>
1186 is a symbol for the pre-processor:
1187  
1188 -symbols => {
1189 DIR => "/var/tmp"
1190 TMP => "/tmp"
1191 }
1192  
1193 Given the above, the following input:
1194  
1195 dir is "P<DIR>" and tmp is "P<TMP>"
1196  
1197 would become after processing:
1198  
1199 dir is "/var/tmp" and tmp is "/tmp"
1200  
1201 as expected.
1202  
1203 =back
1204  
1205 =head1 BUGS
1206  
1207 The C<=pp image> directive is a hack. It should not be implemented at this
1208 level, but it was convenient to do so.
1209  
1210 =head1 AUTHOR
1211  
1212 Raphael Manfredi F<E<lt>Raphael_Manfredi@pobox.comE<gt>>
1213  
1214 This software is currently unmaintained. Please look at:
1215  
1216 http://www.unmaintained-free-software.org/
1217  
1218 if you wish to take over maintenance. I would appreciate being notified,
1219 so that I can transfer the PAUSE (CPAN) ownership to you.
1220  
1221 =head1 SEE ALSO
1222  
1223 Pod::Parser(3).
1224  
« lib/Pod/PP/State/Info.pm ↑ Summary