File Coverage

blib/lib/Wiki/Toolkit/Formatter/Default.pm
Criterion Covered Total %
statement 42 50 84.0
branch 2 6 33.3
condition 0 3 0.0
subroutine 10 10 100.0
pod 2 2 100.0
total 56 71 78.8


line stmt bran cond sub pod time code
1             package Wiki::Toolkit::Formatter::Default;
2              
3 3     3   2552 use strict;
  3         8  
  3         105  
4              
5 3     3   16 use vars qw( $VERSION @ISA @_links_found );
  3         6  
  3         264  
6             $VERSION = '0.02';
7              
8 3     3   1436 use Wiki::Toolkit::Formatter::WikiLinkFormatterParent;
  3         9  
  3         105  
9              
10 3     3   2804 use CGI ":standard";
  3         98201  
  3         22  
11 3     3   11934 use Carp qw(croak carp);
  3         8  
  3         220  
12 3     3   57 use Text::WikiFormat as => 'wikiformat';
  3         9  
  3         24  
13 3     3   2190 use HTML::PullParser;
  3         21485  
  3         1396  
14              
15             @ISA = qw( Wiki::Toolkit::Formatter::WikiLinkFormatterParent );
16              
17             =head1 NAME
18              
19             Wiki::Toolkit::Formatter::Default - A formatter for Wiki::Toolkit.
20              
21             =head1 DESCRIPTION
22              
23             A formatter backend for L.
24              
25             =head1 SYNOPSIS
26              
27             my $store = Wiki::Toolkit::Store::SQLite->new( ... );
28             # See below for parameter details.
29             my $formatter = Wiki::Toolkit::Formatter::Default->new( %config );
30             my $wiki = Wiki::Toolkit->new( store => $store,
31             formatter => $formatter );
32              
33             =head1 METHODS
34              
35             =over 4
36              
37             =item B
38              
39             my $formatter = Wiki::Toolkit::Formatter::Default->new(
40             extended_links => 0,
41             implicit_links => 1,
42             allowed_tags => [qw(b i)], # defaults to none
43             macros => {},
44             node_prefix => 'wiki.cgi?node=' );
45              
46             Parameters will default to the values shown above (apart from
47             C, which defaults to allowing no tags).
48              
49             =over 4
50              
51             =item * macros - be aware that macros are processed I filtering
52             out disallowed HTML tags. Currently macros are just strings, maybe later
53             we can add in subs if we think it might be useful.
54              
55             =back
56              
57             Macro example:
58              
59             macros => { qr/(^|\b)\@SEARCHBOX(\b|$)/ =>
60             qq(
61            
62            
63             ) }
64              
65             =cut
66              
67             sub new {
68 6     6 1 10425 my ($class, @args) = @_;
69 6         15 my $self = {};
70 6         13 bless $self, $class;
71 6 50       17 $self->_init(@args) or return undef;
72 6         28 return $self;
73             }
74              
75             sub _init {
76 6     6   23 my ($self, %args) = @_;
77              
78             # Store the parameters or their defaults.
79 6         28 my %defs = ( extended_links => 0,
80             implicit_links => 1,
81             allowed_tags => [],
82             macros => {},
83             node_prefix => 'wiki.cgi?node=',
84             );
85              
86 6         29 my %collated = (%defs, %args);
87 6         22 foreach my $k (keys %defs) {
88 30         80 $self->{"_".$k} = $collated{$k};
89             }
90              
91 6         34 return $self;
92             }
93              
94             =item B
95              
96             my $html = $formatter->format( $content );
97              
98             Escapes any tags which weren't specified as allowed on creation, then
99             interpolates any macros, then calls Text::WikiFormat::format (with the
100             config set up when B was called) to translate the raw Wiki
101             language supplied into HTML.
102              
103             =cut
104              
105             sub format {
106 2     2 1 9 my ($self, $raw) = @_;
107 2         4 my $safe = "";
108              
109 2         4 my %allowed = map {lc($_) => 1, "/".lc($_) => 1} @{$self->{_allowed_tags}};
  0         0  
  2         4  
110              
111 2 50       7 if (scalar keys %allowed) {
112             # If we are allowing some HTML, parse and get rid of the nasties.
113 0         0 my $parser = HTML::PullParser->new(doc => $raw,
114             start => '"TAG", tag, text',
115             end => '"TAG", tag, text',
116             text => '"TEXT", tag, text');
117 0         0 while (my $token = $parser->get_token) {
118 0         0 my ($flag, $tag, $text) = @$token;
119 0 0 0     0 if ($flag eq "TAG" and !defined $allowed{lc($tag)}) {
120 0         0 $safe .= CGI::escapeHTML($text);
121             } else {
122 0         0 $safe .= $text;
123             }
124             }
125             } else {
126             # Else just escape everything.
127 2         7 $safe = CGI::escapeHTML($raw);
128             }
129              
130             # Now process any macros.
131 2         570 my %macros = %{$self->{_macros}};
  2         7  
132 2         6 foreach my $regexp (keys %macros) {
133 0         0 $safe =~ s/$regexp/$macros{$regexp}/g;
134             }
135              
136             return wikiformat($safe, {},
137             { extended => $self->{_extended_links},
138             prefix => $self->{_node_prefix},
139 2         11 implicit_links => $self->{_implicit_links} } );
140             }
141              
142             =back
143              
144             =head1 SEE ALSO
145              
146             L
147             L
148              
149             =head1 AUTHOR
150              
151             Kake Pugh (kake@earth.li).
152              
153             =head1 COPYRIGHT
154              
155             Copyright (C) 2002-2003 Kake Pugh. All Rights Reserved.
156             Copyright (C) 2006 the Wiki::Toolkit team. All Rights Reserved.
157              
158             This module is free software; you can redistribute it and/or modify it
159             under the same terms as Perl itself.
160              
161             =cut
162              
163             1;