blib/lib/CGI/Wiki/Formatter/Pod.pm

filter: Statementstmt 83.9% 47 / 56 Branchbran 50.0% 3 / 6 Conditioncond 66.6% 4 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 88.8% 8 / 9 Podpod 100% 2 / 2 totaltotal 79.2% 65 / 82 CC 7 SCAR 7.7
CC
7
Cov
79%
format
3.1
new
3.0
new
2.0
CRAP 2.2
↑ Summary
1 package CGI::Wiki::Formatter::Pod;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 $VERSION = '0.03';
7  
8 2 ▶use IO::Scalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Pod::Tree::HTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 CGI::Wiki::Formatter::Pod - A Pod to HTML formatter for CGI::Wiki.
14  
15 =head1 DESCRIPTION
16  
17 A Pod to HTML formatter backend for L<CGI::Wiki>.
18  
19 =head1 SYNOPSIS
20  
21 my $store = CGI::Wiki::Store::SQLite->new( ... );
22 my $formatter = CGI::Wiki::Formatter::Pod->new;
23 my $wiki = CGI::Wiki->new( store => $store,
24 formatter => $formatter );
25  
26 Go look at L<CGI::Wiki> to find out more. This module is distributed
27 separately solely for convenience of testing and maintenance; it's
28 probably not too useful on its own.
29  
30 =head1 METHODS
31  
32 =over 4
33  
34 =item B<new>
35  
36 my $formatter = CGI::Wiki::Formatter::Pod->new(
37 node_prefix => 'wiki.cgi?node=',
38 usemod_extended_links => 0,
39 );
40  
41 C<node_prefix> is optional and defaults to the value shown above.
42  
43 If C<usemod_extended_links> is supplied and true, then UseModWiki-style
44 extended links C<[[like this]]> will be supported - ie
45  
46 [[foo bar]]
47  
48 will be translated into a link to the node named "Foo Bar". (Node
49 names are forced to ucfirst, ie first letter of each word is capitalised.)
50  
51 B<Note:> You must have L<CGI::Wiki::Formatter::UseMod> installed if
52 you wish to use the C<usemod_extended_links> parameter.
53  
54 =cut
55  
56 2 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
57 2 my ($class, %args) = @_;
58 2 my $self = { };
59 2 bless $self, $class;
60 2 ▶ my $node_prefix = $args{node_prefix} || "wiki.cgi?node=";
Condition100%2/2
$args{'node_prefix'} || 'wiki.cgi?node='
l!l
11
MC/DC100%1/1
$args{'node_prefix'} || 'wiki.cgi?node='
$args{'node_prefix'}
Truth table100%2/2
A
A= $args{'node_prefix'}
Aresult
11
00
61 2 $self->{_node_prefix} = $node_prefix;
62 2 ▶ $self->{_usemod_extended_links} = $args{usemod_extended_links} || 0;
Condition50%1/2
$args{'usemod_extended_links'} || 0
l!l
02
MC/DC0%0/1
$args{'usemod_extended_links'} || 0
$args{'usemod_extended_links'}
Truth table50%1/2
A
A= $args{'usemod_extended_links'}
Aresult
11
00
63 2 my $link_mapper = CGI::Wiki::Formatter::Pod::LinkMapper->new(
64 node_prefix => $node_prefix );
65 2 $self->{_link_mapper} = $link_mapper;
66 2 return $self;
67 }
68  
69 =item B<format>
70  
71 my $html = $formatter->format( $content );
72  
73 Uses L<Pod::Tree::HTML> to translate the pod supplied in C<$content>
74 into HTML. Links will be treated as links to other wiki pages.
75  
76 =cut
77  
78 2 ▶sub format {
Subroutine format: called CC=3 SCAR=11.3
Pod documented
79 2 my ($self, $raw) = @_;
80 2 ▶ return "" unless $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $raw 0 2 2
81 2 my $source = \$raw;
82 2 my $formatted;
83 2 my $dest = IO::Scalar->new( \$formatted );
84 2 my %options = ( link_map => $self->{_link_mapper} );
85 2 my $html = Pod::Tree::HTML->new( $source, $dest, %options );
86 2 $html->translate;
87 2 $formatted =~ s/^.*<BODY[^>]*>//s;
88 2 $formatted =~ s|</BODY>.*$||s;
89 2 ▶ if ( $self->{_usemod_extended_links} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_usemod_extended_links"}) 0 2 2
90 # Create link from [[foo bar]].
91 0 $formatted =~ s/(\[\[[^\]]+\]\])/$self->_linkify($1)/egs;
92 }
93 2 return $formatted;
94 }
95  
96 0 ▶sub _linkify {
Subroutine _linkify: not called CC=1 SCAR=6.9
97 0 my ($self, $node) = @_;
98 0 require CGI::Wiki::Formatter::UseMod;
99 my $formatter = CGI::Wiki::Formatter::UseMod->new(
100 implicit_links => 0,
101 extended_links => 1,
102 node_prefix => $self->{_node_prefix},
103 0 );
104 0 my $snippet = $formatter->format($1);
105 # Snippet will be created as a paragraph, which we don't want as we're
106 # inlining this.
107 0 $snippet =~ s/^<p>//s;
108 0 $snippet =~ s/<\/p>$//s;
109 0 return $snippet;
110 }
111  
112 =head1 SEE ALSO
113  
114 L<CGI::Wiki>, L<Pod::Tree::HTML>.
115  
116 =head1 AUTHOR
117  
118 Kake Pugh (kake@earth.li), idea stolen from Matt Sergeant. Many thanks to
119 Steven W McDougall for extending the capabilities of L<Pod::Tree::HTML> so
120 I could make this work.
121  
122 =head1 COPYRIGHT
123  
124 Copyright (C) 2003 Kake Pugh. All Rights Reserved.
125  
126 This module is free software; you can redistribute it and/or modify it
127 under the same terms as Perl itself.
128  
129 =cut
130  
131  
132 package CGI::Wiki::Formatter::Pod::LinkMapper;
133  
134 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
135 2 my ($class, %args) = @_;
136 2 my $self = { };
137 2 bless $self, $class;
138 2 ▶ $self->{_node_prefix} = $args{node_prefix} || "";
Condition50%1/2
$args{'node_prefix'} || ''
l!l
20
MC/DC0%0/1
$args{'node_prefix'} || ''
$args{'node_prefix'}
Truth table50%1/2
A
A= $args{'node_prefix'}
Aresult
11
00
139 2 return $self;
140 }
141  
142 2 ▶sub url {
Subroutine url: called CC=2 SCAR=7.0
143 2 my ($self, $html, $target) = @_;
144 2 my $page = $target->get_page;
145 2 my $section = $target->get_section;
146 2 ▶ if ( $page ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page) 2 0 2
147 2 $page = $self->{_node_prefix} . $html->escape_2396($page);
148 }
149 2 $section = $html->escape_2396($section);
150 2 return $html->assemble_url("", $page, $section);
151 }
152  
153 2 1;
↑ Summary