Author: earle Date: 2008-01-22 16:38:24 +0000 (Tue, 22 Jan 2008) New Revision: 1141
Modified: trunk/Changes trunk/lib/OpenGuides.pm trunk/lib/OpenGuides/Utils.pm trunk/t/45_home_recent_changes.t Log: Allow wiki links in change summaries. Closes #115.
Modified: trunk/Changes =================================================================== --- trunk/Changes 2008-01-20 18:46:51 UTC (rev 1140) +++ trunk/Changes 2008-01-22 16:38:24 UTC (rev 1141) @@ -4,6 +4,8 @@ http://dev.openguides.org/log/trunk.
0.62 + Allow wiki page links (simple: [[Foo]], title: [[Foo|bar]]) in + change summaries. Ensure that all modules we ship are versioned. Added experimental support for local IP blacklisting modules; see the display_node method in "perldoc OpenGuides" for details.
Modified: trunk/lib/OpenGuides/Utils.pm =================================================================== --- trunk/lib/OpenGuides/Utils.pm 2008-01-20 18:46:51 UTC (rev 1140) +++ trunk/lib/OpenGuides/Utils.pm 2008-01-22 16:38:24 UTC (rev 1141) @@ -393,6 +393,47 @@
};
+=item B<parse_change_comment> + + my $change_comment = parse_change_comment($string, $base_url); + +Given a base URL (for example, Chttp://example.com/wiki.cgi?), takes a string, +replaces C<[[page]]> and C<[[page|titled link]]> with + + <a href="http://example.com/wiki.cgi?page">page</a> + +and + + <a href="http://example.com/wiki.cgi?page">titled link</a> + +respectively, and returns it. This is a limited subset of wiki markup suitable for +use in page change comments. + +=cut + +sub parse_change_comment { + my ($comment, $base_url) = @_; + + my @links = $comment =~ m{[[(.*?)]]}g; + + # It's not all that great having to reinvent the wheel in this way, but + # Text::WikiFormat won't let you specify the subset of wiki notation that + # you're interested in. C'est la vie. + foreach (@links) { + if (/(.*?)|(.*)/) { + my ($page, $title) = ($1, $2); + $comment =~ s{[[$page|$title]]} + {<a href="$base_url$page">$title</a>}; + } else { + my $page = $_; + $comment =~ s{[[$page]]} + {<a href="$base_url$page">$page</a>}; + } + } + + return $comment; +} + =item B<send_email>
eval { OpenGuides::Utils->send_email( @@ -421,6 +462,7 @@
=cut
+ sub send_email { my ( $self, %args ) = @_; my $config = $args{config} or die "config argument not supplied";
Modified: trunk/lib/OpenGuides.pm =================================================================== --- trunk/lib/OpenGuides.pm 2008-01-20 18:46:51 UTC (rev 1140) +++ trunk/lib/OpenGuides.pm 2008-01-22 16:38:24 UTC (rev 1141) @@ -338,17 +338,20 @@ last_n_changes => 10, metadata_was => { edit_type => "Normal edit" }, ); + my $base_url = $config->script_name . '?'; @recent = map { { name => CGI->escapeHTML($_->{name}), last_modified => CGI->escapeHTML($_->{last_modified}), version => CGI->escapeHTML($_->{version}), - comment => + comment => OpenGuides::Utils::parse_change_comment( CGI->escapeHTML($_->{metadata}{comment}[0]), + $base_url, + ), username => CGI->escapeHTML($_->{metadata}{username}[0]), - url => $config->script_name . "?" + url => $base_url . CGI->escape($wiki->formatter->node_name_to_node_param($_->{name})) } } @recent; @@ -652,18 +655,23 @@ $criteria{metadata_was} = { edit_type => "Normal edit" } unless $minor_edits; my @rc = $self->{wiki}->list_recent_changes( %criteria ); - + + my $base_url = $config->script_name . '?'; + @rc = map { { name => CGI->escapeHTML($_->{name}), last_modified => CGI->escapeHTML($_->{last_modified}), version => CGI->escapeHTML($_->{version}), - comment => CGI->escapeHTML($_->{metadata}{comment}[0]), + comment => OpenGuides::Utils::parse_change_comment( + CGI->escapeHTML($_->{metadata}{comment}[0]), + $base_url, + ), username => CGI->escapeHTML($_->{metadata}{username}[0]), host => CGI->escapeHTML($_->{metadata}{host}[0]), username_param => CGI->escape($_->{metadata}{username}[0]), edit_type => CGI->escapeHTML($_->{metadata}{edit_type}[0]), - url => $config->script_name . "?" + url => $base_url . CGI->escape($wiki->formatter->node_name_to_node_param($_->{name})), } } @rc; @@ -677,17 +685,22 @@ unless $minor_edits; my @rc = $self->{wiki}->list_recent_changes( %criteria );
+ my $base_url = $config->script_name . '?'; + @rc = map { { name => CGI->escapeHTML($_->{name}), last_modified => CGI->escapeHTML($_->{last_modified}), version => CGI->escapeHTML($_->{version}), - comment => CGI->escapeHTML($_->{metadata}{comment}[0]), + comment => OpenGuides::Utils::parse_change_comment( + CGI->escapeHTML($_->{metadata}{comment}[0]), + $base_url, + ), username => CGI->escapeHTML($_->{metadata}{username}[0]), host => CGI->escapeHTML($_->{metadata}{host}[0]), username_param => CGI->escape($_->{metadata}{username}[0]), edit_type => CGI->escapeHTML($_->{metadata}{edit_type}[0]), - url => $config->script_name . "?" + url => $base_url . CGI->escape($wiki->formatter->node_name_to_node_param($_->{name})), } } @rc; @@ -1021,8 +1034,11 @@ version => CGI->escapeHTML( $version ), modified => CGI->escapeHTML( $node_data{last_modified} ), username => CGI->escapeHTML( $node_data{metadata}{username}[0] ), - comment => CGI->escapeHTML( $node_data{metadata}{comment}[0] ), - } if $node_data{version}; + comment => OpenGuides::Utils::parse_change_comment( + CGI->escapeHTML( $node_data{metadata}{comment}[0] ), + $self->config->script_name . '?', + ), + } if $node_data{version}; } @history = reverse @history; my %tt_vars = (
Modified: trunk/t/45_home_recent_changes.t =================================================================== --- trunk/t/45_home_recent_changes.t 2008-01-20 18:46:51 UTC (rev 1140) +++ trunk/t/45_home_recent_changes.t 2008-01-22 16:38:24 UTC (rev 1141) @@ -10,7 +10,7 @@ plan skip_all => "DBD::SQLite could not be used - no database to test with ($error)"; }
-plan tests => 13; +plan tests => 15;
my ( $config, $guide, $wiki );
@@ -59,6 +59,24 @@ like( $output, qr/Kake/, "...and usernames" ); like( $output, qr/Edit this page/, "...edit this page link is there too" );
+OpenGuides::Test->write_data( + guide => $guide, + node => "Red Lion", + content => "A nice pub.", + username => "Earle", + comment => "I also edited it. For fun, here are two links: [[A Page]], and the same link [[A Page|again]].", + ); + +# Reload page. +$output = $guide->display_node( + id => $config->home_name, + return_output => 1, + ); + +like( $output, qr{<a href="\?A Page">A Page</a>}, "...simple wiki links appear in Recent Changes" ); +like( $output, qr{<a href="\?A Page">again</a>}, "...titled wiki links appear in Recent Changes" ); + + # And that they don't show up if we don't want them. Turn off the navbar # too, since we want to make sure the edit page link shows up regardless (it # normally appears in the recent changes box). @@ -77,3 +95,4 @@ unlike( $output, qr/Kake/, "...nor usernames" ); unlike( $output, qr/Ten most.*recent changes/, "...heading not shown either" ); like( $output, qr/Edit this page/, "...edit this page link is there though" ); +