.\" Automatically generated by Pod::Man 2.28 (Pod::Simple 3.28)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings. \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote. \*(C+ will
.\" give a nicer C++. Capital omega is used to do unbreakable dashes and
.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
. ds -- \(*W-
. ds PI pi
. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
. ds L" ""
. ds R" ""
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds -- \|\(em\|
. ds PI \(*p
. ds L" ``
. ds R" ''
. ds C`
. ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{
. if \nF \{
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. if !\nF==2 \{
. nr % 0
. nr F 2
. \}
. \}
.\}
.rr rF
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear. Run. Save yourself. No user-serviceable parts.
. \" fudge factors for nroff and troff
.if n \{\
. ds #H 0
. ds #V .8m
. ds #F .3m
. ds #[ \f1
. ds #] \fP
.\}
.if t \{\
. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
. ds #V .6m
. ds #F 0
. ds #[ \&
. ds #] \&
.\}
. \" simple accents for nroff and troff
.if n \{\
. ds ' \&
. ds ` \&
. ds ^ \&
. ds , \&
. ds ~ ~
. ds /
.\}
.if t \{\
. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
. \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
. \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
. \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
. ds : e
. ds 8 ss
. ds o a
. ds d- d\h'-1'\(ga
. ds D- D\h'-1'\(hy
. ds th \o'bp'
. ds Th \o'LP'
. ds ae ae
. ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "Parse::BBCode 3pm"
.TH Parse::BBCode 3pm "2015-07-11" "perl v5.20.2" "User Contributed Perl Documentation"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
Parse::BBCode \- Module to parse BBCode and render it as HTML or text
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
Parse::BBCode parses common bbcode like
.PP
.Vb 1
\& [b]bold[/b] [size=10]big[/size]
.Ve
.PP
short tags like
.PP
.Vb 1
\& [foo://test]
.Ve
.PP
and custom bbcode tags.
.PP
For the documentation of short tags, see \*(L"\s-1SHORT TAGS\*(R"\s0.
.PP
To parse a bbcode string, set up a parser with the default \s-1HTML\s0 defintions
of Parse::BBCode::HTML:
.PP
.Vb 5
\& # render bbcode to HTML
\& use Parse::BBCode;
\& my $p = Parse::BBCode\->new();
\& my $code = \*(Aqsome [b]b code[/b]\*(Aq;
\& my $rendered = $p\->render($code);
\&
\& # parse bbcode, manipulate tree and render
\& use Parse::BBCode;
\& my $p = Parse::BBCode\->new();
\& my $code = \*(Aqsome [b]b code[/b]\*(Aq;
\& my $tree = $p\->parse($code);
\& # do something with $tree
\& my $rendered = $p\->render_tree($tree);
.Ve
.PP
Or if you want to define your own tags:
.PP
.Vb 4
\& my $p = Parse::BBCode\->new({
\& tags => {
\& # load the default tags
\& Parse::BBCode::HTML\->defaults,
\&
\& # add/override tags
\& url => \*(Aqurl:%{parse}s\*(Aq,
\& i => \*(Aq%{parse}s\*(Aq,
\& b => \*(Aq%{parse}s\*(Aq,
\& noparse => \*(Aq
%{html}s
\*(Aq,
\& code => sub {
\& my ($parser, $attr, $content, $attribute_fallback) = @_;
\& if ($attr eq \*(Aqperl\*(Aq) {
\& # use some syntax highlighter
\& $content = highlight_perl($content);
\& }
\& else {
\& $content = Parse::BBCode::escape_html($$content);
\& }
\& "$content"
\& },
\& test => \*(Aqthis is klingon: %{klingon}s\*(Aq,
\& },
\& escapes => {
\& klingon => sub {
\& my ($parser, $tag, $text) = @_;
\& return translate_into_klingon($text);
\& },
\& },
\& }
\& );
\& my $code = \*(Aqsome [b]b code[/b]\*(Aq;
\& my $parsed = $p\->render($code);
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
If you set up the Parse::BBCode object without arguments, the default tags
are loaded, and any text outside or inside of parseable tags will go through
a default subroutine which escapes \s-1HTML\s0 and replaces newlines with
tags. If you need to change this you can set the options 'url_finder',
\&'text_processor' and 'linebreaks'.
.SS "\s-1METHODS\s0"
.IX Subsection "METHODS"
.IP "new" 4
.IX Item "new"
Constructor. Takes a hash reference with options as an argument.
.Sp
.Vb 10
\& my $parser = Parse::BBCode\->new({
\& tags => {
\& url => ...,
\& i => ...,
\& },
\& escapes => {
\& link => ...,
\& },
\& close_open_tags => 1, # default 0
\& strict_attributes => 0, # default 1
\& direct_attributes => 1, # default 1
\& url_finder => 1, # default 0
\& smileys => 0, # default 0
\& linebreaks => 1, # default 1
\& );
.Ve
.RS 4
.IP "tags" 4
.IX Item "tags"
See \*(L"\s-1TAG DEFINITIONS\*(R"\s0
.IP "escapes" 4
.IX Item "escapes"
See \*(L"\s-1ESCAPES\*(R"\s0
.IP "url_finder" 4
.IX Item "url_finder"
See \*(L"\s-1URL FINDER\*(R"\s0
.IP "smileys" 4
.IX Item "smileys"
If you want to replace smileys with an icon:
.Sp
.Vb 10
\& my $parser = Parse::BBCode\->new({
\& smileys => {
\& base_url => \*(Aq/your/url/to/icons/\*(Aq,
\& icons => { qw/ :\-) smile.png :\-( sad.png / },
\& # sprintf format:
\& # first argument url
\& # second argument original text smiley (HTML escaped)
\& format => \*(Aq\*(Aq,
\& # if you need the url and text in a different order
\& # see perldoc \-f sprintf, e.g.
\& # format => \*(Aq\*(Aq,
\& },
\& });
.Ve
.Sp
This subroutine will be applied during the url_finder (or first, if
url_finder is 0), and the rest will get processed by the text
procesor (default escaping html and replacing linebreaks).
.Sp
Smileys are only replaced if surrounded by whitespace or start/end of line/text.
.Sp
.Vb 1
\& [b]bold
:\-)[/b] :\-(
.Ve
.Sp
In this example both smileys will be replaced. The first smiley is at the end
of the text because the text inside [b][/b] is processed on its own.
.Sp
Open to any suggestions here.
.IP "linebreaks" 4
.IX Item "linebreaks"
The default text processor replaces linebreaks with
\en.
If you don't want this, set 'linebreaks' to 0.
.IP "text_processor" 4
.IX Item "text_processor"
If you need to add any customized text processing (like smiley parsing, for
example), you can pass a subroutine here. Note that this subroutine also
needs to do \s-1HTML\s0 escaping itself!
.Sp
See \*(L"\s-1TEXT PROCESSORS\*(R"\s0
.IP "close_open_tags" 4
.IX Item "close_open_tags"
Default: 0
.Sp
If set to true (1), it will close open tags at the end or before block tags.
.IP "strict_attributes" 4
.IX Item "strict_attributes"
Default: 1
.Sp
If set to true (1), tags with invalid attributes are left unparsed. If set to
false (0), the attribute for this tags will be empty.
.Sp
An invalid attribute:
.Sp
.Vb 1
\& [foo=bar far boo]...[/foo]
.Ve
.Sp
I might add an option to define your own attribute validation. Contact me if
you'd like to have this.
.IP "direct_attributes" 4
.IX Item "direct_attributes"
Default: 1
.Sp
Normal tag syntax is:
.Sp
.Vb 1
\& [tag=val1 attr2=val2 ...]
.Ve
.Sp
If set to 0, tag syntax is
.Sp
.Vb 1
\& [tag attr2=val2 ...]
.Ve
.IP "attribute_quote" 4
.IX Item "attribute_quote"
You can change how the attribute values shuold be quoted.
Default is a double quote (which is still optional):
.Sp
.Vb 5
\& my $parser = Parse::BBCode\->new(
\& attribute_quote => \*(Aq"\*(Aq,
\& ...
\& );
\& [tag="foo" attr="bar" attr2=baz]...[/tag]
.Ve
.Sp
If you set it to single quote:
.Sp
.Vb 5
\& my $parser = Parse::BBCode\->new(
\& attribute_quote => "\*(Aq",
\& ...
\& );
\& [tag=\*(Aqfoo\*(Aq attr=bar attr2=\*(Aqbaz\*(Aq]...[/tag]
.Ve
.Sp
You can also set it to both: \f(CW\*(C`\*(Aq"\*(C'\fR. Then both quoting types are
allowed:
.Sp
.Vb 5
\& my $parser = Parse::BBCode\->new(
\& attribute_quote => q/\*(Aq"/,
\& ...
\& );
\& [tag=\*(Aqfoo\*(Aq attr="bar" attr2=baz]...[/tag]
.Ve
.IP "attribute_parser" 4
.IX Item "attribute_parser"
You can pass a subref that overrides the default attribute parsing.
See \*(L"\s-1ATTRIBUTE PARSING\*(R"\s0
.IP "strip_linebreaks" 4
.IX Item "strip_linebreaks"
Default: 1
.Sp
Strips linebreaks at start/end of block tags
.RE
.RS 4
.RE
.IP "render" 4
.IX Item "render"
Input: The text to parse, optional hashref
.Sp
Returns: the rendered text
.Sp
.Vb 1
\& my $rendered = $parser\->render($bbcode);
.Ve
.Sp
You can pass an optional hashref with information you need inside
of your self-defined rendering subs.
For example if you want to display code in a codebox with a link to
download the code you need the id of the article (in a forum) and the number
of the code tag.
.Sp
.Vb 8
\& my $parsed = $parser\->render($bbcode, { article_id => 23 });
\& # in the rendering sub:
\& my ($parser, $attr, $content, $attribute_fallback, $tag, $info) = @_;
\& my $article_id = $parser\->get_params\->{article_id};
\& my $code_id = $tag\->get_num;
\& # write downloadlink like
\& # download.pl?article_id=$article_id;code_id=$code_id
\& # in front of the displayed code
.Ve
.Sp
See examples/code_download.pl for a complete example of how to set up
the rendering and how to extract the code from the tree. If run as a \s-1CGI\s0
skript it will give you a dialogue to save the code into a file, including
a reasonable default filename.
.IP "parse" 4
.IX Item "parse"
Input: The text to parse.
.Sp
Returns: the parsed tree (a Parse::BBCode::Tag object)
.Sp
.Vb 1
\& my $tree = $parser\->parse($bbcode);
.Ve
.IP "render_tree" 4
.IX Item "render_tree"
Input: the parse tree
.Sp
Returns: The rendered text
.Sp
.Vb 1
\& my $parsed = $parser\->render_tree($tree);
.Ve
.Sp
You can pass an optional hashref, for explanation see \*(L"render\*(R"
.IP "forbid" 4
.IX Item "forbid"
.Vb 1
\& $parser\->forbid(qw/ img url /);
.Ve
.Sp
Disables the given tags.
.IP "permit" 4
.IX Item "permit"
.Vb 1
\& $parser\->permit(qw/ img url /);
.Ve
.Sp
Enables the given tags if they are in the tag definitions.
.IP "escape_html" 4
.IX Item "escape_html"
Utility to substitute
.Sp
.Vb 1
\& <>&"\*(Aq
.Ve
.Sp
with their \s-1HTML\s0 entities.
.Sp
.Vb 1
\& my $escaped = Parse::BBCode::escape_html($text);
.Ve
.IP "error" 4
.IX Item "error"
If the given bbcode is invalid (unbalanced or wrongly nested classes),
currently \fIParse::BBCode::render()\fR will either leave the invalid tags
unparsed, or, if you set the option \f(CW\*(C`close_open_tags\*(C'\fR, try to add closing
tags.
If this happened \f(CW\*(C`error()\*(C'\fR will return the invalid tag(s), otherwise false.
To get the corrected bbcode (if you set \f(CW\*(C`close_open_tags\*(C'\fR) you can get
the tree and return the raw text from it:
.Sp
.Vb 4
\& if ($parser\->error) {
\& my $tree = $parser\->get_tree;
\& my $corrected = $tree\->raw_text;
\& }
.Ve
.IP "parse_attributes" 4
.IX Item "parse_attributes"
You can inherit from Parse::BBCode and define your own attribute
parsing. See \*(L"\s-1ATTRIBUTE PARSING\*(R"\s0.
.IP "new_tag" 4
.IX Item "new_tag"
Returns a Parse::BBCode::Tag object.
It just does:
shift;
Parse::BBCode::Tag\->new(@_);
.Sp
If you want your own tag class, inherit from Parse::BBCode and
let it return Parse::BBCode::YourTag\->new
.SS "\s-1TAG DEFINITIONS\s0"
.IX Subsection "TAG DEFINITIONS"
Here is an example of all the current definition possibilities:
.PP
.Vb 10
\& my $p = Parse::BBCode\->new({
\& tags => {
\& i => \*(Aq%s\*(Aq,
\& b => \*(Aq%{parse}s\*(Aq,
\& size => \*(Aq%{parse}s\*(Aq,
\& url => \*(Aqurl:%{parse}s\*(Aq,
\& wikipedia => \*(Aqurl:%{parse}s\*(Aq,
\& noparse => \*(Aq%{html}s
\*(Aq,
\& quote => \*(Aqblock:%s
\*(Aq,
\& code => {
\& code => sub {
\& my ($parser, $attr, $content, $attribute_fallback) = @_;
\& if ($attr eq \*(Aqperl\*(Aq) {
\& # use some syntax highlighter
\& $content = highlight_perl($$content);
\& }
\& else {
\& $content = Parse::BBCode::escape_html($$content);
\& }
\& "$content"
\& },
\& parse => 0,
\& class => \*(Aqblock\*(Aq,
\& },
\& hr => {
\& class => \*(Aqblock\*(Aq,
\& output => \*(Aq
\*(Aq,
\& single => 1,
\& },
\& },
\& }
\& );
.Ve
.PP
The following list explains the above tag definitions:
.ie n .IP "%s" 4
.el .IP "\f(CW%s\fR" 4
.IX Item "%s"
.Vb 1
\& i => \*(Aq%s\*(Aq
\&
\& [i] italic [/i]
\& turns out as
\& italic <html>
.Ve
.Sp
So \f(CW%s\fR stands for the tag content. By default, it is parsed itself,
so that you can nest tags.
.ie n .IP """%{parse}s""" 4
.el .IP "\f(CW%{parse}s\fR" 4
.IX Item "%{parse}s"
.Vb 1
\& b => \*(Aq%{parse}s\*(Aq
\&
\& [b] bold [/b]
\& turns out as
\& bold <html>
.Ve
.Sp
\&\f(CW\*(C`%{parse}s\*(C'\fR is the same as \f(CW%s\fR because 'parse' is the default.
.ie n .IP "%a" 4
.el .IP "\f(CW%a\fR" 4
.IX Item "%a"
.Vb 1
\& size => \*(Aq%{parse}s\*(Aq
\&
\& [size=7] some big text [/size]
\& turns out as
\& some big text
.Ve
.Sp
So \f(CW%a\fR stands for the tag attribute. By default it will be \s-1HTML\s0
escaped.
.ie n .IP "url tag, %A, ""%{link}A""" 4
.el .IP "url tag, \f(CW%A\fR, \f(CW%{link}A\fR" 4
.IX Item "url tag, %A, %{link}A"
.Vb 1
\& url => \*(Aqurl:%{parse}s\*(Aq
.Ve
.Sp
the first thing you can see is the \f(CW\*(C`url:\*(C'\fR at the beginning \- this
defines the url tag as a tag with the class 'url', and urls must not
be nested. So this class definition is mainly there to prevent
generating wrong \s-1HTML.\s0 if you nest url tags only the outer one will
be parsed.
.Sp
another thing you can see is how to apply a special escape. The attribute
defined with \f(CW\*(C`%{link}a\*(C'\fR is checked for a valid \s-1URL.
\&\s0\f(CW\*(C`javascript:\*(C'\fR will be filtered.
.Sp
.Vb 3
\& [url=/foo.html]a link[/url]
\& turns out as
\& a link
.Ve
.Sp
Note that a tag like
.Sp
.Vb 1
\& [url]http://some.link.example[/url]
.Ve
.Sp
will turn out as
.Sp
.Vb 1
\& http://some.link.example
.Ve
.Sp
In the cases where the attribute should be the same as the
content you should use \f(CW%A\fR instead of \f(CW%a\fR which takes
the content as the attribute as a fallback. You probably
need this in all url-like tags:
.Sp
.Vb 1
\& url => \*(Aqurl:%{parse}s\*(Aq,
.Ve
.ie n .IP """%{uri}A""" 4
.el .IP "\f(CW%{uri}A\fR" 4
.IX Item "%{uri}A"
You might want to define your own urls, e.g. for wikipedia
references:
.Sp
.Vb 1
\& wikipedia => \*(Aqurl:%{parse}s\*(Aq,
.Ve
.Sp
\&\f(CW\*(C`%{uri}A\*(C'\fR will uri-encode the searched term:
.Sp
.Vb 5
\& [wikipedia]Harold & Maude[/wikipedia]
\& [wikipedia="Harold & Maude"]a movie[/wikipedia]
\& turns out as
\& Harold & Maude
\& a movie
.Ve
.IP "Don't parse tag content" 4
.IX Item "Don't parse tag content"
Sometimes you need to display verbatim bbcode. The simplest
form would be a noparse tag:
.Sp
.Vb 1
\& noparse => \*(Aq%{html}s
\*(Aq
\&
\& [noparse] [some]unbalanced[/foo] [/noparse]
.Ve
.Sp
With this definition the output would be
.Sp
.Vb 1
\& [some]unbalanced[/foo]
.Ve
.Sp
So inside a noparse tag you can write (almost) any invalid bbcode.
The only exception is the noparse tag itself:
.Sp
.Vb 1
\& [noparse] [some]unbalanced[/foo] [/noparse] [b]really bold[/b] [/noparse]
.Ve
.Sp
Output:
.Sp
.Vb 1
\& [some]unbalanced[/foo] really bold [/noparse]
.Ve
.Sp
Because the noparse tag ends at the first closing tag, even if you
have an additional opening noparse tag inside.
.Sp
The \f(CW\*(C`%{html}s\*(C'\fR defines that the content should be \s-1HTML\s0 escaped.
If you don't want any escaping you can't say \f(CW%s\fR because the default
is 'parse'. In this case you have to write \f(CW\*(C`%{noescape}\*(C'\fR.
.IP "Block tags" 4
.IX Item "Block tags"
.Vb 1
\& quote => \*(Aqblock:%s
\*(Aq,
.Ve
.Sp
To force valid html you can add classes to tags. The default
class is 'inline'. To declare it as a block add \f(CW\*(C`\*(Aqblock:"\*(C'\fR to the start
of the string.
Block tags inside of inline tags will either close the outer tag(s) or
leave the outer tag(s) unparsed, depending on the option \f(CW\*(C`close_open_tags\*(C'\fR.
.IP "Define subroutine for tag" 4
.IX Item "Define subroutine for tag"
All these definitions might not be enough if you want to define
your own code, for example to add a syntax highlighter.
.Sp
Here's an example:
.Sp
.Vb 10
\& code => {
\& code => sub {
\& my ($parser, $attr, $content, $attribute_fallback, $tag, $info) = @_;
\& if ($attr eq \*(Aqperl\*(Aq) {
\& # use some syntax highlighter
\& $content = highlight_perl($$content);
\& }
\& else {
\& $content = Parse::BBCode::escape_html($$content);
\& }
\& "$content"
\& },
\& parse => 0,
\& class => \*(Aqblock\*(Aq,
\& },
.Ve
.Sp
So instead of a string you define a hash reference with a 'code'
key and a sub reference.
The other key is \f(CW\*(C`parse\*(C'\fR which is 0 by default. If it is 0 the
content in the tag won't be parsed, just as in the noparse tag above.
If it is set to 1 you will get the rendered content as an argument to
the subroutine.
.Sp
The first argument to the subroutine is the Parse::BBCode object itself.
The second argument is the attribute, the third is the already rendered tag
content as a scalar reference and the fourth argument is the attribute
fallback which is set to the content if the attribute is empty. The fourth
argument is just for convenience.
The fifth argument is the tag object (Parse::BBCode::Tag) itself, so if
necessary you can get the original tag content by using:
.Sp
.Vb 1
\& my $original = $tag\->raw_text;
.Ve
.Sp
The sixth argument is an info hash. It contains:
.Sp
.Vb 5
\& my $info = {
\& tags => $tags,
\& stack => $stack,
\& classes => $classes,
\& };
.Ve
.Sp
The variable \f(CW$tags\fR is a hashref which contains all tag names which are
outside the current tag, with a count. This is convenient if you have
to check if the current processed tag is inside a certain tag and you
want to behave differently, like
.Sp
.Vb 3
\& if ($info\->{tags}\->{special}) {
\& # we are somewhere inside [special]...[/special]
\& }
.Ve
.Sp
The variable \f(CW$stack\fR contains an array ref with all outer tag names.
So while processing the tag 'i' in
.Sp
.Vb 1
\& [quote][quote][b]bold [i]italic[/i][/b][/quote][/quote]
.Ve
.Sp
it contains
[qw/ quote quote b i /]
.Sp
The variable \f(CW$classes\fR contains a hashref with all tag classes and their
counts outside of the current processed tag.
For example if you want to process URIs with URI::Find, and you are
already in a tag with the class 'url' then you don't want to use URI::Find
here.
.Sp
.Vb 4
\& unless ($info\->{classes}\->{url}) {
\& # not inside of a url class tag ([url], [wikipedia, etc.)
\& # parse text for urls with URI::Find
\& }
.Ve
.IP "Single-Tags" 4
.IX Item "Single-Tags"
Sometimes you might want single tags like for a horizontal line:
.Sp
.Vb 5
\& hr => {
\& class => \*(Aqblock\*(Aq,
\& output => \*(Aq
\*(Aq,
\& single => 1,
\& },
.Ve
.Sp
The hr-Tag is a block tag (should not be inside inline tags),
and it has no closing tag (option \f(CW\*(C`single\*(C'\fR)
.Sp
.Vb 3
\& [hr]
\& Output:
\&
.Ve
.SH "ESCAPES"
.IX Header "ESCAPES"
.Vb 7
\& my $p = Parse::BBCode\->new({
\& ...
\& escapes => {
\& link => sub {
\& },
\& },
\& });
.Ve
.PP
You can define or override escapes. Default escapes are html, uri, link, email,
htmlcolor, num.
An escape functions as a validator and filter. For example, the 'link' escape
looks if it got a valid \s-1URI \s0(starting with \f(CW\*(C`/\*(C'\fR or \f(CW\*(C`\ew+://\*(C'\fR) and html-escapes
it. It returns the empty string if the input is invalid.
.PP
See \*(L"default_escapes\*(R" in Parse::BBCode::HTML for the detailed list of escapes.
.SH "URL FINDER"
.IX Header "URL FINDER"
Usually one wants to also create hyperlinks from any url found in the
bbcode, not only in url tags.
The following code will use URI::Find to search for all types of
urls (unless inside of a url tag itself), create a link in the given
format and html-escape the rest.
If the url is longer than 50 chars, it will cut the link title and append three dots.
If you set max_length to 0, the title won't be cut.
.PP
.Vb 8
\& my $p = Parse::BBCode\->new({
\& url_finder => {
\& max_length => 50,
\& # sprintf format:
\& format => \*(Aq%s\*(Aq,
\& },
\& tags => ...
\& });
.Ve
.PP
Note: If you use the special tag '' in the tag definitions you will
overwrite the url finder and have to do that yourself.
.PP
Alternative:
.PP
.Vb 3
\& my $p = Parse::BBCode\->new({
\& url_finder => 1,
\& ...
.Ve
.PP
This will use the default like shown above (max length 50 chars).
.PP
Default is 0.
.SH "ATTRIBUTES"
.IX Header "ATTRIBUTES"
There are two types of tags. The default (option direct_attributes=1):
.PP
.Vb 2
\& [foo=bar a=b c=d]
\& [foo="text with space" a=b c=d]
.Ve
.PP
The parsed attribute structure will look like:
.PP
.Vb 1
\& [ [\*(Aqbar\*(Aq], [\*(Aqa\*(Aq => \*(Aqb\*(Aq], [\*(Aqc\*(Aq => \*(Aqd\*(Aq] ]
.Ve
.PP
Another bbcode variant doesn't use direct attributes:
.PP
.Vb 1
\& [foo a=b c=d]
.Ve
.PP
The resulting attribute structure will have an empty first element:
.PP
.Vb 1
\& [ [\*(Aq\*(Aq], [\*(Aqa\*(Aq => \*(Aqb\*(Aq], [\*(Aqc\*(Aq => \*(Aqd\*(Aq] ]
.Ve
.SH "ATTRIBUTE PARSING"
.IX Header "ATTRIBUTE PARSING"
If you have bbcode attributes that don't fit into the two standard
syntaxes you can inherit from Parse::BBCode and overwrite the
parse_attributes method, or you can pass an option attribute_parser
contaning a subref.
.PP
Example:
.PP
.Vb 1
\& [size=10]big[/size] [foo|bar|boo]footext[/foo] end
.Ve
.PP
The size tag should be parsed normally, the foo tag needs different parsing.
.PP
.Vb 10
\& sub parse_attributes {
\& my ($self, %args) = @_;
\& # $$text contains \*(Aq|bar|boo]footext[/foo] end
\& my $text = $args{text};
\& my $tagname = $args{tag}; # \*(Aqfoo\*(Aq
\& if ($tagname eq \*(Aqfoo\*(Aq) {
\& # work on $$text
\& # result should be something like:
\& # $$text should contain \*(Aqfootext[/foo] end\*(Aq
\& my $valid = 1;
\& my @attr = ( [\*(Aq\*(Aq], [1 => \*(Aqbar\*(Aq], [2 => \*(Aqboo\*(Aq] );
\& my $attr_string = \*(Aq|bar|boo\*(Aq;
\& return ($valid, [@attr], $attr_string, \*(Aq]\*(Aq);
\& }
\& else {
\& return shift\->SUPER::parse_attributes(@_);
\& }
\& }
\& my $parser = Parse::BBCode\->new({
\& ...
\& attribute_parser => \e&parse_attributes,
\& });
.Ve
.PP
If the attributes are not valid, return
.PP
.Vb 1
\& 0, [ [\*(Aq\*(Aq] ], \*(Aq|bar|boo\*(Aq, \*(Aq]\*(Aq
.Ve
.PP
If you don't find a closing square bracket, return:
.PP
.Vb 1
\& 0, [ [\*(Aq\*(Aq] ], \*(Aq|bar|boo\*(Aq, \*(Aq\*(Aq
.Ve
.SH "TEXT PROCESSORS"
.IX Header "TEXT PROCESSORS"
If you set url_finder and linebreaks to 1, the default text processor
will work like this:
.PP
.Vb 4
\& my $post_processor = \e&sub_for_escaping_HTML;
\& $text = code_to_replace_urls($text, $post_processor);
\& $text =~ s/\er?\en|\er/
\en/g;
\& return $text;
.Ve
.PP
It will be applied to text outside of bbcode and inside of parseable
bbcode tags (and not to code tags or other tags with unparsed content).
.PP
If you need an additional post processor this usually cannot be done
after the \s-1HTML\s0 escaping and url finding. So if you write a text processor it
must do the \s-1HTML\s0 escaping itself.
For example if you want to replace smileys with image tags you cannot simply do:
.PP
.Vb 1
\& $text =~ s/ :\-\e) //g;
.Ve
.PP
because then the image tag would be \s-1HTML\s0 escaped after that.
On the other hand it's usually not possible to do something like
that *after* the \s-1HTML\s0 escaping since that might introduce text
sequences that look like a smiley (or whatever you want to replace).
.PP
So a simple example for a customized text processor would be:
.PP
.Vb 10
\& ...
\& url_finder => 1,
\& linebreaks => 1,
\& text_processor => sub {
\& # for $info hash description see render() method
\& my ($text, $info) = @_;
\& my $out = \*(Aq\*(Aq;
\& while ($text =~ s/(.*)( |^)(:\e))(?= |$)//mgs) {
\& # match a smiley and anything before
\& my ($pre, $sp, $smiley) = ($1, $2, $3);
\& # escape text and add smiley image tag
\& $out .= Parse::BBCode::escape_html($pre) . $sp . \*(Aq\*(Aq;
\& }
\& # leftover text
\& $out .= Parse::BBCode::escape_html($text);
\& return $out;
\& },
.Ve
.PP
This will result in:
Replacing urls, applying your text_processor to the rest of the text and
after that replace linebreaks with
tags.
.PP
If you want to completely define the plain text processor yourself (ignoring
the 'linebreak', 'url_finder', 'smileys' and 'text_processor' options) you define the
special tag with the empty string:
.PP
.Vb 8
\& my $p = Parse::BBCode\->new({
\& tags => {
\& \*(Aq\*(Aq => sub {
\& my ($parser, $attr, $content, $info) = @_;
\& return frobnicate($content);
\& # remember to escape HTML!
\& },
\& ...
.Ve
.SH "SHORT TAGS"
.IX Header "SHORT TAGS"
It can be very convenient to have short tags like [foo://id].
This is not really a part of BBCode, but I consider it as quite similar,
so I added it to this module.
For example to link to threads, cpan modules or wikipedia articles:
.PP
.Vb 4
\& [thread://123]
\& [thread://123|custom title]
\& # can be implemented so that it links to thread 123 in the forum
\& # and additionally fetch the thread title.
\&
\& [cpan://Module::Foo|some useful module]
\&
\& [wikipedia://Harold & Maude]
.Ve
.PP
You can define a short tag by adding the option \f(CW\*(C`short\*(C'\fR. The tag will
work as a classic tag and short tag. If you only want to support the
short version, set the option \f(CW\*(C`classic\*(C'\fR to 0.
.PP
.Vb 10
\& my $p = Parse::BBCode\->new({
\& tags => {
\& Parse::BBCode::HTML\->defaults,
\& wikipedia => {
\& short => 1,
\& output => \*(Aq%{parse}s\*(Aq,
\& class => \*(Aqurl\*(Aq,
\& classic => 0, # don\*(Aqt support classic [wikipedia]...[/wikipedia]
\& },
\& thread => {
\& code => sub {
\& my ($parser, $attr, $content, $attribute_fallback) = @_;
\& my $id = $attribute_fallback;
\& if ($id =~ tr/0\-9//c) {
\& return \*(Aq[thread]\*(Aq . encode_entities($id) . \*(Aq[/thread]\*(Aq;
\& }
\& my $name;
\& if ($attr) {
\& # custom title will be in $attr
\& # [thread=123]custom title[/thread]
\& # [thread://123|custom title]
\& # already escaped
\& $name = $$content;
\& }
\& return qq{$name};
\& },
\& short => 1,
\& classic => 1, # default is 1
\& },
\& },
\& }
\& );
.Ve
.SH "WHY ANOTHER BBCODE PARSER"
.IX Header "WHY ANOTHER BBCODE PARSER"
I wrote this module because HTML::BBCode is not extendable (or
I didn't see how) and BBCode::Parser seemed good at the first
glance but has some issues, for example it says that the following bbode
.PP
.Vb 1
\& [code] foo [b] [/code]
.Ve
.PP
is invalid, while I think you should be able to write unbalanced code
in code tags.
Also BBCode::Parser dies if you have invalid code or not-permitted tags,
but in a forum you'd rather show a partly parsed text then an error
message.
.PP
What I also wanted is an easy syntax to define own tags, ideally \- for
simple tags \- as plain text, so you can put it in a configuration file.
This allows forum admins to add tags easily. Some forums might want
a tag for linking to perlmonks.org, other forums need other tags.
.PP
Another goal was to always output a result and don't die. I might add an
option which lets the parser die with unbalanced code.
.SH "WHY BBCODE?"
.IX Header "WHY BBCODE?"
Some forums and blogs prefer a kind of pseudo \s-1HTML\s0 for user comments.
The arguments against bbcode is usually: \*(L"Why should people learn an
additional markup language if they can just use \s-1HTML\s0?\*(R" The problem is
that many people don't know \s-1HTML.\s0
.PP
BBCode is often a bit shorter, for example if you have a code tag
with an attribute that tells the parser what language the content is in.
.PP
.Vb 2
\& [code=perl]...[/code]
\& ...
.Ve
.PP
Also, forum \s-1HTML\s0 is usually not real \s-1HTML.\s0 It is usually a subset and
sometimes with additional tags.
So in the backend you need to parse it anyway to turn it into real \s-1HTML.\s0
.PP
BBCode is widely known and used.
Unfortunately though, there is no specification; some forums only allow
attributes in double quotes, some forums implement only one attribute that
can be separated by spaces, which makes it difficult to parse if you
want to support more than one attribute.
.PP
I tried to support the most common syntax (attributes without quotes, in
single or double quotes) and tags.
If you need additional tags it's relatively easy to implement them.
For example in my forum I implemented a [more] tag that hides long
text or code in thread view. Without Javascript you will see the expanded
content when clicking on the single article, or with Javascript the
content will be added inline via Ajax.
.SH "TODO"
.IX Header "TODO"
.IP "BBCode to Textile|Markdown" 4
.IX Item "BBCode to Textile|Markdown"
There is a Parse::BBCode::Markdown module which is only
roughly tested.
.IP "\s-1API\s0" 4
.IX Item "API"
The main syntax is likely to stay, only the \s-1API\s0 for callbacks
might change. At the moment it is not possible to add callbacks
to the parsing process, only for the rendering phase.
.SH "REQUIREMENTS"
.IX Header "REQUIREMENTS"
perl >= 5.8.0, Class::Accessor::Fast, URI::Escape
.SH "SEE ALSO"
.IX Header "SEE ALSO"
BBCode::Parser \- a parser which supplies the parsed tree if necessary. Too
strict though for using in forums where people write unbalanced bbcode
.PP
HTML::BBCode \- simple processor, no parse tree, good enough for processing
usual bbcode with the most common tags
.PP
HTML::BBReverse \- really simple proccessor, just replaces start and end tags
independently by their \s-1HTML\s0 aequivalents, so not very useful in many cases
.PP
See \f(CW\*(C`examples/compare.html\*(C'\fR for a feature comparison of the
modules and feel free to report mistakes.
.PP
See \f(CW\*(C`examples/bench.pl\*(C'\fR for a benchmark of the modules.
.SH "BUGS"
.IX Header "BUGS"
Please report bugs at http://rt.cpan.org/NoAuth/Bugs.html?Dist=Parse\-BBCode
or https://github.com/perlpunk/Parse\-BBCode/issues
.SH "AUTHOR"
.IX Header "AUTHOR"
Tina Mueller
.SH "CREDITS"
.IX Header "CREDITS"
Thanks to Moritz Lenz for his suggestions about the implementation
and the test cases.
.PP
Viacheslav Tikhanovskii
.PP
Sascha Kiefer
.SH "COPYRIGHT AND LICENSE"
.IX Header "COPYRIGHT AND LICENSE"
Copyright (C) 2014 by Tina Mueller
.PP
This library is free software; you can redistribute it and/or modify it under
the same terms as Perl itself, either Perl version 5.6.1 or, at your option,
any later version of Perl 5 you may have available.