| 1 |
NAME |
| 2 |
Wiki::Toolkit::Formatter::UseMod - UseModWiki-style formatting for Wiki::Toolkit |
| 3 |
|
| 4 |
DESCRIPTION |
| 5 |
A formatter backend for Wiki::Toolkit that supports UseMod-style formatting. |
| 6 |
|
| 7 |
SYNOPSIS |
| 8 |
use Wiki::Toolkit::Formatter::UseMod; |
| 9 |
|
| 10 |
# Instantiate - see below for parameter details. |
| 11 |
my $formatter = Wiki::Toolkit::Formatter::UseMod->new( %config ); |
| 12 |
|
| 13 |
# Format some text. |
| 14 |
my $cooked = $formatter->format($raw); |
| 15 |
|
| 16 |
# Find out which other nodes that text would link to. |
| 17 |
my @links_to = $formatter->find_internal_links($raw); |
| 18 |
|
| 19 |
METHODS |
| 20 |
new |
| 21 |
my $formatter = Wiki::Toolkit::Formatter::UseMod->new( |
| 22 |
extended_links => 0, # $FreeLinks |
| 23 |
implicit_links => 1, # $WikiLinks |
| 24 |
force_ucfirst_nodes => 1, # $FreeUpper |
| 25 |
use_headings => 1, # $UseHeadings |
| 26 |
allowed_tags => [qw(b i)], # defaults to none |
| 27 |
macros => {}, |
| 28 |
pass_wiki_to_macros => 0, |
| 29 |
node_prefix => 'wiki.pl?', |
| 30 |
node_suffix => '', |
| 31 |
edit_prefix => 'wiki.pl?action=edit;id=', |
| 32 |
edit_suffix => '', |
| 33 |
munge_urls => 0, |
| 34 |
); |
| 35 |
|
| 36 |
Parameters will default to the values shown above (apart from |
| 37 |
"allowed_tags", which defaults to allowing no tags). |
| 38 |
|
| 39 |
Internal links |
| 40 |
"node_prefix", "node_suffix", "edit_prefix" and "edit_suffix" |
| 41 |
allow you to control the URLs generated for links to other wiki |
| 42 |
pages. So for example with the defaults given above, a link to |
| 43 |
the Home node will have the URL "wiki.pl?Home" and a link to the |
| 44 |
edit form for the Home node will have the URL |
| 45 |
"wiki.pl?action=edit;id=Home" |
| 46 |
|
| 47 |
(Note that of course the URLs that you wish to have generated |
| 48 |
will depend on how your wiki application processes its CGI |
| 49 |
parameters - you can't just put random stuff in there and hope |
| 50 |
it works!) |
| 51 |
|
| 52 |
Internal links - advanced options |
| 53 |
If you wish to have greater control over the links, you may use |
| 54 |
the "munge_node_name" parameter. The value of this should be a |
| 55 |
subroutine reference. This sub will be called on each internal |
| 56 |
link after all other formatting and munging *except* URL |
| 57 |
escaping has been applied. It will be passed the node name as |
| 58 |
its first parameter and should return a node name. Note that |
| 59 |
this will affect the URLs of internal links, but not the link |
| 60 |
text. |
| 61 |
|
| 62 |
Example: |
| 63 |
|
| 64 |
# The formatter munges links so node names are ucfirst. |
| 65 |
# Ensure 'state51' always appears in lower case in node names. |
| 66 |
munge_node_name => sub { |
| 67 |
my $node_name = shift; |
| 68 |
$node_name =~ s/State51/state51/g; |
| 69 |
return $node_name; |
| 70 |
} |
| 71 |
|
| 72 |
Note: This is *advanced* usage and you should only do it if you |
| 73 |
*really* know what you're doing. Consider in particular whether |
| 74 |
and how your munged nodes are going to be treated by |
| 75 |
"retrieve_node". |
| 76 |
|
| 77 |
URL munging |
| 78 |
If you set "munge_urls" to true, then your URLs will be more |
| 79 |
user-friendly, for example |
| 80 |
|
| 81 |
http://example.com/wiki.cgi?Mailing_List_Managers |
| 82 |
|
| 83 |
rather than |
| 84 |
|
| 85 |
http://example.com/wiki.cgi?Mailing%20List%20Managers |
| 86 |
|
| 87 |
The former behaviour is the actual UseMod behaviour, but |
| 88 |
requires a little fiddling about in your code (see |
| 89 |
"node_name_to_node_param"), so the default is to not munge URLs. |
| 90 |
|
| 91 |
Macros |
| 92 |
Be aware that macros are processed *after* filtering out |
| 93 |
disallowed HTML tags and *before* transforming from wiki markup |
| 94 |
into HTML. They are also not called in any particular order. |
| 95 |
|
| 96 |
The keys of macros should be either regexes or strings. The |
| 97 |
values can be strings, or, if the corresponding key is a regex, |
| 98 |
can be coderefs. The coderef will be called with the first nine |
| 99 |
substrings captured by the regex as arguments. I would like to |
| 100 |
call it with all captured substrings but apparently this is |
| 101 |
complicated. |
| 102 |
|
| 103 |
You may wish to have access to the overall wiki object in the |
| 104 |
subs defined in your macro. To do this: |
| 105 |
|
| 106 |
* Pass the wiki object to the "->formatter" call as described |
| 107 |
below. |
| 108 |
|
| 109 |
* Pass a true value in the "pass_wiki_to_macros" parameter |
| 110 |
when calling "->new". |
| 111 |
|
| 112 |
If you do this, then *all* coderefs will be called with the wiki |
| 113 |
object as the first parameter, followed by the first nine |
| 114 |
captured substrings as described above. Note therefore that |
| 115 |
setting "pass_wiki_to_macros" may cause backwards compatibility |
| 116 |
issues. |
| 117 |
|
| 118 |
Macro examples: |
| 119 |
|
| 120 |
# Simple example - substitute a little search box for '@SEARCHBOX' |
| 121 |
|
| 122 |
macros => { |
| 123 |
|
| 124 |
'@SEARCHBOX' => |
| 125 |
qq(<form action="wiki.pl" method="get"> |
| 126 |
<input type="hidden" name="action" value="search"> |
| 127 |
<input type="text" size="20" name="terms"> |
| 128 |
<input type="submit"></form>), |
| 129 |
} |
| 130 |
|
| 131 |
# More complex example - substitute a list of all nodes in a |
| 132 |
# category for '@INDEX_LINK [[Category Foo]]' |
| 133 |
|
| 134 |
pass_wiki_to_macros => 1, |
| 135 |
macros => { |
| 136 |
qr/\@INDEX_LINK\s+\[\[Category\s+([^\]]+)]]/ => |
| 137 |
sub { |
| 138 |
my ($wiki, $category) = @_; |
| 139 |
my @nodes = $wiki->list_nodes_by_metadata( |
| 140 |
metadata_type => "category", |
| 141 |
metadata_value => $category, |
| 142 |
ignore_case => 1, |
| 143 |
); |
| 144 |
my $return = "\n"; |
| 145 |
foreach my $node ( @nodes ) { |
| 146 |
$return .= "* " |
| 147 |
. $wiki->formatter->format_link( |
| 148 |
wiki => $wiki, |
| 149 |
link => $node, |
| 150 |
) |
| 151 |
. "\n"; |
| 152 |
} |
| 153 |
return $return; |
| 154 |
}, |
| 155 |
} |
| 156 |
|
| 157 |
format |
| 158 |
my $html = $formatter->format($submitted_content, $wiki); |
| 159 |
|
| 160 |
Escapes any tags which weren't specified as allowed on creation, |
| 161 |
then interpolates any macros, then translates the raw Wiki language |
| 162 |
supplied into HTML. |
| 163 |
|
| 164 |
A Wiki::Toolkit object can be supplied as an optional second parameter. |
| 165 |
This object will be used to determine whether a linked-to node |
| 166 |
exists or not, and alter the presentation of the link accordingly. |
| 167 |
This is only really in here for use when this method is being called |
| 168 |
from within Wiki::Toolkit. |
| 169 |
|
| 170 |
format_link |
| 171 |
my $string = $formatter->format_link( |
| 172 |
link => "Home Node", |
| 173 |
wiki => $wiki, |
| 174 |
); |
| 175 |
|
| 176 |
An internal method exposed to make it easy to go from eg |
| 177 |
|
| 178 |
* Foo |
| 179 |
* Bar |
| 180 |
|
| 181 |
to |
| 182 |
|
| 183 |
* <a href="index.cgi?Foo">Foo</a> |
| 184 |
* <a href="index.cgi?Bar">Bar</a> |
| 185 |
|
| 186 |
See Macro Examples above for why you might find this useful. |
| 187 |
|
| 188 |
"link" should be something that would go inside your extended link |
| 189 |
delimiters. "wiki" is optional but should be a Wiki::Toolkit object. If |
| 190 |
you do supply "wiki" then the method will be able to check whether |
| 191 |
the node exists yet or not and so will call "->make_edit_link" |
| 192 |
instead of "->make_internal_link" where appropriate. If you don't |
| 193 |
supply "wiki" then "->make_internal_link" will be called always. |
| 194 |
|
| 195 |
This method used to be private so may do unexpected things if you |
| 196 |
use it in a way that I haven't tested yet. |
| 197 |
|
| 198 |
find_internal_links |
| 199 |
my @links_to = $formatter->find_internal_links( $content ); |
| 200 |
|
| 201 |
Returns a list of all nodes that the supplied content links to. |
| 202 |
|
| 203 |
node_name_to_node_param |
| 204 |
use URI::Escape; |
| 205 |
$param = $formatter->node_name_to_node_param( "Recent Changes" ); |
| 206 |
my $url = "wiki.pl?" . uri_escape($param); |
| 207 |
|
| 208 |
In usemod, the node name is encoded prior to being used as part of |
| 209 |
the URL. This method does this encoding (essentially, whitespace is |
| 210 |
munged into underscores). In addition, if "force_ucfirst_nodes" is |
| 211 |
in action then the node names will be forced ucfirst if they weren't |
| 212 |
already. |
| 213 |
|
| 214 |
Note that unless "munge_urls" was set to true when "new" was called, |
| 215 |
this method will do nothing. |
| 216 |
|
| 217 |
node_param_to_node_name |
| 218 |
my $node = $q->param('node') || ""; |
| 219 |
$node = $formatter->node_param_to_node_name( $node ); |
| 220 |
|
| 221 |
In usemod, the node name is encoded prior to being used as part of |
| 222 |
the URL, so we must decode it before we can get back the original |
| 223 |
node name. |
| 224 |
|
| 225 |
Note that unless "munge_urls" was set to true when "new" was called, |
| 226 |
this method will do nothing. |
| 227 |
|
| 228 |
SUBCLASSING |
| 229 |
The following methods can be overridden to provide custom behaviour. |
| 230 |
|
| 231 |
make_edit_link |
| 232 |
my $link = $self->make_edit_link( |
| 233 |
title => "Home Page", |
| 234 |
url => "http://example.com/?id=Home", |
| 235 |
); |
| 236 |
|
| 237 |
This method will be passed a title and a url and should return |
| 238 |
an HTML snippet. For example, you can add a "title" attribute to |
| 239 |
the link like so: |
| 240 |
|
| 241 |
sub make_edit_link { |
| 242 |
my ($self, %args) = @_; |
| 243 |
my $title = $args{title}; |
| 244 |
my $url = $args{url}; |
| 245 |
return qq|[$title]<a href="$url" title="create">?</a>|; |
| 246 |
} |
| 247 |
|
| 248 |
make_internal_link |
| 249 |
my $link = $self->make_internal_link( |
| 250 |
title => "Home Page", |
| 251 |
url => "http://example.com/?id=Home", |
| 252 |
); |
| 253 |
|
| 254 |
This method will be passed a title and a url and should return |
| 255 |
an HTML snippet. For example, you can add a "class" attribute to |
| 256 |
the link like so: |
| 257 |
|
| 258 |
sub make_internal_link { |
| 259 |
my ($self, %args) = @_; |
| 260 |
my $title = $args{title}; |
| 261 |
my $url = $args{url}; |
| 262 |
return qq|<a href="$url" class="internal">$title</a>|; |
| 263 |
} |
| 264 |
|
| 265 |
make_external_link |
| 266 |
my $link = $self->make_external_link( |
| 267 |
title => "London Perlmongers", |
| 268 |
url => "http://london.pm.org", |
| 269 |
); |
| 270 |
|
| 271 |
This method will be passed a title and a url and should return |
| 272 |
an HTML snippet. For example, you can add a little icon after |
| 273 |
each external link like so: |
| 274 |
|
| 275 |
sub make_external_link { |
| 276 |
my ($self, %args) = @_; |
| 277 |
my $title = $args{title}; |
| 278 |
my $url = $args{url}; |
| 279 |
return qq|<a href="$url">$title</a> <img src="external.gif">|; |
| 280 |
} |
| 281 |
|
| 282 |
AUTHOR |
| 283 |
Kake Pugh ([email protected]). |
| 284 |
|
| 285 |
COPYRIGHT |
| 286 |
Copyright (C) 2003-2004 Kake Pugh. All Rights Reserved. |
| 287 |
|
| 288 |
This module is free software; you can redistribute it and/or modify |
| 289 |
it under the same terms as Perl itself. |
| 290 |
|
| 291 |
CREDITS |
| 292 |
The OpenGuides London team (<http://openguides.org/london/>) sent |
| 293 |
some very helpful bug reports. A lot of the work of this module is |
| 294 |
done within chromatic's module, Text::WikiFormat. |
| 295 |
|
| 296 |
CAVEATS |
| 297 |
This doesn't yet support all of UseMod's formatting features and |
| 298 |
options, by any means. This really truly *is* a 0.* release. Please |
| 299 |
send bug reports, omissions, patches, and stuff, to me at |
| 300 |
"[email protected]". |
| 301 |
|
| 302 |
NOTE ON USEMOD COMPATIBILITY |
| 303 |
UseModWiki "encodes" node names before making them part of a URL, so |
| 304 |
for example a node about Wombat Defenestration will have a URL like |
| 305 |
|
| 306 |
http://example.com/wiki.cgi?Wombat_Defenestration |
| 307 |
|
| 308 |
So if we want to emulate a UseModWiki exactly, we need to munge back |
| 309 |
and forth between node names as titles, and node names as CGI |
| 310 |
params. |
| 311 |
|
| 312 |
my $formatter = Wiki::Toolkit::Formatter::UseMod->new( munge_urls => 1 ); |
| 313 |
my $node_param = $q->param('id') || $q->param('keywords') || ""; |
| 314 |
my $node_name = $formatter->node_param_to_node_name( $node_param ); |
| 315 |
|
| 316 |
use URI::Escape; |
| 317 |
my $url = "http://example.com/wiki.cgi?" |
| 318 |
. uri_escape( |
| 319 |
$formatter->node_name_to_node_param( "Wombat Defenestration" ) |
| 320 |
); |
| 321 |
|
| 322 |
SEE ALSO |
| 323 |
* Wiki::Toolkit |
| 324 |
* Text::WikiFormat |
| 325 |
* UseModWiki (<http://www.usemod.com/cgi-bin/wiki.pl>) |
| 326 |
|