1
package Text::WikiFormat::Blocks;
10
*{ $caller . '::new_block' } = sub
13
my $class = "Text::WikiFormat::Block::$type";
16
unless ($ctor = $class->can( 'new' ))
18
@{ $class . '::ISA' } = ( 'Text::WikiFormat::Block' );
19
$ctor = $class->can( 'new' );
22
return $class->new( type => $type, @_ );
26
package Text::WikiFormat::Block;
28
use Scalar::Util qw( blessed reftype );
32
my ($class, %args) = @_;
34
$args{text} = $class->arg_to_ref( delete $args{text} || '' );
35
$args{args} = [ $class->arg_to_ref( delete $args{args} || [] ) ];
42
my ($class, $value) = @_;
43
return $value if ( reftype( $value ) || '' ) eq 'ARRAY';
50
my $args = shift @{ $self->{args} };
51
return wantarray ? @$args : $args;
56
my $args = $_[0]{args};
57
return wantarray ? @$args : $args;
62
my $text = $_[0]{text};
63
return wantarray ? @$text : $text;
69
push @{ $self->{text} }, @_;
77
blessed( $_ ) ? $_ : $self->formatter( $_ )
83
my ($self, $line) = @_;
84
Text::WikiFormat::format_line( $line, $self->tags(), $self->opts() );
90
push @{ $self->{args} }, @_;
95
for my $attribute (qw( level opts tags type ))
97
*{ $attribute } = sub { $_[0]{$attribute} };
103
my ($self, $next_block) = @_;
105
return $next_block unless $self->type() eq $next_block->type();
106
return $next_block unless $self->level() == $next_block->level();
108
$self->add_text( $next_block->text() );
109
$self->add_args( $next_block->all_args() );
116
return exists $self->{tags}{nests}{ $self->type() };
121
my ($self, $next_block) = @_;
123
return unless $next_block = $self->merge( $next_block );
124
return $next_block unless $self->nests() and $next_block->nests();
125
return $next_block unless $self->level() < $next_block->level();
127
# if there's a nested block at the end, maybe it can nest too
128
my $last_item = ( $self->text() )[-1];
129
return $last_item->nest( $next_block ) if blessed( $last_item );
131
$self->add_text( $next_block );
135
package Text::WikiFormat::Block::code;
137
use base 'Text::WikiFormat::Block';
139
sub formatter { $_[1] }
141
package Text::WikiFormat::Blocks;
147
Text::WikiFormat::Blocks - blocktypes for Text::WikiFormat
151
None. Use L<Text::WikiFormat> as the public interface, unless you want to
152
create your own block type.
156
This module merely creates subclasses of Text::WikiFormat::Block, which is the
157
interesting code. A block is a collection of related lines, such as a code
158
block (text to display verbatim in a monospaced font), a header, an unordered
159
list, an ordered list, and a paragraph (text to display in a proportional
162
Every block extends C<Text::WikiFormat::Block>.
166
The following methods exist:
170
=item * C<new( %args )>
172
Creates and returns a new block. The valid arguments are:
178
The text of the line found in the block.
182
The arguments captured by the block-identifying regular expression.
186
The level of indentation for the block (usually only useful for list blocks).
190
The tags in effect for the current type of wiki formatting.
194
The options in effect for the current type of wiki formatting.
198
Use the accessors of the same names to retrieve the values of the attributes.
200
=item * C<add_text( @lines_of_text )>
202
Adds a list of lines of text to the current text for the block. This is very
203
useful when you encounter a block and want to merge it with the previous block
206
=item * C<add_args( @arguments )>
208
Adds further arguments to the block; useful when merging blocks.
210
=item * C<formatted_text()>
212
Returns text formatted appropriately for this block. Blocks don't have to have
213
formatters, but they may.
215
=item * C<formatter( $line_of_text )>
217
Formats the C<$line> using C<Text::WikiFormat::format_line()>. You can add
218
your own formatter here; this is worth overriding.
220
=item * C<merge( $next_block )>
222
Merges the current block with C<$next_block> (the next block encountered) if
223
they're of the same type and are at the same level. This adds the text and
224
args of C<$next_block> to the current block. It's your responsibility to
225
remove C<$next_block> from whatever your code iterates over.
229
Returns true if this block should nest (as in lists and unordered lists) for
230
the active wiki formatting.
232
=item * C<nest( $next_block )>
234
Nests C<$next_block> under this block if the both nest and if C<$next_block>
235
has a level greater than the current block. This actually adds C<$next_block>
236
as a text item within the current block. Beware.
242
chromatic, C<< chromatic at wgz dot org >>
250
Copyright (c) 2006, chromatic. Some rights reserved.
252
This module is free software; you can use, redistribute, and modify it under
253
the same terms as Perl 5.8.x.