File Coverage

blib/lib/Pod/BBCode.pm
Criterion Covered Total %
statement 38 68 55.8
branch 9 38 23.6
condition 2 3 66.6
subroutine 7 8 87.5
pod 0 5 0.0
total 56 122 45.9


line stmt bran cond sub pod time code
1             # $Id: BBCode.pm,v 1.5 2005/05/15 13:51:33 chaos Exp $
2             # $Log: BBCode.pm,v $
3             # Revision 1.5 2005/05/15 13:51:33 chaos
4             # fixed a little bug on processing whitespaces
5             #
6             # Revision 1.4 2005/05/15 13:45:17 chaos
7             # stripped ending whitespaces in verbatim paragraph
8             #
9             # Revision 1.3 2005/05/15 06:14:53 chaos
10             # change version back to numeric
11             #
12             # Revision 1.2 2005/05/09 12:15:00 chaos
13             # modified tag
14             #
15             # Revision 1.1 2005/05/09 12:13:34 chaos
16             # Pod::BBCode
17             #
18             # vim:ts=4 sw=4
19             package Pod::BBCode;
20 1     1   28234 use strict;
  1         3  
  1         41  
21 1     1   7 use Pod::Parser;
  1         2  
  1         59  
22 1     1   4 use vars qw/ @ISA $VERSION /;
  1         6  
  1         1459  
23              
24             $VERSION = '1.5';
25             @ISA = qw/ Pod::Parser /;
26              
27             sub command
28             {
29 9     9 0 8344 my ($self, $command, $paragraph, $line_num) = @_;
30              
31 9         11 my $expansion;
32 9         76 my $out_fh = $self->output_handle();
33 9         27 my ($headColor,$itemColor)=($self->{-headcolor},$self->{-itemcolor});
34            
35 9         24 for ( $command ) {
36 9 100 66     107 /pod/ || /cut/ and return;
37 8 50       22 /begin/ and do {
38 0         0 $self->{ignore_section} = 1;
39 0         0 return;
40             };
41 8 50       18 /end/ and do {
42 0         0 $self->{ignore_section} = 0;
43 0         0 return;
44             };
45 8 50       35 /head(\d)/ and do {
46 8         24 $expansion = $self->interpolate($paragraph, $line_num);
47             # convert =head[1-4] to [highlight][size=[5-2]][/size][/highlight]
48 8 50       55 $expansion = "\n"
    50          
49             . "[size=" . (6-$1) . "]"
50             . ($headColor ? "[color=$headColor]" : '')
51             . $expansion
52             . ($headColor ? "[/color]" : '')
53             . "[/size]"
54             . "\n";
55 8         12 last;
56             };
57 0 0       0 /over/ and do {
58 0         0 push @{$self->{lists}}, undef;
  0         0  
59             # the opening [list] tag's option can be only determined later
60             # we can do nothing here
61 0         0 return;
62             };
63 0 0       0 /back/ and do {
64 0         0 pop @{$self->{lists}};
  0         0  
65 0         0 $expansion = "[/list]\n";
66 0         0 last;
67             };
68 0 0       0 /item/ and do {
69 0 0       0 if(!defined($self->{lists}[-1])) {
70             # this is the first item
71 0 0       0 $self->{lists}[-1] =
72             ($paragraph =~ /^\d+\.?\s*/)
73             ? '1'
74             : ''; # we consider text item name to be the same as asterisk
75 0 0       0 if($self->{lists}[-1] eq '1') {
76             # numeric list
77 0         0 $expansion = "[list=1]\n";
78             } else {
79             # asterisk list
80 0         0 $expansion = "[list]\n";
81             }
82             } else {
83             # this is the following item
84 0         0 $expansion = ''
85             }
86            
87             # strip item names
88 0         0 $paragraph =~ s/^[*o-]+\s*//;
89 0         0 $paragraph =~ s/^\d+\.?\s*//;
90              
91 0 0       0 $expansion .= '[*]'
    0          
92             . ($itemColor ? "[color=$itemColor]" : '')
93             . $self->interpolate($paragraph, $line_num)
94             . ($itemColor ? "[/color]" : '')
95             . "\n";
96 0         0 last;
97             };
98             }
99              
100 8         301 print $out_fh $expansion;
101             }
102              
103             sub textblock
104             {
105 0     0 0 0 my ($self, $paragraph, $line_num) = @_;
106 0 0       0 return if $self->{ignore_section};
107              
108 0         0 my $textColor=$self->{-textcolor};
109              
110 0         0 my $expansion = $self->interpolate($paragraph, $line_num);
111              
112 0         0 my $out_fh = $self->output_handle();
113 0 0       0 print $out_fh ($textColor ? "[color=$textColor]" : '')
    0          
114             . $expansion
115             . ($textColor ? "[/color]" : '')
116             . "\n";
117             }
118              
119             sub interior_sequence
120             {
121 4     4 0 7 my ($self, $seq_command, $seq_argument) = @_;
122              
123 4         28 my %markup = (
124             B => [ '[b]', '[/b]' ], # boldface
125             I => [ '[i]', '[/i]' ], # italic
126             F => [ '[pre]', '[/pre]' ], # filename
127             C => [ '[pre]', '[/pre]' ], # code
128             );
129            
130 4         204 return $markup{$seq_command}[0] . $seq_argument . $markup{$seq_command}[1];
131             }
132              
133             sub verbatim
134             {
135 1     1 0 2 my ($self, $paragraph, $line_num) = @_;
136              
137 1 50       6 return if $self->{ignore_section};
138              
139 1         6 my $out_fh = $self->output_handle();
140             # strip ending newlines
141 1         11 $paragraph =~ s/\s*$//;
142             # vBulletin forum doesn't seem to be able to handle text looks like BBCode tag in
143             # the middle of tag pair, so we need to convert [] to corresponding HTML entities "[" and "]"
144 1         3 $paragraph =~ s/\[/[/g;
145 1         2 $paragraph =~ s/\]/]/g;
146             # make paragraph looking like this:
147             # [code]
148             # ...
149             # [/code]
150 1 50       7 $paragraph = "[code]\n$paragraph\n[/code]\n"
151             if length($paragraph)>0;
152 1         74 print $out_fh $paragraph;
153             }
154              
155             sub interpolate
156             {
157 8     8 0 10 my $self = shift;
158 8         727 local $_ = $self->SUPER::interpolate(@_);
159 8         23 tr/ \t\r\n/ /s;
160 8         39 s/\s+$//;
161 8         22 return $_;
162             }
163              
164             1;
165              
166             =head1 NAME
167              
168             Pod::BBCode - converts a POD file to a page using BB code.
169              
170             =head1 SYNOPSIS
171              
172             use Pod::BBCode;
173              
174             my $p = new Pod::BBCode(-headcolor=>'red',-itemcolor=>'blue',-textcolor=>'black');
175             $p->parse_from_file('in.pod');
176              
177             =head1 DESCRIPTION
178              
179             This class converts a file in POD syntax to the BBCode syntax, in order to simplify
180             the posting process on vBulletin forums. See any vBulletin forum's help for a description
181             of the BBCode syntax.
182              
183             Pod::BBCode derives from Pod::Parser and therefore inherits all its methods.
184              
185             This module was modified from Pod::TikiWiki module. Thanks to the original author.
186              
187             =head2 Supported formatting
188              
189             =over 4
190              
191             =item *
192              
193             Heading directives (C<=head[1234]>) are handled with [size][/size] tag.
194              
195             =head1 NAME --> [size=5]NAME[/size]
196             =head2 Methods --> [size=4]Methods[/size]
197              
198             =item *
199              
200             List items are rendered with [list=1][/list] (for ordered lists) or [list][/list] (for unordered lists) tag.
201              
202             =over [list]
203              
204             =item *
205             --> [*]
206             Text Text
207              
208             =over [list=1]
209              
210             =item 1
211             --> [*]
212             Text Text
213              
214             =back [/list]
215              
216             =back [/list]
217              
218             Items with a string are rendered into a asterisked list
219              
220             =item Text
221             --> [*]Text
222             Definition Definition
223              
224             =item *
225              
226             Interior sequences C, C, C and C are honored. Both C and C
227             are rendered as monospaced text.
228              
229             B --> [b]bold[/b]
230             I --> [i]italic[/i]
231             F --> [pre]file[/pre]
232             C --> [pre]code[/pre]
233              
234             =back
235              
236             =head1 LIMITATIONS
237              
238             =over
239              
240             =item *
241              
242             Only the above four interior sequences are handled. C, C, C, C are
243             ignored.
244              
245             =item *
246              
247             BBCode-like text can't display correctly in non-verbatim environment, so be careful.
248              
249             =item *
250              
251             ...
252              
253             =back
254              
255             =head1 SEE ALSO
256              
257             L, L
258              
259             =head1 AUTHOR
260              
261             chaoslawful (chaoslaw@cpan.org)
262              
263             This module is free software. You can redistribute and/or modify it under the
264             terms of the GNU General Public License.
265              
266             Thanks to the author of Pod::TikiWiki again!
267              
268             =cut
269