| 1 | package Text::Decorator; | ||||||||||
| 2 | |||||||||||
| 3 | 6 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 6 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 6 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 6 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 6 | ▶ | use UNIVERSAL::require; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 6 | 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<Text::Decorator::Node>, but if you want to subclass that to change | ||||||||||
| 41 | its behaviour, then you need to override this. | ||||||||||
| 42 | |||||||||||
| 43 | =cut | ||||||||||
| 44 | |||||||||||
| 45 | 12 | ▶ | sub nodeclass { "Text::Decorator::Node" } | ||||||||
|
Subroutine
nodeclass: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 46 | |||||||||||
| 47 | 6 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 48 | 6 | my ($class, $text) = @_; | |||||||||
| 49 | 6 | $class->nodeclass->require; | |||||||||
| 50 | 6 | 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 | 8 | ▶ | sub add_filter { | ||||||||
|
Subroutine
add_filter: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 66 | 8 | my ($self, $filter, @args) = @_; | |||||||||
| 67 | 8 | ▶ | $filter = "Text::Decorator::Filter::$filter" unless $filter =~ /::/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 8 | ▶ | $filter->require or croak "Can't use filter $filter"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 6 | push @{ $self->{filters} }, { filter => $filter, args => [@args] }; | |||||||||
| 70 | 6 | 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 | 7 | ▶ | sub format_as { | ||||||||
|
Subroutine
format_as: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 84 | 7 | my ($self, $format) = @_; | |||||||||
| 85 | |||||||||||
| 86 | # Do the formatting stage; since we pull stuff off the stack, this | ||||||||||
| 87 | # is only done once. | ||||||||||
| 88 | 7 | ▶ | while (my $filter = shift @{ $self->{filters} }) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 6 | my ($filterclass, $args) = @{$filter}{qw(filter args)}; | |||||||||
| 90 | 6 | @{ $self->{nodes} } = $filterclass->filter($args, @{ $self->{nodes} }); | |||||||||
| 91 | } | ||||||||||
| 92 | 7 | return join "", map $_->format_as($format), @{ $self->{nodes} }; | |||||||||
| 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<Text::Decorator::Filter>, L<Text::Decorator::Node>, | ||||||||||
| 121 | L<Text::Decorator::Group> | ||||||||||
| 122 | |||||||||||
| 123 | =cut | ||||||||||
| 124 | |||||||||||
| 125 | 6 | 1; | |||||||||