| 1 | # $Id: Parser.pm 284 2006-12-01 07:51:49Z chronos $ | ||||||||||||||||||||||||||||
| 2 | package BBCode::Parser; | ||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use BBCode::Util qw(:parse :tag); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use BBCode::TagSet; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use BBCode::Tag; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use BBCode::Body; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 6 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 6 | our $VERSION = '0.34'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 6 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 6 | die "EBCDIC platforms not supported" unless ord "A" == 0x41; | |||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 NAME | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | BBCode::Parser - Parses BBCode tags | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | BBCode is a simplified markup language used in several online forums | ||||||||||||||||||||||||||||
| 24 | and bulletin boards. It originated with phpBB, and remains most popular | ||||||||||||||||||||||||||||
| 25 | among applications written in PHP. Generally, users author their posts in | ||||||||||||||||||||||||||||
| 26 | BBCode, and the forum converts it to a permitted subset of well-formed HTML. | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | C<BBCode::Parser> is a proper recursive parser for BBCode-formatted text. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | =head1 OVERVIEW | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | A C<BBCode::Parser> object stores various settings that affect the parsing | ||||||||||||||||||||||||||||
| 33 | process. Simple settings are typically set when the parser is created using | ||||||||||||||||||||||||||||
| 34 | L<new()|/"new">, but they can be queried using L<get()|/"get"> and altered | ||||||||||||||||||||||||||||
| 35 | using L<set()|/"set">. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | See L</"SETTINGS"> for more information. | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | In addition to the simple settings, specific BBCode tags (or classes of tags) | ||||||||||||||||||||||||||||
| 40 | can be permitted or forbidden, using L<permit()|/"permit"> and | ||||||||||||||||||||||||||||
| 41 | L<forbid()|/"forbid"> respectively. By default, the only forbidden tag is | ||||||||||||||||||||||||||||
| 42 | C<[HTML]>, which is normally a security violation if permitted. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | See L</"CLASSES"> for a list of tag classes. | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | Once the parser has been configured appropriately, parse trees can be created | ||||||||||||||||||||||||||||
| 47 | using the L<parse()|/"parse"> method. The parse tree will consist of objects | ||||||||||||||||||||||||||||
| 48 | derived from L<BBCode::Tag|BBCode::Tag>; the root of the tree will be a | ||||||||||||||||||||||||||||
| 49 | L<BBCode::Body|BBCode::Body> object. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | Converting the parse tree to HTML is quite simple: call L<toHTML()|BBCode::Tag/"toHTML"> | ||||||||||||||||||||||||||||
| 52 | on the root of the tree. Likewise, the parse tree can be converted back to | ||||||||||||||||||||||||||||
| 53 | BBCode by calling L<toBBCode()|BBCode::Tag/"toBBCode">. See | ||||||||||||||||||||||||||||
| 54 | L<"METHODS" in BBCode::Tag|BBCode::Tag/"METHODS"> to find out what other | ||||||||||||||||||||||||||||
| 55 | output methods are available. | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =head1 SETTINGS | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | The following settings can be manipulated using L<get()|/"get"> and L<set()|/"set">. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =over | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =item css_prefix | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | (Type: String; Default: "bbcode-") | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | Many BBCode tags will add CSS classes as style hooks in the output HTML, such | ||||||||||||||||||||||||||||
| 68 | as C<< <div class="bbcode-quote">...</div> >>. This setting allows you to | ||||||||||||||||||||||||||||
| 69 | override the naming scheme for those hooks. At the moment, more direct control | ||||||||||||||||||||||||||||
| 70 | of the CSS class names is not available. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =item css_direct_styles | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | (Type: Boolean; Default: FALSE) | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | Certain style-related BBCode tags, such as [U] (underline) and [S] | ||||||||||||||||||||||||||||
| 77 | (strike-through) don't have a direct equivalent in modern XHTML 1.0 Strict. | ||||||||||||||||||||||||||||
| 78 | If this value is TRUE, then the generated HTML will use a C<style> attribute | ||||||||||||||||||||||||||||
| 79 | on a C<E<lt>spanE<gt>> tag to simulate the effects. If this value is FALSE, | ||||||||||||||||||||||||||||
| 80 | then the style attribute will be omitted. In either case, a C<class> attribute | ||||||||||||||||||||||||||||
| 81 | is provided for use as a hook by external CSS stylesheets (not provided). | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =item follow_links | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | (Type: Boolean; Default: FALSE) | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | To prevent blog spam and the like, many search engines now allow HTML authors | ||||||||||||||||||||||||||||
| 88 | to indicate that specific URLs on a page should not be indexed. If this value | ||||||||||||||||||||||||||||
| 89 | is TRUE, then there will be nothing special about the URL (meaning that search | ||||||||||||||||||||||||||||
| 90 | engines are encouraged to follow the link). If this value is FALSE, then a | ||||||||||||||||||||||||||||
| 91 | C<rel="nofollow"> attribute will be added wherever it makes sense (warning | ||||||||||||||||||||||||||||
| 92 | search engines that the link might be spam). | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | Whether or not to set this value to TRUE will depend on what you're using | ||||||||||||||||||||||||||||
| 95 | C<BBCode::Parser> for. If you're implementing a forum or bulletin board, TRUE | ||||||||||||||||||||||||||||
| 96 | might be reserved for senior, more trusted members. If you're implementing a | ||||||||||||||||||||||||||||
| 97 | blog, the value might be TRUE for the blog owner but FALSE for visitors. | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | For more information, see L<http://www.google.com/webmasters/bot.html#www>. | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | (If you turn this setting on, C<follow_override> behaves as if it were on as | ||||||||||||||||||||||||||||
| 102 | well. That way, users can explicitly mark links with C<FOLLOW=0> if | ||||||||||||||||||||||||||||
| 103 | necessary.) | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =item follow_override | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | (Type: Boolean; Default: FALSE) | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | This BBCode implementation allows a user to override C<follow_links> using a | ||||||||||||||||||||||||||||
| 110 | BBCode extension, the C<FOLLOW> parameter. If this value is TRUE, the user | ||||||||||||||||||||||||||||
| 111 | can override C<follow_links> with C<FOLLOW=1>; otherwise, the user must abide | ||||||||||||||||||||||||||||
| 112 | by C<follow_links>. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | (However, a user can always specify C<FOLLOW=0> regardless of this setting. | ||||||||||||||||||||||||||||
| 115 | If the user posting the link doesn't think the link is trustworthy, it's | ||||||||||||||||||||||||||||
| 116 | obviously not trustworthy.) | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | The same considerations that apply to C<follow_links> also apply to this | ||||||||||||||||||||||||||||
| 119 | setting. | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =item newwindow_links | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | (Type: Boolean; Default: FALSE) | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | For reasons largely having to do with site aesthetics, some site owners prefer | ||||||||||||||||||||||||||||
| 126 | for external links to each open in a new window using C<E<lt>a target="_blank"E<gt>>. | ||||||||||||||||||||||||||||
| 127 | For reasons largely having to do with browsing experience, some users prefer | ||||||||||||||||||||||||||||
| 128 | to summarily execute the aforementioned site owners in the most painful manner | ||||||||||||||||||||||||||||
| 129 | available. If you turn this option on, you will anger and frustrate people | ||||||||||||||||||||||||||||
| 130 | who suddenly find that their back buttons and/or tabs don't work right when | ||||||||||||||||||||||||||||
| 131 | they visit your site. Please take due consideration of that before setting | ||||||||||||||||||||||||||||
| 132 | this option to a TRUE value and taking choices away from the people reading | ||||||||||||||||||||||||||||
| 133 | your website. | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =item newwindow_override | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | (Type: Boolean; Default: FALSE) | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | This BBCode implementation allows a user to override C<newwindow_links> using | ||||||||||||||||||||||||||||
| 140 | a BBCode extension, the C<NEWWINDOW> parameter. If this value is TRUE, the | ||||||||||||||||||||||||||||
| 141 | user can force the link to open in the same window with C<NEWWINDOW=0>, or | ||||||||||||||||||||||||||||
| 142 | force the link to open in a new window with C<NEWWINDOW=1>. If this value is | ||||||||||||||||||||||||||||
| 143 | FALSE, the user has no say whatsoever. | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | The same considerations that apply to C<newwindow_links> also apply to this | ||||||||||||||||||||||||||||
| 146 | setting, but in drastically reduced form. If you feel the need to open links | ||||||||||||||||||||||||||||
| 147 | in new windows, please do it by turning this setting on and leaving | ||||||||||||||||||||||||||||
| 148 | C<newwindow_links> off. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =item allow_image_bullets | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | (Type: Boolean; Default: TRUE) | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | This setting allows you to restrict users from creating lists with custom | ||||||||||||||||||||||||||||
| 155 | bullets. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =back | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =head1 CLASSES | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =over 4 | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | =item BLOCK | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Tags with the C<BLOCK> class are those that translate into block-level | ||||||||||||||||||||||||||||
| 166 | elements in HTML, e.g. C<[QUOTE]>, which becomes C<E<lt>blockquoteE<gt>>. | ||||||||||||||||||||||||||||
| 167 | They represent blocks of content that stand alone from other blocks, often | ||||||||||||||||||||||||||||
| 168 | with vertical padding to separate them visually. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | In general, C<BLOCK> tags are not allowed inside C<INLINE> tags. | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =item INLINE | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | Tags with the C<INLINE> class are those that translate into inline elements | ||||||||||||||||||||||||||||
| 175 | in HTML, e.g. C<[URL]>, which becomes C<E<lt>aE<gt>>. They represent content | ||||||||||||||||||||||||||||
| 176 | that's still part of the current flow of text, not the start of a new block. | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =item LINK | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | Tags with the C<LINK> class are hyperlinks to external resources. At the | ||||||||||||||||||||||||||||
| 181 | moment, the two tags with the C<LINK> class are C<[URL]> and C<[EMAIL]>. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =item TEXT | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | Tags with the C<TEXT> class are plain text. At the moment, the three tags | ||||||||||||||||||||||||||||
| 186 | with the C<TEXT> class are C<[TEXT]>, C<[ENT]>, and C<[BR]>. | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =back | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =head1 METHODS | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =cut | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 0 | ▶ | sub parseCSSPrefix($) { | ||||||||||||||||||||||||||
|
Subroutine
parseCSSPrefix: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 195 | 0 | ▶ | if(defined $_[0] and $_[0] =~ /^([\w-]*)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $_[0] and $_[0] =~ /^([\w-]*)$/
MC/DC0%0/2
defined $_[0] and $_[0] =~ /^([\w-]*)$/
defined $_[0]
$_[0] =~ /^([\w-]*)$/
Truth table0%0/3
A and B
A= defined $_[0]B= $_[0] =~ /^([\w-]*)$/
| |||||||||||||||||||||||||||||
| 196 | 0 | return $1; | |||||||||||||||||||||||||||
| 197 | } else { | ||||||||||||||||||||||||||||
| 198 | 0 | return undef; | |||||||||||||||||||||||||||
| 199 | } | ||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 6 | my @SETTINGS; | |||||||||||||||||||||||||||
| 203 | my %SETTINGS; | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 6 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 206 | 6 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 6 | @SETTINGS = ( | |||||||||||||||||||||||||||
| 209 | [ 'css_prefix', \&parseCSSPrefix, 'bbcode-' ], | ||||||||||||||||||||||||||||
| 210 | [ 'css_direct_styles', \&parseBool, 0 ], | ||||||||||||||||||||||||||||
| 211 | [ 'follow_links', \&parseBool, 0 ], | ||||||||||||||||||||||||||||
| 212 | [ 'follow_override', \&parseBool, 0 ], | ||||||||||||||||||||||||||||
| 213 | [ 'newwindow_links', \&parseBool, 0 ], | ||||||||||||||||||||||||||||
| 214 | [ 'newwindow_override', \&parseBool, 0 ], | ||||||||||||||||||||||||||||
| 215 | [ 'allow_image_bullets', \&parseBool, 1 ], | ||||||||||||||||||||||||||||
| 216 | ); | ||||||||||||||||||||||||||||
| 217 | 6 | %SETTINGS = map { $_->[0] => $_ } @SETTINGS; | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 6 | foreach(@SETTINGS) { | |||||||||||||||||||||||||||
| 220 | 42 | my $attr = $_->[0]; | |||||||||||||||||||||||||||
| 221 | 87 | ▶ | *$attr = sub($;$):method { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 222 | 87 | my $this = shift; | |||||||||||||||||||||||||||
| 223 | 87 | ▶ | if(@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | 0 | return $this->set($attr, @_); | |||||||||||||||||||||||||||
| 225 | } else { | ||||||||||||||||||||||||||||
| 226 | 87 | return $this->get($attr); | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | 42 | }; | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 150 | ▶ | sub _canonize($) { | ||||||||||||||||||||||||||
|
Subroutine
_canonize: called CC=1 SCAR=0.5
| |||||||||||||||||||||||||||||
| 233 | 150 | local $_ = $_[0]; | |||||||||||||||||||||||||||
| 234 | 0 | s/([[:upper:]]+)([[:upper:]][[:lower:]]+)/$1.'_'.lc($2)/eg; | |||||||||||||||||||||||||||
| 235 | 0 | s/([[:lower:]])([[:upper:]]+)/$1.'_'.lc($2)/eg; | |||||||||||||||||||||||||||
| 236 | 0 | s/([[:upper:]]+)/lc($1)/eg; | |||||||||||||||||||||||||||
| 237 | 150 | return $_; | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | =head2 DEFAULT | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | my $tree = BBCode::Parser->DEFAULT->parse($code); | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | C<DEFAULT> returns the default parser. If you change the default parser, all | ||||||||||||||||||||||||||||
| 245 | future parsers created with L<new()|/"new"> will incorporate your changes. | ||||||||||||||||||||||||||||
| 246 | However, all existing parsers will be unaffected. | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | =cut | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 6 | my $DEFAULT; | |||||||||||||||||||||||||||
| 251 | 6 | ▶ | sub DEFAULT() { | ||||||||||||||||||||||||||
|
Subroutine
DEFAULT: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 252 | 6 | ▶ | return $DEFAULT if defined $DEFAULT; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 6 | $DEFAULT = bless {}; | |||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | 6 | foreach(@SETTINGS) { | |||||||||||||||||||||||||||
| 257 | 42 | $DEFAULT->{$_->[0]} = $_->[2]; | |||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 6 | $DEFAULT->{_tags} = {}; | |||||||||||||||||||||||||||
| 261 | 6 | foreach( | |||||||||||||||||||||||||||
| 262 | # Pure text | ||||||||||||||||||||||||||||
| 263 | 'TEXT', | ||||||||||||||||||||||||||||
| 264 | 'ENT', | ||||||||||||||||||||||||||||
| 265 | 'BR', | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | # Links | ||||||||||||||||||||||||||||
| 268 | 'URL', | ||||||||||||||||||||||||||||
| 269 | 'EMAIL', | ||||||||||||||||||||||||||||
| 270 | 'IMG', | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | # Structural | ||||||||||||||||||||||||||||
| 273 | 'LIST', | ||||||||||||||||||||||||||||
| 274 | 'OL', | ||||||||||||||||||||||||||||
| 275 | 'UL', | ||||||||||||||||||||||||||||
| 276 | 'LI', | ||||||||||||||||||||||||||||
| 277 | 'QUOTE', | ||||||||||||||||||||||||||||
| 278 | 'CODE', | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | # Semantic | ||||||||||||||||||||||||||||
| 281 | 'ABBR', | ||||||||||||||||||||||||||||
| 282 | 'ACRONYM', | ||||||||||||||||||||||||||||
| 283 | 'HIDDEN', | ||||||||||||||||||||||||||||
| 284 | 'Q', | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | # Presentational | ||||||||||||||||||||||||||||
| 287 | 'HR', | ||||||||||||||||||||||||||||
| 288 | 'FONT', | ||||||||||||||||||||||||||||
| 289 | 'SIZE', | ||||||||||||||||||||||||||||
| 290 | 'COLOR', | ||||||||||||||||||||||||||||
| 291 | 'SUB', | ||||||||||||||||||||||||||||
| 292 | 'SUP', | ||||||||||||||||||||||||||||
| 293 | 'TT', | ||||||||||||||||||||||||||||
| 294 | 'B', | ||||||||||||||||||||||||||||
| 295 | 'I', | ||||||||||||||||||||||||||||
| 296 | 'S', | ||||||||||||||||||||||||||||
| 297 | 'U', | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | # Other | ||||||||||||||||||||||||||||
| 300 | 'HTML', | ||||||||||||||||||||||||||||
| 301 | ) { | ||||||||||||||||||||||||||||
| 302 | 168 | $DEFAULT->addTag($_ => "BBCode::Tag::$_"); | |||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 6 | $DEFAULT->{_permit} = BBCode::TagSet->new; | |||||||||||||||||||||||||||
| 306 | 6 | $DEFAULT->{_forbid} = BBCode::TagSet->new; | |||||||||||||||||||||||||||
| 307 | 6 | $DEFAULT->permit(':ALL'); | |||||||||||||||||||||||||||
| 308 | 6 | $DEFAULT->forbid('HTML'); | |||||||||||||||||||||||||||
| 309 | 6 | return $DEFAULT; | |||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | =head2 clone | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | my $parser = BBCode::Parser->new(follow_links => 1); | ||||||||||||||||||||||||||||
| 315 | my $clone = $parser->clone; | ||||||||||||||||||||||||||||
| 316 | $clone->forbid('IMG'); | ||||||||||||||||||||||||||||
| 317 | printf "[IMG] is%s OK\n", ($parser->isPermitted('IMG') ? "" : " not"); | ||||||||||||||||||||||||||||
| 318 | # Prints "[IMG] is OK", since forbid('IMG') applies only to the clone. | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | C<clone> creates a new parser that copies the settings of an existing parser. | ||||||||||||||||||||||||||||
| 321 | After cloning, the two parsers are completely independent; changing settings | ||||||||||||||||||||||||||||
| 322 | in one does not affect the other. | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | If any arguments are given, they are handed off to L<the set() method|/"set">. | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | =cut | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | 6 | ▶ | sub clone($%):method { | ||||||||||||||||||||||||||
|
Subroutine
clone: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 329 | 6 | my $this = shift; | |||||||||||||||||||||||||||
| 330 | 6 | ▶ | $this = $this->DEFAULT if not ref $this; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 331 | 6 | my $that = bless {}, ref($this); | |||||||||||||||||||||||||||
| 332 | 6 | %{$that->{_tags}} = %{$this->{_tags}}; | |||||||||||||||||||||||||||
| 333 | 6 | $that->{_permit} = $this->{_permit}->clone; | |||||||||||||||||||||||||||
| 334 | 6 | $that->{_forbid} = $this->{_forbid}->clone; | |||||||||||||||||||||||||||
| 335 | 6 | foreach(map { $_->[0] } @SETTINGS) { | |||||||||||||||||||||||||||
| 336 | 42 | $that->{$_} = $this->get($_); | |||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||
| 338 | 6 | ▶ | $that->set(@_) if @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 339 | 6 | return $that; | |||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | =head2 new | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | my $parser = BBCode::Parser->new(%args); | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | C<new> creates a new C<BBCode::Parser>. Any arguments | ||||||||||||||||||||||||||||
| 347 | are handed off to L<the set() method|/"set">. | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | =cut | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | 6 | ▶ | sub new($%):method { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 352 | 6 | return shift->DEFAULT->clone(@_); | |||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | =head2 get | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | if($parser->get('follow_override')) { | ||||||||||||||||||||||||||||
| 358 | # [URL FOLLOW] permitted | ||||||||||||||||||||||||||||
| 359 | } else { | ||||||||||||||||||||||||||||
| 360 | # [URL FOLLOW] forbidden | ||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | C<get> fetches the current settings for the given parser. See L</"SETTINGS"> | ||||||||||||||||||||||||||||
| 364 | for a list of available settings. | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | =cut | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | 137 | ▶ | sub get($@):method { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=6 SCAR=18.4
Pod
documented
| |||||||||||||||||||||||||||||
| 369 | 137 | my $this = shift; | |||||||||||||||||||||||||||
| 370 | 137 | my @ret; | |||||||||||||||||||||||||||
| 371 | 137 | ▶ | while(@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 372 | 145 | my $key = _canonize shift; | |||||||||||||||||||||||||||
| 373 | 145 | ▶ | croak qq(Unknown setting "$key") unless exists $SETTINGS{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 374 | 145 | ▶ | warn qq(BUG: Setting $key does not exist) unless exists $this->{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 375 | 145 | push @ret, $this->{$key}; | |||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||
| 377 | 137 | ▶ | return @ret if wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 378 | 129 | ▶ | return $ret[0] if @ret == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 379 | 0 | return \@ret; | |||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | =head2 set | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | $parser->set(follow_override => 1); | ||||||||||||||||||||||||||||
| 385 | |||||||||||||||||||||||||||||
| 386 | C<set> alters the settings for the given parser. See L</"SETTINGS"> for a list | ||||||||||||||||||||||||||||
| 387 | of available settings. | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | =cut | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | 5 | ▶ | sub set($%):method { | ||||||||||||||||||||||||||
|
Subroutine
set: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 392 | 5 | my $this = shift; | |||||||||||||||||||||||||||
| 393 | 5 | ▶ | while(@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 394 | 5 | my $key = _canonize shift; | |||||||||||||||||||||||||||
| 395 | 5 | my $val = shift; | |||||||||||||||||||||||||||
| 396 | 5 | ▶ | croak qq(Unknown setting "$key") unless exists $SETTINGS{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 397 | 5 | $val = $SETTINGS{$key}->[1]->($val); | |||||||||||||||||||||||||||
| 398 | 5 | ▶ | $val = $SETTINGS{$key}->[2] if not defined $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 399 | 5 | $this->{$key} = $val; | |||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||
| 401 | 5 | return $this; | |||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | =head2 addTag | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | TODO: Implement and document | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | =cut | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | 168 | ▶ | sub addTag($$$):method { | ||||||||||||||||||||||||||
|
Subroutine
addTag: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||||||||||
| 411 | 168 | my($this, $tag, $class) = @_; | |||||||||||||||||||||||||||
| 412 | 168 | ▶ | return if $class eq "BBCode::Tag::$tag"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 413 | 0 | die qq(Not implemented); | |||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | 0 | ▶ | sub removeTag($$):method { | ||||||||||||||||||||||||||
|
Subroutine
removeTag: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 417 | 0 | die qq(Not implemented); | |||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | 209 | ▶ | sub resolveTag($$):method { | ||||||||||||||||||||||||||
|
Subroutine
resolveTag: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 421 | 209 | my($this, $tag) = @_; | |||||||||||||||||||||||||||
| 422 | 209 | return tagLoadPackage($tag); | |||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | =head2 permit | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | $parser->permit(qw(:INLINE !:LINK)); | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | C<permit> adds TAGs and :CLASSes to the list of permitted tags. Use '!' in | ||||||||||||||||||||||||||||
| 430 | front of a tag or class to negate the meaning. | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | =cut | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | 7 | ▶ | sub permit($@):method { | ||||||||||||||||||||||||||
|
Subroutine
permit: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 435 | 7 | my $this = shift; | |||||||||||||||||||||||||||
| 436 | 7 | my $set = BBCode::TagSet->new(@_); | |||||||||||||||||||||||||||
| 437 | 7 | $this->{_permit}->add($set); | |||||||||||||||||||||||||||
| 438 | 7 | $this->{_forbid}->remove($set); | |||||||||||||||||||||||||||
| 439 | 7 | return $this; | |||||||||||||||||||||||||||
| 440 | } | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | =head2 forbid | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | $parser->forbid(qw(:ALL !:TEXT)); | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | C<forbid> adds TAGs and :CLASSes to the list of forbidden tags. Use '!' in | ||||||||||||||||||||||||||||
| 447 | front of a tag or class to negate the meaning. | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | =cut | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | 6 | ▶ | sub forbid($@):method { | ||||||||||||||||||||||||||
|
Subroutine
forbid: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 452 | 6 | my $this = shift; | |||||||||||||||||||||||||||
| 453 | 6 | my $set = BBCode::TagSet->new(@_); | |||||||||||||||||||||||||||
| 454 | 6 | $this->{_forbid}->add($set); | |||||||||||||||||||||||||||
| 455 | 6 | $this->{_permit}->remove($set); | |||||||||||||||||||||||||||
| 456 | 6 | return $this; | |||||||||||||||||||||||||||
| 457 | } | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | =head2 isPermitted | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | if($parser->isPermitted('IMG')) { | ||||||||||||||||||||||||||||
| 462 | # Yay, [IMG] tags | ||||||||||||||||||||||||||||
| 463 | } else { | ||||||||||||||||||||||||||||
| 464 | # Darn, no [IMG] tags | ||||||||||||||||||||||||||||
| 465 | } | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | C<isPermitted> checks if a tag is permitted by the current settings. | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | =cut | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | 270 | ▶ | sub isPermitted($$):method { | ||||||||||||||||||||||||||
|
Subroutine
isPermitted: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 472 | 270 | my($this,$tag) = @_; | |||||||||||||||||||||||||||
| 473 | 270 | foreach(tagHierarchy($tag)) { | |||||||||||||||||||||||||||
| 474 | 877 | ▶ | return 0 if $this->{_forbid}->contains($_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 475 | 876 | ▶ | return 1 if $this->{_permit}->contains($_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 476 | } | ||||||||||||||||||||||||||||
| 477 | 0 | return 0; | |||||||||||||||||||||||||||
| 478 | } | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | 44 | ▶ | sub _args(\$) { | ||||||||||||||||||||||||||
|
Subroutine
_args: called CC=22 SCAR=31.4
| |||||||||||||||||||||||||||||
| 481 | 44 | my $ref = shift; | |||||||||||||||||||||||||||
| 482 | 44 | my $ok = 0; | |||||||||||||||||||||||||||
| 483 | 44 | my $arg = 0; | |||||||||||||||||||||||||||
| 484 | 44 | my $k = undef; | |||||||||||||||||||||||||||
| 485 | 44 | my $v = ''; | |||||||||||||||||||||||||||
| 486 | 44 | my @args; | |||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | 44 | ▶ | while(length $$ref > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 489 | 722 | ▶ | if($$ref =~ s/^\\//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 490 | 3 | ▶ | croak qq(Invalid BBCode: Backslash at end of text) unless $$ref =~ s/^(.)//s; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 491 | 3 | ▶ | $v .= $1 unless $1 eq "\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 492 | 3 | next; | |||||||||||||||||||||||||||
| 493 | } | ||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | 719 | ▶ | if(not defined $k and $$ref =~ s/^\s*=\s*//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $k and $$ref =~ s/^\s*=\s*//
MC/DC100%2/2
not defined $k and $$ref =~ s/^\s*=\s*//
not defined $k
$$ref =~ s/^\s*=\s*//
Truth table100%3/3
not(A) and B
A= not defined $kB= $$ref =~ s/^\s*=\s*//
| |||||||||||||||||||||||||||||
| 496 | 59 | ▶ | if($arg) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 497 | 26 | $k = uc $v; | |||||||||||||||||||||||||||
| 498 | } else { | ||||||||||||||||||||||||||||
| 499 | 33 | $arg = 1; | |||||||||||||||||||||||||||
| 500 | 33 | $k = ''; | |||||||||||||||||||||||||||
| 501 | } | ||||||||||||||||||||||||||||
| 502 | 59 | $v = ''; | |||||||||||||||||||||||||||
| 503 | 59 | next; | |||||||||||||||||||||||||||
| 504 | } | ||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | 660 | ▶ | if($$ref =~ s/^(["'])//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 507 | 18 | my $q = $1; | |||||||||||||||||||||||||||
| 508 | 18 | my $qok = 0; | |||||||||||||||||||||||||||
| 509 | 18 | ▶ | while(length $$ref > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 510 | 251 | ▶ | $qok++, last if $$ref =~ s/^\Q$q\E//; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 511 | 233 | ▶ | if($$ref =~ s/^\\//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 512 | 3 | ▶ | croak qq(Invalid BBCode: Backslash at end of text) unless $$ref =~ s/^(.)//s; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 513 | 3 | ▶ | $v .= $1 unless $1 eq "\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 514 | 3 | next; | |||||||||||||||||||||||||||
| 515 | } | ||||||||||||||||||||||||||||
| 516 | 230 | $$ref =~ s/^(.)//s; | |||||||||||||||||||||||||||
| 517 | 230 | ▶ | $v .= $1 eq "\n" ? " " : $1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 518 | } | ||||||||||||||||||||||||||||
| 519 | 18 | ▶ | croak qq(Invalid BBCode: Quoted string never ends) unless $qok; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 520 | 18 | $arg = 1; | |||||||||||||||||||||||||||
| 521 | 18 | next; | |||||||||||||||||||||||||||
| 522 | } | ||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||
| 524 | 642 | ▶ | if($$ref =~ s/^\s*\]//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 525 | 44 | ▶ | push @args, [ $k, $v ] if $arg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 526 | 44 | $ok++; | |||||||||||||||||||||||||||
| 527 | 44 | last; | |||||||||||||||||||||||||||
| 528 | } | ||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||
| 530 | 598 | ▶ | if($$ref =~ s/^(\s+|\s*,\s*)//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 531 | 27 | ▶ | push @args, [ $k, $v ] if $arg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 532 | 27 | $arg = 0; | |||||||||||||||||||||||||||
| 533 | 27 | $k = undef; | |||||||||||||||||||||||||||
| 534 | 27 | $v = ''; | |||||||||||||||||||||||||||
| 535 | 27 | next; | |||||||||||||||||||||||||||
| 536 | } | ||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||
| 538 | 571 | $$ref =~ s/^(.)//; | |||||||||||||||||||||||||||
| 539 | 571 | $arg = 1; | |||||||||||||||||||||||||||
| 540 | 571 | $v .= $1; | |||||||||||||||||||||||||||
| 541 | } | ||||||||||||||||||||||||||||
| 542 | |||||||||||||||||||||||||||||
| 543 | 44 | ▶ | croak qq(Invalid BBCode: Unterminated tag) unless $ok; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 544 | 44 | ▶ | return @args if wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 545 | 0 | return \@args; | |||||||||||||||||||||||||||
| 546 | } | ||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | 61 | ▶ | sub _tokenize($$) { | ||||||||||||||||||||||||||
|
Subroutine
_tokenize: called CC=18 SCAR=40.2
| |||||||||||||||||||||||||||||
| 549 | 61 | my($this,$ref) = @_; | |||||||||||||||||||||||||||
| 550 | 61 | my(@tokens); | |||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | 61 | ▶ | while(length $$ref > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 553 | 286 | ▶ | if($$ref =~ s/^ ([^\[\]<&]+) //x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 554 | 128 | push @tokens, [ 'TEXT', [ undef, $1 ] ]; | |||||||||||||||||||||||||||
| 555 | 128 | next; | |||||||||||||||||||||||||||
| 556 | } | ||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||
| 558 | 158 | ▶ | if($$ref =~ s/^ \[ \s* \] //x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 559 | 0 | push @tokens, [ 'TEXT', [ undef, '[' ] ]; | |||||||||||||||||||||||||||
| 560 | 0 | next; | |||||||||||||||||||||||||||
| 561 | } | ||||||||||||||||||||||||||||
| 562 | 158 | ▶ | if($$ref =~ s/^ \[ \[ //x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 563 | 0 | push @tokens, [ 'TEXT', [ undef, '[' ] ]; | |||||||||||||||||||||||||||
| 564 | 0 | next; | |||||||||||||||||||||||||||
| 565 | } | ||||||||||||||||||||||||||||
| 566 | 158 | ▶ | if($$ref =~ s/^ \] \] //x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 567 | 0 | push @tokens, [ 'TEXT', [ undef, ']' ] ]; | |||||||||||||||||||||||||||
| 568 | 0 | next; | |||||||||||||||||||||||||||
| 569 | } | ||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||
| 571 | # Special case | ||||||||||||||||||||||||||||
| 572 | 158 | ▶ | if($$ref =~ s/^ \[ \s* HTML \s* \] //xi) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 573 | 2 | $$ref =~ s/^ (.*?) \[ \s* \/ \s* HTML \s* \] //xis; | |||||||||||||||||||||||||||
| 574 | 2 | push @tokens, [ 'HTML', [ undef, $1 ] ]; | |||||||||||||||||||||||||||
| 575 | 2 | next; | |||||||||||||||||||||||||||
| 576 | } | ||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||
| 578 | # Special case | ||||||||||||||||||||||||||||
| 579 | 156 | ▶ | if($$ref =~ s/^ \[ \s* (\/?) \s* \* \s* \] //x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 580 | 2 | push @tokens, [ $1.'LI' ]; | |||||||||||||||||||||||||||
| 581 | 2 | next; | |||||||||||||||||||||||||||
| 582 | } | ||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||
| 584 | 154 | ▶ | if($$ref =~ s/^ \[ ( \s* \/? \s* \w+ \s* ) \] //x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 585 | 110 | my $tag = uc($1); | |||||||||||||||||||||||||||
| 586 | 110 | $tag =~ s/\s+//g; | |||||||||||||||||||||||||||
| 587 | 110 | ▶ | if(tagExists($tag)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 588 | 110 | push @tokens, [ $tag ]; | |||||||||||||||||||||||||||
| 589 | } else { | ||||||||||||||||||||||||||||
| 590 | 0 | push @tokens, [ 'TEXT', [ undef, "[$1]" ] ]; | |||||||||||||||||||||||||||
| 591 | } | ||||||||||||||||||||||||||||
| 592 | 110 | next; | |||||||||||||||||||||||||||
| 593 | } | ||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||
| 595 | 44 | ▶ | if($$ref =~ s/^ \[ \s* \/ \s* \] //x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 596 | 0 | push @tokens, [ '/' ]; | |||||||||||||||||||||||||||
| 597 | 0 | next; | |||||||||||||||||||||||||||
| 598 | } | ||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||
| 600 | 44 | ▶ | if($$ref =~ s/^ \[ ( \s* \w+ \s* ) (?= \s | , | = | \] )//x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 601 | 44 | my $str = $1; | |||||||||||||||||||||||||||
| 602 | 44 | my $tag = uc($str); | |||||||||||||||||||||||||||
| 603 | 44 | $tag =~ s/\s+//g; | |||||||||||||||||||||||||||
| 604 | 44 | ▶ | if(tagExists($tag)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 605 | 44 | push @tokens, [ $tag, _args($$ref) ]; | |||||||||||||||||||||||||||
| 606 | } else { | ||||||||||||||||||||||||||||
| 607 | 0 | push @tokens, [ 'TEXT', [ undef, "[$str" ] ]; | |||||||||||||||||||||||||||
| 608 | } | ||||||||||||||||||||||||||||
| 609 | 44 | next; | |||||||||||||||||||||||||||
| 610 | } | ||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||
| 612 | 0 | ▶ | if($$ref =~ s/^ <URL: ([^<>]*) > //x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 613 | 0 | my $text = $1; | |||||||||||||||||||||||||||
| 614 | 0 | my $url; | |||||||||||||||||||||||||||
| 615 | 0 | ▶ | if(defined($url = parseURL($text))) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 616 | 0 | push @tokens, [ 'URL', [ undef, $url->as_string ] ]; | |||||||||||||||||||||||||||
| 617 | 0 | push @tokens, [ 'TEXT', [ undef, $text ] ]; | |||||||||||||||||||||||||||
| 618 | 0 | push @tokens, [ '/URL' ]; | |||||||||||||||||||||||||||
| 619 | } elsif(defined($url = parseMailURL($text))) { | ||||||||||||||||||||||||||||
| 620 | 0 | push @tokens, [ 'EMAIL', [ undef, $url->as_string ] ]; | |||||||||||||||||||||||||||
| 621 | 0 | push @tokens, [ 'TEXT', [ undef, $text ] ]; | |||||||||||||||||||||||||||
| 622 | 0 | push @tokens, [ '/EMAIL' ]; | |||||||||||||||||||||||||||
| 623 | } else { | ||||||||||||||||||||||||||||
| 624 | 0 | push @tokens, [ 'TEXT', [ undef, "<URL:$text>" ] ]; | |||||||||||||||||||||||||||
| 625 | } | ||||||||||||||||||||||||||||
| 626 | 0 | next; | |||||||||||||||||||||||||||
| 627 | } | ||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||
| 629 | 0 | ▶ | if($$ref =~ s/^ & ( \#? [\w+-]+ ) ; //x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 630 | 0 | ▶ | if(defined parseEntity($1)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 631 | 0 | push @tokens, [ 'ENT', [ undef, $1 ] ]; | |||||||||||||||||||||||||||
| 632 | } else { | ||||||||||||||||||||||||||||
| 633 | 0 | push @tokens, [ 'TEXT', [ undef, "&$1;" ] ]; | |||||||||||||||||||||||||||
| 634 | } | ||||||||||||||||||||||||||||
| 635 | 0 | next; | |||||||||||||||||||||||||||
| 636 | } | ||||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||
| 638 | 0 | $$ref =~ s/^ (.) //x; | |||||||||||||||||||||||||||
| 639 | 0 | push @tokens, [ 'TEXT', [ undef, $1 ] ]; | |||||||||||||||||||||||||||
| 640 | } | ||||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||
| 642 | 61 | return \@tokens; | |||||||||||||||||||||||||||
| 643 | } | ||||||||||||||||||||||||||||
| 644 | |||||||||||||||||||||||||||||
| 645 | 200 | ▶ | sub _top(\@) { | ||||||||||||||||||||||||||
|
Subroutine
_top: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 646 | 200 | my $stack = shift; | |||||||||||||||||||||||||||
| 647 | 200 | return $$stack[$#$stack]; | |||||||||||||||||||||||||||
| 648 | } | ||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||
| 650 | 61 | ▶ | sub _parse($$$) { | ||||||||||||||||||||||||||
|
Subroutine
_parse: called CC=11 SCAR=24.1
| |||||||||||||||||||||||||||||
| 651 | 61 | my($this,$root,$ref) = @_; | |||||||||||||||||||||||||||
| 652 | 61 | my @st = ($root); | |||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||
| 654 | 61 | ▶ | TOKEN:while(@$ref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 655 | 269 | my $token = shift @$ref; | |||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||
| 657 | # TODO: Add option to make "Illegal close tag" non-fatal | ||||||||||||||||||||||||||||
| 658 | 269 | ▶ | if($token->[0] =~ s#^/##) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 659 | 62 | my @old = @st; | |||||||||||||||||||||||||||
| 660 | 62 | ▶ | while(@st) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 661 | 62 | my $top = pop(@st); | |||||||||||||||||||||||||||
| 662 | 62 | ▶ | if($token->[0] eq $top->Tag or $token->[0] eq '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$token->[0] eq $top->Tag or $token->[0] eq ""
MC/DC50%1/2
$token->[0] eq $top->Tag or $token->[0] eq ""
$token->[0] eq $top->Tag
$token->[0] eq ""
Truth table66%2/3
A or B
A= $token->[0] eq $top->TagB= $token->[0] eq ""
| |||||||||||||||||||||||||||||
| 663 | 61 | next TOKEN; | |||||||||||||||||||||||||||
| 664 | } | ||||||||||||||||||||||||||||
| 665 | } | ||||||||||||||||||||||||||||
| 666 | 1 | croak 'Illegal close tag: expected [/'._top(@old)->Tag.'], got [/'.$token->[0].']'; | |||||||||||||||||||||||||||
| 667 | } | ||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||
| 669 | 207 | my $tag = BBCode::Tag->new($this, @$token); | |||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||
| 671 | # TODO: This code is over-zealous about "correcting" things. | ||||||||||||||||||||||||||||
| 672 | 197 | ▶ | while(@st) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 673 | 199 | eval { | |||||||||||||||||||||||||||
| 674 | 199 | _top(@st)->pushBody($tag); | |||||||||||||||||||||||||||
| 675 | }; | ||||||||||||||||||||||||||||
| 676 | 199 | ▶ | last if not $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 677 | 2 | ▶ | croak $@ if not $@ =~ /^Invalid tag nesting/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 678 | 2 | pop(@st); | |||||||||||||||||||||||||||
| 679 | } | ||||||||||||||||||||||||||||
| 680 | 197 | ▶ | croak qq(Invalid tag nesting) if not @st; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||
| 682 | 197 | ▶ | push @st, $tag if $tag->BodyPermitted; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 683 | } | ||||||||||||||||||||||||||||
| 684 | } | ||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||
| 686 | =head2 parse | ||||||||||||||||||||||||||||
| 687 | |||||||||||||||||||||||||||||
| 688 | my $tree = $parser->parse('[b]BBCode[/b] text.'); | ||||||||||||||||||||||||||||
| 689 | |||||||||||||||||||||||||||||
| 690 | C<parse> creates a parse tree for the given BBCode. The result is a | ||||||||||||||||||||||||||||
| 691 | tree of L<BBCode::Tag|BBCode::Tag> objects. The most common use of the parse tree is | ||||||||||||||||||||||||||||
| 692 | to convert it to HTML using L<BBCode::Tag-E<gt>toHTML()|BBCode::Tag/"toHTML">: | ||||||||||||||||||||||||||||
| 693 | |||||||||||||||||||||||||||||
| 694 | my $html = $tree->toHTML; | ||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||
| 696 | =cut | ||||||||||||||||||||||||||||
| 697 | |||||||||||||||||||||||||||||
| 698 | 61 | ▶ | sub parse($@):method { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 699 | 61 | my $this = shift; | |||||||||||||||||||||||||||
| 700 | 61 | ▶ | $this = $this->new() unless ref $this; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 701 | |||||||||||||||||||||||||||||
| 702 | 61 | my $text = join "\n", @_; | |||||||||||||||||||||||||||
| 703 | 61 | $text =~ s/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F]+//; | |||||||||||||||||||||||||||
| 704 | 61 | $text =~ s/(?:\r\n|\r|\n)/\n/g; | |||||||||||||||||||||||||||
| 705 | |||||||||||||||||||||||||||||
| 706 | 61 | my $tokens = $this->_tokenize(\$text); | |||||||||||||||||||||||||||
| 707 | 61 | my $body = BBCode::Body->new($this); | |||||||||||||||||||||||||||
| 708 | 61 | $this->_parse($body, $tokens); | |||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||
| 710 | 50 | return $body->replaceBody; | |||||||||||||||||||||||||||
| 711 | } | ||||||||||||||||||||||||||||
| 712 | |||||||||||||||||||||||||||||
| 713 | 6 | 1; | |||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||
| 715 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||
| 717 | L<BBCode::Tag|BBCode::Tag> | ||||||||||||||||||||||||||||
| 718 | |||||||||||||||||||||||||||||
| 719 | L<svn://chronos-tachyon.net/projects/BBCode-Parser> | ||||||||||||||||||||||||||||
| 720 | |||||||||||||||||||||||||||||
| 721 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 722 | |||||||||||||||||||||||||||||
| 723 | Donald King E<lt>dlking@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 724 | |||||||||||||||||||||||||||||
| 725 | =cut | ||||||||||||||||||||||||||||