| 1 | package Wiki::Toolkit::Formatter::Multiple; | ||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | 2 | ▶ | use vars qw( $VERSION ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 2 | $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<Wiki::Toolkit>. 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<new> | ||||||||||||||||||||
| 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<formatter> 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<formatter> stored in its | ||||||||||||||||||||
| 66 | metadata. The C<_DEFAULT> formatter, if not supplied to C<< ->new >>, | ||||||||||||||||||||
| 67 | will default to the very basic L<Wiki::Toolkit::Formatter::Default>. | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | =cut | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 1 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||
| 72 | 1 | my ($class, %args) = @_; | |||||||||||||||||||
| 73 | 1 | my $self = bless {}, $class; | |||||||||||||||||||
| 74 | 1 | ▶ | unless ( $args{_DEFAULT} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 75 | 0 | require Wiki::Toolkit::Formatter::Default; | |||||||||||||||||||
| 76 | 0 | $args{_DEFAULT} = Wiki::Toolkit::Formatter::Default->new; | |||||||||||||||||||
| 77 | } | ||||||||||||||||||||
| 78 | 1 | $self->{formatters} = \%args; | |||||||||||||||||||
| 79 | 1 | return $self; | |||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | =item B<format( $raw, \%metadata )> | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | my $output = $formatter->format( "Here is some text.", undef, | ||||||||||||||||||||
| 85 | { formatter => "discussion" } ); | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | Uses the value of C<formatter> 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<undef> second element of the parameter array in the example is | ||||||||||||||||||||
| 92 | there because when this is called from a L<Wiki::Toolkit> object, the wiki | ||||||||||||||||||||
| 93 | object passes itself in as the second parameter. | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | =cut | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 0 | ▶ | sub format { | ||||||||||||||||||
|
Subroutine
format: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 98 | 0 | my ($self, $raw, $wiki, $metadata) = @_; | |||||||||||||||||||
| 99 | 0 | return $self->_formatter($metadata)->format($raw, $wiki); | |||||||||||||||||||
| 100 | } | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | =item B<find_internal_links( $raw, $metadata )> | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | =cut | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 0 | ▶ | sub find_internal_links { | ||||||||||||||||||
|
Subroutine
find_internal_links: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 107 | 0 | my ($self, $raw, $metadata) = @_; | |||||||||||||||||||
| 108 | 0 | ▶ | return () unless $self->_formatter($metadata); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 109 | 0 | ▶ | return () unless $self->_formatter($metadata)->can("find_internal_links"); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | 0 | ▶ | sub _formatter { | ||||||||||||||||||
|
Subroutine
_formatter: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 117 | 0 | my $self = shift; | |||||||||||||||||||
| 118 | 0 | my $metadata = shift; | |||||||||||||||||||
| 119 | 0 | ▶ | my $label = $metadata->{formatter} || "_DEFAULT"; | ||||||||||||||||||
|
Condition0%0/2
$metadata->{'formatter'} || '_DEFAULT'
MC/DC0%0/1
$metadata->{'formatter'} || '_DEFAULT'
$metadata->{'formatter'}
Truth table0%0/2
A
A= $metadata->{'formatter'}
| |||||||||||||||||||||
| 120 | 0 | ▶ | $label = $label->[0] if ref($label); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 121 | 0 | ▶ | return $self->{formatters}{$label} || $self->{formatters}{_DEFAULT}; | ||||||||||||||||||
|
Condition0%0/3
$self->{'formatters'}{$label} || $self->{'formatters'}{'_DEFAULT'}
MC/DC0%0/2
$self->{'formatters'}{$label} || $self->{'formatters'}{'_DEFAULT'}
$self->{'formatters'}{$label}
$self->{'formatters'}{'_DEFAULT'}
Truth table0%0/3
A || B
A= $self->{'formatters'}{$label}B= $self->{'formatters'}{'_DEFAULT'}
| |||||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | =back | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | =head1 SEE ALSO | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | L<Wiki::Toolkit> | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | =head1 AUTHOR | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | Kake Pugh <kake@earth.li> | ||||||||||||||||||||
| 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 | 2 | 1; | |||||||||||||||||||