HTML::ForumCode - BBCode-esque forum markup


Template-Plugin-ForumCode documentation Contained in the Template-Plugin-ForumCode distribution.

Index


Code Index:

NAME

Top

HTML::ForumCode - BBCode-esque forum markup

SYNOPSIS

Top

Usage in a perl module:

  use HTML::ForumCode;

  my $tt_forum  = HTML::ForumCode->new();
  my $formatted = $tt_forum->forumcode($text);

Standard usage in a Template Toolkit file:

  # load the TT module
  [% USE ForumCode %]

  # ForumCodify some text
  [% ForumCode.forumcode('[b]bold[/u] [u]underlined[/u] [i]italic[/i]') %]
  [% ForumCode.forumcode('**bold** __underlined__') %]

DESCRIPTION

Top

This module implements ForumCode, a simple markup language inspired by the likes of BBCode.

ForumCode allows end-users (of a web-site) limited access to a set of HTML markup through a HTML-esque syntax.

This module works by using Template::Plugin::HTML to escape all HTML entities and markup. It then performs a series of transformations to convert ForumCode markup into the appropriate HTML markup.

MARKUP

Top

HTML::ForumCode plugin will perform the following transformations:

[b]...[/b] or **...**

Make the text between the markers bold.

[u]...[/u] or __...___

Make the text between the markers underlined.

[i]...[/i]

Make the text between the markers italicised.

[url]http://...[/url] or [url="http://..."]LinkText[/url]

Make the text between the markers into a HTML link. If you would like to give the link a name, use the following format:

[url name="..."]http://...[/url]

[img]http://...[/img]

Insert an image, specified by the URL between the markers.

You may also include extra attributes such as title=, alt=, etc. Research the HTML <img> tag for the full list of attributes.

  e.g.
   [img title='Powered by Catalyst' width='50']http://.../images/button.png[/img]

[colour=code]...[/colour]

Make a block of text appear in the colour specified by code.

code can be any of the named colours: red, orange, yellow, green, blue, black, white.

code may also be a #RGB value in either the #XYZ or #XXYYZZ format.

For the sake of international relations [color=code]...[/color] may also be used.

  e.g. red text
   [colour=red]Red Text[/colour]
   [colour=#ff0000]Red Text[/colour]

[list]...[/list]

Create an ordered or unordered list of items. To create an unordered list use [*] to mark the start of each list item. To create an ordered list use [1] to mark the start of each list item.

  e.g. an unordered list
   [list]
   [*]apple
   [*]orange
   [*]banana
   [/list]

  e.g. an ordered list
   [list]
   [1]first
   [1]second
   [1]third
   [/list]

[code]...[/code]

Marks a block of text with the CSS forumcode_code class. How this displays is dependant on the CSS definitions in your application templates.

  /* Example CSS */

  .forumcode_code {
    font-family:        monospace;
    border:             1px solid #333;
    font-size:          95%;
    margin:             15px 20px 15px 20px;
    padding:            6px;
    width:              85%;
    overflow:           auto;
    white-space:        pre;

    color:              #ff0;
    background-color:   #333;
    border:             1px solid #666;
  }

[pre]...[/pre]

Marks a block of text with the CSS forumcode_pre class. How this displays is dependant on the CSS definitions in your application templates.

  /* Example CSS */

  .forumcode_pre {
    background-color:   transparent;
    font-family:        monospace;
    font-size:          95%;
    border:             1px dashed #333;
    margin:             15px 20px 15px 20px;
    padding:            6px;
    width:              85%;
    overflow:           auto;
    white-space:        pre;
  }

[quote]...[/quote]

Marks a block of text with the CSS forumcode_pre class. How this displays is dependant on the CSS definitions in your application templates.

You may specify the name of ther person you are quoting using the following addition to the markup:

[quote quoting="..."]Lorem ipsum ...[/quote]

The quoted text will be prefixed with Quoting Name:. This extra output will be wrapped in with the CSS forumcode_quoting class.

  /* Example CSS */

  .forumcode_quote {
    background-color:   #eee;
    font-family:        monospace;
    font-style:         italic;
    border:             1px dotted #333;
    font-size:          95%;
    margin:             15px 20px 15px 20px;
    padding:            6px;
    width:              85%;
    overflow:           auto;
  }

  .forumcode_quoting {
    font-weight:        bold;
    margin-bottom:      3px;
  }

PUBLIC METHODS

Top

new

Create a new instance of the plugin for TT usage

forumcode

The transformation function

PRIVATE METHODS

Top

init

Called during the object set-up to initialise the object with the required information and voodoo.

SEE ALSO

Top

Template::Plugin::ForumCode, Template::Plugin::HTML, Template::Toolkit, HTML::ForumCode::Cookbook

AUTHOR

Top

Chisel Wright <chiselwright@users.berlios.de>

LICENSE

Top

This library is free software, you can redistribute it and/or modify it under the same terms as Perl itself.


Template-Plugin-ForumCode documentation Contained in the Template-Plugin-ForumCode distribution.

package HTML::ForumCode;
# vim: ts=8 sts=4 et sw=4 sr sta
use strict;
use warnings;

use version; our $VERSION = qv('0.0.5')->numify;

use Template::Plugin::HTML;

sub new {
    my ($class, @args) = @_;

    my $new_obj = bless {}, (ref($class)||$class);
    $new_obj->init;

    return $new_obj;
}

sub init {
    my $self = shift;

    # simple [x][/x] --> <x></x> tags
    $self->{simple_tags} = [qw{
        b
        u
        i
    }];
    # replacements; e.g. __x__ --> <u>x</u>
    $self->{replacements} = [
        {   from => '__',       to => 'u'   },
        {   from => '\*\*',     to => 'b'   },
    ];

    return;
}

sub forumcode {
    my ($self, $text) = @_;

    # if we don't have any text, we don't have to do any work
    if (not defined $text) {
        return q{};
    }

    # first of all ESCAPE EVERYTHING!
    $text = Template::Plugin::HTML->escape($text);

    # turn newlines into <br /> tags
    $self->_preserve_newlines(\$text );

    $self->_simple_tags     ( \$text );
    $self->_replacements    ( \$text );
    $self->_colouring       ( \$text );
    $self->_lists           ( \$text );
    $self->_url_links       ( \$text );
    $self->_images          ( \$text );
    $self->_styled_block    ( \$text );
    $self->_quoted_block    ( \$text );
    
    return $text;
}

sub _preserve_newlines {
    my ($self, $textref) = @_;

    $$textref =~ s{\n}{<br />}xmsg;
}

sub _simple_tags {
    my ($self, $textref) = @_;

    # deal with acceptable [x]...[/x] markup
    foreach my $tag (@{ $self->{simple_tags} }) {
        # we should be able to combine these two into one
        #$$textref =~ s{\[$tag\]}{<$tag>}g;
        #$$textref =~ s{\[/$tag\]}{</$tag>}g;
        $$textref =~ s{
                        \[($tag)\]
                        (.+?)
                        \[/$tag\]
                }
                {<$1>$2</$1>}xmsg;
    }
}

sub _replacements {
    my ($self, $textref) = @_;

    # now deal with replacements
    foreach my $tag (@{ $self->{replacements} }) {
        $$textref =~ s{
                        $tag->{from}
                        (.+?)
                        $tag->{from}
                }
                {<$tag->{to}>$1</$tag->{to}>}gx;
    }
}

sub _url_links {
    my ($self, $textref) = @_;

    # deal with links with no text-label
    $$textref =~ s{\[url\](.+?)\[/url\]}
                {<a href="$1">$1</a>}xmsg;
    # deal with links with a text-label
    $$textref =~ s{
                \[url           # start of url tag
                \s+             # need some whitespace
                name=&quot;     # name="
                (.+?)           # the name
                &quot;          # closing "
                \s*             # optional whitespace
                \]              # close the opening tag
                (.+?)           # the url
                \[/url\]        # close the URL tag
        }
        {<a href="$2">$1</a>}xmsg;
    # bbcode / explosm style urls
    $$textref =~ s{
                \[URL=&quot;    # opening url tag
                (.+?)           # the url
                &quot;\]        # close-opening tag
                (.+?)           # link name/text/label
                \[/URL]         # closing tag
        }
        {<a href="$1">$2</a>}ximsg;
}

sub _images {
    my ($self, $textref) = @_;

    # deal with image tags
    $$textref =~ s{
                \[img           # opening img tag
                (.*?)           # optional parameters
                \]              # close-opening tag
                (.+?)           # image URL
                \[/img\]        # closing img tag
        }
        #{<img src="$2"$1 />}ximsg;
        {&_image_tag_with_attr($2,$1)}ximsge;
}

sub _image_tag_with_attr {
    my ($uri, $attr_match) = @_;

    # no annoying image attributes to worry about
    if (q{} eq $attr_match) {
        return qq{<img src="$uri" />};
    }
    # deal with annoying image attributes
    else {
        $attr_match =~ s{
                        (\w+)
                        =
                        &quot;
                        (\w+?)
                        &quot;
                }
                {$1="$2"}xmsg;
        return qq{<img src="$uri"$attr_match />};
    }
}

sub _colouring {
    my ($self, $textref) = @_;

    # deal with colouring
    $$textref =~ s{
                \[(colou?r)
                =
                (
                            red | orange | yellow | green | blue
                        | black | white
                        | \#[0-9a-fA-F]{3}
                        | \#[0-9a-fA-F]{6}
                )
                \]
                (.+?)
                \[/\1\]
        }
        {<span style="color: $2">$3</span>}ixmsg;
}

sub _lists {
    my ($self, $textref) = @_;

    $$textref =~ s{
                \[list\]
                (?:
                        \s*
                        (?:
                                <br\s*?/>
                        )?
                        \s*
                )
                (.+?)
                \[/list\]
                [\s]*
                (?:
                        <br\s*?/>
                )?
        }
        {_list_elements($1)}xmsge;
}

sub _list_elements {
    my ($text) = @_;

    # ordered lists
    if (
        $text =~ s{
                        \[\*\]
                        \s*
                        (.+?)
                        <br\s*?/>
                        \s*
                }
                {<li>$1</li>}xmsg
    ) {
        return qq{<ul>$text</ul>};
    }

    # ordered lists
    if (
        $text =~ s{
                        \[1\]
                        \s*
                        (.+?)
                        <br\s*?/>
                        \s*
                }
                {<li>$1</li>}xmsg
    ) {
        return qq{<ol>$text</ol>};
    }


    # otherwise, just return what we were given
    return $text;
}

sub _styled_block {
    my ($self, $textref) = @_;

    $$textref =~ s{
                \[(code|pre|quote)\]
                (.+?)
                \[/\1\]
        }
        {<div class="forumcode_$1">$2</div>}xmsg;
}

# this deals with the extended case of [quote] where we have the quoting=
# attribute
sub _quoted_block {
    my ($self, $textref) = @_;

    $$textref =~ s{
                \[
                        (quote)
                        \s+
                        quoting=
                        &quot;
                        (.+?)
                        &quot;
                \]
                (.+?)
                \[/\1\]
        }
        {<div class="forumcode_$1"><div class="forumcode_quoting">Quoting $2:</div>$3</div>}xmsg;
}

1;
__END__
vim: ts=8 sts=4 et sw=4 sr sta