~ubuntu-branches/ubuntu/quantal/libtext-wikiformat-perl/quantal

« back to all changes in this revision

Viewing changes to .pc/0001-added-missing-newline-to-Blocks.pm.patch/lib/Text/WikiFormat/Blocks.pm

  • Committer: Bazaar Package Importer
  • Author(s): Benjamin Mako Hill
  • Date: 2011-02-12 14:44:49 UTC
  • mfrom: (1.1.3 upstream) (3.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20110212144449-6lcsb4ihr1ayymis
Tags: 0.79-1
* New upstream release. (Closes: #583317)
* Fixed broken watch file. (Closes: #449734)
* Move to new source format and update package in a various ways.
* Suppress packlist.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package Text::WikiFormat::Blocks;
 
2
 
 
3
use strict;
 
4
use warnings;
 
5
 
 
6
sub import
 
7
{
 
8
        my $caller = caller();
 
9
        no strict 'refs';
 
10
        *{ $caller . '::new_block' } = sub
 
11
        {
 
12
                my $type  = shift;
 
13
                my $class = "Text::WikiFormat::Block::$type";
 
14
                my $ctor;
 
15
                
 
16
                unless ($ctor = $class->can( 'new' ))
 
17
                {
 
18
                        @{ $class . '::ISA' } = ( 'Text::WikiFormat::Block' );
 
19
                        $ctor = $class->can( 'new' );
 
20
                }
 
21
 
 
22
                return $class->new( type => $type, @_ );
 
23
        };
 
24
}
 
25
 
 
26
package Text::WikiFormat::Block;
 
27
 
 
28
use Scalar::Util qw( blessed reftype );
 
29
 
 
30
sub new
 
31
{
 
32
        my ($class, %args) = @_;
 
33
 
 
34
        $args{text}        =   $class->arg_to_ref( delete $args{text} || '' );
 
35
        $args{args}        = [ $class->arg_to_ref( delete $args{args} || [] ) ];
 
36
 
 
37
        bless \%args, $class;
 
38
}
 
39
 
 
40
sub arg_to_ref
 
41
{
 
42
        my ($class, $value) = @_;
 
43
        return   $value if ( reftype( $value ) || '' ) eq 'ARRAY';
 
44
        return [ $value ];
 
45
}
 
46
 
 
47
sub shift_args
 
48
{
 
49
        my $self = shift;
 
50
        my $args = shift @{ $self->{args} };
 
51
        return wantarray ? @$args : $args;
 
52
}
 
53
 
 
54
sub all_args
 
55
 
56
        my $args = $_[0]{args};
 
57
        return wantarray ? @$args : $args;
 
58
}
 
59
 
 
60
sub text
 
61
{
 
62
        my $text = $_[0]{text};
 
63
        return wantarray ? @$text : $text;
 
64
}
 
65
 
 
66
sub add_text
 
67
{
 
68
        my $self = shift;
 
69
        push @{ $self->{text} }, @_;
 
70
}
 
71
 
 
72
sub formatted_text
 
73
{
 
74
        my $self = shift;
 
75
        return map
 
76
        {
 
77
                blessed( $_ ) ? $_ : $self->formatter( $_ )
 
78
        } $self->text();
 
79
}
 
80
 
 
81
sub formatter
 
82
{
 
83
        my ($self, $line) = @_;
 
84
        Text::WikiFormat::format_line( $line, $self->tags(), $self->opts() );
 
85
}
 
86
 
 
87
sub add_args
 
88
{
 
89
        my $self = shift;
 
90
        push @{ $self->{args} }, @_;
 
91
}
 
92
 
 
93
{
 
94
        no strict 'refs';
 
95
        for my $attribute (qw( level opts tags type ))
 
96
        {
 
97
                *{ $attribute } = sub { $_[0]{$attribute} };
 
98
        }
 
99
}
 
100
 
 
101
sub merge
 
102
{
 
103
        my ($self, $next_block) = @_;
 
104
 
 
105
        return $next_block unless $self->type()  eq $next_block->type();
 
106
        return $next_block unless $self->level() == $next_block->level();
 
107
 
 
108
        $self->add_text( $next_block->text() );
 
109
        $self->add_args( $next_block->all_args() );
 
110
        return;
 
111
}
 
112
 
 
113
sub nests
 
114
{
 
115
        my $self = shift;
 
116
        return exists $self->{tags}{nests}{ $self->type() };
 
117
}
 
118
 
 
119
sub nest
 
120
{
 
121
        my ($self, $next_block) = @_;
 
122
 
 
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();
 
126
 
 
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 );
 
130
 
 
131
        $self->add_text( $next_block );
 
132
        return;
 
133
}
 
134
 
 
135
package Text::WikiFormat::Block::code;
 
136
 
 
137
use base 'Text::WikiFormat::Block';
 
138
 
 
139
sub formatter { $_[1] }
 
140
 
 
141
package Text::WikiFormat::Blocks;
 
142
 
 
143
1;
 
144
__END__
 
145
=head1 NAME
 
146
 
 
147
Text::WikiFormat::Blocks - blocktypes for Text::WikiFormat
 
148
 
 
149
=head1 SYNOPSIS
 
150
 
 
151
None.  Use L<Text::WikiFormat> as the public interface, unless you want to
 
152
create your own block type.
 
153
 
 
154
=head1 DESCRIPTION
 
155
 
 
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
 
160
font).
 
161
 
 
162
Every block extends C<Text::WikiFormat::Block>.
 
163
 
 
164
=head1 METHODS
 
165
 
 
166
The following methods exist:
 
167
 
 
168
=over 4
 
169
 
 
170
=item * C<new( %args )>
 
171
 
 
172
Creates and returns a new block.  The valid arguments are:
 
173
 
 
174
=over 4
 
175
 
 
176
=item * C<text>
 
177
 
 
178
The text of the line found in the block.
 
179
 
 
180
=item * C<args>
 
181
 
 
182
The arguments captured by the block-identifying regular expression.
 
183
 
 
184
=item * C<level>
 
185
 
 
186
The level of indentation for the block (usually only useful for list blocks).
 
187
 
 
188
=item * C<tags>
 
189
 
 
190
The tags in effect for the current type of wiki formatting.
 
191
 
 
192
=item * C<opts>
 
193
 
 
194
The options in effect for the current type of wiki formatting.
 
195
 
 
196
=back
 
197
 
 
198
Use the accessors of the same names to retrieve the values of the attributes.
 
199
 
 
200
=item * C<add_text( @lines_of_text )>
 
201
 
 
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
 
204
of the same type
 
205
 
 
206
=item * C<add_args( @arguments )>
 
207
 
 
208
Adds further arguments to the block; useful when merging blocks.
 
209
 
 
210
=item * C<formatted_text()>
 
211
 
 
212
Returns text formatted appropriately for this block.  Blocks don't have to have
 
213
formatters, but they may.
 
214
 
 
215
=item * C<formatter( $line_of_text )>
 
216
 
 
217
Formats the C<$line> using C<Text::WikiFormat::format_line()>.  You can add
 
218
your own formatter here; this is worth overriding.
 
219
 
 
220
=item * C<merge( $next_block )>
 
221
 
 
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.
 
226
 
 
227
=item * C<nests()>
 
228
 
 
229
Returns true if this block should nest (as in lists and unordered lists) for
 
230
the active wiki formatting.
 
231
 
 
232
=item * C<nest( $next_block )>
 
233
 
 
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.
 
237
 
 
238
=back
 
239
 
 
240
=head1 AUTHOR
 
241
 
 
242
chromatic, C<< chromatic at wgz dot org >>
 
243
 
 
244
=head1 BUGS
 
245
 
 
246
No known bugs.
 
247
 
 
248
=head1 COPYRIGHT
 
249
 
 
250
Copyright (c) 2006, chromatic.  Some rights reserved.
 
251
 
 
252
This module is free software; you can use, redistribute, and modify it under
 
253
the same terms as Perl 5.8.x.