blib/lib/Template/Plugin/Chump.pm

filter: Statementstmt 100% 37 / 37 Branchbran 66.6% 4 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 4 totaltotal 89.2% 50 / 56 CC 4 SCAR 5.1
CC
4
Cov
95%
init
3.0
filter
2.0
BEGIN
1.0
CRAP 1.7
↑ Summary
1 package Template::Plugin::Chump;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Text::Chump;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Template::Plugin::Filter;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use base qw(Template::Plugin::Filter);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use vars qw($VERSION $FILTER_NAME);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 $VERSION = '1.3';
10 6 $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 <html>
24 <body>
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 =[<stuff>]
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 </body>
64 </html>
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<Text::Chump> 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<Template::Plugin::Chump> brings that interface to B<Template::Toolkit>.
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<Template::Plugin::Chump> can handle the same options as B<Text::Chump>.
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<Text::Chump>
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 <simon@thegestalt.org>
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<Text::Chump>, L<Template::Plugin::Filter>
192  
193 =cut
194  
195  
196  
197 6 ▶sub init {
Subroutine init: called CC=3 SCAR=11.0
Pod undocumented
198 6 my ($self,@args) = @_;
199 6 ▶ my $config = (ref $args[-1] eq 'HASH')? pop @args : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $args[-1] eq 'HASH' ? : 6 0 6
200  
201 6 my $tc;
202  
203 6 ▶ if (defined $config->{chump}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config->{'chump'}) { } 1 5 6
204 1 $tc = $config->{chump};
205 } else {
206 5 $tc = Text::Chump->new($config);
207 }
208  
209
210  
211 6 $self->{_DYNAMIC} = 1;
212 6 $self->{_TC} = $tc;
213  
214  
215 6 $self->install_filter($FILTER_NAME);
216
217 6 return $self;
218  
219 }
220  
221 # possibly extraneous cargo culting but it works so ...
222 8 ▶sub filter {
Subroutine filter: called CC=2 SCAR=7.1
Pod undocumented
223 8 my ($self, $text, @args) = @_;
224 8 ▶ my $config = (ref $args[-1] eq 'HASH')? pop @args : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $args[-1] eq 'HASH' ? : 8 0 8
225 8 return $self->{_TC}->chump($text, $config);
226  
227 }
228  
229 # pass through methods to the same methods in Text::Chump
230 2 ▶sub new_type {
Subroutine new_type: called CC=1 SCAR=0.0
Pod undocumented
231 2 my $self = shift;
232 2 $self->{_TC}->new_type(@_);
233  
234 2 return;
235 }
236  
237 3 ▶sub install {
Subroutine install: called CC=1 SCAR=0.0
Pod undocumented
238 3 my $self = shift;
239 3 $self->{_TC}->install(@_);
240 3 return;
241 }
242  
243  
244 6 1;
245  
↑ Summary