File Coverage

blib/lib/Wiki/Toolkit/Formatter/Multiple.pm
Criterion Covered Total %
statement 11 24 45.8
branch 1 8 12.5
condition 0 5 0.0
subroutine 3 6 50.0
pod 3 3 100.0
total 18 46 39.1


line stmt bran cond sub pod time code
1             package Wiki::Toolkit::Formatter::Multiple;
2 2     2   1069 use strict;
  2         3  
  2         56  
3              
4 2     2   8 use vars qw( $VERSION );
  2         3  
  2         510  
5             $VERSION = '0.02';
6              
7             =head1 NAME
8              
9             Wiki::Toolkit::Formatter::Multiple - Allows a Wiki::Toolkit wiki to use more than one formatter.
10              
11             =head1 DESCRIPTION
12              
13             A "dummy" formatter for L. Passes methods through to other
14             Wiki::Toolkit formatters, depending on supplied metadata.
15              
16             =head1 SYNOPSIS
17              
18             use Wiki::Toolkit::Formatter::Multiple;
19             use Wiki::Toolkit::Formatter::Pod;
20             use Wiki::Toolkit::Formatter::UseMod;
21              
22             my $pod_fmtr = Wiki::Toolkit::Formatter::Pod->new(
23             node_prefix => "wiki.cgi?node=",
24             );
25              
26             my $usemod_fmtr = Wiki::Toolkit::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 = Wiki::Toolkit::Formatter::Multiple->new(
33             documentation => $pod_fmtr,
34             discussion => $usemod_fmtr,
35             _DEFAULT => $usemod_fmtr,
36             );
37              
38             my $wiki = Wiki::Toolkit->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 = Wiki::Toolkit::Formatter::Multiple->new(
50             label_1 => Formatter1->new( ... ),
51             label_2 => Formatter2->new( ... ),
52             _DEFAULT => Wiki::Toolkit::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 1     1 1 23 my ($class, %args) = @_;
73 1         3 my $self = bless {}, $class;
74 1 50       6 unless ( $args{_DEFAULT} ) {
75 0         0 require Wiki::Toolkit::Formatter::Default;
76 0         0 $args{_DEFAULT} = Wiki::Toolkit::Formatter::Default->new;
77             }
78 1         5 $self->{formatters} = \%args;
79 1         2 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;