home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 10 / AU_CD10.iso / Updates / Perl / Perl_Libs / site_perl / HTML / FormatText.pm < prev    next >
Text File  |  1997-10-12  |  4KB  |  237 lines

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