File Coverage

blib/lib/Text/Decorator.pm
Criterion Covered Total %
statement 35 35 100.0
branch 3 4 75.0
condition n/a
subroutine 9 9 100.0
pod 4 4 100.0
total 51 52 98.0


line stmt bran cond sub pod time code
1             package Text::Decorator;
2              
3 5     5   117422 use 5.006;
  5         20  
  5         209  
4              
5 5     5   32 use strict;
  5         13  
  5         182  
6 5     5   40 use warnings;
  5         11  
  5         167  
7              
8 5     5   29 use Carp;
  5         10  
  5         603  
9 5     5   5820 use UNIVERSAL::require;
  5         10022  
  5         51  
10              
11             our $VERSION = '1.65';
12              
13             =head1 NAME
14              
15             Text::Decorator - Apply a chain of filters to text
16              
17             =head1 SYNOPSIS
18              
19             $self->new(...);
20             $self->format_as(...);
21              
22             =head1 DESCRIPTION
23              
24             Text::Decorator is a framework for marking up plain text into various
25             formats by applying a chain of filters. For instance, you might apply
26             a filter which will cause URIs in text to be presented as links if the
27             text is exported as HTML.
28              
29             =head1 METHODS
30              
31             =head2 new
32              
33             $self->new($text)
34              
35             Creates a new Text::Decorator instance.
36              
37             =head2 nodeclass
38              
39             The class we will use for our nodes. This defaults to
40             L, but if you want to subclass that to change
41             its behaviour, then you need to override this.
42              
43             =cut
44              
45 12     12 1 95 sub nodeclass { "Text::Decorator::Node" }
46              
47             sub new {
48 6     6 1 777 my ($class, $text) = @_;
49 6         31 $class->nodeclass->require;
50 6         99 return bless {
51             nodes => [ $class->nodeclass->new($text) ],
52             filters => [],
53             } => $class;
54             }
55              
56             =head2 add_filter
57              
58             $self->add_filter("EscapeHTML" => @arguments);
59              
60             This adds a new filter onto the queue of filters which will be applied
61             to this decorator; returns the decorator object.
62              
63             =cut
64              
65             sub add_filter {
66 8     8 1 74 my ($self, $filter, @args) = @_;
67 8 50       56 $filter = "Text::Decorator::Filter::$filter" unless $filter =~ /::/;
68 8 100       64 $filter->require or croak "Can't use filter $filter";
69 6         146 push @{ $self->{filters} }, { filter => $filter, args => [@args] };
  6         55  
70 6         22 return $self;
71             }
72              
73             =head2 format_as
74              
75             $self->format_as("html")
76              
77             Apply all the filters and return the text in the specified
78             representation. If the representation is unknown, plain text will be
79             returned.
80              
81             =cut
82              
83             sub format_as {
84 7     7 1 670 my ($self, $format) = @_;
85              
86             # Do the formatting stage; since we pull stuff off the stack, this
87             # is only done once.
88 7         12 while (my $filter = shift @{ $self->{filters} }) {
  13         67  
89 6         15 my ($filterclass, $args) = @{$filter}{qw(filter args)};
  6         17  
90 6         12 @{ $self->{nodes} } = $filterclass->filter($args, @{ $self->{nodes} });
  6         40  
  6         56  
91             }
92 7         14 return join "", map $_->format_as($format), @{ $self->{nodes} };
  7         35  
93             }
94              
95             =head1 AUTHOR
96              
97             Original author: Simon Cozens
98              
99             Current maintainer: Tony Bowden
100              
101             =head1 BUGS and QUERIES
102              
103             Please direct all correspondence regarding this module to:
104             bug-Text-Decorator@rt.cpan.org
105              
106             =head1 COPYRIGHT AND LICENSE
107              
108             Copyright (C) 2003-4 Simon Cozens, 2004-6 Tony Bowden
109              
110             This program is free software; you can redistribute it and/or modify it under
111             the terms of the GNU General Public License; either version 2 of the License,
112             or (at your option) any later version.
113              
114             This program is distributed in the hope that it will be useful, but WITHOUT
115             ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
116             FOR A PARTICULAR PURPOSE.
117              
118             =head1 SEE ALSO
119              
120             L, L,
121             L
122              
123             =cut
124              
125             1;