blib/lib/Text/Chump.pm

filter: Statementstmt 95.1% 158 / 166 Branchbran 89.0% 57 / 64 Conditioncond 66.6% 32 / 48 MC/DCmcdc 40.7% 11 / 27 Subroutinesub 96.1% 25 / 26 Podpod 100% 4 / 4 totaltotal 85.6% 287 / 335 CC 58 SCAR 20.1
CC
58
Cov
89%
chump
18.1
_make_link
7.2
_chump_image
7.1
CRAP 7.4
↑ Summary
1 package Text::Chump;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Text::DelimMatch;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Tie::IxHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use URI::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 $VERSION = "1.02";
11  
12  
13  
14 =pod
15  
16 =head1 NAME
17  
18 Text::Chump - a module for parsing Chump like syntax
19  
20  
21 =head1 SYNOPSIS
22  
23 use Text::Chump;
24  
25 my $tc = Text::Chump->new();
26  
27 $tc->chump('[all mine!|http://thegestalt.org]');
28 # returns <a href='http://thegestalt.org'>all mine!</a>
29  
30 $tc->chump('+[all mine!|http://thegestalt.org]');
31 # returns <img src='http://thegestalt.org' alt='all mine!'>
32  
33 $tc->chump('http://thegestalt.org');
34 # returns <a href='http://thegestalt.org'>http;//thegestalt.org</a>
35  
36 my $tc = Text::Chump->new({images=>0});
37  
38 $tc->chump('+[all mine!|http://thegestalt.org]');
39 # returns '+[all mine!|http://thegestalt.org]'
40  
41  
42 sub foo {
43 my ($url, $label) = @_;
44  
45 return "$label ($url)";
46 }
47  
48 $tc->install('link',\&foo);
49 $tc->chump('[foo|http://bar.com]');
50 # returns 'foo (http://bar.com)'
51  
52 sub quirka {
53 my ($opts, $match, $label) = @_;
54  
55 return "<a href="blog.cgi?entry=$match">$label</a>";
56  
57 }
58  
59  
60  
61 $tc->install('link',\$quirka,'\d+');
62 $tc->chump('[stuff|4444]');
63 # returns "<a href="blog.cgi?entry=4444">stuff</a>"
64  
65  
66  
67 =head1 DESCRIPTION
68  
69 Chump is an IRC bot that allows people to post links and comments
70 onto a website from within an IRC Channel. Some people call this a blog
71 but I hate that term. Hate it. *HATE IT*! ... *cough* ... so I'll avoid
72 it from now on.
73  
74 The Chump is based on an original idea by Bijan Parsia. Bijan wrote a bot in Squeak
75 called DiaWebLogBot, which powers the Monkeyfist Daily Churn and subsequently
76 Useful Inc. "stole all his good ideas". Therefore The Chump syntax is derived
77 and extended from diaweblogbot.
78  
79 The bot is available from B<http://usefulinc.com/chump/> and the original page
80 that uses this form of markup is B<http://pants.heddley.com>.
81  
82 The items which are displayed on the page can have a special format. These,
83 in turn get marked up as HTML (by default). Essentially this provides a simple
84 markup language. Yes - they could have used XML and been fully buzzword compliant
85 (it uses XML in the backend if that's any help) but they didn't.
86  
87 Since then the syntax has been appropriated by a number of projects including one
88 of my own, so, like the good little code that I am, it all went in a module.
89  
90 Which I may as well release because somebody else wants to release a module which
91 depends on it and it might be useful to someone else.
92  
93 Alternatives to this module include B<Text::WikiFormat> and B<HTML::FromText>
94 although they do subtly different things. In fact you could probably chain them
95 together - especially B<HTML::FromText> with uri set to 0.
96  
97 =head1 SYNTAX
98  
99 As described here
100  
101 B<http://usefulinc.com/chump/MANUAL.txt>
102  
103  
104  
105 =over 4
106  
107 =item * Links :
108  
109 [<text>|url]
110  
111 This creates an inline link (i.e. turning a word into a link). So, for example
112  
113 They also have [another site|http://foobar.com]
114  
115 will make the words "another site" appear as a hyperlink to
116 the URL http://foobar.com.
117  
118 =item * Images :
119  
120 +[http://url.of.image.com/image.jpg]
121  
122 This creates an inline image in some text. By providing some text you can provide
123 an alt tag which is considered a good thing to do.
124  
125 +[This is the alt text|http://url.of.image.com/image.jpg]
126  
127 By providing a url in the middle
128  
129 +[This is the alt text|http://foobar.com|http://url.of.image.com/image.jpg]
130  
131 You can turn the image into a clickable link.
132  
133  
134 =item * Urls :
135  
136 http://foobar.com
137  
138 this will be turned into a clicable link.
139  
140 =back
141  
142  
143 =head1 METHODS
144  
145 =head2 new <opts>
146  
147 Can take an hashref of options (target defaults to nothing, border defaults to 0,
148 everything else defaults to 1 == yes)
149  
150 =over 4
151  
152 =item * target :
153  
154 A default target for a URL (such as _blank)
155  
156 =item * border :
157  
158 Whether inline images should have a border
159  
160 =item * images :
161  
162 Whether to process image markup
163  
164 =item * links :
165  
166 Whether to process link markup
167  
168 =item * urls :
169  
170 Whether to process urls
171  
172 =back
173  
174  
175 =cut
176  
177  
178 # standard set up stuff
179 8 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
180 8 my $class = shift;
181 8 ▶ my $self = shift || {};
Condition100%2/2
shift() || {}
l!l
62
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
182  
183 8 $self->{plugins} = {};
184 $self->{types} = {
185 8 'link' => 'link',
186 '+' => 'image',
187 'url' => 'url',
188 };
189  
190  
191 8 bless $self, $class;
192  
193 # we'll be macthing between '[' and ']'
194 8 $self->{_mc} = $self->_make_matcher();
195  
196 # Default handlers.
197 8 ▶ $self->install('link', sub { $self->_chump_link(@_) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
198 8 ▶ $self->install('image', sub { $self->_chump_image(@_) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
199 8 ▶ $self->install('url', sub { $self->_chump_url(@_) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
200  
201 8 return $self;
202  
203 }
204  
205  
206 =pod
207  
208 =head2 new_type [name] [char] [coderef] <regexp>
209  
210 Installs a new type so that if the parser comes across
211  
212 $char[stuff|nonsense]
213  
214 then the parts will be passed to the coderef in the
215 normal way. If you pass in a regexp then that will be
216 used to determine the match, just like if you install a
217 new handler.
218  
219 In order to turn off handling of the new type pass in
220  
221 $opt->{"${name}s"} = 0;
222  
223 as the options to I<chump()>. So
224  
225 my $text = 'foo bar %[foo|http://quux.com]';
226  
227 $mc->new_type('percent','%', sub { return $_[1] });
228 $mc->chump($text);
229
230 returns
231  
232 'foo bar http://quux.com'
233  
234 but
235  
236 my $text = 'foo bar %[foo|http://quux.com]';
237  
238 $mc->new_type('percent','%', sub { return $_[1] }, 'foo');
239 $mc->chump($text);
240  
241 returns
242  
243 'foo bar foo'
244  
245 but
246  
247 my $text = 'foo bar %[foo|http://quux.com]';
248  
249 $mc->new_type('percent','%', sub { return $_[1] }, 'foo');
250 $mc->chump($text, { 'percents' => 0 });
251  
252 returns
253  
254 'foo bar %[foo|http://quux.com]'
255  
256  
257 So that's all clear then :)
258  
259  
260 =cut
261  
262  
263 0 ▶sub new_type {
Subroutine new_type: not called CC=1 SCAR=6.9
Pod documented
264 0 my ($self, $name, $char, $code, $regexp) = @_;
265  
266  
267 0 $self->{types}->{$char} = $name;
268 0 $self->{_mc} = $self->_make_matcher();
269 0 $self->{"${name}s"} = 1;
270 0 $self->install($name, $code, $regexp);
271  
272 }
273  
274  
275 8 ▶sub _make_matcher {
Subroutine _make_matcher: called CC=4 SCAR=13.9
276 8 my ($self) = @_;
277  
278 8 my $regexp = "";
279 8 foreach my $key (keys %{$self->{types}}) {
280 24 ▶ next if length $key != 1;
Branch: 1 branch
BranchTrueFalseTotal
if length $key != 1 16 8 24
281 8 ▶ next if $key =~ m!^[a-z\d]$!m;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^[a-z\d]$/m 0 8 8
282 8 $regexp .= '\\'.$key;
283 }
284  
285  
286 8 return Text::DelimMatch->new("[$regexp]{0,1}\\[","\\]");
287  
288  
289 }
290  
291  
292 =pod
293  
294 =head2 chump [text]
295  
296 Takes some text to munge and returns it, fully chumped. Can optionally take
297 a hashref with the same options as I<new> except that these options will only
298 apply to this bit of text.
299  
300 =cut
301  
302 # the real work
303 53 ▶sub chump {
Subroutine chump: called CC=18 SCAR=29.0
Pod documented
304 # get the text, remembering that we may not actually be passed anything
305 53 my $self = shift;
306 53 ▶ my $text = shift || "";
Condition50%1/2
shift() || ''
l!l
530
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
307 53 ▶ my $opts = shift || {};
Condition100%2/2
shift() || {}
l!l
3221
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
308  
309  
310  
311 # set up options
312 53 ▶ my $border = (defined $self->{border})? $self->{border} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'border'} ? : 12 41 53
313 53 ▶ $opts->{border} = $border unless defined $opts->{border};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opts->{"border"} 28 25 53
314 53 ▶ $opts->{border} = "border='$opts->{border}'" unless $opts->{border} =~ /border/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"border"} =~ /border/i 43 10 53
315 # (urgh)
316  
317  
318  
319 53 foreach my $val (values %{$self->{types}})
320 {
321 159 ▶ my $tmp = (defined $self->{"${val}s"})? $self->{"${val}s"} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $$self{"${val}s"} ? : 12 147 159
322 159 ▶ $opts->{"${val}s"} = $tmp unless defined $opts->{"${val}s"};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$opts{"${val}s"} 83 76 159
323 }
324  
325  
326  
327 # curse the tedious URI::Find interface
328 $self->{_finder} = URI::Find->new(
329 53 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
330 53 my($uri, $orig_uri) = @_;
331 53 return $self->_make_link($uri,$orig_uri,$opts);
332 },
333 53 );
334  
335  
336  
337 # get all our tokens
338 53 my @tokens = $self->_get_tokens($text);
339  
340  
341  
342 # pre declare
343 53 my $return;
344  
345 # for each token we've got, decide ...
346 53 TOKEN: foreach my $token (@tokens) {
347  
348 151 my $orig = $token;
349  
350 # is it a bracket match? and if so is it an image ...
351 151 ▶ if ($token =~ s/^([^\[]{0,1})\[(.*)\]$/$2/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ s/^([^\[]{0,1})\[(.*)\]$/$2/) { } 49 102 151
352  
353 49 ▶ my $type = $1 || 'link';
Condition100%2/2
$1 || 'link'
l!l
3019
MC/DC100%1/1
$1 || 'link'
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
354  
355 49 my $typename = $self->{types}->{$type};
356
357 49 ▶ unless (defined $opts->{"${typename}s"} && $opts->{"${typename}s"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$opts{"${typename}s"} and $$opts{"${typename}s"}) 4 45 49
Condition66%2/3
defined $$opts{"${typename}s"} and $$opts{"${typename}s"}
!ll&&!rl&&r
0445
MC/DC50%1/2
defined $$opts{"${typename}s"} and $$opts{"${typename}s"}
defined $$opts{"${typename}s"} $$opts{"${typename}s"}
Truth table66%2/3
A and B
A= defined $$opts{"${typename}s"}B= $$opts{"${typename}s"}
ABresult
0X0
100
111
358 4 $return .= $orig;
359 4 next TOKEN;
360 }
361  
362
363 45 my @parts = split /\|/, $token, 3;
364
365 # check to see if there's a user defined regexp
366 45 ▶ if (my $tmp = $self->_do_regexp_plugins($typename, $opts,@parts)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tmp = $self->_do_regexp_plugins($typename, $opts, @parts)) 4 41 45
367 4 $return .= $tmp;
368 4 next TOKEN;
369 }
370  
371 # stick it back on
372 # $return .= $type unless (defined $typename);
373  
374 # if not then work out which one is the image url,
375 # the label and the optional link url
376 41 ▶ my ($url, $label, $link) = $self->_order_params(sub { $self->_is_url($_[0]) }, @parts);
Subroutine __ANON__: called CC=1 SCAR=0.0
377  
378 # check to see if there's a user defined regexp
379 41 ▶ if (my $tmp = $self->_do_normal_plugins($typename, $opts, $url, $label, $link)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tmp = $self->_do_normal_plugins($typename, $opts, $url, $label, $link)) 36 5 41
380 36 $return .= $tmp;
381 36 next TOKEN;
382 }
383  
384 # otherwise return the original
385 5 $return .= $orig;
386  
387 # otherwise it's plain text
388 } else {
389 # check to see if there's a user defined regexp
390 102 ▶ if (my $tmp = $self->_do_regexp_plugins('url', $opts, $orig)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tmp = $self->_do_regexp_plugins("url", $opts, $orig)) 0 102 102
391 0 $return .= $tmp;
392 0 next TOKEN;
393 }
394
395 # check to see if there's a user defined regexp
396 102 ▶ if (my $tmp = $self->_do_normal_plugins('url', $opts, $orig)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tmp = $self->_do_normal_plugins("url", $opts, $orig)) 18 84 102
397 18 $return .= $tmp;
398 18 next TOKEN;
399 }
400
401 84 $return .= $orig;
402  
403 }
404 }
405
406 # return the whole caboodle
407 53 return $return;
408 }
409  
410 =head2 install [type] [coderef] <regexp>
411  
412 if you pass in either 'image', 'link' or 'url' and a valid coderef
413 then that code ref will be called on the original sting instead of the
414 default behaviour.
415  
416 This is useful for outputting something other than HTML.
417  
418 And, in a special, one time only offer, if optionally you pass in
419 a regexp then you can add your own handlers. So, for example, if you
420 did :
421  
422 $tc->install('link', sub { return 'foo' }, '\d{4}');
423 print $tc->chump('[test|1234]'); # prints "foo"
424  
425 However you regexps are checked in reverse order they're put in so if
426 you then do :
427  
428 $tc->install('link', sub { return 'bar' }, '\d{5}');
429  
430 then :
431  
432 print $tc->chump('[test|1234]'); # prints "foo"
433 print $tc->chump('[test|12345]'); # prints "bar"
434  
435  
436 Note: all regexps are assumed to be case insensitive.
437  
438 If you want to monkey around with the ordering post install then the IxHash
439 object that they're installed in can be found in
440  
441 $tc->{plugins}->{[name]}->{regexp}
442  
443  
444  
445 For a link or and image the values passed to the coderef are a hashref of
446 options then the match then the label and then optionally a middle value.
447  
448 If no label is passed then it will be set to the same value as the link.
449  
450 So for these
451  
452 [foo|bar|http://thegestalt.org]
453 [http://thegestaltorg|bar|foo]
454  
455 a sub will be passed
456  
457 my ($opt, $link, $label, $middle) = @_;
458
459 # $opt = hashref of options
460 # $link = http://thegestalt.org
461 # $label = foo
462 # $middle = bar
463  
464  
465 and for
466  
467 [http://thegestalt.org]
468  
469 you'll get
470  
471 # $opt = hashref of options
472 # $link = http://thegestalt.org
473 # $label = http://thegestalt.org
474 # $middle = undef
475  
476  
477  
478 For a url you'll only get passed an opt and the original string.
479  
480  
481  
482 =cut
483  
484 32 ▶sub install {
Subroutine install: called CC=6 SCAR=19.2
Pod documented
485  
486 32 ▶ my $self = shift || carp "Must be called in an OO manner\n";
Condition33%1/3
shift() || carp("Must be called in an OO manner\n")
l!l&&r!l&&!r
3200
MC/DC0%0/2
shift() || carp("Must be called in an OO manner\n")
shift() carp("Must be called in an OO manner\n")
Truth table33%1/3
A || B
A= shift()B= carp("Must be called in an OO manner\n")
ABresult
1X1
011
000
487 32 ▶ my $name = shift || carp "Must pass a name\n";
Condition33%1/3
shift() || carp("Must pass a name\n")
l!l&&r!l&&!r
3200
MC/DC0%0/2
shift() || carp("Must pass a name\n")
shift() carp("Must pass a name\n")
Truth table33%1/3
A || B
A= shift()B= carp("Must pass a name\n")
ABresult
1X1
011
000
488 32 ▶ my $code = shift || carp "Must pass a coderef\n";
Condition33%1/3
shift() || carp("Must pass a coderef\n")
l!l&&r!l&&!r
3200
MC/DC0%0/2
shift() || carp("Must pass a coderef\n")
shift() carp("Must pass a coderef\n")
Truth table33%1/3
A || B
A= shift()B= carp("Must pass a coderef\n")
ABresult
1X1
011
000
489 32 my $regexp = shift;
490  
491  
492 32 ▶ if (defined $regexp) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $regexp) { } 5 27 32
493 $self->{plugins}->{$name}->{regexp} = Tie::IxHash->new()
494 5 ▶ unless defined $self->{plugins}->{$name}->{regexp};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"plugins"}{$name}{"regexp"} 3 2 5
495  
496 5 $self->{plugins}->{$name}->{regexp}->Unshift($regexp => $code);
497 } else {
498 27 $self->{plugins}->{$name}->{default} = $code;
499 }
500 }
501  
502  
503  
504  
505 sub _get_tokens
506 53 ▶{
Subroutine _get_tokens: called CC=5 SCAR=16.1
507 53 my $self = shift;
508 53 ▶ my $text = shift || "";
Condition50%1/2
shift() || ''
l!l
530
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
509  
510  
511 # we'll be matching stuff between '[' and ']'
512 53 my $mc = $self->{_mc};
513  
514 # pre declare
515 53 my @tokens;
516  
517 # loop through all the matches
518 # Why isn't this a standard method in Text::DelimMatch?
519 # And if it is then why is it badly documented?
520 53 ▶ while (my $match = $mc->match($text))
Branch: 1 branch
BranchTrueFalseTotal
if (my $match = $mc->match($text)) 49 53 102
521 {
522 # if we've got anything from before the match then whack it in
523 49 ▶ my $pre = $mc->pre_matched() || "";
Condition100%2/2
$mc->pre_matched || ''
l!l
841
MC/DC100%1/1
$mc->pre_matched || ''
$mc->pre_matched
Truth table100%2/2
A
A= $mc->pre_matched
Aresult
11
00
524 49 push @tokens, $pre;
525  
526 # push the match in
527 49 push @tokens, $match;
528  
529 # and reset $text so that we don't loop infinitely
530 49 ▶ $text = $mc->post_matched() || "";
Condition100%2/2
$mc->post_matched || ''
l!l
643
MC/DC100%1/1
$mc->post_matched || ''
$mc->post_matched
Truth table100%2/2
A
A= $mc->post_matched
Aresult
11
00
531 }
532 # push anything left onto the tokens. This also catches the case
533 # of there being no matches
534 53 push @tokens, $text;
535  
536 53 return @tokens;
537  
538 }
539  
540  
541  
542 =pod
543  
544 =head2 _order_params [function] [@params]
545  
546 Given a function and an array of params it will return the first
547 parameter that matches the function.
548  
549 The order that it checks in is last element of the array and then
550 the first element.
551  
552 Why this weird order? Because it's more natural to write
553  
554 [foo|http://bar.com]
555  
556 or, at least, that seems to be the behaviour I've observed.
557  
558 A typical function would look like this
559  
560  
561 sub {
562 return $_[0] =~ /\d+/;
563 }
564  
565  
566 =cut
567  
568 sub _order_params
569 73 ▶{
Subroutine _order_params: called CC=4 SCAR=13.9
570 73 my ($self, $function,@parts) = @_;
571
572 73 ▶ return unless @parts;
Branch: 1 branch
BranchTrueFalseTotal
unless @parts 0 73 73
573
574 73 my $one = shift @parts;
575 73 my $two = pop @parts;
576
577  
578 73 my ($first, $second);
579  
580 73 ▶ if ($function->($one)) {
Branch: 2 branches
BranchTrueFalseTotal
if (&$function($one)) { } 21 52 73
elsif (&$function($two)) { } 19 33 52
581 21 $first = $one;
582 21 $second = $two;
583 } elsif ($function->($two)) {
584 19 $first = $two;
585 19 $second = $one;
586 } else {
587 33 return undef;
588 }
589  
590 40 return ($first, $second, @parts);
591 }
592  
593  
594 sub _do_regexp_plugins
595 147 ▶{
Subroutine _do_regexp_plugins: called CC=6 SCAR=18.0
596 147 my ($self, $type, $opts, @parts) = @_;
597  
598 147 ▶ return undef unless defined $self->{plugins}->{$type}->{regexp};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"plugins"}{$type}{"regexp"} 122 25 147
599  
600 25 foreach my $re ($self->{plugins}->{$type}->{regexp}->Keys())
601 {
602 32 ▶ my ($a, $b, $c) = $self->_order_params(sub { return $_[0] =~ m!$re!i }, @parts );
Subroutine __ANON__: called CC=1 SCAR=0.0
603 32 ▶ next unless defined $a;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a 28 4 32
604 4 ▶ $b = $a unless defined $b;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b 0 4 4
605
606
607 4 my $tmp;
608 4 eval {
609 4 $tmp = $self->{plugins}->{$type}->{regexp}->FETCH($re)->($opts, $a, $b, $c);
610 };
611 4 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 4 0 4
612 4 return $tmp;
613 }
614 }
615
616 21 return undef;
617  
618  
619 }
620  
621  
622  
623 143 ▶sub _do_normal_plugins {
Subroutine _do_normal_plugins: called CC=5 SCAR=16.3
624 143 my ($self, $type, $opts, $a, $b, $c) = @_;
625  
626  
627  
628 143 ▶ return undef unless defined $a;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a 5 138 143
629 138 ▶ return undef unless defined $self->{plugins}->{$type}->{default};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"plugins"}{$type}{"default"} 0 138 138
630
631  
632  
633  
634  
635 138 ▶ $b = $a unless defined $b;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b 111 27 138
636 138 my $tmp;
637 138 eval {
638 138 $tmp = $self->{plugins}->{$type}->{default}($opts, $a, $b, $c);
639 };
640 138 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 138 0 138
641 138 return $tmp;
642 }
643  
644 0 return undef;
645 }
646  
647  
648 =pod
649  
650 =head2 _chump_link [opts] [url] [labe]
651  
652 Just incase you want to call this from your own plugin,
653 this is the default action for links.
654  
655 Calls, I<_make_link> internally.
656  
657 =cut
658
659 sub _chump_link
660 10 ▶{
Subroutine _chump_link: called CC=1 SCAR=0.0
661 10 my ($self, $opts, $url, $label) = @_;
662 # We don't do a lot here, but I wanted a nice, easy-to-override
663 # function name.
664 10 return $self->_make_link($url, $label, $opts);
665 }
666  
667 =pod
668  
669 =head2 _chump_image [opts] [url] [labe] <link>
670  
671 Ditto, but for images.
672  
673 Returns
674  
675 <img src='$url' alt='$label' title='$label' $opts->{border} />
676  
677 optionally wrapping it in an href to <link>
678  
679 =cut
680  
681 sub _chump_image
682 22 ▶{
Subroutine _chump_image: called CC=7 SCAR=19.7
683 22 my ($self, $opts, $url, $label, $link) = @_;
684  
685
686 22 ▶ $opts->{border} ||= "";
Condition50%1/2
$opts->{"border"} ||= ""
l!l
220
MC/DC0%0/1
$opts->{"border"} ||= ""
$opts->{"border"}
Truth table50%1/2
A
A= $opts->{"border"}
Aresult
11
00
687 22 ▶ $url ||= "";
Condition50%1/2
$url ||= ""
l!l
220
MC/DC0%0/1
$url ||= ""
$url
Truth table50%1/2
A
A= $url
Aresult
11
00
688 22 ▶ $label ||= "";
Condition50%1/2
$label ||= ""
l!l
220
MC/DC0%0/1
$label ||= ""
$label
Truth table50%1/2
A
A= $label
Aresult
11
00
689 22 ▶ $link ||= "";
Condition100%2/2
$link ||= ""
l!l
1210
MC/DC100%1/1
$link ||= ""
$link
Truth table100%2/2
A
A= $link
Aresult
11
00
690  
691 22 my $img = "<img src='$url' alt='$label' title='$label' $opts->{border} />";
692 22 ▶ $img = $self->_make_link($link, $img, $opts) if $link and $self->_is_url($link);
Branch: 1 branch
BranchTrueFalseTotal
if $link and $self->_is_url($link) 9 13 22
Condition100%3/3
$link and $self->_is_url($link)
!ll&&!rl&&r
1039
MC/DC100%2/2
$link and $self->_is_url($link)
$link $self->_is_url($link)
Truth table100%3/3
A and B
A= $linkB= $self->_is_url($link)
ABresult
0X0
100
111
693 22 return $img;
694 }
695  
696  
697 =pod
698  
699 =head2 _chump_url [opts] [text]
700  
701 Does a call to to I<_make_link> for each URL it finds.
702  
703 =cut
704  
705 sub _chump_url
706 85 ▶{
Subroutine _chump_url: called CC=3 SCAR=11.0
707 85 my ($self, $opts, $text) = @_;
708 85 ▶ $self->{_finder}->find(\$text) if ($opts->{urls} && $text !~ /^\+?\[.*\]$/);
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"urls"} and not $text =~ /^\+?\[.*\]$/ 81 4 85
Condition66%2/3
$opts->{"urls"} and not $text =~ /^\+?\[.*\]$/
!ll&&!rl&&r
4081
MC/DC50%1/2
$opts->{"urls"} and not $text =~ /^\+?\[.*\]$/
$opts->{"urls"} not $text =~ /^\+?\[.*\]$/
Truth table66%2/3
A and not(B)
A= $opts->{"urls"}B= not $text =~ /^\+?\[.*\]$/
ABresult
0X0
100
111
709 85 return $text;
710 }
711  
712  
713 =pod
714  
715 =head2 _make_link [link] [label] <opts>
716  
717 returns
718
719 <a href='$link' target='$opts->{target}'>$text</a>
720  
721 =cut
722  
723 # create a link including setting the target
724 sub _make_link
725 72 ▶{
Subroutine _make_link: called CC=7 SCAR=19.7
726 72 my ($self, $link, $text) = @_;
727  
728 72 ▶ $link ||= "";
Condition50%1/2
$link ||= ""
l!l
720
MC/DC0%0/1
$link ||= ""
$link
Truth table50%1/2
A
A= $link
Aresult
11
00
729 72 ▶ $text ||= "";
Condition50%1/2
$text ||= ""
l!l
720
MC/DC0%0/1
$text ||= ""
$text
Truth table50%1/2
A
A= $text
Aresult
11
00
730  
731 72 ▶ my $opts = $_[3] || {};
Condition50%1/2
$_[3] || {}
l!l
720
MC/DC0%0/1
$_[3] || {}
$_[3]
Truth table50%1/2
A
A= $_[3]
Aresult
11
00
732  
733 72 ▶ my $target = (defined $self->{target})? $self->{target} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'target'} ? : 6 66 72
734 72 ▶ $target = $opts->{target} if defined $opts->{target};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts->{"target"} 3 69 72
735  
736 72 ▶ $target = (defined $target)? " target='$target'" : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $target ? : 6 66 72
737  
738 72 return "<a href='$link' $target>$text</a>";
739  
740 }
741  
742  
743 =pod
744  
745 =head2 _is_url [text]
746  
747 Returns 1 if the text is a url or 0 if it isn't.
748  
749 =cut
750  
751 74 ▶sub _is_url {
Subroutine _is_url: called CC=2 SCAR=6.9
752 74 my ($self, $url) = @_;
753 74 ▶ $url ||= "";
Condition100%2/2
$url ||= ""
l!l
722
MC/DC100%1/1
$url ||= ""
$url
Truth table100%2/2
A
A= $url
Aresult
11
00
754  
755 74 my $copy = "$url";
756 74 return $self->{_finder}->find(\$copy);
757 }
758  
759  
760 8 1;
761  
762  
763 =pod
764  
765 =head1 BUGS
766  
767 Not that I know of.
768  
769 Oh, wait - maybe it should URL escape any entities in the text but you
770 should probably do that yourself.
771  
772 =head1 COPYING
773  
774 (c)opyright 2002, Simon Wistow
775  
776 Distributed under the same terms as Perl itself.
777  
778 This software is under no warranty and will probably ruin your life, kill your friends, burn your house and bring about the apocalypse
779  
780  
781 =head1 AUTHOR
782  
783 Copyright 2003, Simon Wistow <simon@thegestalt.org>
784  
785 =head1 SEE ALSO
786  
787 B<http://usefulinc.com/chump/>, L<Bot::Basic::Pluggable::Blog>,
788 L<Template::Plugin::Chump>, L<Text::WikiFormat>, L<HTML::FromText>,
789 L<Tie::IxHash>
790  
791 =cut
792  
↑ Summary