blib/lib/Pod/pod2rtf.pl untested

filter: Statementstmt 0.0% 0 / 294 Branchbran 0.0% 0 / 142 Conditioncond 0.0% 0 / 87 MC/DCmcdc 0.0% 0 / 50 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 615 CC 57 SCAR 45.1
CC
57
Cov
0%
lrefs
210.0
scan_thing
156.0
do_rest
90.0
CRAP 90.7
1  
2 #/usr/local/bin/perl
3 # pod2rtf - convert pod format to rtf (for Winhelp compiler HC)
4 #
5 # given to the public domain 1996 by Reini Urban,
6 # <rurban@xarch.tu-graz.ac.at>
7 #
8 # usage: pod2rtf [podfiles]
9 # will read the cwd and parse all files with .pod extension
10 # if no arguments are given on the command line.
11 # derived from Larry Wall's pod2html.pl
12 #
13 # Translation:
14 # Bold {\b text}
15 # Italic {\i text}
16 # Code {\f2 text}
17 # =back \page
18 # PopupLink-To {\ul link-text}{\v link-key}
19 # Link-To {\uldb link-text}{\v link-key}
20 # Link-Def {\super #}{\footnote link-key}
21 # {\super $}{\footnote link-title}
22 # {\super K}{\footnote link-search}
23 # =item
24 #---------------------------------------------------------------
25 BEGIN {
26 # thats for my ntperl only
27 # push @INC, qw( \\reini\public\dosapp\perl\dll-src\ext \\reini\public\dosapp\perl\lib \\reini\p
28 ublic\dosapp\perl\ext );
29  
30 %HTML_Escapes = (
31 'amp' => '&', # ampersand
32 'lt' => '<', # left chevron, less-than
33 'gt' => '>', # right chevron, greater-than
34 'quot' => '"', # double quote
35  
36 "Aacute" => "\\\'e7", # capital A, acute accent
37 "aacute" => "\\\'87", # small a, acute accent
38 "Acirc" => "\\\'e5", # capital A, circumflex accent
39 "acirc" => "\\\'89", # small a, circumflex accent
40 "AElig" => '\\\'ae', # capital AE diphthong (ligature)
41 "aelig" => '\\\'be', # small ae diphthong (ligature)
42 "Agrave" => "\\\'cb", # capital A, grave accent
43 "agrave" => "\\\'88", # small a, grave accent
44 "Aring" => '\\\'81', # capital A, ring
45 "aring" => '\\\'8c', # small a, ring
46 "Atilde" => '\\\'cc', # capital A, tilde
47 "atilde" => '\\\'8b', # small a, tilde
48 "Auml" => '\\\'80', # capital A, dieresis or umlaut mark
49 "auml" => '\\\'8a', # small a, dieresis or umlaut mark
50 "Ccedil" => '\\\'82', # capital C, cedilla
51 "ccedil" => '\\\'8d', # small c, cedilla
52 "Eacute" => "\\\'83", # capital E, acute accent
53 "eacute" => "\\\'8e", # small e, acute accent
54 "Ecirc" => "\\\'e6", # capital E, circumflex accent
55 "ecirc" => "\\\'90", # small e, circumflex accent
56 "Egrave" => "\\\'e9", # capital E, grave accent
57 "egrave" => "\\\'8f", # small e, grave accent
58 "Euml" => "\\\'e8", # capital E, dieresis or umlaut mark
59 "euml" => "\\\'91", # small e, dieresis or umlaut mark
60 "Iacute" => "\\\'ea", # capital I, acute accent
61 "iacute" => "\\\'92", # small i, acute accent
62 "Icirc" => "\\\'eb", # capital I, circumflex accent
63 "icirc" => "\\\'90", # small i, circumflex accent
64 "Igrave" => "\\\'e9", # capital I, grave accent
65 "igrave" => "\\\'93", # small i, grave accent
66 "Iuml" => "\\\'ec", # capital I, dieresis or umlaut mark
67 "iuml" => "\\\'95", # small i, dieresis or umlaut mark
68 "Ntilde" => '\\\'84', # capital N, tilde
69 "ntilde" => '\\\'96', # small n, tilde
70 "Oacute" => "\\\'ee", # capital O, acute accent
71 "oacute" => "\\\'97", # small o, acute accent
72 "Ocirc" => "\\\'ef", # capital O, circumflex accent
73 "ocirc" => "\\\'99", # small o, circumflex accent
74 "Ograve" => "\\\'f1", # capital O, grave accent
75 "ograve" => "\\\'98", # small o, grave accent
76 "Oslash" => "\\\'af", # capital O, slash
77 "oslash" => "\\\'bf", # small o, slash
78 "Otilde" => "\\\'cd", # capital O, tilde
79 "otilde" => "\\\'9b", # small o, tilde
80 "Ouml" => "\\\'85", # capital O, dieresis or umlaut mark
81 "ouml" => "\\\'9a", # small o, dieresis or umlaut mark
82 "Uacute" => "\\\'f2", # capital U, acute accent
83 "uacute" => "\\\'9c", # small u, acute accent
84 "Ucirc" => "\\\'f3", # capital U, circumflex accent
85 "ucirc" => "\\\'9e", # small u, circumflex accent
86 "Ugrave" => "\\\'f4", # capital U, grave accent
87 "ugrave" => "\\\'9d", # small u, grave accent
88 "Uuml" => "\\\'86", # capital U, dieresis or umlaut mark
89 "uuml" => "\\\'9f", # small u, dieresis or umlaut mark
90 "yuml" => "\\\'d8", # small y, dieresis or umlaut mark
91 );
92 }
93  
94 *RS = */;
95 *ERRNO = *!;
96  
97 use Carp;
98  
99 $gensym = 0;
100  
101 while ($ARGV[0] =~ /^-d(.*)/) {
102 shift;
103 $Debug{ lc($1 || shift) }++;
104 }
105  
106 # look in these pods for things not found within the current pod
107 @inclusions = qw[
108 perlfunc perlvar perlrun perlop
109 ];
110  
111 # ck for podnames on command line
112 while ($ARGV[0]) {
113 push(@Pods,shift);
114 }
115 $A={};
116  
117 # location of pods
118 $dir=".";
119 $debug = 0;
120  
121 # rtf tokens
122 $type ='{\uldb ';
123 $head = '\b\f1\fs28 ';
124 $head1 = '\b\f1\fs28 ';
125 $head2 = '\b\i\f1\fs24 ';
126 $indent = '\li360\widctlpar';
127 $bullet = '\par{\f3\\\'B7}\tab';
128  
129 unless(@Pods){
130 opendir(DIR,$dir) or die "Can't opendir $dir: $ERRNO";
131 @Pods = grep(/\.pod$/,readdir(DIR));
132 closedir(DIR) or die "Can't closedir $dir: $ERRNO";
133 }
134 @Pods or die "expected pods";
135  
136 # loop twice through the pods, first to learn the links, then to produce rtf
137 for $count (0,1){
138 (print "Scanning pods...\n") unless $count;
139 foreach $podfh ( @Pods ) {
140 ($pod = $podfh) =~ s/\.pod$//;
141 Debug("files", "opening 2 $podfh" );
142 (print "Creating $pod.rtf from $podfh\n") if $count;
143 $RS = "\n=";
144 open($podfh,"<".$podfh) || die "can't open $podfh: $ERRNO";
145 @all=<$podfh>;
146 close($podfh);
147 $RS = "\n";
148 $all[0]=~s/^=//;
149 for(@all){s/=$//;}
150 $Podnames{$pod} = 1;
151 $in_list=0;
152 $rtf=$pod.".rtf";
153 if($count){
154 for(@all){
155 s/\\/\\\\/gm;
156 s/{/\\{/gm;
157 s/}/\\}/gm;
158 }
159 open(RTF,">$rtf") || die "can't create $rtf: $ERRNO";
160 # <!-- \$RCSfile\$\$Revision\$\$Date\$ -->
161 # <!-- \$Log\$ -->
162 print RTF <<'RTF__EOQ';
163 {\rtf1\ansi \deff0\deflang1024
164  
165 {\fonttbl
166 {\f0\froman Times New Roman;}
167 {\f1\fswiss Arial;}
168 {\f2\fmodern Courier New;}
169 {\f3\froman Symbol;}
170 }
171 {\colortbl;
172 \red0\green0\blue0;
173 \red0\green0\blue255;
174 \red0\green255\blue0;
175 \red255\green0\blue0;
176 \red255\green255\blue255;}
177 {\info{\author Reini Urban}}
178 \pard\plain {\b\f1\fs28\li120\sb340\sa120\sl-320
179 RTF__EOQ
180 print RTF def_name($pod,$pod);
181 print RTF '\pard}';
182 }
183  
184 for($i=0;$i<=$#all;$i++){
185  
186 $all[$i] =~ /^(\w+)\s*(.*)\n?([^\0]*)$/ ;
187 ($cmd, $title, $rest) = ($1,$2,$3);
188 if ($cmd eq "item") {
189 if($count ){
190 ($depth) or do_list("over",$all[$i],\$in_list,\$depth);
191 do_item($title,$rest,$in_list);
192 }
193 else{
194 # scan item
195 scan_thing("item",$title,$pod);
196 }
197 }
198 elsif ($cmd =~ /^head([12])/){
199 $num=$1;
200 if($count){
201 do_hdr($num,$title,$rest,$depth);
202 }
203 else{
204 # header scan
205 scan_thing($cmd,$title,$pod); # skip head1
206 }
207 }
208 elsif ($cmd =~ /^over/) {
209 $count and $depth and do_list("over",$all[$i+1],\$in_list,\$depth);
210 }
211 elsif ($cmd =~ /^back/) {
212 if($count){
213 ($depth) or next; # just skip it
214 do_list("back",$all[$i+1],\$in_list,\$depth);
215 do_rest("$title.$rest");
216 }
217 }
218 elsif ($cmd =~ /^cut/) {
219 next;
220 }
221 elsif($Debug){
222 (warn "unrecognized header: $cmd") if $Debug;
223 }
224 }
225 # close open lists without '=back' stmts
226 if($count){
227 while($depth){
228 do_list("back",$all[$i+1],\$in_list,\$depth);
229 }
230 print RTF "\n} \n";
231 }
232 }
233 # print "execute HC to compile to WinHelp\n";
234 }
235  
236 sub do_list{
237 my($which,$next_one,$list_type,$depth)=@_;
238 my($key);
239 if($which eq "over"){
240 ($next_one =~ /^item\s+(.*)/ ) or (warn "Bad list, $1\n") if $Debug;
241 $key=$1;
242 if($key =~ /^1\.?/){
243 $$list_type = "OL";
244 }
245 elsif($key =~ /\*\s*$/){
246 $$list_type="UL";
247 }
248 elsif($key =~ /\*?\s*\w/){
249 $$list_type="DL";
250 }
251 else{
252 (warn "unknown list type for item $key") if $Debug;
253 }
254 # print RTF '\par ';
255 # print RTF '{\li284\widctlpar ';
256 # print RTF qq{<$$list_type>};
257 $$depth++;
258 }
259 elsif($which eq "back"){
260 print RTF "\n\\pard\\page \n"; #qq{\par</$$list_type>\n};
261 $$depth--;
262 }
263 }
264  
265 sub do_hdr{
266 my($num,$title,$rest,$depth)=@_;
267 ($num == 1) and print RTF '\par\sln ';
268 # def_link($title,"");
269 process_thing(\$title,"NAME");
270 if ($num==1) {
271 print RTF "\\par {$head1";
272 } elsif ($num == 2) {
273 print RTF "\\par {$head2";
274 } else {
275 print RTF "\\par {$head";
276 }
277 print RTF $title;
278 print RTF '}\par';
279 do_rest($rest);
280 }
281  
282 sub do_item{
283 my($title,$rest,$list_type)=@_;
284 process_thing(\$title,"NAME");
285 if($list_type eq "DL"){
286 print RTF "\n{\\li0\\b ";
287 print RTF "$title";
288 print RTF '}\par ';
289 print RTF "{$indent ";
290 }
291 else{
292 print RTF "{\\f3\\\'B7}\\tab";
293 ($list_type ne "OL") && (print RTF $title,"\n");
294 }
295 do_rest($rest) if $rest ne "\n";
296 print RTF ($list_type eq "DL" )? "}\n" : "\n";
297 }
298  
299 sub do_rest{
300 my($rest)=@_;
301 my(@lines,$p,$q,$line,,@paras,$inpre);
302 @paras=split(/\n\n+/,$rest);
303 for($p=0;$p<=$#paras;$p++){
304 @lines=split(/\n/,$paras[$p]);
305 if($lines[0] =~ /^\s+\w*\t.*/){ # listing or unordered list
306 print RTF "{$indent ";
307 foreach $line (@lines){
308 ($line =~ /^\s+(\w*)\t(.*)/) && (($key,$rem) = ($1,$2));
309 print RTF defined($Podnames{$key}) ?
310 "$bullet {\\uldb $key}{\\v $key}\\tab $rem\n" :
311 "$bullet $line\n";
312 }
313 print RTF "}\n\\par";
314 }
315 elsif($lines[0] =~ /^\s/){ # preformatted code
316 if($paras[$p] =~/>>|<</){
317 print RTF "{\\f2 ";
318 $inpre=1;
319 }
320 else{
321 print RTF "{\\f2 ";
322 $inpre=0;
323 }
324 inner:
325 while(defined($paras[$p])){
326 @lines=split(/\n/,$paras[$p]);
327 foreach $q (@lines){
328 if($paras[$p]=~/>>|<</){
329 if($inpre){
330 process_thing(\$q,"RTF");
331 }
332 else {
333 print RTF "\n}";
334 print RTF '\par{\f2 ';
335 $inpre=1;
336 process_thing(\$q,"RTF");
337 }
338 }
339 while($q =~ s/\t+/' 'x (length($&) * 8 - length($`) % 8)/e){
340 1;
341 }
342 print RTF $q,"\n\\par ";
343 }
344 last if $paras[$p+1] !~ /^\s/;
345 $p++;
346 }
347 print RTF ($inpre==1) ? "\n} " : "\n} ";
348 }
349 else{ # other text
350 @lines=split(/\n/,$paras[$p]);
351 foreach $line (@lines){
352 process_thing(\$line,"RTF");
353 print RTF "$line\n\\par ";
354 }
355 }
356 print RTF '\par ';
357 }
358 }
359  
360 sub process_thing{
361 my($thing,$htype)=@_;
362 pre_escapes($thing);
363 find_refs($thing,$htype);
364 post_escapes($thing);
365 }
366  
367 sub scan_thing{
368 my($cmd,$title,$pod)=@_;
369 $_=$title;
370 s/\n$//;
371 s/E<(.*?)>/isokey($1)/eg;
372 # remove any formatting information for the headers
373 s/[SFCBI]<(.*?)>/$1/g;
374 # the "don't format me" thing
375 s/Z<>//g;
376 if ($cmd eq "item") {
377  
378 if (/^\*/) { return } # skip bullets
379 if (/^\d+\./) { return } # skip numbers
380 s/(-[a-z]).*/$1/i;
381 trim($_);
382 return if defined $A->{$pod}->{"Items"}->{$_};
383 $A->{$pod}->{"Items"}->{$_} = gensym($pod, $_);
384 $A->{$pod}->{"Items"}->{(split(' ',$_))[0]}=$A->{$pod}->{"Items"}->{$_};
385 Debug("items", "item $_");
386 if (!/^-\w$/ && /([%\$\@\w]+)/ && $1 ne $_
387 && !defined($A->{$pod}->{"Items"}->{$_}) && ($_ ne $1))
388 {
389 $A->{$pod}->{"Items"}->{$1} = $A->{$pod}->{"Items"}->{$_};
390 Debug("items", "item $1 REF TO $_");
391 }
392 if ( m{^(tr|y|s|m|q[qwx])/.*[^/]} ) {
393 my $pf = $1 . '//';
394 $pf .= "/" if $1 eq "tr" || $1 eq "y" || $1 eq "s";
395 if ($pf ne $_) {
396 $A->{$pod}->{"Items"}->{$pf} = $A->{$pod}->{"Items"}->{$_};
397 Debug("items", "item $pf REF TO $_");
398 }
399 }
400 }
401 elsif ($cmd =~ /^head[12]/){
402 return if defined($Headers{$_});
403 $A->{$pod}->{"Headers"}->{$_} = gensym($pod, $_);
404 Debug("headers", "header $_");
405 }
406 else {
407 (warn "unrecognized header: $cmd") if $Debug;
408 }
409 }
410  
411 sub def_name {
412 my ($value, $bigkey) = @_;
413 $bigkey = $value if $bigkey eq "";
414 return "\n{\\super \#{\\footnote \# $value}}\n".
415 "{\\super \${\\footnote \$ $bigkey}}\n".
416 "{\\super K{\\footnote K $bigkey}}\n".
417 " $bigkey\n";
418 }
419 sub def_link {
420 my ($value, $bigkey) = @_;
421 return "\n{\\uldb $bigkey}{\\v $value}";
422 }
423  
424 sub picrefs {
425 my($char, $bigkey, $lilkey,$htype) = @_;
426 my ($key,$ref,$podname);
427 for $podname ($pod,@inclusions){
428 for $ref ( "Items", "Headers" ) {
429 if (defined $A->{$podname}->{$ref}->{$bigkey}) {
430 $value = $A->{$podname}->{$ref}->{$key=$bigkey};
431 Debug("subs", "bigkey is $bigkey, value is $value\n");
432 }
433 elsif (defined $A->{$podname}->{$ref}->{$lilkey}) {
434 $value = $A->{$podname}->{$ref}->{$key=$lilkey};
435 return "" if $lilkey eq '';
436 Debug("subs", "lilkey is $lilkey, value is $value\n");
437 }
438 }
439 if (length($key)) {
440 ($pod2,$num) = split(/_/,$value,2);
441 if($htype eq "NAME"){
442 return def_name($value, $bigkey);
443 } else{
444 return def_link($value, $bigkey);
445 }
446 }
447 }
448 if ($char =~ /[IF]/) {
449 return "{\\i $bigkey} ";
450 } elsif($char =~ /C/) {
451 return "{\\f2 $bigkey} ";
452 } else {
453 return "{\\b $bigkey} ";
454 }
455 }
456  
457 sub find_refs {
458 my($thing,$htype)=@_;
459 my($orig) = $$thing;
460 # LREF: a manpage(3f) we don't know about
461 $$thing=~s:L<([a-zA-Z][^\s\/]+)(\([^\)]+\))>:the I<$1>$2 section:g;
462 $$thing=~s/L<([^>]*)>/lrefs($1,$htype)/ge;
463 $$thing=~s/([CIBF])<(\W*?(-?\w*).*?)>/picrefs($1, $2, $3, $htype)/ge;
464 $$thing=~s/((\w+)\(\))/picrefs("I", $1, $2,$htype)/ge;
465 $$thing=~s/([\$\@%](?!&[gl]t)([\w:]+|\W\b))/varrefs($1,$htype)/ge;
466 (($$thing eq $orig) && ($htype eq "NAME")) &&
467 ($$thing=picrefs("I", $$thing, "", $htype));
468 }
469  
470 sub lrefs {
471 my($page, $item) = split(m#/#, $_[0], 2);
472 my($htype)=$_[1];
473 my($podname);
474 my($section) = $page =~ /\((.*)\)/;
475 my $selfref;
476 if ($page =~ /^[A-Z]/ && $item) {
477 $selfref++;
478 $item = "$page/$item";
479 $page = $pod;
480 } elsif (!$item && $page =~ /[^a-z\-]/ && $page !~ /^\$.$/) {
481 $selfref++;
482 $item = $page;
483 $page = $pod;
484 }
485 $item =~ s/\(\)$//;
486 if (!$item) {
487 if (!defined $section && defined $Podnames{$page}) {
488 return "\n\\par {\\uldb the {\\i $page} manpage}{\\v $page}\n";
489 } else {
490 (warn "Bizarre entry $page/$item") if $Debug;
491 return "the {\\i $_[0]} manpage\n";
492 }
493 }
494  
495 if ($item =~ s/"(.*)"/$1/ || ($item =~ /[^\w\/\-]/ && $item !~ /^\$.$/)) {
496 $text = "{\\i $item} ";
497 $ref = "Headers";
498 } else {
499 $text = "{\\i $item} ";
500 $ref = "Items";
501 }
502 for $podname ($pod, @inclusions){
503 undef $value;
504 if ($ref eq "Items") {
505 if (defined($value = $A->{$podname}->{$ref}->{$item})) {
506 ($pod2,$num) = split(/_/,$value,2);
507 return (($pod eq $pod2) && ($htype eq "NAME"))
508 ? def_name($value, $bigkey)
509 : def_link($value, $bigkey);
510 }
511 }
512 elsif($ref eq "Headers") {
513 if (defined($value = $A->{$podname}->{$ref}->{$item})) {
514 ($pod2,$num) = split(/_/,$value,2);
515 return (($pod eq $pod2) && ($htype eq "NAME"))
516 ? def_name($value, $text)
517 : def_link($value, $text);
518 }
519 }
520 }
521 (warn "No $ref reference for $item (@_)") if $Debug;
522 return $text;
523 }
524  
525 sub varrefs {
526 my ($var,$htype) = @_;
527 for $podname ($pod,@inclusions){
528 if ($value = $A->{$podname}->{"Items"}->{$var}) {
529 ($pod2,$num) = split(/_/,$value,2);
530 Debug("vars", "way cool -- var ref on $var");
531 return (($pod eq $pod2) && ($htype eq "NAME")) # INHERIT $_, $pod
532 ? def_name($value, $text)
533 : def_link($value, $text);
534 }
535 }
536 Debug( "vars", "bummer, $var not a var");
537 return "{\\b $var} ";
538 }
539  
540 # convert illegal names
541 sub gensym {
542 my($podname, $key) = @_;
543 $key =~ s/\s.*/_/; # trim whitespace
544 $key =~ s/\$/VAR_/; # $ARG -> VAR_ARG
545 $key =~ s/\@/LIST_/; # @ARG -> LIST_ARG
546 ($key = lc($key)) =~ tr/a-z/_/cs;
547 my $name = "${podname}_${key}_0";
548 # $name =~ s/__/_/g;
549 while ($sawsym{$name}++) {
550 $name =~ s/_?(\d+)$/'_' . ($1 + 1)/e;
551 }
552 return $name;
553 }
554  
555 sub pre_escapes {
556 my($thing)=@_;
557 $$thing=~s/(?:[^ESIBLCF])</noremap("<")/eg;
558 $$thing=~s/E<([^\/][^<>]*)>/isokey($1)/eg; # embedded special
559 }
560  
561 sub isokey {
562 $char = $_[0];
563 exists $HTML_Escapes{$char}
564 ? $char = $HTML_Escapes{$char}
565 : $char =~ s/([0-9A-F][0-9A-F])/\\\'$1 /;
566 $char;
567 }
568  
569 sub noremap {
570 my $hide = $_[0];
571 $hide =~ tr/\000-\177/\200-\377/;
572 $hide;
573 }
574  
575 sub post_escapes {
576 my($thing)=@_;
577 # $$thing=~s/[^GM]>>/\&gt\;\&gt\;/g;
578 # $$thing=~s/([^"MGAE])>/$1>/g;
579 $$thing=~tr/\200-\377/\000-\177/;
580 }
581  
582 sub Debug {
583 my $level = shift;
584 print STDERR @_,"\n" if $Debug{$level};
585 }
586  
587 sub dumptable {
588 my $t = shift;
589 print STDERR "TABLE DUMP $t\n";
590 foreach $k (sort keys %$t) {
591 printf STDERR "%-20s <%s>\n", $t->{$k}, $k;
592 }
593 }
594 sub trim {
595 for (@_) {
596 s/^\s+//;
597 s/\s\n?$//;
598 }
599 }