Tag.pm

filter: Statementstmt 89.6% 156 / 174 Branchbran 77.5% 62 / 80 Conditioncond 71.4% 15 / 21 MC/DCmcdc 50.0% 7 / 14 Subroutinesub 89.1% 33 / 37 Podpod 77.4% 24 / 31 totaltotal 83.1% 297 / 357 CC 54 SCAR 15.5
CC
54
Cov
85%
new
9.1
forbidTags
8.0
toBBCode
7.0
CRAP 4.7
« Tag/LIST.pm ↑ Summary Tag/URL.pm »
1 # $Id: Tag.pm 284 2006-12-01 07:51:49Z chronos $
2 package BBCode::Tag;
3 6 ▶use BBCode::Util qw(:quote :tag multilineText);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use BBCode::TagSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use HTML::Entities ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 our $VERSION = '0.34';
10  
11 # Note: Due to the huge differences between using BBCode::Tag and
12 # subclassing BBCode::Tag, the POD is no longer interleaved
13 # with the code. It has been moved to the end of the file.
14  
15  
16 # Class methods meant for overriding
17  
18 2097 ▶sub Tag($):method {
Subroutine Tag: called CC=2 SCAR=7.0
Pod documented
19 2097 my $class = shift;
20 2097 ▶ $class = ref($class) || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
42316740
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
21 2097 $class =~ s/'/::/g;
22 2097 $class =~ s/^.*:://;
23 2097 return $class;
24 }
25  
26 103 ▶sub Class($):method {
Subroutine Class: called CC=1 SCAR=0.0
Pod documented
27 103 return ();
28 }
29  
30 261 ▶sub BodyPermitted($):method {
Subroutine BodyPermitted: called CC=1 SCAR=0.0
Pod documented
31 261 return 0;
32 }
33  
34 0 ▶sub BodyTags($):method {
Subroutine BodyTags: not called CC=1 SCAR=6.9
Pod documented
35 0 return ();
36 }
37  
38 139 ▶sub NamedParams($):method {
Subroutine NamedParams: called CC=1 SCAR=0.0
Pod documented
39 139 return ();
40 }
41  
42 0 ▶sub RequiredParams($):method {
Subroutine RequiredParams: not called CC=1 SCAR=6.9
Pod documented
43 0 return shift->NamedParams;
44 }
45  
46 2 ▶sub DefaultParam($):method {
Subroutine DefaultParam: called CC=1 SCAR=0.0
Pod documented
47 2 return undef;
48 }
49  
50 56 ▶sub OpenPre($):method {
Subroutine OpenPre: called CC=1 SCAR=0.0
Pod documented
51 56 return "";
52 }
53  
54 56 ▶sub OpenPost($):method {
Subroutine OpenPost: called CC=1 SCAR=0.0
Pod documented
55 56 return "";
56 }
57  
58 54 ▶sub ClosePre($):method {
Subroutine ClosePre: called CC=1 SCAR=0.0
Pod documented
59 54 return "";
60 }
61  
62 54 ▶sub ClosePost($):method {
Subroutine ClosePost: called CC=1 SCAR=0.0
Pod documented
63 54 return "";
64 }
65  
66  
67 # Instance methods meant for overriding
68  
69 137 ▶sub validateParam($$$):method {
Subroutine validateParam: called CC=1 SCAR=0.0
Pod documented
70 137 return $_[2];
71 }
72  
73  
74 # Methods meant to be inherited
75  
76 270 ▶sub new:method {
Subroutine new: called CC=9 SCAR=22.0
Pod documented
77 270 my($pkg, $parser, $tag) = splice(@_, 0, 2);
78  
79 270 ▶ if($pkg eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg eq __PACKAGE__) 209 61 270
80 209 $tag = shift;
81 209 $pkg = $parser->resolveTag($tag);
82 }
83 270 $tag = $pkg->Tag;
84  
85 270 ▶ croak "Tag [$tag] is not permitted by current settings"
Branch: 1 branch
BranchTrueFalseTotal
unless $parser->isPermitted($tag) 1 269 270
86 if not $parser->isPermitted($tag);
87  
88 269 my $this = (bless { parser => $parser }, $pkg)->init();
89  
90 269 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 183 260 443
91 183 my($k,$v) = (undef,shift);
92 183 ▶ ($k,$v) = @$v if ref $v and UNIVERSAL::isa($v,'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $v and UNIVERSAL::isa($v, "ARRAY") 183 0 183
Condition33%1/3
ref $v and UNIVERSAL::isa($v, "ARRAY")
!ll&&!rl&&r
00183
MC/DC0%0/2
ref $v and UNIVERSAL::isa($v, "ARRAY")
ref $v UNIVERSAL::isa($v, "ARRAY")
Truth table33%1/3
A and B
A= ref $vB= UNIVERSAL::isa($v, "ARRAY")
ABresult
0X0
100
111
93 183 ▶ $k = $this->DefaultParam if not defined $k or $k eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $k or $k eq "" 155 28 183
Condition100%3/3
not defined $k or $k eq ""
l!l&&r!l&&!r
1223328
MC/DC100%2/2
not defined $k or $k eq ""
not defined $k $k eq ""
Truth table100%3/3
not(A) or B
A= not defined $kB= $k eq ""
ABresult
1X1
011
000
94 183 ▶ croak "No default parameter for [".$this->Tag."]" if not defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k 1 182 183
95 182 $this->param($k, $v);
96 }
97  
98 260 return $this;
99 }
100  
101 269 ▶sub init($):method {
Subroutine init: called CC=4 SCAR=13.9
Pod undocumented
102 269 my $this = shift;
103  
104 269 $this->{params} = {};
105 269 foreach($this->NamedParams) {
106 291 $this->{params}->{$_} = undef;
107 }
108  
109 269 ▶ if($this->BodyPermitted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->BodyPermitted) 139 130 269
110 139 $this->{body} = [];
111 139 $this->{permit} = BBCode::TagSet->new;
112 139 $this->{forbid} = BBCode::TagSet->new;
113 139 ▶ if($this->BodyTags) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->BodyTags) { } 139 0 139
114 139 $this->{permit}->add($this->BodyTags);
115 } else {
116 0 $this->{permit}->add(':ALL');
117 }
118 }
119  
120 269 return $this;
121 }
122  
123 97 ▶sub parser($):method {
Subroutine parser: called CC=1 SCAR=0.0
Pod documented
124 97 return shift->{parser};
125 }
126  
127 199 ▶sub isPermitted($$):method {
Subroutine isPermitted: called CC=5 SCAR=16.3
Pod documented
128 199 my($this,$child) = @_;
129 199 ▶ if(exists $this->{body}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{"body"}) 199 0 199
130 199 foreach(tagHierarchy($child)) {
131 583 ▶ return 0 if $this->{forbid}->contains($_);
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"forbid"}->contains($_) 0 583 583
132 583 ▶ return 1 if $this->{permit}->contains($_);
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"permit"}->contains($_) 197 386 583
133 }
134 }
135 2 return 0;
136 }
137  
138 197 ▶sub forbidTags($@):method {
Subroutine forbidTags: called CC=6 SCAR=20.8
Pod documented
139 197 my $this = shift;
140 197 ▶ if(exists $this->{body}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{"body"}) 68 129 197
141 68 my $set;
142 68 ▶ if(@_ == 1 and UNIVERSAL::isa($_[0],'BBCode::TagSet')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and UNIVERSAL::isa($_[0], 'BBCode::TagSet')) { } 68 0 68
Condition33%1/3
@_ == 1 and UNIVERSAL::isa($_[0], "BBCode::TagSet")
!ll&&!rl&&r
0068
MC/DC0%0/2
@_ == 1 and UNIVERSAL::isa($_[0], "BBCode::TagSet")
@_ == 1 UNIVERSAL::isa($_[0], "BBCode::TagSet")
Truth table33%1/3
A and B
A= @_ == 1B= UNIVERSAL::isa($_[0], "BBCode::TagSet")
ABresult
0X0
100
111
143 68 $set = shift;
144 } else {
145 0 $set = BBCode::TagSet->new(@_);
146 }
147 68 $this->{permit}->remove($set);
148 68 $this->{forbid}->add($set);
149 68 foreach my $child ($this->body) {
150 0 ▶ warn qq(Nested child is now forbidden) unless $this->isPermitted($child);
Branch: 1 branch
BranchTrueFalseTotal
unless $this->isPermitted($child) 0 0 0
151 0 $child->forbidTags($set);
152 }
153 }
154 197 return $this;
155 }
156  
157 552 ▶sub body($):method {
Subroutine body: called CC=4 SCAR=13.9
Pod documented
158 552 my $this = shift;
159 552 ▶ if(exists $this->{body}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $this->{'body'}) { } 406 146 552
160 406 ▶ return @{$this->{body}} if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 287 119 406
161 119 return $this->{body};
162 } else {
163 146 ▶ return () if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 16 130 146
164 130 return [];
165 }
166 }
167  
168 94 ▶sub bodyHTML($):method {
Subroutine bodyHTML: called CC=1 SCAR=0.0
Pod documented
169 94 return multilineText map { scalar $_->toHTML } shift->body;
170 }
171  
172 5 ▶sub bodyText($):method {
Subroutine bodyText: called CC=1 SCAR=0.0
Pod documented
173 5 return multilineText map { scalar $_->toText } shift->body;
174 }
175  
176 199 ▶sub pushBody($@):method {
Subroutine pushBody: called CC=6 SCAR=18.3
Pod documented
177 199 my $this = shift;
178 199 ▶ croak qq(Body contents not permitted) unless $this->BodyPermitted;
Branch: 1 branch
BranchTrueFalseTotal
unless $this->BodyPermitted 0 199 199
179 199 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 199 197 396
180 199 my $tag = shift;
181 199 ▶ if(ref $tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tag) { } 199 0 199
182 199 ▶ croak qq(Expected a BBCode::Tag) unless UNIVERSAL::isa($tag, 'BBCode::Tag');
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($tag, "BBCode::Tag") 0 199 199
183 } else {
184 0 $tag = BBCode::Tag->new($this->{parser}, 'TEXT', [ undef, $tag ]);
185 }
186 199 ▶ croak qq(Invalid tag nesting) if not $this->isPermitted($tag);
Branch: 1 branch
BranchTrueFalseTotal
unless $this->isPermitted($tag) 2 197 199
187 197 $tag->forbidTags($this->{forbid});
188 197 push @{$this->{body}}, $tag;
189 }
190 197 return $this;
191 }
192  
193 568 ▶sub param($$;$):method {
Subroutine param: called CC=6 SCAR=18.0
Pod undocumented
194 568 my($this,$param) = splice @_, 0, 2;
195  
196 568 ▶ $param = $this->DefaultParam if not defined $param or $param eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $param or $param eq "" 22 546 568
Condition66%2/3
not defined $param or $param eq ""
l!l&&r!l&&!r
220546
MC/DC50%1/2
not defined $param or $param eq ""
not defined $param $param eq ""
Truth table66%2/3
not(A) or B
A= not defined $paramB= $param eq ""
ABresult
1X1
011
000
197 568 ▶ croak qq(Missing parameter name) unless defined $param;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param 0 568 568
198 568 $param = uc $param;
199 568 ▶ croak qq(Invalid parameter name "$param") unless exists $this->{params}->{$param};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $this->{"params"}{$param} 1 567 568
200  
201 567 ▶ if(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 182 385 567
202 182 $this->{params}->{$param} = $this->validateParam($param,@_);
203 }
204  
205 560 return $this->{params}->{$param};
206 }
207  
208 78 ▶sub params($):method {
Subroutine params: called CC=5 SCAR=16.6
Pod undocumented
209 78 my $this = shift;
210 78 my @ret;
211 78 foreach my $k ($this->NamedParams) {
212 110 ▶ next unless exists $this->{params}->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $this->{"params"}{$k} 0 110 110
213 110 my $v = $this->{params}->{$k};
214 110 ▶ push @ret, $k, $v if defined $v;
Branch: 1 branch
BranchTrueFalseTotal
if defined $v 35 75 110
215 }
216 78 ▶ return @ret if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 78 0 78
217 0 return { @ret };
218 }
219  
220 304 ▶sub replace($):method {
Subroutine replace: called CC=1 SCAR=0.0
Pod undocumented
221 304 return $_[0];
222 }
223  
224 243 ▶sub replaceBody($):method {
Subroutine replaceBody: called CC=1 SCAR=0.0
Pod undocumented
225 243 my $this = shift->replace;
226 243 my $body = $this->body;
227 243 @$body = grep { defined } map { $_->replaceBody } @$body;
228 243 return $this;
229 }
230  
231 14 ▶sub isFollowed($):method {
Subroutine isFollowed: called CC=4 SCAR=13.9
Pod undocumented
232 14 my $this = shift;
233 14 my $follow = $this->parser->follow_links;
234 14 ▶ if($follow or $this->parser->follow_override) {
Branch: 1 branch
BranchTrueFalseTotal
if ($follow or $this->parser->follow_override) 11 3 14
Condition100%3/3
$follow or $this->parser->follow_override
l!l&&r!l&&!r
383
MC/DC100%2/2
$follow or $this->parser->follow_override
$follow $this->parser->follow_override
Truth table100%3/3
A or B
A= $followB= $this->parser->follow_override
ABresult
1X1
011
000
235 11 eval {
236 11 my $f = $this->param('FOLLOW');
237 11 ▶ $follow = $f if defined $f;
Branch: 1 branch
BranchTrueFalseTotal
if defined $f 3 8 11
238 };
239 }
240 14 return $follow;
241 }
242  
243 8 ▶sub openInNewWindow($):method {
Subroutine openInNewWindow: called CC=3 SCAR=15.0
Pod undocumented
244 8 my $this = shift;
245 8 my($nw,$nwo) = $this->parser->get(qw(newwindow_links newwindow_override));
246 8 ▶ if($nwo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nwo) 0 8 8
247 0 eval {
248 0 my $user = $this->param('NEWWINDOW');
249 0 ▶ $nw = $user if defined $user;
Branch: 1 branch
BranchTrueFalseTotal
if defined $user 0 0 0
250 };
251 }
252 8 return $nw;
253 }
254  
255 56 ▶sub toBBCode($):method {
Subroutine toBBCode: called CC=7 SCAR=19.5
Pod documented
256 56 my $this = shift->replace;
257  
258 56 my $ret = $this->OpenPre.'['.$this->Tag;
259  
260 56 my @p = $this->params;
261  
262 56 ▶ if(@p) {
Branch: 1 branch
BranchTrueFalseTotal
if (@p) 23 33 56
263 23 my $def = $this->DefaultParam;
264 23 my @params;
265  
266 23 ▶ while(@p) {
Branch: 1 branch
BranchTrueFalseTotal
if (@p) 35 23 58
267 35 my($k,$v) = splice @p, 0, 2;
268 35 ▶ if(defined $def and $def eq $k) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $def and $def eq $k) { } 16 19 35
Condition100%3/3
defined $def and $def eq $k
!ll&&!rl&&r
13616
MC/DC100%2/2
defined $def and $def eq $k
defined $def $def eq $k
Truth table100%3/3
A and B
A= defined $defB= $def eq $k
ABresult
0X0
100
111
269 16 $ret .= '='.quote($v);
270 16 $def = undef;
271 } else {
272 19 push @params, quote($k).'='.quote($v);
273 }
274 }
275  
276 23 $ret = join(", ", $ret, @params);
277 }
278  
279 56 $ret .= ']'.$this->OpenPost;
280  
281 56 ▶ if($this->BodyPermitted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->BodyPermitted) 54 2 56
282 54 foreach($this->body) {
283 80 $ret .= $_->toBBCode;
284 }
285 54 $ret .= $this->ClosePre.'[/'.$this->Tag.']'.$this->ClosePost;
286 }
287  
288 56 return multilineText $ret;
289 }
290  
291 0 ▶sub toHTML($):method {
Subroutine toHTML: not called CC=2 SCAR=17.9
Pod documented
292 0 my $this = shift;
293 0 my $that = $this->replace;
294 0 ▶ if($this == $that) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this == $that) { } 0 0 0
295 0 croak qq(Not implemented);
296 } else {
297 0 return $that->toHTML;
298 }
299 }
300  
301 0 ▶sub toText($):method {
Subroutine toText: not called CC=1 SCAR=6.9
Pod documented
302 0 my $this = shift->replace;
303 0 return $this->bodyText();
304 }
305  
306 26 ▶sub toLinkList($;$):method {
Subroutine toLinkList: called CC=4 SCAR=13.9
Pod documented
307 26 my $this = shift->replace;
308 26 my $ret = shift;
309 26 ▶ $ret = [] if not defined $ret;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ret 1 25 26
310 26 foreach my $child ($this->body) {
311 25 $child->toLinkList($ret);
312 }
313 26 ▶ return @$ret if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 1 25 26
314 25 return $ret;
315 }
316  
317 6 1;
318  
319 =head1 NAME
320  
321 BBCode::Tag - Perl representation of a BBCode tag
322  
323 =head1 DESCRIPTION
324  
325 See L<the documentation on BBCode::Parser|BBCode::Parser> for an overview of
326 the typical usage of this package.
327  
328 =head1 GENERAL USE
329  
330 =head2 METHODS
331  
332 =head3 new
333  
334 $parser = BBCode::Parser->new(...);
335 $tag = BBCode::Tag->new($parser, 'B');
336  
337 Called as a class method. Takes three or more parameters: a class name
338 (ignored), a L<BBCode::Parser object|BBCode::Parser>, the tag to be created,
339 and any initial parameters. Returns a newly constructed tag of the
340 appropriate subclass.
341  
342 Initial parameters can be provided in one of two ways:
343  
344 =over
345  
346 =item *
347  
348 The value for the default parameter can be given as a plain string.
349  
350 =item *
351  
352 The value for any named parameter can be given as an anonymous array of length
353 2. The first element is the parameter name, and the second is the value. If
354 the first element is undefined or the empty string, the default parameter is
355 set instead.
356  
357 =back
358  
359 Example:
360  
361 $url = BBCode::Tag->new(
362 $parser,
363 'URL',
364 # Sets the default parameter (style 1)
365 'http://www.example.com/',
366 # Sets the FOLLOW parameter
367 [ 'FOLLOW', '1' ],
368 );
369 $text = BBCode::Tag->new(
370 $parser,
371 'TEXT',
372 # Sets the default parameter (style 2)
373 [ undef, 'Example.com' ],
374 );
375 $url->pushBody($text);
376  
377 =head3 parser
378  
379 $parser = $tag->parser();
380  
381 Returns the L<BBCode::Parser object|BBCode::Parser> that this tag was
382 constructed with.
383  
384 =head3 isPermitted
385  
386 if($tag->isPermitted('URL')) {
387 # $tag can contain [URL] tags
388 } else {
389 # [URL] tags are forbidden
390 }
391  
392 Checks if the given BBCode tag is allowed in the body of this tag.
393  
394 =head3 forbidTags
395  
396 $tag->forbidTags(qw(IMG URL));
397  
398 Mark the given tagZ<>(s) as forbidden, so that this tag (including all its
399 children, grandchildren, etc.) can never contain any of the forbidden tags.
400  
401 At the moment, if a tag already contains one of the tags now forbidden, a
402 warning is raised. In the future, this behavior will likely change.
403  
404 =head3 body
405  
406 # Iterate over all this tag's immediate children
407 my @body = $tag->body();
408 foreach my $subtag (@body) { ...; }
409  
410 # Forcibly add a new child, overriding $tag->isPermitted()
411 my $body = $tag->body();
412 my $bold = BBCode::Tag->new($tag->parser(), 'B');
413 push @$body, $bold;
414  
415 Returns the list of child tags for this tag. In list context, returns
416 a list; otherwise, returns an array reference.
417  
418 CAUTION: The reference returned in scalar context is a direct pointer to a
419 C<BBCode::Tag> internal structure. It is possible to bypass checks on
420 security and correctness by altering it directly.
421  
422 =head3 bodyHTML
423  
424 print HANDLE $tag->bodyHTML();
425  
426 Recursively converts everything inside this tag into HTML. In array context,
427 returns the HTML line-by-line (with '\n' already appended); in scalar context,
428 returns the HTML as one string.
429  
430 Odds are that you want to use L<toHTML()|/"toHTML"> instead.
431  
432 =head3 bodyText
433  
434 print HANDLE $tag->bodyText();
435  
436 Recursively converts everything inside this tag into plain text. In array
437 context, returns the plain text line-by-line (with '\n' already appended); in
438 scalar context, returns the text as one string.
439  
440 Odds are that you want to use L<toText()|/"toText"> instead.
441  
442 =head3 pushBody
443  
444 $tag->pushBody(
445 'Image: ',
446 BBCode::Tag->new(
447 $tag->parser(),
448 'IMG',
449 'http://www.example.org/img.png',
450 )
451 );
452  
453 Appends one or more new child tags to this tag's body. Security and
454 correctness checks are performed. Use C<eval> to catch any exceptions.
455  
456 If any arguments are strings, they are upgraded to virtual [TEXT] tags.
457  
458 =head3 toBBCode
459  
460 Converts this BBCode tree back to BBCode. The resulting "deparsed" BBCode can
461 reveal discrepancies between what the user means vs. what BBCode::Parser
462 thinks the user means.
463  
464 In a web environment, a round-trip using C<toBBCode> is recommended each time
465 the user previews his/her message. This makes it easier for the user to spot
466 troublesome code.
467  
468 =head3 toHTML
469  
470 Converts this BBCode tree to HTML. This is generally the entire point of
471 using BBCode.
472  
473 At the moment, only XHTML 1.0 Strict output is supported. Future versions will
474 likely support other HTML standards.
475  
476 =head3 toText
477  
478 Converts this BBCode tree to plain text.
479  
480 Note that the result may contain Unicode characters. It is strongly
481 recommended that you use UTF-8 encoding whenever you store or transmit the
482 resulting text, to prevent loss of information. You might look at
483 L<the Text::Unidecode module|Text::Unidecode> if you want 7-bit ASCII output.
484  
485 =head3 toLinkList
486  
487 foreach $link ($tag->toLinkList) {
488 my($followed,$tag,$href,$text) = @$link;
489 print "<URL:$href> $text\n";
490 }
491  
492 Converts this BBCode tree into a list of all hyperlinks.
493  
494 Each hyperlink is itself an anonymous array of length 4. The first element
495 is a boolean that tells whether or not the link should be followed by search
496 engines (see L<the follow_links setting|BBCode::Parser/"follow_links"> for
497 details). The second element is a string that holds the BBCode tag name
498 that created this hyperlink. The third element is a string that holds the
499 actual hyperlink address. The fourth element is the text content (if any)
500 describing the link.
501  
502 In scalar context, returns a reference to the array of hyperlinks. In list
503 context, returns the array itself.
504  
505 =head1 SUBCLASSING
506  
507 While the details of subclassing presented below are currently accurate, a
508 number of major changes are likely (mostly dealing with the addition of new
509 BBCode tags at runtime). The API is not yet stable and will almost certainly
510 change in incompatible ways. Hic sunt dracones.
511  
512 =head2 CLASS METHODS
513  
514 =head3 Tag
515  
516 Returns the name of the tag as used in BBCode. For instance, the
517 following code prints "URL":
518  
519 my $parser = BBCode::Parser->new;
520 my $tree = $parser->parse("[URL]example.com[/URL]");
521 printf "%s\n", $tree->body->[0]->Tag;
522  
523 The default implementation returns the final component of the object's class
524 name. (For instance, C<BBCode::Tag::URL> becomes "URL".) Override this in
525 subclasses as needed.
526  
527 =head3 Class
528  
529 Returns a list of zero or more strings, each of which is a class
530 that this tag belongs to (without any colon prefixes). For instance, [B] and
531 [I] tags are both of class :INLINE, meaning that they can be found inside
532 fellow inline tags. Therefore, both their implementations return qw(INLINE).
533 Tag classes are listed in order from most specific to least.
534  
535 For a more thorough discussion of tag classes, see
536 L<"CLASSES" in BBCode::Parser|BBCode::Parser/"CLASSES">.
537  
538 The default implementation returns an empty list.
539  
540 =head3 BodyPermitted
541  
542 C<BodyPermitted> indicates whether or not the tag can contain a body of some
543 sort (whether it be text, more tags, or both).
544  
545 The default implementation returns false.
546  
547 =head3 BodyTags
548  
549 Returns a list of tags and classes that are permitted or forbidden
550 in the body of this tag. See L<BBCode::Parser-E<gt>permit()|BBCode::Parser/"permit">
551 for syntax. If this tag doesn't permit a body at all, this value is ignored.
552  
553 The default implementation returns an empty list (all tags are permitted).
554  
555 =head3 NamedParams
556  
557 Returns a list of named parameters that can be set on this tag.
558 By default, the order in this list determines the order in "deparsed" BBCode.
559 Override L<toBBCode()|/"toBBCode"> if this isn't acceptable.
560  
561 At the moment, parameter aliases are not available. This may change in the
562 future.
563  
564 The default implementation returns an empty list (no parameters are permitted).
565  
566 =head3 RequiredParams
567  
568 Returns a list of named parameters that B<must> be set on this tag.
569  
570 If the returned list contains a named parameter that doesn't exist in the
571 C<NamedParams()> list, then the tag cannot be used. So don't do that.
572  
573 The default implementation returns whatever C<NamedParams()> returns (all
574 permitted parameters are required).
575  
576 (At the moment, this value B<still> doesn't do anything, despite having been
577 there since before 0.01 was released. However, it will eventually take effect
578 somewhere around $tag->replaceBody time as $parser->parse finishes up. I think
579 a $tag->finalize method is in order.)
580  
581 =head3 DefaultParam
582  
583 Returns the name of a single parameter that is fundamental
584 enough that it is I<the> parameter of the tag. Returns C<undef> if no such
585 parameter exists.
586  
587 As an example, the C<[URL HREF]> parameter is important enough to the C<[URL]>
588 tag that the following two lines of BBCode are equivalent:
589  
590 [URL HREF=example.com]Link[/URL]
591 [URL=example.com]Link[/URL]
592  
593 In this example, C<DefaultParam()> returns 'HREF'.
594  
595 The default implementation returns C<undef>.
596  
597 =head3 OpenPre
598  
599 Returns a "fudge factor" value used in the default C<toBBCode()>. The
600 returned string is inserted into the "deparsed" BBCode just before the opening
601 tag.
602  
603 It is B<STRONGLY> recommended that this value should only contain whitespace.
604  
605 The default implementation returns the empty string.
606  
607 =head3 OpenPost
608  
609 Returns a "fudge factor" value used in the default C<toBBCode()>. The
610 returned string is inserted into the "deparsed" BBCode just after the opening
611 tag and before the contents begin.
612  
613 It is B<STRONGLY> recommended that this value should only contain whitespace.
614  
615 The default implementation returns the empty string.
616  
617 =head3 ClosePre
618  
619 Returns a "fudge factor" value used in the default C<toBBCode()>. The
620 returned string is inserted into the "deparsed" BBCode just before the closing
621 tag and after the contents end.
622  
623 It is B<STRONGLY> recommended that this value should only contain whitespace.
624  
625 The default implementation returns the empty string.
626  
627 =head3 ClosePost
628  
629 Returns a "fudge factor" value used in the default C<toBBCode()>. The
630 returned string is inserted into the "deparsed" BBCode just after the closing
631 tag.
632  
633 It is B<STRONGLY> recommended that this value should only contain whitespace.
634  
635 The default implementation returns the empty string.
636  
637 =head2 INSTANCE METHODS
638  
639 =head3 validateParam
640  
641 Takes three parameters: the object, the name of a parameter, and the requested
642 value for the parameter. Returns the actual value for the parameter. Throws
643 an exception if the requested value is entirely unacceptable.
644  
645 The default implementation returns all values unchanged. Override this to
646 perform checking on the values of named parameters.
647  
648 FIXME: This API is clunky, especially for inheriting.
649  
650 =head1 SEE ALSO
651  
652 L<BBCode::Parser|BBCode::Parser>
653  
654 =head1 AUTHOR
655  
656 Donald King E<lt>dlking@cpan.orgE<gt>
657  
658 =cut
« Tag/LIST.pm ↑ Summary Tag/URL.pm »