| 1 | |||
| 2 | package Pod::Rtf; | ||
| 3 | |||
| 4 | use Pod::Parser; | ||
| 5 | require Pod::PlainText; | ||
| 6 | use strict; | ||
| 7 | |||
| 8 | use vars qw(@ISA $VERSION); | ||
| 9 | $VERSION = '0.02'; | ||
| 10 | |||
| 11 | use base qw(Pod::Parser); | ||
| 12 | |||
| 13 | |||
| 14 | BEGIN { | ||
| 15 | $li = ""; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub begin_input { | ||
| 19 | my $parser = shift; | ||
| 20 | my $header = <<"EOHEADER"; | ||
| 21 | {\\rtf1\\ansi \\deff0\\deflang1024 | ||
| 22 | |||
| 23 | {\\fonttbl | ||
| 24 | {\\f0\\froman Times New Roman;} | ||
| 25 | {\\f1\\fmodern Fixedsys;} | ||
| 26 | } | ||
| 27 | |||
| 28 | {\\colortbl; | ||
| 29 | \\red0\\green0\\blue0; | ||
| 30 | \\red0\\green0\\blue255; | ||
| 31 | \\red0\\green255\\blue0; | ||
| 32 | \\red255\\green0\\blue0; | ||
| 33 | \\red255\\green255\\blue255; | ||
| 34 | } | ||
| 35 | EOHEADER | ||
| 36 | |||
| 37 | if ($pound_note) { | ||
| 38 | $pound_note = context_string($pound_note); | ||
| 39 | $header .= "#{\\footnote $pound_note}\n"; | ||
| 40 | push(@pound_pages,$pound_note); | ||
| 41 | } | ||
| 42 | if ($dollar_note) { | ||
| 43 | $header .= "\${\\footnote $dollar_note}\n"; | ||
| 44 | push(@index_pages,$dollar_note); | ||
| 45 | } | ||
| 46 | if ($K_note) { | ||
| 47 | $header .= "K{\\footnote $K_note}\n"; | ||
| 48 | push(@K_pages,$dollar_note); | ||
| 49 | } | ||
| 50 | $header .= "\n{\\pard}\n"; | ||
| 51 | |||
| 52 | my $out_fh = $parser->output_handle(); | ||
| 53 | print $out_fh $header; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub command { | ||
| 57 | my ($parser, $command, $paragraph, $line_num) = @_; | ||
| 58 | my $cmd_open = "{"; | ||
| 59 | my $cmd_close = "}\n"; | ||
| 60 | if ($command eq 'head1') { | ||
| 61 | my $line = $paragraph; | ||
| 62 | $line = context_string("$pound_note$line"); | ||
| 63 | if (length($line) > 0) { | ||
| 64 | push(@pound_topics,$line); | ||
| 65 | $cmd_open = "#{\\footnote $line}\n$cmd_open\\f0\\fs36 "; | ||
| 66 | } | ||
| 67 | $cmd_close .= "{\\par}{\\pard}\n"; | ||
| 68 | $li = ""; | ||
| 69 | } | ||
| 70 | elsif ($command eq 'head2') { | ||
| 71 | my $line = $paragraph; | ||
| 72 | $line = context_string("$pound_note$line"); | ||
| 73 | if (length($line) > 0) { | ||
| 74 | push(@pound_topics,$line); | ||
| 75 | $cmd_open = "#{\\footnote $line}\n$cmd_open\\f0\\fs28 "; | ||
| 76 | } | ||
| 77 | $cmd_close .= "{\\par}{\\pard}\n"; | ||
| 78 | $li = ""; | ||
| 79 | } | ||
| 80 | elsif ($command =~ /head[3-9]/) { | ||
| 81 | my $line = $paragraph; | ||
| 82 | $line = context_string("$pound_note$line"); | ||
| 83 | if (length($line) > 0) { | ||
| 84 | push(@pound_topics,$line); | ||
| 85 | $cmd_open = "#{\\footnote $line}\n$cmd_open\\f0\\fs24 "; | ||
| 86 | } | ||
| 87 | $cmd_close .= "{\\par}{\\pard}\n"; | ||
| 88 | $li = ""; | ||
| 89 | } | ||
| 90 | elsif ($command eq 'item') { | ||
| 91 | my $non_space = $paragraph; | ||
| 92 | $non_space =~ s/\s+//g; | ||
| 93 | if ($non_space eq '*') { $paragraph =~ s/\*/\\bullet/; } | ||
| 94 | # HCW on NT seems to have difficulty w/ RTF, we hack around: | ||
| 95 | if ($is_first_item eq "yes") { | ||
| 96 | $cmd_open =~ s/\{//; | ||
| 97 | $is_first_item = ""; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | elsif ($command eq 'over') { | ||
| 101 | my $amount = $paragraph; | ||
| 102 | if ($amount !~ s/(\d+)/$1/) { $amount = 4 } | ||
| 103 | # @10 pt fontw/ 2:1 aspect: 5 pts/space * 20 twips/point | ||
| 104 | $amount *= 100; | ||
| 105 | $paragraph = ""; | ||
| 106 | $li = "\\li$amount"; | ||
| 107 | $cmd_open = "$cmd_open$li\\f0\\fs20"; | ||
| 108 | # HCW on NT seems to have difficulty w/ RTF, we hack around: | ||
| 109 | $cmd_close =~ s/\}//; | ||
| 110 | $is_first_item = "yes"; | ||
| 111 | } | ||
| 112 | elsif ($command eq 'back') { | ||
| 113 | $li = ""; | ||
| 114 | $cmd_close = "{\\par}{\\pard}$cmd_close"; | ||
| 115 | } | ||
| 116 | elsif ($command eq 'cut') { | ||
| 117 | $li = ""; | ||
| 118 | } | ||
| 119 | elsif ($command eq 'pod') { | ||
| 120 | $li = ""; | ||
| 121 | } | ||
| 122 | # perhaps for and begin ought to be handled as verbatim(?) | ||
| 123 | elsif ($command eq 'for') { | ||
| 124 | $li = ""; | ||
| 125 | } | ||
| 126 | elsif ($command eq 'begin') { | ||
| 127 | } | ||
| 128 | elsif ($command eq 'end') { | ||
| 129 | } | ||
| 130 | ## ... other commands and their actions | ||
| 131 | else { | ||
| 132 | warn "Unrecognized command '$command'\n" if $VERBOSE; | ||
| 133 | } | ||
| 134 | my $expansion = $parser->interpolate($paragraph, $line_num); | ||
| 135 | $expansion = "$cmd_open$expansion$cmd_close"; | ||
| 136 | |||
| 137 | my $out_fh = $parser->output_handle(); | ||
| 138 | print $out_fh $expansion; | ||
| 139 | } | ||
| 140 | |||
| 141 | sub verbatim { | ||
| 142 | my ($parser, $paragraph, $line_num) = @_; | ||
| 143 | ## Format verbatim paragraph; sample actions might be: | ||
| 144 | my @lines = split(/\n/,$paragraph); | ||
| 145 | my $out_fh = $parser->output_handle(); | ||
| 146 | foreach my $line (@lines) { | ||
| 147 | chomp($line); | ||
| 148 | $line = rtf_escape($line); | ||
| 149 | $line = "{{\\keep}\\f1\\fs20 $line {\\line}}\n"; | ||
| 150 | print $out_fh $line; | ||
| 151 | } | ||
| 152 | $line="{\\par}{\\pard}\n"; | ||
| 153 | print $out_fh $line; | ||
| 154 | } | ||
| 155 | |||
| 156 | sub textblock { | ||
| 157 | my ($parser, $paragraph, $line_num) = @_; | ||
| 158 | ## Translate/Format this block of text; sample actions might be: | ||
| 159 | # Because we do the rtf escape here it is not necessary to do it | ||
| 160 | # for the interior sequences individually. | ||
| 161 | my $rtf_paragraph = rtf_escape($paragraph); | ||
| 162 | my $expansion = $parser->interpolate($rtf_paragraph, $line_num); | ||
| 163 | my $rtf_par = "{$li\\f0\\fs20\n$expansion\{\\par}}\n{\\par}{\\pard}\n"; | ||
| 164 | my $out_fh = $parser->output_handle(); | ||
| 165 | print $out_fh $rtf_par; | ||
| 166 | } | ||
| 167 | |||
| 168 | sub interior_sequence { | ||
| 169 | my ($parser, $seq_command, $argument) = @_; | ||
| 170 | my $cfont = '\\f1'; | ||
| 171 | my $cfonts = '\\fs20'; | ||
| 172 | # rtf escape has already been done by textblock | ||
| 173 | # my $argument = rtf_escape($seq_argument); | ||
| 174 | ## Expand an interior sequence; sample actions might be: | ||
| 175 | return "{\\i $argument}" if ($seq_command eq 'I'); | ||
| 176 | return "{\\b $argument}" if ($seq_command eq 'B'); | ||
| 177 | return "{{\\keep} $argument{\\pard}}" | ||
| 178 | if ($seq_command eq 'S'); | ||
| 179 | return "{$cfont$cfonts $argument}{\\pard}" | ||
| 180 | if ($seq_command eq 'C'); | ||
| 181 | if ($seq_command eq 'L') { | ||
| 182 | my @links = split(/\|/,$argument); | ||
| 183 | my $show = $argument; | ||
| 184 | if ($#links >= 1) { | ||
| 185 | $show = $links[0]; | ||
| 186 | $argument = join('|',@links[1..$#links]); | ||
| 187 | } | ||
| 188 | if ($argument =~ /\/?\"[^\"\|\/]+\"/) { # an L<"sec"> or L</"sec"> | ||
| 189 | my $vlink = context_string("$pound_note$argument"); | ||
| 190 | return "{\\strike $show}{\\v $vlink}" | ||
| 191 | } | ||
| 192 | if ($argument =~ /[^\"\|\/]+/) { # an L<name> or L<sec> | ||
| 193 | my $vlink = context_string($argument); | ||
| 194 | # *if* we have seen it before: | ||
| 195 | if (scalar(grep(/$vlink/,@pound_topics))) { | ||
| 196 | $vlink = context_string("$pound_note$vlink"); | ||
| 197 | } | ||
| 198 | elsif (scalar(grep(/$vlink/,@pound_pages))) { | ||
| 199 | if ($vlink !~ /\./) { $vlink .= ".pod"; } | ||
| 200 | } | ||
| 201 | else { | ||
| 202 | if ($vlink !~ /\./) { | ||
| 203 | if ($vlink =~ /perl/) { | ||
| 204 | $vlink .= ".pod"; | ||
| 205 | } | ||
| 206 | elsif ($argument =~ /::/) { | ||
| 207 | $vlink .= ".pm"; | ||
| 208 | } | ||
| 209 | } | ||
| 210 | } | ||
| 211 | return "{\\strike $show}{\\v $vlink}" | ||
| 212 | } | ||
| 213 | # if (($argument !~ /\//) || | ||
| 214 | # ($argument !~ /\|/)); | ||
| 215 | #if ( m{^ ([a-zA-Z][^\s\/]+) (\([^\)]+\))? $}x ) { | ||
| 216 | # ## LREF: a manpage(3f) | ||
| 217 | # $_ = "the $1$2 manpage"; | ||
| 218 | #} | ||
| 219 | #elsif ( m{^ ([^/]+) / ([:\w]+(\(\))?) $}x ) { | ||
| 220 | # ## LREF: an =item on another manpage | ||
| 221 | # $_ = "the \"$2\" entry in the $1 manpage"; | ||
| 222 | #} | ||
| 223 | #elsif ( m{^ / ([:\w]+(\(\))?) $}x ) { | ||
| 224 | # ## LREF: an =item on this manpage | ||
| 225 | # $_ = $self->internal_lrefs($1); | ||
| 226 | #} | ||
| 227 | #elsif ( m{^ (?: ([a-zA-Z]\S+?) / )? "?(.*?)"? $}x ) { | ||
| 228 | # ## LREF: a =head2 (head1?), maybe on a manpage, maybe right here | ||
| 229 | # ## the "func" can disambiguate | ||
| 230 | # $_ = ((defined $1) && $1) | ||
| 231 | # ? "the section on \"$2\" in the $1 manpage" | ||
| 232 | # : "the section on \"$2\""; | ||
| 233 | #} | ||
| 234 | } | ||
| 235 | return "$argument" if ($seq_command eq 'F'); | ||
| 236 | if ($seq_command eq 'X') { | ||
| 237 | return "{\\strike $argument}{\\v $argument}"; | ||
| 238 | } | ||
| 239 | return "$argument" if ($seq_command eq 'Z'); | ||
| 240 | if ($seq_command eq 'E') { | ||
| 241 | if (defined($Pod::PlainText::HTML_Escapes{$argument})) { | ||
| 242 | return "$Pod::PlainText::HTML_Escapes{$argument}"; | ||
| 243 | } | ||
| 244 | else { | ||
| 245 | warn "Unrecognized escape sequence '$seq_command<$argument>'\n" if $VERBOSE; | ||
| 246 | return "$argument"; | ||
| 247 | } | ||
| 248 | } | ||
| 249 | ## ... other sequence commands and their resulting text | ||
| 250 | warn "Unrecognized interior sequence '$seq_command<$argument>'\n" if $VERBOSE; | ||
| 251 | return "$argument"; | ||
| 252 | } | ||
| 253 | |||
| 254 | sub end_input { | ||
| 255 | my $parser = shift; | ||
| 256 | my $footer = <<"EOFOOTER"; | ||
| 257 | } | ||
| 258 | EOFOOTER | ||
| 259 | my $out_fh = $parser->output_handle(); | ||
| 260 | print $out_fh $footer; | ||
| 261 | } | ||
| 262 | |||
| 263 | sub rtf_escape { | ||
| 264 | my $text = shift; | ||
| 265 | # | ||
| 266 | # According to Keith Bugg we need to \ escape \ {} [] # $ | ||
| 267 | # Experience with HCW on NT 4 shows that only \ {} need be escaped. | ||
| 268 | # | ||
| 269 | $text =~ s/(\\)/\\$1/g; | ||
| 270 | $text =~ s/(\{)/\\$1/g; | ||
| 271 | $text =~ s/(\})/\\$1/g; | ||
| 272 | return($text); | ||
| 273 | } | ||
| 274 | |||
| 275 | sub context_string { | ||
| 276 | my $text = shift; | ||
| 277 | $text =~ s/[^A-Za-z0-9_\.]//g; | ||
| 278 | if (length($text) > 255) { | ||
| 279 | return(substr($text,0,255)); | ||
| 280 | } | ||
| 281 | return($text); | ||
| 282 | } | ||
| 283 | |||
| 284 | 1; # happy package | ||
| 285 | |||
| 286 | |||
| 287 | =head1 NAME | ||
| 288 | |||
| 289 | Pod::Rtf | ||
| 290 | |||
| 291 | =head1 SYNOPSIS | ||
| 292 | |||
| 293 | $parser = new Pod::Rtf(); | ||
| 294 | $parser->parse_from_filehandle(\*STDIN) if (@ARGV == 0); | ||
| 295 | for (@ARGV) { | ||
| 296 | my $file = $_; | ||
| 297 | $file =~ s/\..*$//; | ||
| 298 | $Pod::Rtf::pound_note = $_; | ||
| 299 | $Pod::Rtf::dollar_note = $file; | ||
| 300 | $Pod::Rtf::K_note = $file; | ||
| 301 | $parser->parse_from_file($_); | ||
| 302 | } | ||
| 303 | |||
| 304 | =head1 DESCRIPTION | ||
| 305 | |||
| 306 | Uses Pod::Parser to convert pod documentation to Rich Text Format (rtf) | ||
| 307 | suitable for compilation by a Windows Help compiler. | ||
| 308 | |||
| 309 | |||
| 310 | =head1 SEE ALSO | ||
| 311 | |||
| 312 | L<perl>. L<Pod::Rtf>, L<Pod::Parser>. | ||
| 313 | |||
| 314 | =head1 AUTHOR | ||
| 315 | |||
| 316 | Peter Prymmer. | ||
| 317 | |||
| 318 | =cut | ||
| 319 | |||
| 320 |