| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Wiki::Toolkit::Formatter::Default; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 3 |  |  | 3 |  | 1784 | use strict; | 
|  | 3 |  |  |  |  | 4 |  | 
|  | 3 |  |  |  |  | 83 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 3 |  |  | 3 |  | 11 | use vars qw( $VERSION @ISA @_links_found ); | 
|  | 3 |  |  |  |  | 3 |  | 
|  | 3 |  |  |  |  | 176 |  | 
| 6 |  |  |  |  |  |  | $VERSION = '0.02'; | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 3 |  |  | 3 |  | 1032 | use Wiki::Toolkit::Formatter::WikiLinkFormatterParent; | 
|  | 3 |  |  |  |  | 7 |  | 
|  | 3 |  |  |  |  | 81 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 3 |  |  | 3 |  | 2390 | use CGI ":standard"; | 
|  | 3 |  |  |  |  | 64169 |  | 
|  | 3 |  |  |  |  | 20 |  | 
| 11 | 3 |  |  | 3 |  | 5614 | use Carp qw(croak carp); | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 197 |  | 
| 12 | 3 |  |  | 3 |  | 11 | use Text::WikiFormat as => 'wikiformat'; | 
|  | 3 |  |  |  |  | 6 |  | 
|  | 3 |  |  |  |  | 21 |  | 
| 13 | 3 |  |  | 3 |  | 1800 | use HTML::PullParser; | 
|  | 3 |  |  |  |  | 14425 |  | 
|  | 3 |  |  |  |  | 1056 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | @ISA = qw( Wiki::Toolkit::Formatter::WikiLinkFormatterParent ); | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | =head1 NAME | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | Wiki::Toolkit::Formatter::Default - A formatter for Wiki::Toolkit. | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | A formatter backend for L. | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | my $store     = Wiki::Toolkit::Store::SQLite->new( ... ); | 
| 28 |  |  |  |  |  |  | # See below for parameter details. | 
| 29 |  |  |  |  |  |  | my $formatter = Wiki::Toolkit::Formatter::Default->new( %config ); | 
| 30 |  |  |  |  |  |  | my $wiki      = Wiki::Toolkit->new( store     => $store, | 
| 31 |  |  |  |  |  |  | formatter => $formatter ); | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | =head1 METHODS | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | =over 4 | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | =item B | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | my $formatter = Wiki::Toolkit::Formatter::Default->new( | 
| 40 |  |  |  |  |  |  | extended_links  => 0, | 
| 41 |  |  |  |  |  |  | implicit_links  => 1, | 
| 42 |  |  |  |  |  |  | allowed_tags    => [qw(b i)],  # defaults to none | 
| 43 |  |  |  |  |  |  | macros          => {}, | 
| 44 |  |  |  |  |  |  | node_prefix     => 'wiki.cgi?node=' ); | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | Parameters will default to the values shown above (apart from | 
| 47 |  |  |  |  |  |  | C, which defaults to allowing no tags). | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | =over 4 | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | =item * macros - be aware that macros are processed I filtering | 
| 52 |  |  |  |  |  |  | out disallowed HTML tags.  Currently macros are just strings, maybe later | 
| 53 |  |  |  |  |  |  | we can add in subs if we think it might be useful. | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | =back | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | Macro example: | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | macros => { qr/(^|\b)\@SEARCHBOX(\b|$)/ => | 
| 60 |  |  |  |  |  |  | qq( | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | ) } | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | =cut | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | sub new { | 
| 68 | 6 |  |  | 6 | 1 | 7496 | my ($class, @args) = @_; | 
| 69 | 6 |  |  |  |  | 8 | my $self = {}; | 
| 70 | 6 |  |  |  |  | 13 | bless $self, $class; | 
| 71 | 6 | 50 |  |  |  | 15 | $self->_init(@args) or return undef; | 
| 72 | 6 |  |  |  |  | 13 | return $self; | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | sub _init { | 
| 76 | 6 |  |  | 6 |  | 15 | my ($self, %args) = @_; | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | # Store the parameters or their defaults. | 
| 79 | 6 |  |  |  |  | 24 | my %defs = ( extended_links  => 0, | 
| 80 |  |  |  |  |  |  | implicit_links  => 1, | 
| 81 |  |  |  |  |  |  | allowed_tags    => [], | 
| 82 |  |  |  |  |  |  | macros          => {}, | 
| 83 |  |  |  |  |  |  | node_prefix     => 'wiki.cgi?node=', | 
| 84 |  |  |  |  |  |  | ); | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 6 |  |  |  |  | 21 | my %collated = (%defs, %args); | 
| 87 | 6 |  |  |  |  | 13 | foreach my $k (keys %defs) { | 
| 88 | 30 |  |  |  |  | 53 | $self->{"_".$k} = $collated{$k}; | 
| 89 |  |  |  |  |  |  | } | 
| 90 |  |  |  |  |  |  |  | 
| 91 | 6 |  |  |  |  | 26 | return $self; | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | =item B | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | my $html = $formatter->format( $content ); | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | Escapes any tags which weren't specified as allowed on creation, then | 
| 99 |  |  |  |  |  |  | interpolates any macros, then calls Text::WikiFormat::format (with the | 
| 100 |  |  |  |  |  |  | config set up when B was called) to translate the raw Wiki | 
| 101 |  |  |  |  |  |  | language supplied into HTML. | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | =cut | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | sub format { | 
| 106 | 2 |  |  | 2 | 1 | 11 | my ($self, $raw) = @_; | 
| 107 | 2 |  |  |  |  | 2 | my $safe = ""; | 
| 108 |  |  |  |  |  |  |  | 
| 109 | 2 |  |  |  |  | 3 | my %allowed = map {lc($_) => 1, "/".lc($_) => 1} @{$self->{_allowed_tags}}; | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 2 |  |  |  |  | 3 |  | 
| 110 |  |  |  |  |  |  |  | 
| 111 | 2 | 50 |  |  |  | 5 | if (scalar keys %allowed) { | 
| 112 |  |  |  |  |  |  | # If we are allowing some HTML, parse and get rid of the nasties. | 
| 113 | 0 |  |  |  |  | 0 | my $parser = HTML::PullParser->new(doc   => $raw, | 
| 114 |  |  |  |  |  |  | start => '"TAG", tag, text', | 
| 115 |  |  |  |  |  |  | end   => '"TAG", tag, text', | 
| 116 |  |  |  |  |  |  | text  => '"TEXT", tag, text'); | 
| 117 | 0 |  |  |  |  | 0 | while (my $token = $parser->get_token) { | 
| 118 | 0 |  |  |  |  | 0 | my ($flag, $tag, $text) = @$token; | 
| 119 | 0 | 0 | 0 |  |  | 0 | if ($flag eq "TAG" and !defined $allowed{lc($tag)}) { | 
| 120 | 0 |  |  |  |  | 0 | $safe .= CGI::escapeHTML($text); | 
| 121 |  |  |  |  |  |  | } else { | 
| 122 | 0 |  |  |  |  | 0 | $safe .= $text; | 
| 123 |  |  |  |  |  |  | } | 
| 124 |  |  |  |  |  |  | } | 
| 125 |  |  |  |  |  |  | } else { | 
| 126 |  |  |  |  |  |  | # Else just escape everything. | 
| 127 | 2 |  |  |  |  | 6 | $safe = CGI::escapeHTML($raw); | 
| 128 |  |  |  |  |  |  | } | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | # Now process any macros. | 
| 131 | 2 |  |  |  |  | 389 | my %macros = %{$self->{_macros}}; | 
|  | 2 |  |  |  |  | 5 |  | 
| 132 | 2 |  |  |  |  | 4 | foreach my $regexp (keys %macros) { | 
| 133 | 0 |  |  |  |  | 0 | $safe =~ s/$regexp/$macros{$regexp}/g; | 
| 134 |  |  |  |  |  |  | } | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | return wikiformat($safe, {}, | 
| 137 |  |  |  |  |  |  | { extended       => $self->{_extended_links}, | 
| 138 |  |  |  |  |  |  | prefix         => $self->{_node_prefix}, | 
| 139 | 2 |  |  |  |  | 21 | implicit_links => $self->{_implicit_links} } ); | 
| 140 |  |  |  |  |  |  | } | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | =back | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | L | 
| 147 |  |  |  |  |  |  | L | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | =head1 AUTHOR | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | Kake Pugh (kake@earth.li). | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | =head1 COPYRIGHT | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | Copyright (C) 2002-2003 Kake Pugh.  All Rights Reserved. | 
| 156 |  |  |  |  |  |  | Copyright (C) 2006 the Wiki::Toolkit team. All Rights Reserved. | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | This module is free software; you can redistribute it and/or modify it | 
| 159 |  |  |  |  |  |  | under the same terms as Perl itself. | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | =cut | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | 1; |