<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">NAME
    Parse::BBCode - Module to turn BBCode into HTML or plain text

SYNOPSIS
    To parse a bbcode string:

        use Parse::BBCode;
        my $p = Parse::BBCode-&gt;new();
        my $code = 'some [b]b code[/b]';
        my $parsed = $p-&gt;render($code);

    This sets up a parser with the default HTML defintions of
    Parse::BBCode::HTML.

        # or if you want to define your own tags:
        my $p = Parse::BBCode-&gt;new({
                tags =&gt; {
                    url =&gt; '&lt;a href="%{link}A"&gt;%{parse}s&lt;/a&gt;',
                    i   =&gt; '&lt;i&gt;%{parse}s&lt;/i&gt;',
                    b   =&gt; '&lt;b&gt;%{parse}s&lt;/b&gt;',
                    noparse =&gt; '&lt;pre&gt;%{html}s&lt;/pre&gt;',
                    code =&gt; sub {
                        my ($parser, $attr, $content, $attribute_fallback) = @_;
                        if ($attr eq 'perl') {
                            # use some syntax highlighter
                            $content = highlight_perl($content);
                        }
                        else {
                            $content = Parse::BBCode::escape_html($content);
                        }
                        "&lt;tt&gt;$content&lt;/tt&gt;"
                    },
                    test =&gt; 'this is klingon: %{klingon}s',
                },
                escapes =&gt; {
                    klingon =&gt; sub {
                        my ($parser, $tag, $text) = @_;
                        return translate_into_klingon($text);
                    },
                },
            }
        );
        my $code = 'some [b]b code[/b]';
        my $parsed = $p-&gt;render($code);

DESCRIPTION
    Note: This module is still experimental, the syntax is subject to
    change. I'm open for any suggestions on how to improve the syntax.

    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 he following bbode

        [code] foo [b] [/code]

    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.

    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.

    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.

  METHODS
    new Constructor. Takes a hash reference with options as an argument.

            my $parser = Parse::BBCode-&gt;new({
                tags =&gt; {
                    url =&gt; ...,
                    i   =&gt; ...,
                },
                escapes =&gt; {
                    link =&gt; ...,
                },
                close_open_tags   =&gt; 1, # default 0
                strict_attributes =&gt; 0, # default 0
            );

        tags
            See "TAG DEFINITIONS"

        escapes
            See "ESCAPES"

        close_open_tags
            If set to true (1), it will close open tags at the end or before
            block tags.

        strict_attributes
            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.

            An invalid attribute:

                [foo=bar far boo]...[/foo]

            I might add an option to define your own attribute validation.
            Contact me if you'd like to have this.

    render
        Input: The text to parse

        Returns: the rendered text

            my $parsed = $parser-&gt;render($bbcode);

    parse
        Input: The text to parse.

        Returns: the parsed tree (a Parse::BBCode::Tag object)

            my $tree = $parser-&gt;parse($bbcode);

    render_tree
        Input: the parse tree

        Returns: The rendered text

            my $parsed = $parser-&gt;render_tree($tree);

    forbid
            $parser-&gt;forbid(qw/ img url /);

        Disables the given tags.

    permit
            $parser-&gt;permit(qw/ img url /);

        Enables the given tags if they are in the tag definitions.

    escape_html
        Utility to substitute

            &lt;&gt;&amp;"'

        with their HTML entities.

            my $escaped = Parse::BBCode::escape_html($text);

    error
        If the given bbcode is invalid (unbalanced or wrongly nested
        classes), currently Parse::BBCode::render() will either leave the
        invalid tags unparsed, or, if you set the option "close_open_tags",
        try to add closing tags. If this happened "error()" will return the
        invalid tag(s), otherwise false. To get the corrected bbcode (if you
        set "close_open_tags") you can get the tree and return the raw text
        from it:

            if ($parser-&gt;error) {
                my $tree = $parser-&gt;get_tree;
                my $corrected = $tree-&gt;raw_text;
            }

  TAG DEFINITIONS
    Here is an example of all the current definition possibilities:

        my $p = Parse::BBCode-&gt;new({
                tags =&gt; {
                    '' =&gt; sub {
                        my $e = Parse::BBCode::escape_html($_[1]);
                        $e =~ s/\r?\n|\r/&lt;br&gt;\n/g;
                        $e
                    },
                    i   =&gt; '&lt;i&gt;%s&lt;/i&gt;',
                    b   =&gt; '&lt;b&gt;%{parse}s&lt;/b&gt;',
                    size =&gt; '&lt;font size="%a"&gt;%{parse}s&lt;/font&gt;',
                    url =&gt; '&lt;a href="%{link}A"&gt;%{parse}s&lt;/a&gt;',
                    wikipedia =&gt; '&lt;a href="http://wikipedia.../?search=%{uri}A"&gt;%{parse}s&lt;/a&gt;',
                    noparse =&gt; '&lt;pre&gt;%{html}s&lt;/pre&gt;',
                    quote =&gt; 'block:&lt;blockquote&gt;%s&lt;/blockquote&gt;',
                    code =&gt; {
                        code =&gt; sub {
                            my ($parser, $attr, $content, $attribute_fallback) = @_;
                            if ($attr eq 'perl') {
                                # use some syntax highlighter
                                $content = highlight_perl($content);
                            }
                            else {
                                $content = Parse::BBCode::escape_html($content);
                            }
                            "&lt;tt&gt;$content&lt;/tt&gt;"
                        },
                        parse =&gt; 0,
                        class =&gt; 'block',
                    },
                },
            }
        );

    The following list explains the above tag definitions:

    Plain text not in tags
        This defines how plain text should be rendered:

            '' =&gt; sub {
                my $e = Parse::BBCode::escape_html($_[2]);
                $e =~ s/\r?\n|\r/&lt;br&gt;\n/g;
                $e
            },

        In the most cases, you would want HTML escaping like shown above.
        This is the default, so you can leave it out. Only if you want to
        render BBCode into plain text or something else, you need this
        option.

    %s
            i =&gt; '&lt;i&gt;%s&lt;/i&gt;'

            [i] italic &lt;html&gt; [/i]
            turns out as
            &lt;i&gt; italic &amp;lt;html&amp;gt; &lt;/i&gt;

        So %s stands for the tag content. By default, it is parsed itself,
        so that you can nest tags.

    "%{parse}s"
            b   =&gt; '&lt;b&gt;%{parse}s&lt;/b&gt;'

            [b] bold &lt;html&gt; [/b]
            turns out as
            &lt;b&gt; bold &amp;lt;html&amp;gt; &lt;/b&gt;

        "%{parse}s" is the same as %s because 'parse' is the default.

    %a
            size =&gt; '&lt;font size="%a"&gt;%{parse}s&lt;/font&gt;'

            [size=7] some big text [/size]
            turns out as
            &lt;font size="7"&gt; some big text &lt;/font&gt;

        So %a stands for the tag attribute. By default it will be HTML
        escaped.

    %A, "%{link}A"
            url =&gt; '&lt;a href="%{link}a"&gt;%{parse}s&lt;/a&gt;'

        Here you can see how to apply a special escape. The attribute
        defined with "%{link}a" is checked for a valid URL. "javascript:"
        will be filtered.

            [url=/foo.html]a link[/url]
            turns out as
            &lt;a href="/foo.html"&gt;a link&lt;/a&gt;

        Note that a tag like

            [url]http://some.link.example[/url]

        will turn out as

            &lt;a href=""&gt;http://some.link.example&lt;/a&gt;

        In the cases where the attribute should be the same as the content
        you should use %A instead of %a which takes the content as the
        attribute as a fallback. You probably need this in all url-like
        tags:

            url =&gt; '&lt;a href="%{link}A"&gt;%{parse}s&lt;/a&gt;',

    "%{uri}A"
        You might want to define your own urls, e.g. for wikipedia
        references:

            wikipedia =&gt; '&lt;a href="http://wikipedia/?search=%{uri}A"&gt;%{parse}s&lt;/a&gt;',

        "%{uri}A" will uri-encode the searched term:

            [wikipedia]Harold &amp; Maude[/wikipedia]
            [wikipedia="Harold &amp; Maude"]a movie[/wikipedia]
            turns out as
            &lt;a href="http://wikipedia/?search=Harold+%26+Maude"&gt;Harold &amp;amp; Maude&lt;/a&gt;
            &lt;a href="http://wikipedia/?search=Harold+%26+Maude"&gt;a movie&lt;/a&gt;

    Don't parse tag content
        Sometimes you need to display verbatim bbcode. The simplest form
        would be a noparse tag:

            noparse =&gt; '&lt;pre&gt;%{html}s&lt;/pre&gt;'

            [noparse] [some]unbalanced[/foo] [/noparse]

        With this definition the output would be

            &lt;pre&gt; [some]unbalanced[/foo] &lt;/pre&gt;

        So inside a noparse tag you can write (almost) any invalid bbcode.
        The only exception is the noparse tag itself:

            [noparse] [some]unbalanced[/foo] [/noparse] [b]really bold[/b] [/noparse]

        Output:

            [some]unbalanced[/foo] &lt;b&gt;really bold&lt;/b&gt; [/noparse]

        Because the noparse tag ends at the first closing tag, even if you
        have an additional opening noparse tag inside.

        The "%{html}s" defines that the content should be HTML escaped. If
        you don't want any escaping you can't say %s because the default is
        'parse'. In this case you have to write "%{noescape}".

    Block tags
            quote =&gt; 'block:&lt;blockquote&gt;%s&lt;/blockquote&gt;',

        To force valid html you can add classes to tags. The default class
        is 'inline'. To declare it as a block add "'block:"" 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 "close_open_tags".

    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.

        Here's an example:

            code =&gt; {
                code =&gt; sub {
                    my ($parser, $attr, $content, $attribute_fallback) = @_;
                    if ($attr eq 'perl') {
                        # use some syntax highlighter
                        $content = highlight_perl($$content);
                    }
                    else {
                        $content = Parse::BBCode::escape_html($$content);
                    }
                    "&lt;tt&gt;$content&lt;/tt&gt;"
                },
                parse =&gt; 0,
                class =&gt; 'block',
            },

        So instead of a string you define a hash reference with a 'code' key
        and a sub reference. The other key is "parse" 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.

        The first argument to the subroutine is the Parse::BBCode object
        itself. The second argument is the attribute, the third the 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.

ESCAPES
        my $p = Parse::BBCode-&gt;new(
            ...
            escapes =&gt; {
                link =&gt; sub {
                },
            },
        );

    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 URI (starting
    with "/" or "\w+://") and html-escapes it. It returns the empty string
    if the input is invalid.

    See "default_escapes" in Parse::BBCode::HTML for the detailed list of
    escapes.

TODO
    Add a bbcode-to-textile module.

REQUIREMENTS
    perl &gt;= 5.6.1, Class::Accessor::Fast, URI::Escape

SEE ALSO
    BBCode::Parser, HTML::BBCode, HTML::BBReverse

    See "examples/compare.html" for a feature comparison of the modules and
    feel free to report mistakes.

    See "examples/bench.pl" for a benchmark of the modules.

BUGS
    Please report bugs at
    http://rt.cpan.org/NoAuth/Bugs.html?Dist=Parse-BBCode

AUTHOR
    Tina Mueller

CREDITS
    Thanks to Moritz Lenz for his suggestions about the implementation and
    the test cases.

COPYRIGHT AND LICENSE
    Copyright (C) 2008 by Tina Mueller

    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.

</pre></body></html>