| 1 | package EBook::Tools::EReader; | ||
| 2 | 1 | ▶ | use warnings; use strict; use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use version 0.74; our $VERSION = qv("0.5.2"); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | # Double-sigils are needed for lexical variables in clear print statements | ||
| 6 | ## no critic (Double-sigil dereference) | ||
| 7 | # Mixed case subs and the variable %record are inherited from Palm::PDB | ||
| 8 | ## no critic (ProhibitAmbiguousNames) | ||
| 9 | ## no critic (ProhibitMixedCaseSubs) | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | EBook::Tools::EReader - Palm::PDB handler for manipulating the Fictionwise/PeanutPress eReader format. | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | use EBook::Tools::EReader; | ||
| 18 | my $pdb = EBook::Tools::EReader->new(); | ||
| 19 | $pdb->Load('myfile-er.pdb'); | ||
| 20 | print "Loaded '",$pdb->{title},"' by ",$pdb->{author},"\n"; | ||
| 21 | my $html = $pdb->html; | ||
| 22 | my $pml = $pdb->pml | ||
| 23 | $pdb->write_unknown_records | ||
| 24 | |||
| 25 | =head1 DEPENDENCIES | ||
| 26 | |||
| 27 | =over | ||
| 28 | |||
| 29 | =item * C<Compress::Zlib> | ||
| 30 | |||
| 31 | =item * C<Image::Size> | ||
| 32 | |||
| 33 | =item * C<P5-Palm> | ||
| 34 | |||
| 35 | =back | ||
| 36 | |||
| 37 | =cut | ||
| 38 | |||
| 39 | |||
| 40 | require Exporter; | ||
| 41 | 1 | ▶ | use base qw(Exporter Palm::Raw); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 42 | |||
| 43 | our @EXPORT_OK; | ||
| 44 | @EXPORT_OK = qw ( | ||
| 45 | &cp1252_to_pml | ||
| 46 | &pml_to_html | ||
| 47 | ); | ||
| 48 | our %EXPORT_TAGS = ('all' => [@EXPORT_OK]); | ||
| 49 | |||
| 50 | sub import ## no critic (Always unpack @_ first) | ||
| 51 | 0 | ▶ | { |
|
Subroutine
import: not called CC=1 SCAR=6.9
| |||
| 52 | 0 | &Palm::PDB::RegisterPDBHandlers( __PACKAGE__, [ "PPrs", "PNRd" ], ); | |
| 53 | 0 | &Palm::PDB::RegisterPRCHandlers( __PACKAGE__, [ "PPrs", "PNRd" ], ); | |
| 54 | 0 | EBook::Tools::EReader->export_to_level(1, @_); | |
| 55 | 0 | return; | |
| 56 | } | ||
| 57 | |||
| 58 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 59 | 1 | ▶ | use Compress::Zlib; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 60 | 0 | ▶ | use EBook::Tools qw(debug split_metadata system_tidy_xhtml); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 61 | use EBook::Tools::PalmDoc qw(uncompress_palmdoc); | ||
| 62 | use Encode; | ||
| 63 | use Fcntl qw(SEEK_CUR SEEK_SET); | ||
| 64 | use File::Basename qw(dirname fileparse); | ||
| 65 | use File::Path; # Exports 'mkpath' and 'rmtree' | ||
| 66 | use HTML::TreeBuilder; | ||
| 67 | use Image::Size; | ||
| 68 | use List::Util qw(min); | ||
| 69 | use List::MoreUtils qw(uniq); | ||
| 70 | use Palm::PDB; | ||
| 71 | use Palm::Raw(); | ||
| 72 | |||
| 73 | |||
| 74 | ################################# | ||
| 75 | ########## CONSTRUCTOR ########## | ||
| 76 | ################################# | ||
| 77 | |||
| 78 | =head1 CONSTRUCTOR | ||
| 79 | |||
| 80 | =head2 C<new()> | ||
| 81 | |||
| 82 | Instantiates a new Ebook::Tools::EReader object. | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | sub new ## no critic (Always unpack @_ first) | ||
| 87 | { | ||
| 88 | my $class = shift; | ||
| 89 | my $self = $class->SUPER::new(@_); | ||
| 90 | |||
| 91 | $self->{'creator'} = 'PNRd'; | ||
| 92 | $self->{'type'} = 'PPrs'; | ||
| 93 | |||
| 94 | $self->{attributes}{resource} = 0; | ||
| 95 | |||
| 96 | $self->{appinfo} = undef; | ||
| 97 | $self->{sort} = undef; | ||
| 98 | $self->{records} = []; | ||
| 99 | |||
| 100 | $self->{header} = {}; | ||
| 101 | $self->{text} = ''; | ||
| 102 | |||
| 103 | $self->{title} = ''; | ||
| 104 | $self->{author} = ''; | ||
| 105 | $self->{rights} = ''; | ||
| 106 | $self->{publisher} = ''; | ||
| 107 | $self->{isbn} = ''; | ||
| 108 | |||
| 109 | return $self; | ||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | ###################################### | ||
| 114 | ########## ACCESSOR METHODS ########## | ||
| 115 | ###################################### | ||
| 116 | |||
| 117 | =head1 ACCESSOR METHODS | ||
| 118 | |||
| 119 | =head2 C<filebase> | ||
| 120 | |||
| 121 | In scalar context, this is the basename of the object attribute | ||
| 122 | C<filename>. In list context, it actually returns the basename, | ||
| 123 | directory, and extension as per C<fileparse> from L<File::Basename>. | ||
| 124 | |||
| 125 | =cut | ||
| 126 | |||
| 127 | sub filebase | ||
| 128 | { | ||
| 129 | my $self = shift; | ||
| 130 | return fileparse($$self{filename},'\.\w+$'); | ||
| 131 | } | ||
| 132 | |||
| 133 | |||
| 134 | =head2 C<footnotes()> | ||
| 135 | |||
| 136 | Returns a hash containing all of the footnotes found in the file, | ||
| 137 | where the keys are the footnote ids and the values contain the | ||
| 138 | footnote text. | ||
| 139 | |||
| 140 | =cut | ||
| 141 | |||
| 142 | sub footnotes | ||
| 143 | { | ||
| 144 | my $self = shift; | ||
| 145 | my $subname = ( caller(0) )[3]; | ||
| 146 | debug(2,"DEBUG[",$subname,"]"); | ||
| 147 | |||
| 148 | my %footnotehash; | ||
| 149 | my @footnoteids = (); | ||
| 150 | my @footnotes = (); | ||
| 151 | my $lastindex; | ||
| 152 | |||
| 153 | if(ref $self->{footnoteids} eq 'ARRAY' and @{$self->{footnoteids}}) | ||
| 154 | { | ||
| 155 | @footnoteids = @{$self->{footnoteids}}; | ||
| 156 | } | ||
| 157 | |||
| 158 | if(ref $self->{footnotes} eq 'ARRAY' and @{$self->{footnotes}}) | ||
| 159 | { | ||
| 160 | @footnotes = @{$self->{footnotes}}; | ||
| 161 | } | ||
| 162 | |||
| 163 | if($#footnotes != $#footnoteids) | ||
| 164 | { | ||
| 165 | carp($subname,"(): found ",scalar(@footnotes)," footnotes but ", | ||
| 166 | scalar(@footnoteids)," footnote ids\n"); | ||
| 167 | } | ||
| 168 | $lastindex = min($#footnotes, $#footnoteids); | ||
| 169 | |||
| 170 | foreach my $idx (0 .. $lastindex) | ||
| 171 | { | ||
| 172 | $footnotehash{$footnoteids[$idx]} = $footnotes[$idx]; | ||
| 173 | } | ||
| 174 | |||
| 175 | return %footnotehash; | ||
| 176 | } | ||
| 177 | |||
| 178 | |||
| 179 | =head2 C<footnotes_pml()> | ||
| 180 | |||
| 181 | Returns a string containing all of the footnotes in a form suitable to | ||
| 182 | append to the end of PML text output. This is called as part of | ||
| 183 | L</pml()>. | ||
| 184 | |||
| 185 | =cut | ||
| 186 | |||
| 187 | sub footnotes_pml | ||
| 188 | { | ||
| 189 | my $self = shift; | ||
| 190 | my $subname = ( caller(0) )[3]; | ||
| 191 | debug(2,"DEBUG[",$subname,"]"); | ||
| 192 | |||
| 193 | my %footnotehash = $self->footnotes; | ||
| 194 | my $text = ''; | ||
| 195 | |||
| 196 | foreach my $footnoteid (sort keys %footnotehash) | ||
| 197 | { | ||
| 198 | $text .= '<footnote id="' . $footnoteid . '">'; | ||
| 199 | $text .= $footnotehash{$footnoteid}; | ||
| 200 | $text .= "</footnote>\n\n"; | ||
| 201 | } | ||
| 202 | return $text; | ||
| 203 | } | ||
| 204 | |||
| 205 | |||
| 206 | =head2 C<footnotes_html()> | ||
| 207 | |||
| 208 | Returns a string containing all of the footnotes in a form suitable to | ||
| 209 | append to the end of HTML text output. This is called as part of | ||
| 210 | L</html()>. | ||
| 211 | |||
| 212 | =cut | ||
| 213 | |||
| 214 | sub footnotes_html | ||
| 215 | { | ||
| 216 | my $self = shift; | ||
| 217 | my $subname = ( caller(0) )[3]; | ||
| 218 | debug(2,"DEBUG[",$subname,"]"); | ||
| 219 | |||
| 220 | my %footnotehash = $self->footnotes; | ||
| 221 | my $text = '<h2 id="footnotes">Footnotes</h2>'; | ||
| 222 | $text .= "\n<dl>\n"; | ||
| 223 | |||
| 224 | foreach my $footnoteid (sort keys %footnotehash) | ||
| 225 | { | ||
| 226 | $text .= '<dt>[<a id="' . $footnoteid . '" href="#'; | ||
| 227 | $text .= $footnoteid . '-ref">' . $footnoteid; | ||
| 228 | $text .= "</a>]</dt>\n"; | ||
| 229 | |||
| 230 | $text .= '<dd>' . $footnotehash{$footnoteid} . "</dd>\n"; | ||
| 231 | } | ||
| 232 | $text .= "</dl>\n"; | ||
| 233 | $text = pml_to_html($text,$self->filebase); | ||
| 234 | return $text; | ||
| 235 | } | ||
| 236 | |||
| 237 | |||
| 238 | =head2 C<pml()> | ||
| 239 | |||
| 240 | Returns a string containing the entire original document text in its | ||
| 241 | original encoding, including all sidebars and footnotes. | ||
| 242 | |||
| 243 | =cut | ||
| 244 | |||
| 245 | sub pml | ||
| 246 | { | ||
| 247 | my $self = shift; | ||
| 248 | my $subname = ( caller(0) )[3]; | ||
| 249 | debug(2,"DEBUG[",$subname,"]"); | ||
| 250 | debug(2,"DEBUG: returning ",length($self->{text})," bytes of PML text"); | ||
| 251 | return $self->{text} . "\n" . $self->sidebars_pml . $self->footnotes_pml; | ||
| 252 | } | ||
| 253 | |||
| 254 | |||
| 255 | =head2 C<html()> | ||
| 256 | |||
| 257 | Returns a string containing the entire document text (including all | ||
| 258 | sidebars and footnotes) converted to HTML. | ||
| 259 | |||
| 260 | Note that the PML text is stored in the object (and thus retrieving it | ||
| 261 | is very fast), but generating the HTML output requires that the text | ||
| 262 | be converted every time this method is used, consuming extra | ||
| 263 | processing time. | ||
| 264 | |||
| 265 | =cut | ||
| 266 | |||
| 267 | sub html | ||
| 268 | { | ||
| 269 | my $self = shift; | ||
| 270 | my $subname = ( caller(0) )[3]; | ||
| 271 | debug(2,"DEBUG[",$subname,"]"); | ||
| 272 | |||
| 273 | my $header = <<"END"; | ||
| 274 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | ||
| 275 | "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | ||
| 276 | <html> | ||
| 277 | <head> | ||
| 278 | <title>$self->{title}</title> | ||
| 279 | </head> | ||
| 280 | <body> | ||
| 281 | END | ||
| 282 | my $footer = "</body>\n</html>\n"; | ||
| 283 | |||
| 284 | return | ||
| 285 | $EBook::Tools::utf8xmldec . $header | ||
| 286 | . pml_to_html($self->{text},$self->filebase) | ||
| 287 | . $self->sidebars_html . $self->footnotes_html . $footer; | ||
| 288 | } | ||
| 289 | |||
| 290 | |||
| 291 | =head2 C<sidebars()> | ||
| 292 | |||
| 293 | Returns a hash containing all of the sidebars found in the file, where | ||
| 294 | the keys are the sidebar ids and the values contain the sidebar text. | ||
| 295 | |||
| 296 | =cut | ||
| 297 | |||
| 298 | sub sidebars | ||
| 299 | { | ||
| 300 | my $self = shift; | ||
| 301 | my $subname = ( caller(0) )[3]; | ||
| 302 | debug(2,"DEBUG[",$subname,"]"); | ||
| 303 | |||
| 304 | my %sidebarhash; | ||
| 305 | my @sidebarids = (); | ||
| 306 | my @sidebars = (); | ||
| 307 | my $lastindex; | ||
| 308 | |||
| 309 | if(ref $self->{sidebarids} eq 'ARRAY' and @{$self->{sidebarids}}) | ||
| 310 | { | ||
| 311 | @sidebarids = @{$self->{sidebarids}}; | ||
| 312 | } | ||
| 313 | |||
| 314 | if(ref $self->{sidebars} eq 'ARRAY' and @{$self->{sidebars}}) | ||
| 315 | { | ||
| 316 | @sidebars = @{$self->{sidebars}}; | ||
| 317 | } | ||
| 318 | |||
| 319 | if($#sidebars != $#sidebarids) | ||
| 320 | { | ||
| 321 | carp($subname,"(): found ",scalar(@sidebars)," sidebars but ", | ||
| 322 | scalar(@sidebarids)," sidebar ids\n"); | ||
| 323 | } | ||
| 324 | $lastindex = min($#sidebars, $#sidebarids); | ||
| 325 | |||
| 326 | foreach my $idx (0 .. $lastindex) | ||
| 327 | { | ||
| 328 | $sidebarhash{$sidebarids[$idx]} = $sidebars[$idx]; | ||
| 329 | } | ||
| 330 | |||
| 331 | return %sidebarhash; | ||
| 332 | } | ||
| 333 | |||
| 334 | |||
| 335 | =head2 C<sidebars_pml()> | ||
| 336 | |||
| 337 | Returns a string containing all of the sidebars in a form suitable to | ||
| 338 | append to the end of PML text output. This is called as part of | ||
| 339 | L</pml()>. | ||
| 340 | |||
| 341 | =cut | ||
| 342 | |||
| 343 | sub sidebars_pml | ||
| 344 | { | ||
| 345 | my $self = shift; | ||
| 346 | my $subname = ( caller(0) )[3]; | ||
| 347 | debug(2,"DEBUG[",$subname,"]"); | ||
| 348 | |||
| 349 | my %sidebarhash = $self->sidebars; | ||
| 350 | my $text = ''; | ||
| 351 | |||
| 352 | foreach my $sidebarid (sort keys %sidebarhash) | ||
| 353 | { | ||
| 354 | $text .= '<sidebar id="' . $sidebarid . '">'; | ||
| 355 | $text .= $sidebarhash{$sidebarid}; | ||
| 356 | $text .= "</sidebar>\n\n"; | ||
| 357 | } | ||
| 358 | return $text; | ||
| 359 | } | ||
| 360 | |||
| 361 | |||
| 362 | =head2 C<sidebars_html()> | ||
| 363 | |||
| 364 | Returns a string containing all of the sidebars in a form suitable to | ||
| 365 | append to the end of HTML text output. This is called as part of | ||
| 366 | L</html()>. | ||
| 367 | |||
| 368 | =cut | ||
| 369 | |||
| 370 | sub sidebars_html | ||
| 371 | { | ||
| 372 | my $self = shift; | ||
| 373 | my $subname = ( caller(0) )[3]; | ||
| 374 | debug(2,"DEBUG[",$subname,"]"); | ||
| 375 | |||
| 376 | my %sidebarhash = $self->sidebars; | ||
| 377 | my $text = '<h2 id="sidebars">Sidebars</h2>'; | ||
| 378 | $text .= "\n<dl>\n"; | ||
| 379 | |||
| 380 | foreach my $sidebarid (sort keys %sidebarhash) | ||
| 381 | { | ||
| 382 | $text .= '<dt>[<a id="' . $sidebarid . '" href="#'; | ||
| 383 | $text .= $sidebarid . '-ref">' . $sidebarid; | ||
| 384 | $text .= "</a>]</dt>\n"; | ||
| 385 | |||
| 386 | $text .= '<dd>' . $sidebarhash{$sidebarid} . "</dd>\n"; | ||
| 387 | } | ||
| 388 | $text .= "</dl>\n"; | ||
| 389 | $text = pml_to_html($text,$self->filebase); | ||
| 390 | return $text; | ||
| 391 | } | ||
| 392 | |||
| 393 | |||
| 394 | =head2 C<write_html($filename)> | ||
| 395 | |||
| 396 | Writes the raw book text to disk in PML form (including all sidebars | ||
| 397 | and footnotes) with the given filename. | ||
| 398 | |||
| 399 | If C<$filename> is not specified, writes to C<< $self->filebase >> with | ||
| 400 | a ".html" extension. | ||
| 401 | |||
| 402 | Returns the filename used on success, or undef if there was no text to | ||
| 403 | write. | ||
| 404 | |||
| 405 | =cut | ||
| 406 | |||
| 407 | sub write_html :method | ||
| 408 | { | ||
| 409 | my $self = shift; | ||
| 410 | my $filename = shift; | ||
| 411 | my $subname = ( caller(0) )[3]; | ||
| 412 | debug(3,"DEBUG[",$subname,"]"); | ||
| 413 | |||
| 414 | $filename = $self->filebase . ".html" unless($filename); | ||
| 415 | return unless($$self{text}); | ||
| 416 | |||
| 417 | debug(1,"DEBUG: writing HTML text to '",$filename,"'"); | ||
| 418 | |||
| 419 | open(my $fh,'>:encoding(UTF-8)',$filename) | ||
| 420 | or croak($subname,"(): unable to open '",$filename,"' for writing!\n"); | ||
| 421 | print {*$fh} $self->html; | ||
| 422 | close($fh) | ||
| 423 | or croak($subname,"(): unable to close '",$filename,"'!\n"); | ||
| 424 | |||
| 425 | croak($subname,"(): failed to generate any text") | ||
| 426 | if(-z $filename); | ||
| 427 | |||
| 428 | return $filename; | ||
| 429 | } | ||
| 430 | |||
| 431 | |||
| 432 | =head2 C<write_images()> | ||
| 433 | |||
| 434 | Writes each image record to the disk. | ||
| 435 | |||
| 436 | Returns a list containing the filenames of all images written, or | ||
| 437 | undef if none were found. | ||
| 438 | |||
| 439 | =cut | ||
| 440 | |||
| 441 | sub write_images :method | ||
| 442 | { | ||
| 443 | my $self = shift; | ||
| 444 | my $subname = ( caller(0) )[3]; | ||
| 445 | debug(3,"DEBUG[",$subname,"]"); | ||
| 446 | |||
| 447 | return unless($self->{imagedata}); | ||
| 448 | my %imagedata = %{$self->{imagedata}}; | ||
| 449 | my $imagedir = $self->filebase . "_img"; | ||
| 450 | |||
| 451 | mkpath($imagedir); | ||
| 452 | |||
| 453 | foreach my $image (sort keys %imagedata) | ||
| 454 | { | ||
| 455 | debug(1,"Writing image '",$imagedir,"/",$image,"' [", | ||
| 456 | length(${$imagedata{$image}})," bytes]"); | ||
| 457 | open(my $fh,">:raw","$imagedir/$image") | ||
| 458 | or croak("Unable to open '$imagedir/$image' to write image\n"); | ||
| 459 | print {*$fh} ${$imagedata{$image}}; | ||
| 460 | close($fh) | ||
| 461 | or croak("Unable to close image file '$imagedir/$image'\n"); | ||
| 462 | } | ||
| 463 | return keys %imagedata; | ||
| 464 | } | ||
| 465 | |||
| 466 | |||
| 467 | =head2 C<write_pml($filename)> | ||
| 468 | |||
| 469 | Writes the raw book text to disk in PML form (including all sidebars | ||
| 470 | and footnotes) with the given filename. | ||
| 471 | |||
| 472 | If C<$filename> is not specified, writes to C<< $self->filebase >> with | ||
| 473 | a ".pml" extension. | ||
| 474 | |||
| 475 | Returns the filename used on success, or undef if there was no text to | ||
| 476 | write. | ||
| 477 | |||
| 478 | =cut | ||
| 479 | |||
| 480 | sub write_pml :method | ||
| 481 | { | ||
| 482 | my $self = shift; | ||
| 483 | my $filename = shift; | ||
| 484 | my $subname = ( caller(0) )[3]; | ||
| 485 | debug(3,"DEBUG[",$subname,"]"); | ||
| 486 | |||
| 487 | $filename = $self->filebase . ".pml" unless($filename); | ||
| 488 | return unless($$self{text}); | ||
| 489 | |||
| 490 | debug(1,"DEBUG: writing PML text to '",$filename,"'"); | ||
| 491 | |||
| 492 | open(my $fh,">:raw",$filename) | ||
| 493 | or croak($subname,"(): unable to open '",$filename,"' for writing!\n"); | ||
| 494 | print {*$fh} $self->pml; | ||
| 495 | close($fh) | ||
| 496 | or croak($subname,"(): unable to close '",$filename,"'!\n"); | ||
| 497 | |||
| 498 | croak($subname,"(): failed to generate any text") | ||
| 499 | if(-z $filename); | ||
| 500 | |||
| 501 | return $filename; | ||
| 502 | } | ||
| 503 | |||
| 504 | |||
| 505 | =head2 C<write_unknown_records()> | ||
| 506 | |||
| 507 | Writes each unidentified record to disk with a filename in the format of | ||
| 508 | 'raw-record-####', where #### is the record number (not the record ID). | ||
| 509 | |||
| 510 | Returns the number of records written. | ||
| 511 | |||
| 512 | =cut | ||
| 513 | |||
| 514 | sub write_unknown_records :method | ||
| 515 | { | ||
| 516 | my $self = shift; | ||
| 517 | my $subname = ( caller(0) )[3]; | ||
| 518 | debug(3,"DEBUG[",$subname,"]"); | ||
| 519 | |||
| 520 | my %unknowndata = %{$self->{unknowndata}}; | ||
| 521 | |||
| 522 | foreach my $rec (sort keys %unknowndata) | ||
| 523 | { | ||
| 524 | my $recstring = sprintf("%04d",$rec); | ||
| 525 | debug(1,"Dumping raw record ",$recstring); | ||
| 526 | my $rawname = "raw-record-" . $recstring; | ||
| 527 | open(my $fh,">:raw",$rawname) | ||
| 528 | or croak("Unable to open '",$rawname,"' to write raw record\n"); | ||
| 529 | print {*$fh} $$self{unknowndata}{$rec}; | ||
| 530 | close($fh) | ||
| 531 | or croak("Unable to close raw record file '",$rawname,"'\n"); | ||
| 532 | } | ||
| 533 | return scalar(keys %unknowndata); | ||
| 534 | } | ||
| 535 | |||
| 536 | |||
| 537 | ###################################### | ||
| 538 | ########## MODIFIER METHODS ########## | ||
| 539 | ###################################### | ||
| 540 | |||
| 541 | =head1 MODIFIER METHODS | ||
| 542 | |||
| 543 | =head2 C<Load($filename)> | ||
| 544 | |||
| 545 | Sets C<< $self->{filename} >> and then loads and parses the file specified | ||
| 546 | by C<$filename>, calling L</ParseRecord(%record)> on every record | ||
| 547 | found. | ||
| 548 | |||
| 549 | =cut | ||
| 550 | |||
| 551 | sub Load :method | ||
| 552 | { | ||
| 553 | my $self = shift; | ||
| 554 | my $filename = shift; | ||
| 555 | |||
| 556 | $self->{filename} = $filename; | ||
| 557 | return $self->SUPER::Load($filename); | ||
| 558 | } | ||
| 559 | |||
| 560 | |||
| 561 | =head2 C<ParseRecord(%record)> | ||
| 562 | |||
| 563 | Parses PDB records, updating the object attributes. This method is | ||
| 564 | called automatically on every database record during C<Load()>. | ||
| 565 | |||
| 566 | =cut | ||
| 567 | |||
| 568 | sub ParseRecord :method ## no critic (Always unpack @_ first) | ||
| 569 | { | ||
| 570 | ## The long if-elsif chain is the best logic for record number handling | ||
| 571 | ## no critic (Cascading if-elsif chain) | ||
| 572 | my $self = shift; | ||
| 573 | my %record = @_; | ||
| 574 | my $subname = ( caller(0) )[3]; | ||
| 575 | debug(3,"DEBUG[",$subname,"]"); | ||
| 576 | |||
| 577 | my $currentrecord = scalar @{$$self{records}}; | ||
| 578 | my $version = $self->{header}->{version}; | ||
| 579 | my $recordtext; | ||
| 580 | |||
| 581 | my $uncompress; # Coderef to decompression sub | ||
| 582 | |||
| 583 | if($currentrecord == 0) | ||
| 584 | { | ||
| 585 | $self->ParseRecord0($record{data}); | ||
| 586 | return \%record; | ||
| 587 | } | ||
| 588 | |||
| 589 | # Determine how to handle the remaining records | ||
| 590 | if($version == 2) | ||
| 591 | { | ||
| 592 | $uncompress = \&uncompress_palmdoc; | ||
| 593 | } | ||
| 594 | elsif($version == 10) | ||
| 595 | { | ||
| 596 | $uncompress = \&uncompress; | ||
| 597 | } | ||
| 598 | elsif($version > 255) | ||
| 599 | { | ||
| 600 | croak($subname,"(): eReader DRM not supported [version ", | ||
| 601 | $version,"]\n"); | ||
| 602 | } | ||
| 603 | else | ||
| 604 | { | ||
| 605 | croak($subname,"(): unknown eReader version: ",$version,"\n"); | ||
| 606 | } | ||
| 607 | |||
| 608 | |||
| 609 | # Start handling non-header records | ||
| 610 | if($currentrecord < $self->{header}->{nontextoffset}) | ||
| 611 | { | ||
| 612 | $recordtext = $uncompress->($record{data}); | ||
| 613 | $recordtext =~ s/\0//x; | ||
| 614 | if($recordtext) | ||
| 615 | { | ||
| 616 | $self->{text} .= $recordtext; | ||
| 617 | debug(3,"DEBUG: record ",$currentrecord," is text"); | ||
| 618 | } | ||
| 619 | else | ||
| 620 | { | ||
| 621 | debug(1,"DEBUG: record ",$currentrecord, | ||
| 622 | " could not be decompressed (", | ||
| 623 | length($record{data})," bytes)"); | ||
| 624 | $$self{unknowndata}{$currentrecord} = $record{data}; | ||
| 625 | } | ||
| 626 | } | ||
| 627 | elsif($currentrecord >= $self->{header}->{nontextoffset} | ||
| 628 | && $currentrecord < $self->{header}->{bookmarkoffset}) | ||
| 629 | { | ||
| 630 | $recordtext = uncompress($record{data}); | ||
| 631 | $recordtext = uncompress_palmdoc($record{data}) unless($recordtext); | ||
| 632 | if($recordtext) | ||
| 633 | { | ||
| 634 | $$self{unknowndata}{$currentrecord} = $recordtext; | ||
| 635 | debug(1,"DEBUG: record ",$currentrecord," contains ", | ||
| 636 | length($record{data})," bytes of unknown text"); | ||
| 637 | } | ||
| 638 | else | ||
| 639 | { | ||
| 640 | $$self{unknowndata}{$currentrecord} = $record{data}; | ||
| 641 | debug(1,"DEBUG: record ",$currentrecord," contains ", | ||
| 642 | length($record{data})," bytes of unknown data"); | ||
| 643 | } | ||
| 644 | } | ||
| 645 | elsif($currentrecord >= $self->{header}->{bookmarkoffset} | ||
| 646 | && $currentrecord < $self->{header}->{imagedataoffset}) | ||
| 647 | { | ||
| 648 | my @list = unpack('nn',$record{data}); | ||
| 649 | $recordtext = substr($record{data},4); | ||
| 650 | $recordtext =~ s/\0//gx; | ||
| 651 | |||
| 652 | debug(3,"DEBUG: record ",$currentrecord," is bookmark '", | ||
| 653 | $recordtext, | ||
| 654 | "' [",sprintf("unk=0x%04x offset=0x%04x",@list),"]"); | ||
| 655 | } | ||
| 656 | elsif($currentrecord >= $self->{header}->{imagedataoffset} | ||
| 657 | && $currentrecord < $self->{header}->{metadataoffset}) | ||
| 658 | { | ||
| 659 | my $pngoffset = index($record{data},"\x{89}PNG",5); | ||
| 660 | my $imagedata = substr($record{data},$pngoffset); | ||
| 661 | my ($imagetype,$imagename) = $record{data} =~ m/^ (\w+) \s (.*?)\0/x; | ||
| 662 | debug(1,"DEBUG: record ",$currentrecord," is ",$imagetype, | ||
| 663 | " image at offset ",$pngoffset,": '",$imagename,"'"); | ||
| 664 | $$self{imagedata}{$imagename} = \$imagedata; | ||
| 665 | } | ||
| 666 | elsif($currentrecord == $self->{header}->{metadataoffset}) | ||
| 667 | { | ||
| 668 | debug(1,"DEBUG: record ",$currentrecord," contains ", | ||
| 669 | length($record{data})," bytes of metadata"); | ||
| 670 | # The metadata record consists of five null-terminated | ||
| 671 | # strings | ||
| 672 | my @list = $record{data} =~ m/(.*?)\0/gx; | ||
| 673 | $self->{title} = $list[0]; | ||
| 674 | $self->{author} = $list[1]; | ||
| 675 | $self->{rights} = $list[2]; | ||
| 676 | $self->{publisher} = $list[3]; | ||
| 677 | $self->{isbn} = $list[4]; | ||
| 678 | } | ||
| 679 | elsif($self->{header}->{sidebarrecs} | ||
| 680 | && $currentrecord == $self->{header}->{sidebaroffset}) | ||
| 681 | { | ||
| 682 | my @sidebarids = $record{data} =~ m/(\w+)\0/gx; | ||
| 683 | $self->{sidebarids} = \@sidebarids; | ||
| 684 | debug(2,"DEBUG: record ",$currentrecord," has sidebar ids: '", | ||
| 685 | join("' '",@sidebarids),"'"); | ||
| 686 | } | ||
| 687 | elsif($self->{header}->{sidebarrecs} | ||
| 688 | && $currentrecord > $self->{header}->{sidebaroffset} | ||
| 689 | && $currentrecord < $self->{header}->{footnoteoffset}) | ||
| 690 | { | ||
| 691 | my @sidebars; | ||
| 692 | my @sidebarids; | ||
| 693 | |||
| 694 | if(ref $self->{sidebarids} eq 'ARRAY' and @{$self->{sidebarids}}) | ||
| 695 | { | ||
| 696 | @sidebarids = @{$self->{sidebarids}}; | ||
| 697 | } | ||
| 698 | else | ||
| 699 | { | ||
| 700 | carp($subname, | ||
| 701 | "(): adding a footnote, but no footnote IDs found\n"); | ||
| 702 | @sidebarids = []; | ||
| 703 | } | ||
| 704 | |||
| 705 | if(ref $self->{sidebars} eq 'ARRAY' and @{$self->{sidebars}}) | ||
| 706 | { | ||
| 707 | @sidebars = @{$self->{sidebars}}; | ||
| 708 | } | ||
| 709 | |||
| 710 | $recordtext = $uncompress->($record{data}); | ||
| 711 | if($recordtext) | ||
| 712 | { | ||
| 713 | $recordtext =~ s/\0//x; | ||
| 714 | chomp($recordtext); | ||
| 715 | push(@sidebars,$recordtext); | ||
| 716 | } | ||
| 717 | |||
| 718 | if( scalar(@sidebars) > scalar(@sidebarids) ) | ||
| 719 | { | ||
| 720 | carp($subname, | ||
| 721 | "(): sidebar ",scalar(@sidebars), | ||
| 722 | " has no associated ID\n"); | ||
| 723 | } | ||
| 724 | else | ||
| 725 | { | ||
| 726 | debug(2,"DEBUG: record ",$currentrecord," is sidebar '", | ||
| 727 | $sidebarids[$#sidebars],"'"); | ||
| 728 | } | ||
| 729 | |||
| 730 | $self->{sidebars} = \@sidebars; | ||
| 731 | } | ||
| 732 | elsif($self->{header}->{footnoterecs} | ||
| 733 | && $currentrecord == $self->{header}->{footnoteoffset}) | ||
| 734 | { | ||
| 735 | my @footnoteids = $record{data} =~ m/(\w+)\0/gx; | ||
| 736 | $self->{footnoteids} = \@footnoteids; | ||
| 737 | debug(2,"DEBUG: record ",$currentrecord," has footnote ids: '", | ||
| 738 | join("' '",@footnoteids),"'"); | ||
| 739 | } | ||
| 740 | elsif($self->{header}->{footnoterecs} | ||
| 741 | && $currentrecord > $self->{header}->{footnoteoffset} | ||
| 742 | && $currentrecord < $self->{header}->{lastdataoffset}) | ||
| 743 | { | ||
| 744 | my @footnotes; | ||
| 745 | my @footnoteids; | ||
| 746 | |||
| 747 | if(ref $self->{footnoteids} eq 'ARRAY' and @{$self->{footnoteids}}) | ||
| 748 | { | ||
| 749 | @footnoteids = @{$self->{footnoteids}}; | ||
| 750 | } | ||
| 751 | else | ||
| 752 | { | ||
| 753 | carp($subname, | ||
| 754 | "(): adding a footnote, but no footnote IDs found\n"); | ||
| 755 | @footnoteids = []; | ||
| 756 | } | ||
| 757 | |||
| 758 | if(ref $self->{footnotes} eq 'ARRAY' and @{$self->{footnotes}}) | ||
| 759 | { | ||
| 760 | @footnotes = @{$self->{footnotes}}; | ||
| 761 | } | ||
| 762 | |||
| 763 | $recordtext = $uncompress->($record{data}); | ||
| 764 | if($recordtext) | ||
| 765 | { | ||
| 766 | $recordtext =~ s/\0//x; | ||
| 767 | chomp($recordtext); | ||
| 768 | push(@footnotes,$recordtext); | ||
| 769 | } | ||
| 770 | |||
| 771 | if( scalar(@footnotes) > scalar(@footnoteids) ) | ||
| 772 | { | ||
| 773 | carp($subname, | ||
| 774 | "(): footnote ",scalar(@footnotes), | ||
| 775 | " has no associated ID\n"); | ||
| 776 | } | ||
| 777 | else | ||
| 778 | { | ||
| 779 | debug(2,"DEBUG: record ",$currentrecord," is footnote '", | ||
| 780 | $footnoteids[$#footnotes],"'"); | ||
| 781 | } | ||
| 782 | |||
| 783 | $self->{footnotes} = \@footnotes; | ||
| 784 | } | ||
| 785 | else | ||
| 786 | { | ||
| 787 | my ($imagex,$imagey,$imagetype) = imgsize(\$record{data}); | ||
| 788 | $recordtext = uncompress($record{data}); | ||
| 789 | $recordtext = uncompress_palmdoc($record{data}) unless($recordtext); | ||
| 790 | if(defined($imagex) && $imagetype) | ||
| 791 | { | ||
| 792 | debug(1,"DEBUG: record ",$currentrecord," is image"); | ||
| 793 | $$self{unknowndata}{$currentrecord} = $record{data}; | ||
| 794 | } | ||
| 795 | elsif($recordtext) | ||
| 796 | { | ||
| 797 | debug(1,"DEBUG: record ",$currentrecord," has extra text:"); | ||
| 798 | debug(1," '",$recordtext,"'"); | ||
| 799 | $$self{unknowndata}{$currentrecord} = $recordtext; | ||
| 800 | } | ||
| 801 | else | ||
| 802 | { | ||
| 803 | debug(1,"DEBUG: record ",$currentrecord," is unknown (", | ||
| 804 | length($record{data})," bytes)"); | ||
| 805 | $$self{unknowndata}{$currentrecord} = $record{data}; | ||
| 806 | } | ||
| 807 | } | ||
| 808 | return \%record; | ||
| 809 | } | ||
| 810 | |||
| 811 | |||
| 812 | =head2 C<ParseRecord0($data)> | ||
| 813 | |||
| 814 | Parses the header record and places the parsed values into the hashref | ||
| 815 | C<< $self->{header} >>. | ||
| 816 | |||
| 817 | Returns the hash (not the hashref). | ||
| 818 | |||
| 819 | =cut | ||
| 820 | |||
| 821 | sub ParseRecord0 :method | ||
| 822 | { | ||
| 823 | my $self = shift; | ||
| 824 | my $data = shift; | ||
| 825 | my $subname = ( caller(0) )[3]; | ||
| 826 | debug(2,"DEBUG[",$subname,"]"); | ||
| 827 | |||
| 828 | my $version; # EReader version | ||
| 829 | # Expected values are: | ||
| 830 | # 02 - PalmDoc Compression | ||
| 831 | # 10 - Inflate Compression | ||
| 832 | # >255 - data is in Record 1 | ||
| 833 | my $headerdata; # used for holding temporary data segments | ||
| 834 | my $offset; | ||
| 835 | my %header; | ||
| 836 | my @list; | ||
| 837 | |||
| 838 | debug(1,"DEBUG: EReader Record 0 is ",length($data)," bytes"); | ||
| 839 | $headerdata = substr($data,0,16); | ||
| 840 | @list = unpack('nnNnnnn',$headerdata); | ||
| 841 | $header{version} = $list[0]; # Bytes 0-1 | ||
| 842 | $header{unknown2} = $list[1]; # Bytes 2-3 | ||
| 843 | $header{unknown4} = $list[2]; # Bytes 4-7 | ||
| 844 | $header{unknown8} = $list[3]; # Bytes 8-9 | ||
| 845 | $header{unknown10} = $list[4]; # Bytes 10-11 | ||
| 846 | $header{nontextoffset} = $list[5]; # Bytes 12-13 | ||
| 847 | $header{nontextoffset2} = $list[5]; # Bytes 14-15 | ||
| 848 | |||
| 849 | $headerdata = substr($data,16,16); | ||
| 850 | @list = unpack('nnNnnnn',$headerdata); | ||
| 851 | $header{unknown16} = $list[0]; | ||
| 852 | $header{unknown18} = $list[1]; | ||
| 853 | $header{unknown20} = $list[2]; | ||
| 854 | $header{unknown22} = $list[3]; | ||
| 855 | $header{unknown24} = $list[4]; | ||
| 856 | $header{footnoterecs} = $list[5]; | ||
| 857 | $header{sidebarrecs} = $list[6]; | ||
| 858 | |||
| 859 | $headerdata = substr($data,32,24); | ||
| 860 | @list = unpack('nnnnnnnnnnnn',$headerdata); | ||
| 861 | $header{bookmarkoffset} = $list[0]; | ||
| 862 | $header{unknown34} = $list[1]; | ||
| 863 | $header{nontextoffset3} = $list[2]; | ||
| 864 | $header{unknown38} = $list[3]; | ||
| 865 | $header{imagedataoffset} = $list[4]; | ||
| 866 | $header{imagedataoffset2} = $list[5]; | ||
| 867 | $header{metadataoffset} = $list[6]; | ||
| 868 | $header{metadataoffset2} = $list[7]; | ||
| 869 | $header{footnoteoffset} = $list[8]; | ||
| 870 | $header{sidebaroffset} = $list[9]; | ||
| 871 | $header{lastdataoffset} = $list[10]; | ||
| 872 | $header{unknown54} = $list[11]; | ||
| 873 | |||
| 874 | # If the footnoteoffset and sidebarrec are the same, only one or the | ||
| 875 | # other exists, and there's no way to tell which. | ||
| 876 | |||
| 877 | $offset = 60; | ||
| 878 | while($headerdata = substr($data,$offset,4)) | ||
| 879 | { | ||
| 880 | @list = unpack('nn',$headerdata); | ||
| 881 | debug(2,"DEBUG: offset ",$offset,"=",sprintf("0x%04x",$list[0]), | ||
| 882 | " offset ",$offset+2,"=",sprintf("0x%04x",$list[1])) | ||
| 883 | if($list[0] || $list[1]); | ||
| 884 | $offset += 4; | ||
| 885 | } | ||
| 886 | |||
| 887 | foreach my $key (sort keys %header) | ||
| 888 | { | ||
| 889 | debug(2,'DEBUG: ereader{',$key,'}=0x',sprintf("%04x",$header{$key})); | ||
| 890 | # if($header{$key}); | ||
| 891 | } | ||
| 892 | |||
| 893 | $$self{header} = \%header; | ||
| 894 | return %header; | ||
| 895 | } | ||
| 896 | |||
| 897 | |||
| 898 | ################################ | ||
| 899 | ########## PROCEDURES ########## | ||
| 900 | ################################ | ||
| 901 | |||
| 902 | =head1 PROCEDURES | ||
| 903 | |||
| 904 | =head2 C<cp1252_to_pml()> | ||
| 905 | |||
| 906 | An unfinished and completely nonfunctional procedure to convert | ||
| 907 | Windows-1252 characters to PML \a codes. | ||
| 908 | |||
| 909 | DO NOT USE. | ||
| 910 | |||
| 911 | =cut | ||
| 912 | |||
| 913 | sub cp1252_to_pml | ||
| 914 | { | ||
| 915 | my $text = shift; | ||
| 916 | my $subname = ( caller(0) )[3]; | ||
| 917 | debug(2,"DEBUG[",$subname,"]"); | ||
| 918 | |||
| 919 | return unless(defined $text); | ||
| 920 | |||
| 921 | my %cppml = ( | ||
| 922 | "\x{92}" => '\a146', | ||
| 923 | ); | ||
| 924 | |||
| 925 | foreach my $char (keys %cppml) | ||
| 926 | { | ||
| 927 | $text =~ s/$char/$cppml{$char}/gx; | ||
| 928 | } | ||
| 929 | return $text; | ||
| 930 | } | ||
| 931 | |||
| 932 | |||
| 933 | =head2 C<pml_to_html($text,$filebase)> | ||
| 934 | |||
| 935 | Takes as input a text string in Windows-1252 encoding containing PML | ||
| 936 | markup codes and returns a string with those codes converted to UTF-8 | ||
| 937 | HTML. | ||
| 938 | |||
| 939 | Requires a second argument C<$filebase> to specify the basename of the | ||
| 940 | file (or specifically, the basename of the file to which output text | ||
| 941 | will be written) so that image links can be generated correctly. | ||
| 942 | |||
| 943 | =cut | ||
| 944 | |||
| 945 | sub pml_to_html | ||
| 946 | { | ||
| 947 | my $text = shift; | ||
| 948 | my $filebase = shift; | ||
| 949 | my $subname = ( caller(0) )[3]; | ||
| 950 | debug(2,"DEBUG[",$subname,"]"); | ||
| 951 | |||
| 952 | return unless(defined $text); | ||
| 953 | $text = decode('Windows-1252',$text); | ||
| 954 | |||
| 955 | # Font markers can be terminated by a \n as well as their own | ||
| 956 | # code. | ||
| 957 | $text =~ s#\\l (.*?) (?:\\l|\\n) | ||
| 958 | #<div style="font-size: 120%">$1</div>#gsx; | ||
| 959 | $text =~ s#\\n (.*?) \\n | ||
| 960 | #<div style="font-size: 100%">$1</div>#gsx; | ||
| 961 | $text =~ s#\\s (.*?) (?:\\s|\\n) | ||
| 962 | #<div style="font-size: 80%">$1</div>#gsx; | ||
| 963 | |||
| 964 | my %pmlcodes = ( | ||
| 965 | '\p' => '<br style="page-break-after: always" />\n', | ||
| 966 | '\x' => [ '<h1 style="page-break-before: always">','</h1>\n' ], | ||
| 967 | '\X0' => [ '<h1>','</h1>\n' ], | ||
| 968 | '\X1' => [ '<h2>','</h2>\n' ], | ||
| 969 | '\X2' => [ '<h3>','</h3>\n' ], | ||
| 970 | '\X3' => [ '<h4>','</h4>\n' ], | ||
| 971 | '\X4' => [ '<h5>','</h5>\n' ], | ||
| 972 | '\C0=' => '<div class="C0"></div>', | ||
| 973 | '\C1=' => '<div class="C1"></div>', | ||
| 974 | '\C2=' => '<div class="C2"></div>', | ||
| 975 | '\C3=' => '<div class="C3"></div>', | ||
| 976 | '\C4=' => '<div class="C4"></div>', | ||
| 977 | '\c' => [ '<div style="text-align: center">','</div>' ], | ||
| 978 | '\r' => [ '<div style="text-align: right">','</div>' ], | ||
| 979 | '\i' => [ '<em>','</em>' ], | ||
| 980 | '\u' => [ '<ul>','</ul>' ], | ||
| 981 | '\o' => [ '<strike>','</strike>' ], | ||
| 982 | '\v' => [ '<!-- ',' -->' ], | ||
| 983 | '\t' => [ '<ul>','</ul>' ], | ||
| 984 | '\T=' => '', | ||
| 985 | '\b' => [ '<b>','</b>' ], | ||
| 986 | '\B' => [ '<strong>','</strong>' ], | ||
| 987 | '\Sb' => [ '<sub>','</sub>' ], | ||
| 988 | '\Sp' => [ '<sup>','</sup>' ], | ||
| 989 | # font-variant: small-caps is very badly supported on most | ||
| 990 | # browsers, so using a workaround. | ||
| 991 | # '\k' => [ '<div style="font-variant: small-caps">','</div>' ], | ||
| 992 | '\k' => [ '<div style="font-size: smaller; text-transform: uppercase;">', | ||
| 993 | '</div>' ], | ||
| 994 | "\\\\" => "\\", | ||
| 995 | '\-' => '', | ||
| 996 | '\I' => [ '<div class="refindex">','</div>' ], | ||
| 997 | ); | ||
| 998 | |||
| 999 | # Convert newlines followed by at least two spaces to <p> | ||
| 1000 | $text =~ s#\n(\s{2,})(.*?)(?=\n) | ||
| 1001 | #<p>$2</p>#gosx; | ||
| 1002 | |||
| 1003 | # Convert remaining newlines to <br /> | ||
| 1004 | $text =~ s#\n(.*?)\n | ||
| 1005 | #\n<br />$1<br />\n#gsx; | ||
| 1006 | |||
| 1007 | # Reinsert newlines on <p> and <br> tags | ||
| 1008 | $text =~ s#(<p>)#\n$1#g; | ||
| 1009 | $text =~ s#(<br />)#$1\n#g; | ||
| 1010 | |||
| 1011 | |||
| 1012 | # Handle simple tag replacements | ||
| 1013 | while(my ($pmlcode,$replacement) = each(%pmlcodes) ) | ||
| 1014 | { | ||
| 1015 | if(ref $replacement eq 'ARRAY') | ||
| 1016 | { | ||
| 1017 | if($pmlcode =~ / = $/x) | ||
| 1018 | { | ||
| 1019 | # This doesn't work? Need to rewrite for RHS eval? | ||
| 1020 | $pmlcode =~ s/= $//x; | ||
| 1021 | $text =~ s#\Q$pmlcode\E | ||
| 1022 | ="(.*?)" (.*?) \Q $pmlcode \E | ||
| 1023 | #$replacement->[0]$2$replacement->[1]#gsx; | ||
| 1024 | } | ||
| 1025 | else | ||
| 1026 | { | ||
| 1027 | $text =~ s#\Q$pmlcode\E (.*?) \Q$pmlcode\E | ||
| 1028 | #$replacement->[0]$1$replacement->[1]#gsx; | ||
| 1029 | } | ||
| 1030 | } | ||
| 1031 | else | ||
| 1032 | { | ||
| 1033 | if($pmlcode =~ / = $/x) | ||
| 1034 | { | ||
| 1035 | $text =~ s#\Q$pmlcode\E "(.*?)" | ||
| 1036 | #$replacement#gsx; | ||
| 1037 | } | ||
| 1038 | else | ||
| 1039 | { | ||
| 1040 | $text =~ s#\Q$pmlcode\E | ||
| 1041 | #$replacement#gsx; | ||
| 1042 | } | ||
| 1043 | |||
| 1044 | } | ||
| 1045 | } # while(my ($pmlcode,$replacement) = each(%pmlcodes) ) | ||
| 1046 | |||
| 1047 | # Strip leftover \n codes | ||
| 1048 | $text =~ s#\\n | ||
| 1049 | ##gsx; | ||
| 1050 | |||
| 1051 | # Horizontal rules | ||
| 1052 | $text =~ s!\\w="(.*?)" | ||
| 1053 | !<hr width="$1" />!gsx; | ||
| 1054 | |||
| 1055 | # Images | ||
| 1056 | $text =~ s!\\m="(.*?)" | ||
| 1057 | !<img src="${filebase}_img/$1" />!gsx; | ||
| 1058 | |||
| 1059 | # Anchors and references | ||
| 1060 | $text =~ s!\\q="(.*?)"(.*?)\\q | ||
| 1061 | !<a href="#$1">$2</a>!gsx; | ||
| 1062 | $text =~ s!\\Q="(.*?)" | ||
| 1063 | !<a id="$1"></a>!gsx; | ||
| 1064 | |||
| 1065 | # Footnotes and sidebars | ||
| 1066 | $text =~ s!\\Fn="(.*?)"(.*?)\\Fn | ||
| 1067 | !<a id="$1-ref" href="#$1">$2</a>!gsx; | ||
| 1068 | $text =~ s!\\Sd="(.*?)"(.*?)\\Sd | ||
| 1069 | !<a id="$1-ref" href="#$1">$2</a>!gsx; | ||
| 1070 | |||
| 1071 | # Double-newlines after page breaks | ||
| 1072 | $text =~ s#("page-break-after: always" />)#$1\n\n#g; | ||
| 1073 | |||
| 1074 | return $text; | ||
| 1075 | } | ||
| 1076 | |||
| 1077 | ########## END CODE ########## | ||
| 1078 | |||
| 1079 | =head1 BUGS AND LIMITATIONS | ||
| 1080 | |||
| 1081 | =over | ||
| 1082 | |||
| 1083 | =item * HTML conversion doesn't handle handle the \T command used to | ||
| 1084 | indent. | ||
| 1085 | |||
| 1086 | =item * HTML conversion may be suboptimal in many ways. | ||
| 1087 | |||
| 1088 | Most notably, most linebreaks are handled as <br />, and without any | ||
| 1089 | heed to whether those linebreaks occur inside of some other element. | ||
| 1090 | Validation is extremely unlikely. | ||
| 1091 | |||
| 1092 | =back | ||
| 1093 | |||
| 1094 | =head1 AUTHOR | ||
| 1095 | |||
| 1096 | Zed Pobre <zed@debian.org> | ||
| 1097 | |||
| 1098 | =head1 LICENSE AND COPYRIGHT | ||
| 1099 | |||
| 1100 | Copyright 2008 Zed Pobre | ||
| 1101 | |||
| 1102 | Licensed to the public under the terms of the GNU GPL, version 2 | ||
| 1103 | |||
| 1104 | =cut | ||
| 1105 | |||
| 1106 | 1; | ||