home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / perl5 / HTML / FormatText.pm < prev    next >
Encoding:
Perl POD Document  |  2004-06-02  |  4.8 KB  |  263 lines

  1.  
  2. require 5;
  3. package HTML::FormatText;
  4.  
  5. =head1 NAME
  6.  
  7. HTML::FormatText - Format HTML as plaintext
  8.  
  9. =head1 SYNOPSIS
  10.  
  11.  require HTML::TreeBuilder;
  12.  $tree = HTML::TreeBuilder->new->parse_file("test.html");
  13.  
  14.  require HTML::FormatText;
  15.  $formatter = HTML::FormatText->new(leftmargin => 0, rightmargin => 50);
  16.  print $formatter->format($tree);
  17.  
  18. =head1 DESCRIPTION
  19.  
  20. The HTML::FormatText is a formatter that outputs plain latin1 text.
  21. All character attributes (bold/italic/underline) are ignored.
  22. Formatting of HTML tables and forms is not implemented.
  23.  
  24. You might specify the following parameters when constructing the
  25. formatter:
  26.  
  27. =over 4
  28.  
  29. =item I<leftmargin> (alias I<lm>)
  30.  
  31. The column of the left margin. The default is 3.
  32.  
  33. =item I<rightmargin> (alias I<rm>)
  34.  
  35. The column of the right margin. The default is 72.
  36.  
  37. =back
  38.  
  39. =head1 SEE ALSO
  40.  
  41. L<HTML::Formatter>
  42.  
  43. =head1 COPYRIGHT
  44.  
  45. Copyright (c) 1995-2002 Gisle Aas, and 2002- Sean M. Burke. All rights
  46. reserved.
  47.  
  48. This library is free software; you can redistribute it and/or
  49. modify it under the same terms as Perl itself.
  50.  
  51. This program is distributed in the hope that it will be useful, but
  52. without any warranty; without even the implied warranty of
  53. merchantability or fitness for a particular purpose.
  54.  
  55.  
  56. =head1 AUTHOR
  57.  
  58. Current maintainer: Sean M. Burke <sburke@cpan.org>
  59.  
  60. Original author: Gisle Aas <gisle@aas.no>
  61.  
  62.  
  63. =cut
  64.  
  65. use strict;
  66. use vars qw(@ISA $VERSION);
  67.  
  68. use HTML::Formatter ();
  69. BEGIN { *DEBUG = \&HTML::Formatter::DEBUG unless defined &DEBUG }
  70.  
  71. @ISA = qw(HTML::Formatter);
  72.  
  73. $VERSION = sprintf("%d.%02d", q$Revision: 2.04 $ =~ /(\d+)\.(\d+)/);
  74.  
  75.  
  76. sub default_values
  77. {
  78.     (
  79.      shift->SUPER::default_values(),
  80.      lm =>  3, # left margin
  81.      rm => 72, # right margin (actually, maximum text width)
  82.     );
  83. }
  84.  
  85. sub configure
  86. {
  87.     my($self,$hash) = @_;
  88.     my $lm = $self->{lm};
  89.     my $rm = $self->{rm};
  90.  
  91.     $lm = delete $hash->{lm} if exists $hash->{lm};
  92.     $lm = delete $hash->{leftmargin} if exists $hash->{leftmargin};
  93.     $rm = delete $hash->{rm} if exists $hash->{rm};
  94.     $rm = delete $hash->{rightmargin} if exists $hash->{rightmargin};
  95.  
  96.     my $width = $rm - $lm;
  97.     if ($width < 1) {
  98.     warn "Bad margins, ignored" if $^W;
  99.     return;
  100.     }
  101.     if ($width < 20) {
  102.     warn "Page probably too narrow" if $^W;
  103.     }
  104.  
  105.     for (keys %$hash) {
  106.     warn "Unknown configure option '$_'" if $^W;
  107.     }
  108.  
  109.     $self->{lm} = $lm;
  110.     $self->{rm} = $rm;
  111.     $self;
  112. }
  113.  
  114.  
  115. sub begin
  116. {
  117.     my $self = shift;
  118.     $self->HTML::Formatter::begin;
  119.     $self->{curpos} = 0;  # current output position.
  120.     $self->{maxpos} = 0;  # highest value of $pos (used by header underliner)
  121.     $self->{hspace} = 0;  # horizontal space pending flag
  122. }
  123.  
  124.  
  125. sub end
  126. {
  127.     shift->collect("\n");
  128. }
  129.  
  130.  
  131. sub header_start
  132. {
  133.     my($self, $level, $node) = @_;
  134.     $self->vspace(1 + (6-$level) * 0.4);
  135.     $self->{maxpos} = 0;
  136.     1;
  137. }
  138.  
  139. sub header_end
  140. {
  141.     my($self, $level, $node) = @_;
  142.     if ($level <= 2) {
  143.     my $line;
  144.     $line = '=' if $level == 1;
  145.     $line = '-' if $level == 2;
  146.     $self->vspace(0);
  147.     $self->out($line x ($self->{maxpos} - $self->{lm}));
  148.     }
  149.     $self->vspace(1);
  150.     1;
  151. }
  152.  
  153. sub bullet {
  154.   my $self = shift;
  155.   $self->SUPER::bullet($_[0] . ' ');
  156. }
  157.  
  158.  
  159. sub hr_start
  160. {
  161.     my $self = shift;
  162.     $self->vspace(1);
  163.     $self->out('-' x ($self->{rm} - $self->{lm}));
  164.     $self->vspace(1);
  165. }
  166.  
  167.  
  168. sub pre_out
  169. {
  170.     my $self = shift;
  171.     # should really handle bold/italic etc.
  172.     if (defined $self->{vspace}) {
  173.     if ($self->{out}) {
  174.         $self->nl() while $self->{vspace}-- >= 0;
  175.         $self->{vspace} = undef;
  176.     }
  177.     }
  178.     my $indent = ' ' x $self->{lm};
  179.     my $pre = shift;
  180.     $pre =~ s/^/$indent/mg;
  181.     $self->collect($pre);
  182.     $self->{out}++;
  183. }
  184.  
  185.  
  186. sub out
  187. {
  188.     my $self = shift;
  189.     my $text = shift;
  190.  
  191.     $text =~ tr/\xA0\xAD/ /d;
  192.  
  193.     if ($text =~ /^\s*$/) {
  194.     $self->{hspace} = 1;
  195.     return;
  196.     }
  197.  
  198.     if (defined $self->{vspace}) {
  199.     if ($self->{out}) {
  200.         $self->nl while $self->{vspace}-- >= 0;
  201.         }
  202.     $self->goto_lm;
  203.     $self->{vspace} = undef;
  204.     $self->{hspace} = 0;
  205.     }
  206.  
  207.     if ($self->{hspace}) {
  208.     if ($self->{curpos} + length($text) > $self->{rm}) {
  209.         # word will not fit on line; do a line break
  210.         $self->nl;
  211.         $self->goto_lm;
  212.     } else {
  213.         # word fits on line; use a space
  214.         $self->collect(' ');
  215.         ++$self->{curpos};
  216.     }
  217.     $self->{hspace} = 0;
  218.     }
  219.  
  220.     $self->collect($text);
  221.     my $pos = $self->{curpos} += length $text;
  222.     $self->{maxpos} = $pos if $self->{maxpos} < $pos;
  223.     $self->{'out'}++;
  224. }
  225.  
  226.  
  227. sub goto_lm
  228. {
  229.     my $self = shift;
  230.     my $pos = $self->{curpos};
  231.     my $lm  = $self->{lm};
  232.     if ($pos < $lm) {
  233.     $self->{curpos} = $lm;
  234.     $self->collect(" " x ($lm - $pos));
  235.     }
  236. }
  237.  
  238.  
  239. sub nl
  240. {
  241.     my $self = shift;
  242.     $self->{'out'}++;
  243.     $self->{curpos} = 0;
  244.     $self->collect("\n");
  245. }
  246.  
  247.  
  248. sub adjust_lm
  249. {
  250.     my $self = shift;
  251.     $self->{lm} += $_[0];
  252.     $self->goto_lm;
  253. }
  254.  
  255.  
  256. sub adjust_rm
  257. {
  258.     shift->{rm} += $_[0];
  259. }
  260.  
  261. 1;
  262.  
  263.