File Coverage

blib/lib/Template/Plugin/Chump.pm
Criterion Covered Total %
statement 4 6 66.6
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 8 75.0


line stmt bran cond sub pod time code
1             package Template::Plugin::Chump;
2              
3 6     6   168202 use strict;
  6         13  
  6         220  
4 6     6   9352 use Text::Chump;
  0            
  0            
5             use Template::Plugin::Filter;
6             use base qw(Template::Plugin::Filter);
7             use vars qw($VERSION $FILTER_NAME);
8              
9             $VERSION = '1.3';
10             $FILTER_NAME = 'chump';
11              
12              
13             =pod
14              
15             =head1 NAME
16              
17             Template::Plugin::Chump - provides a Template Toolkit filter for Chump like syntax
18              
19             =head1 SYNOPSIS
20              
21             [% USE Chump %]
22              
23            
24            
25              
26             [% FILTER chump %]
27              
28             This will get turned into a clickable link
29             http://something.com
30              
31             This will turn the word 'foo' into a link to bar.com
32             [foo|http://bar.com]
33              
34             This will get turned into an image link
35             +[http://foo.com/quux.jpg]
36              
37             This will get turned into an inline image with the label 'bar'
38             +[bar|http://foo.com/quux.jpg]
39              
40             This will get turned into an image with the label 'bar' which
41             is a link to http://foobar.com
42             +[bar|http://foobar.com|http://url.of.image.com/image.jpg]
43              
44              
45             [% END %]
46              
47             And this will do what you expect
48             [% somevar FILTER chump %]
49              
50             [% Chump.new_type('equal','=', subref,'rege+xp') %]
51             [% FILTER chump %]
52             =[foo|regeeeeexp]
53             [% END %]
54             the subroutine subref will now be called for all links
55             of the format =[]
56              
57             [% FILTER chump( {links=>0}) %]
58             links won't be parsed so this ...
59             [foo|http://bar.com]
60             will remain as is
61             [% END %]
62            
63            
64            
65              
66             Alternatively you can pass in a Text::Chump object and use that.
67              
68              
69             [% USE Chump({ chump => my_chump_object }) %]
70              
71              
72             =head1 DESCRIPTION
73              
74             Chump is a simplified markup language that allows for simple markup of
75             text to include urls, links and inline images.
76              
77             Chump is based on an original idea by Bijan Parsia who wrote a bot named DiaWebLogBot in Squeak
78             for the Monkeyfist Daily Churn. Subsequently Useful Inc. "stole all his good ideas" for their own
79             IRC bot.
80              
81             The bot is available from http://usefulinc.com/chump/ and the original page that uses this form of
82             markup is http://pants.heddley.com
83              
84             From there the syntax was adopted by various other projects where more
85             complex solutions such as Text::WikiFormat or HTML::FromText aren't
86             needed or don't have the right features and an extensible parser was
87             written because that's what good little programmers do.
88              
89             B is a surprisingly (too the author if not anyone else),
90             flexible and powerful Chump parser. From humble beginnings it has evolved
91             to allow installable handlers and new types. All with a nice simple interface.
92              
93             B brings that interface to B.
94              
95             It's probably useful for allowing users to enter text which is HTML safe
96             (i.e no nasty cross site scripting bugs) but still allows them to provide
97             links and inline images.
98              
99             =head1 OPTIONS
100              
101             B can handle the same options as B.
102              
103             Just pass them in either when loading the plugin or filtering with it.
104              
105             [% USE Chump({ urls=>0 }) %]
106              
107             [% FILTER chump({ urls=>1 }) %]
108              
109             some text
110            
111             [% END %]
112              
113             Like that.
114              
115              
116             You can also call the new_type and install methods from Text::Chump
117             using
118              
119             [% Chump.install(...) %]
120              
121             or
122              
123             [% Chump.new_type(...) %]
124              
125             the code ref you pass in must be a variable in the vars hash ref you
126             pass into Template::Toolkit. Remember, because sub refs get called
127             automatically you need something which returns a sub ref.
128              
129             So, in your CGI script you need something like
130              
131             $vars->{uc} = sub { return sub { return uc $_[1] } };
132              
133             # stuff
134              
135             $tt->process($template, $vars);
136              
137             And then in your template you do
138              
139             [% Chump.install('equal', '=', uc) %]
140             [% FILTER chump %]
141             =[foo]
142             This will give FOO
143             [% END %]
144              
145              
146             Que convenient :)
147              
148             Alternatively you can pass in a fully formed B
149             object as a template var and use that instead inorder to more
150             cleanly seperate code from presentation.
151              
152             sub uc { return uc $_[1] };
153              
154             my $tc = Text::Chump->new();
155             $tc->install('link',\&uc);
156             $tc->new_type('equal','=',\&uc);
157            
158             $tt->process($template, { my_chump => $tc });
159              
160              
161             And then in the template
162              
163             [% USE Chump ( { chump => my_chump } ) %]
164              
165             [% FILTER chump %]
166             =[foo]
167             This will give FOO
168             [% END %]
169              
170              
171              
172             =head1 AUTHOR
173              
174             Simon Wistow
175              
176             =head1 COPYING
177              
178             (C)opyright 2003, Simon Wistow
179              
180             Distributed under the same terms as Perl itself.
181              
182             This software is under no warranty whatsoever and will probably ruin your life,
183             kill your friends, burn down your house and bring about the apocalypse.
184              
185             =head1 BUGS
186              
187             None known.
188              
189             =head1 SEE ALSO
190              
191             L, L
192              
193             =cut
194              
195              
196              
197             sub init {
198             my ($self,@args) = @_;
199             my $config = (ref $args[-1] eq 'HASH')? pop @args : {};
200              
201             my $tc;
202              
203             if (defined $config->{chump}) {
204             $tc = $config->{chump};
205             } else {
206             $tc = Text::Chump->new($config);
207             }
208              
209            
210              
211             $self->{_DYNAMIC} = 1;
212             $self->{_TC} = $tc;
213              
214              
215             $self->install_filter($FILTER_NAME);
216            
217             return $self;
218              
219             }
220              
221             # possibly extraneous cargo culting but it works so ...
222             sub filter {
223             my ($self, $text, @args) = @_;
224             my $config = (ref $args[-1] eq 'HASH')? pop @args : {};
225             return $self->{_TC}->chump($text, $config);
226              
227             }
228              
229             # pass through methods to the same methods in Text::Chump
230             sub new_type {
231             my $self = shift;
232             $self->{_TC}->new_type(@_);
233              
234             return;
235             }
236              
237             sub install {
238             my $self = shift;
239             $self->{_TC}->install(@_);
240             return;
241             }
242              
243              
244             1;
245