File Coverage

blib/lib/CGI/Wiki/Formatter/Pod.pm
Criterion Covered Total %
statement 45 54 83.3
branch 3 6 50.0
condition 4 6 66.6
subroutine 8 9 88.8
pod 2 2 100.0
total 62 77 80.5


line stmt bran cond sub pod time code
1             package CGI::Wiki::Formatter::Pod;
2              
3 2     2   45937 use strict;
  2         6  
  2         95  
4              
5 2     2   13 use vars qw( $VERSION );
  2         4  
  2         115  
6             $VERSION = '0.03';
7              
8 2     2   6227 use IO::Scalar;
  2         50444  
  2         16992  
9 2     2   2239 use Pod::Tree::HTML;
  2         204769  
  2         1294  
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.
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 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
35              
36             my $formatter = CGI::Wiki::Formatter::Pod->new(
37             node_prefix => 'wiki.cgi?node=',
38             usemod_extended_links => 0,
39             );
40              
41             C is optional and defaults to the value shown above.
42              
43             If C 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 You must have L installed if
52             you wish to use the C parameter.
53              
54             =cut
55              
56             sub new {
57 2     2 1 3113 my ($class, %args) = @_;
58 2         6 my $self = { };
59 2         8 bless $self, $class;
60 2   100     14 my $node_prefix = $args{node_prefix} || "wiki.cgi?node=";
61 2         13 $self->{_node_prefix} = $node_prefix;
62 2   50     13 $self->{_usemod_extended_links} = $args{usemod_extended_links} || 0;
63 2         19 my $link_mapper = CGI::Wiki::Formatter::Pod::LinkMapper->new(
64             node_prefix => $node_prefix );
65 2         4 $self->{_link_mapper} = $link_mapper;
66 2         7 return $self;
67             }
68              
69             =item B
70              
71             my $html = $formatter->format( $content );
72              
73             Uses L 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             sub format {
79 2     2 1 2068 my ($self, $raw) = @_;
80 2 50       8 return "" unless $raw;
81 2         4 my $source = \$raw;
82 2         3 my $formatted;
83 2         21 my $dest = IO::Scalar->new( \$formatted );
84 2         218 my %options = ( link_map => $self->{_link_mapper} );
85 2         24 my $html = Pod::Tree::HTML->new( $source, $dest, %options );
86 2         2757 $html->translate;
87 2         2010 $formatted =~ s/^.*]*>//s;
88 2         13 $formatted =~ s|.*$||s;
89 2 50       11 if ( $self->{_usemod_extended_links} ) {
90             # Create link from [[foo bar]].
91 0         0 $formatted =~ s/(\[\[[^\]]+\]\])/$self->_linkify($1)/egs;
  0         0  
92             }
93 2         28 return $formatted;
94             }
95              
96             sub _linkify {
97 0     0   0 my ($self, $node) = @_;
98 0         0 require CGI::Wiki::Formatter::UseMod;
99 0         0 my $formatter = CGI::Wiki::Formatter::UseMod->new(
100             implicit_links => 0,
101             extended_links => 1,
102             node_prefix => $self->{_node_prefix},
103             );
104 0         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         0 $snippet =~ s/^

//s;

108 0         0 $snippet =~ s/<\/p>$//s;
109 0         0 return $snippet;
110             }
111              
112             =head1 SEE ALSO
113              
114             L, L.
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 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             sub new {
135 2     2   7 my ($class, %args) = @_;
136 2         6 my $self = { };
137 2         7 bless $self, $class;
138 2   50     17 $self->{_node_prefix} = $args{node_prefix} || "";
139 2         5 return $self;
140             }
141              
142             sub url {
143 2     2   4585 my ($self, $html, $target) = @_;
144 2         11 my $page = $target->get_page;
145 2         16 my $section = $target->get_section;
146 2 50       14 if ( $page ) {
147 2         11 $page = $self->{_node_prefix} . $html->escape_2396($page);
148             }
149 2         22 $section = $html->escape_2396($section);
150 2         18 return $html->assemble_url("", $page, $section);
151             }
152              
153             1;