File: HtmlToText.pm

package info (click to toggle)
libtemplate-plugin-htmltotext-perl 0.03-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 76 kB
  • sloc: perl: 29; makefile: 2
file content (74 lines) | stat: -rw-r--r-- 1,650 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package Template::Plugin::HtmlToText;

use strict;
use vars qw( @ISA $VERSION );
use base qw( Template::Plugin );
use Template::Plugin;

$VERSION = '0.03';

sub new {
    my ($class, $context, $arg) = @_;
    $context->define_filter('html2text', [ \&html2text => 1 ]);
    return \&tt_wrap;
}

sub html2text {
    my ($context, $args) = @_;
    return sub {
        my $html = shift;
        return $html unless ($html =~ m#(<|>)#s);
        
        require HTML::TreeBuilder;
        my $tree = HTML::TreeBuilder->new->parse($html);
        require HTML::FormatText;
        my $formatter = HTML::FormatText->new(%{$args});
        my $text = $formatter->format($tree);
        return $text;
    }
}


1;

__END__

=head1 NAME

Template::Plugin::HtmlToText - Plugin interface to HTML::FormatText

=head1 SYNOPSIS

Quick summary of what the module does.

Perhaps a little code snippet.

    [% USE HtmlToText %]

    # use html2text FILTER to var 'myhtml' or 'myhtmltext'
    [% myhtml FILTER html2text(leftmargin => 0, rightmargin => 50) %]
    [% myhtmltext | html2text %]
    
    # not to a var, but to html code
    [% FILTER html2text %]
    <b>heavy</b>
    [% END %]
    [%# output is "heavy", no <b></b> %]

=head1 DESCRIPTION

This plugin provides an interface to the HTML::FormatText module which 
format HTML as plaintext.

=head1 AUTHOR

Fayland Lam, C<< <fayland> >>

=head1 COPYRIGHT & LICENSE

Copyright 2006 Fayland Lam, all rights reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut