File Coverage

blib/lib/CGI/Wiki/Formatter/Multiple.pm
Criterion Covered Total %
statement 6 24 25.0
branch 0 8 0.0
condition 0 5 0.0
subroutine 2 6 33.3
pod 3 3 100.0
total 11 46 23.9


line stmt bran cond sub pod time code
1             package CGI::Wiki::Formatter::Multiple;
2 1     1   726 use strict;
  1         2  
  1         41  
3              
4 1     1   5 use vars qw( $VERSION );
  1         1  
  1         351  
5             $VERSION = '0.02';
6              
7             =head1 NAME
8              
9             CGI::Wiki::Formatter::Multiple - Allows a CGI::Wiki wiki to use more than one formatter.
10              
11             =head1 DESCRIPTION
12              
13             A "dummy" formatter for L. Passes methods through to other
14             CGI::Wiki formatters, depending on supplied metadata.
15              
16             =head1 SYNOPSIS
17              
18             use CGI::Wiki::Formatter::Multiple;
19             use CGI::Wiki::Formatter::Pod;
20             use CGI::Wiki::Formatter::UseMod;
21              
22             my $pod_fmtr = CGI::Wiki::Formatter::Pod->new(
23             node_prefix => "wiki.cgi?node=",
24             );
25              
26             my $usemod_fmtr = CGI::Wiki::Formatter::UseMod->new(
27             node_prefix => "wiki.cgi?node=",
28             extended_links => 1,
29             allowed_tags => [ qw( p b i div br ) ],
30             );
31              
32             my $formatter = CGI::Wiki::Formatter::Multiple->new(
33             documentation => $pod_fmtr,
34             discussion => $usemod_fmtr,
35             _DEFAULT => $usemod_fmtr,
36             );
37              
38             my $wiki = CGI::Wiki->new( store => ...,
39             formatter => $formatter );
40             my $output = $wiki->format( "This is some discussion.",
41             { formatter => "discussion" } );
42            
43             =head1 METHODS
44              
45             =over
46              
47             =item B
48              
49             my $formatter = CGI::Wiki::Formatter::Multiple->new(
50             label_1 => Formatter1->new( ... ),
51             label_2 => Formatter2->new( ... ),
52             _DEFAULT => CGI::Wiki::Formatter::Default->new,
53             );
54              
55             You may supply as many formatter objects as you wish. They don't have
56             to be of different classes; you may just wish to, for example, permit
57             different HTML tags to be used on different types of pages.
58              
59             The "labels" supplied as the keys of the parameter hash should be
60             unique. When you write a node, you should store a key-value pair in
61             its metadata where the key is C and the value is the label
62             of the formatter that should be used to render that node.
63              
64             The C<_DEFAULT> label is special - it defines the formatter that will
65             be used for any node that does not have a C stored in its
66             metadata. The C<_DEFAULT> formatter, if not supplied to C<< ->new >>,
67             will default to the very basic L.
68              
69             =cut
70              
71             sub new {
72 0     0 1   my ($class, %args) = @_;
73 0           my $self = bless {}, $class;
74 0 0         unless ( $args{_DEFAULT} ) {
75 0           require CGI::Wiki::Formatter::Default;
76 0           $args{_DEFAULT} = CGI::Wiki::Formatter::Default->new;
77             }
78 0           $self->{formatters} = \%args;
79 0           return $self;
80             }
81              
82             =item B
83              
84             my $output = $formatter->format( "Here is some text.", undef,
85             { formatter => "discussion" } );
86              
87             Uses the value of C given in the metadata to decide which
88             of the formatter objects passed on instantiation to use, then uses it
89             to format the provided rawwikitext.
90              
91             The C second element of the parameter array in the example is
92             there because when this is called from a L object, the wiki
93             object passes itself in as the second parameter.
94              
95             =cut
96              
97             sub format {
98 0     0 1   my ($self, $raw, $wiki, $metadata) = @_;
99 0           return $self->_formatter($metadata)->format($raw, $wiki);
100             }
101              
102             =item B
103              
104             =cut
105              
106             sub find_internal_links {
107 0     0 1   my ($self, $raw, $metadata) = @_;
108 0 0         return () unless $self->_formatter($metadata);
109 0 0         return () unless $self->_formatter($metadata)->can("find_internal_links");
110 0           return $self->_formatter($metadata)->find_internal_links($raw, $metadata);
111             }
112              
113             # internal method to return the correct formatter for the current
114             # page.
115              
116             sub _formatter {
117 0     0     my $self = shift;
118 0           my $metadata = shift;
119 0   0       my $label = $metadata->{formatter} || "_DEFAULT";
120 0 0         $label = $label->[0] if ref($label);
121 0   0       return $self->{formatters}{$label} || $self->{formatters}{_DEFAULT};
122             }
123              
124             =back
125              
126             =head1 SEE ALSO
127              
128             L
129              
130             =head1 AUTHOR
131              
132             Kake Pugh
133              
134             =head1 SUPPORT
135              
136             Bug reports, questions and feature requests should go to cgi-wiki-dev@earth.li
137              
138             =head1 COPYRIGHT
139              
140             Copyright (C) 2003-4 Kake Pugh. All Rights Reserved.
141              
142             This module is free software; you can redistribute it and/or modify it
143             under the same terms as Perl itself.
144              
145             =cut
146              
147             1;