Toolkit/Formatter/Default.pm

filter: Statementstmt 84.9% 45 / 53 Branchbran 25.0% 2 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 75.6% 59 / 78 CC 8 SCAR 17.3
CC
8
Cov
73%
format
13.5
new
2.0
_init
2.0
CRAP 5.6
« Toolkit/Search/Plucene.pm ↑ Summary Toolkit/Setup/SII.pm »
1 package Wiki::Toolkit::Formatter::Default;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use vars qw( $VERSION @ISA @_links_found );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 $VERSION = '0.02';
7  
8 3 ▶use Wiki::Toolkit::Formatter::WikiLinkFormatterParent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use CGI ":standard";
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Text::WikiFormat as => 'wikiformat';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use HTML::PullParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 @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<Wiki::Toolkit>.
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<new>
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<allowed_tags>, which defaults to allowing no tags).
48  
49 =over 4
50  
51 =item * macros - be aware that macros are processed I<after> 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(<form action="wiki.cgi" method="get">
61 <input type="hidden" name="action" value="search">
62 <input type="text" size="20" name="terms">
63 <input type="submit"></form>) }
64  
65 =cut
66  
67 6 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
68 6 my ($class, @args) = @_;
69 6 my $self = {};
70 6 bless $self, $class;
71 6 ▶ $self->_init(@args) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->_init(@args) or return undef 6 0 6
72 6 return $self;
73 }
74  
75 6 ▶sub _init {
Subroutine _init: called CC=2 SCAR=6.9
76 6 my ($self, %args) = @_;
77  
78 # Store the parameters or their defaults.
79 6 my %defs = ( extended_links => 0,
80 implicit_links => 1,
81 allowed_tags => [],
82 macros => {},
83 node_prefix => 'wiki.cgi?node=',
84 );
85  
86 6 my %collated = (%defs, %args);
87 6 foreach my $k (keys %defs) {
88 30 $self->{"_".$k} = $collated{$k};
89 }
90  
91 6 return $self;
92 }
93  
94 =item B<format>
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<new> was called) to translate the raw Wiki
101 language supplied into HTML.
102  
103 =cut
104  
105 2 ▶sub format {
Subroutine format: called CC=6 SCAR=26.0
Pod documented
106 2 my ($self, $raw) = @_;
107 2 my $safe = "";
108  
109 0 my %allowed = map {lc($_) => 1, "/".lc($_) => 1} @{$self->{_allowed_tags}};
110  
111 2 ▶ if (scalar keys %allowed) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %allowed) { } 0 2 2
112 # If we are allowing some HTML, parse and get rid of the nasties.
113 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 ▶ while (my $token = $parser->get_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $parser->get_token) 0 0 0
118 0 my ($flag, $tag, $text) = @$token;
119 0 ▶ if ($flag eq "TAG" and !defined $allowed{lc($tag)}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag eq 'TAG' and not defined $allowed{lc $tag}) { } 0 0 0
Condition0%0/3
$flag eq "TAG" and not defined $allowed{lc $tag}
!ll&&!rl&&r
000
MC/DC0%0/2
$flag eq "TAG" and not defined $allowed{lc $tag}
$flag eq "TAG" not defined $allowed{lc $tag}
Truth table0%0/3
A and not(B)
A= $flag eq "TAG"B= not defined $allowed{lc $tag}
ABresult
0X0
100
111
120 0 $safe .= CGI::escapeHTML($text);
121 } else {
122 0 $safe .= $text;
123 }
124 }
125 } else {
126 # Else just escape everything.
127 2 $safe = CGI::escapeHTML($raw);
128 }
129  
130 # Now process any macros.
131 2 my %macros = %{$self->{_macros}};
132 2 foreach my $regexp (keys %macros) {
133 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 implicit_links => $self->{_implicit_links} } );
140 }
141  
142 =back
143  
144 =head1 SEE ALSO
145  
146 L<Wiki::Toolkit::Formatter::WikiLinkFormatterParent>
147 L<Wiki::Toolkit>
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 3 1;
« Toolkit/Search/Plucene.pm ↑ Summary Toolkit/Setup/SII.pm »