Parse::BBCode::Tag - Tag Class for Parse::BBCode


Parse-BBCode documentation Contained in the Parse-BBCode distribution.

Index


Code Index:

NAME

Top

Parse::BBCode::Tag - Tag Class for Parse::BBCode

DESCRIPTION

Top

If you parse a bbcode with Parse::BBCode Parse::BBCode::parse returns a parse tree of Tag objects.

METHODS

Top

add_content
    $tag->add_content('string');

Adds 'string' to the end of the tag content.

    $tag->add_content($another_tag);

Adds $another_tag to the end of the tag content.

raw_text
    my $bbcode = $tag->raw_text;

Returns the raw text of the parse tree, so all tags are converted back to bbcode.

raw_content
    my $bbcode = $tag->raw_text;

Returns the raw content of the tag without the opening and closing tags. So if you have tag that was parsed from

    [i]italic and [bold]test[/b][/i]

it will return

    italic and [bold]test[/b]

ACCESSORS

Top

The accessors of a tag are currently

    name attr attr_raw content finished start end close class

You can call each accessor with get_* and set_*

name

The tag name. for [i]...[/i] it is i, the lowercase tag name.

attr

TODO

attr_raw

The raw text of the attribute

content

An arrayref of the content of the tag, each element either a string or a tag itself.

finished

Used during parsing, true if the end of the tag was found.

start

The original start string, e.g. '[size=7]'

end

The original end string, e.g. '[/size]'

close

True if the tag needs a closing tag. A tag which doesn't need a closing tag is [*] for example, inside of [list] tags.

class

'block', 'inline' or 'url'

single

If this tag does not have a closing tag and also no content, like [hr], for example, set this to true. Default is 0.


Parse-BBCode documentation Contained in the Parse-BBCode distribution.

package Parse::BBCode::Tag;
use strict;
use warnings;
use Carp qw(croak carp);

our $VERSION = '0.02';
use base 'Class::Accessor::Fast';
__PACKAGE__->follow_best_practice;
__PACKAGE__->mk_accessors(qw/ name attr attr_raw content
    finished start end close class single in_url /);

sub add_content {
    my ($self, $new) = @_;
    my $content = $self->get_content;
    if (ref $new) {
        push @$content, $new;
        return;
    }
    if (@$content and not ref $content->[-1]) {
        $content->[-1] .= $new;
    }
    else {
        push @$content, $new;
    }
}

sub raw_text {
    my ($self) = @_;
    my ($start, $end) = ($self->get_start, $self->get_end);
    my $text = $start;
    $text .= $self->raw_content;
    no warnings;
    $text .= $end;
    return $text;
}

sub raw_content {
    my ($self) = @_;
    my $content = $self->get_content;
    my $text = '';
    #warn __PACKAGE__.':'.__LINE__.$".Data::Dumper->Dump([\$self], ['self']);
    for my $c (@$content) {
        if (ref $c eq ref $self) {
            $text .= $c->raw_text;
        }
        else {
            $text .= $c;
        }
    }
    return $text;
}

sub _reduce {
    my ($self) = @_;
    if ($self->get_finished) {
        return $self;
    }
    my @text = $self->get_start;
    my $content = $self->get_content;
    for my $c (@$content) {
        if (ref $c eq ref $self) {
            push @text, $c->_reduce;
        }
        else {
            push @text, $c;
        }
    }
    push @text, $self->get_end if defined $self->get_end;
    return @text;
}


1;

__END__