package MojoMojo::Formatter::Wiki;
use base qw/MojoMojo::Formatter/;
use URI;
use Scalar::Util qw/blessed/;
use MojoMojo::Formatter::TOC;
=head1 NAME
MojoMojo::Formatter::Wiki - Handle interpage linking.
=head1 DESCRIPTION
This formatter handles intra-Wiki links specified between double square brackets
or parentheses: [[wiki link]] or ((another wiki link)). It will also indicate
missing links with a question mark and a link to the edit page. Links can be
implicit (like the two above), where the path is derived from the link text
by replacing spaces with underscores (wiki link), or
explicit, where the path is specified before a '|' sign:
[[/explicit/path|Link text goes here]]
Note that external links have a different syntax: [Link text](http://foo.com).
=head1 METHODS
=over 4
=item format_content_order
Format order can be 1-99. The Wiki formatter runs on 10
=cut
sub format_content_order { 10 }
## list of start-end delimiter pairs
my @explicit_delims = (qw{ \[\[ \]\] \(\( \)\) });
my $explicit_separator = '\|';
my $wikiword_escape = qr{\\};
sub _explicit_start_delims {
my %delims = @explicit_delims;
return keys %delims;
}
sub _explicit_end_delims {
my %delims = @explicit_delims;
return values %delims;
}
sub _generate_explicit_start {
my $delims = join '|', _explicit_start_delims();
return qr{(?: $delims )}x; # non-capturing match
}
sub _generate_explicit_end {
my $delims = join '|', _explicit_end_delims();
return qr{(?: $delims )}x; # non-capturing match
}
sub _generate_explicit_path {
# non-greedily match characters that don't match the start-end and text delimiters
my $not_an_end_delimiter_or_separator = '(?:(?!' . (join '|', _explicit_end_delims(), $explicit_separator) . ').)'; # produces (?: (?! ]] | \)\) | \| ) .) # a character in a place where neither a ]], nor a )), nor a | is
return qr{$not_an_end_delimiter_or_separator+?};
}
sub _generate_explicit_text {
# non-greedily match characters that don't match the start-end delimiters
my $not_an_end_delimiter = '(?:(?!' . join '|', _explicit_end_delims() . ').)'; # produces (?: (?! ]] | \)\) ) .) # a character in a place where neither a ]] nor a )) starts
return qr{$not_an_end_delimiter+?};
}
my $explicit_start = _generate_explicit_start();
my $explicit_end = _generate_explicit_end();
my $explicit_path = _generate_explicit_path();
my $explicit_text = _generate_explicit_text();
sub _generate_non_wikiword_check {
# FIXME: this evaluates incorrectly to a regexp that's clearly mistaken: (?x-ism:( ?]*>}sx
)
{
# $$content =~ s{^.+?<\s*pre\b[^>]*>}{}sx;
$$content =~ s{^.+?<\s*pre(?:\s+lang=['"]*(.*?)['"]*")?>}{}sx;
my $lang = $1 || '';
my ($inner) = $$content =~ m{^(.+?)<\s*/pre\s*>}sx;
unless ($inner) {
$res .= $part;
last;
}
push @parts, $inner;
$res .= $part . "";
$$content =~ s{^.+?<\s*/pre\s*>}{}sx;
}
$res .= $$content;
return $res, @parts;
}
sub reinsert_pre {
my ( $content, @parts ) = @_;
foreach my $part (@parts) {
$$content =~ s{}{
$part
}sx;
}
return $$content;
}
=item format_content
calls the formatter. Takes a ref to the content as well as the
context object.
=cut
sub format_content {
my ( $class, $content, $c, $self ) = @_;
# Extract wikiwords, avoiding escaped and part of urls
my @parts;
( $$content, @parts ) = strip_pre($content);
# Do explicit links, e.g. [[ /path/to/page | link text ]]
$$content =~ s{
$non_wikiword_check
$explicit_start
\s*
($explicit_path)
\s*
(?:
$explicit_separator
\s*
($explicit_text)
\s*
)?
$explicit_end
}{ $class->format_link($c, $1, $c->req->base, $2) }gex;
$$content =~ s{
$non_wikiword_check
(
$explicit_start
\s*
$explicit_path
\s*
(?:
$explicit_separator
\s*
$explicit_text
\s*
)?
$explicit_end
)
}{ $1 }gx;
# Remove escapes on escaped wikiwords. The escape means
# that this wikiword is NOT a link to a wiki page.
$$content =~ s{$wikiword_escape($explicit_start)}{$1}g;
$$content = reinsert_pre( $content, @parts );
}
=item format_link []
Format a wikilink as an HTML hyperlink with the given link_text. If the wikilink
doesn't exist, it will be rendered as a hyperlink to an .edit page ready to be
created.
Since there is no difference in syntax between new and existing links, some
abiguities my occur when it comes to characters that are invalid in URLs. For
example,
* [[say "NO" to #8]] should be rendered as say "NO" to #8
* [[100% match]] should be rendered as Mambo_%235
* [[Mambo#origins]] - do not URL-escape
* [[existing/link#Introduction|See the Introduction]] - definitely do not URL-escape
Since escaping is somewhat magic and therefore potentially counter-intuitive,
we will:
* only URL-escape '#' if it follows a whitespace directly
* always URL-escape '%' unless it is followed by two uppercase hex digits
* always escape other characters that are invalid in URLs
=cut
sub format_link {
#FIXME: why both base and $c?
my ( $class, $c, $wikilink, $base, $link_text ) = @_;
$base ||= $c->req->base;
# prepend the base, unless the wikilink is a relative path
$wikilink = ( blessed $c->stash->{page} ? $c->stash->{page}->path : $c->stash->{page}->{path} ). '/' . $wikilink
unless $wikilink =~ m'^(\.\.)?/';
$c = MojoMojo->context unless ref $c;
# keep the original wikilink for display, stripping leading slashes
my $orig_wikilink = $wikilink;
if ( $orig_wikilink =~ m|^ \s* /+ \s* $|x ) {
$orig_wikilink = '/';
}
else {
$orig_wikilink =~ s/.*\///;
}
my $fragment = '';
for ($wikilink) {
s/(??{|}] # escape all other characters that are invalid in URLs
/sprintf('%%%02X', ord($&))/egx; # all other characters in the 0x21..0x7E range are OK in URLs; see the conflicting guidelines at http://www.ietf.org/rfc/rfc1738.txt and http://labs.apache.org/webarch/uri/rfc/rfc3986.html#reserved
}
}
# if the fragment was not properly formatted as a fragment (per the rules explained in MojoMojo::Formatter::TOC::assembleAnchorName, i.e. i has an invalid character), convert it, unless it contains escaped characters already (.[0-9A-F]{2})
if(MojoMojo::Formatter::TOC->module_loaded){
$fragment = MojoMojo::Formatter::TOC::assembleAnchorName(undef, undef, undef, undef, $fragment)
if $fragment ne '' and ($fragment =~ /[^A-Za-z0-9_:.-]/ or $fragment !~ /\.[0-9A-F]{2}/);
}
my $formatted = $link_text || $class->expand_wikilink($orig_wikilink);
# convert relative paths to absolute paths
if (
$c->stash->{page}
&&
# drop spaces
ref $c->stash->{page} eq 'MojoMojo::Model::DBIC::Page' && $wikilink !~ m|^/|
)
{
$wikilink = URI->new_abs( $wikilink, $c->stash->{page}->path . "/" );
}
elsif ( $c->stash->{page_path} && $wikilink !~ m|^/| ) {
$wikilink = URI->new_abs( $wikilink, $c->stash->{page_path} . "/" );
}
# make sure that base URL has no trailing slash, since the page path will have a leading slash
my $url = $base;
$url =~ s/[\/]+$//;
# remove http://host/ from url
$url =~ s!^https?://[^/]+!!;
# use the normalized path string returned by path_pages:
my ( $path_pages, $proto_pages ) = $c->model('DBIC::Page')->path_pages($wikilink);
if ( defined $proto_pages && @$proto_pages ) {
my $proto_page = pop @$proto_pages;
$url .= $proto_page->{path};
return qq{$formatted?};
}
else {
my $page = pop @$path_pages;
$url .= $page->path;
$url .= "#$fragment" if $fragment ne '';
return qq{$formatted};
}
}
=item expand_wikilink
Replace _ with spaces and unescape URL-encoded characters
=cut
sub expand_wikilink {
my ( $class, $wikilink ) = @_;
for ($wikilink) {
s/\_/ /g;
s/%([0-9A-F]{2})/chr(hex($1))/eg;
}
return $wikilink;
}
=item find_links
Find wiki links in content.
Return a listref of linked (existing) and wanted pages.
=cut
sub find_links {
my ( $class, $content, $page ) = @_;
my @linked_pages;
my @wanted_pages;
my @parts;
( $$content, @parts ) = strip_pre($content);
my $explicit_regex =
qr/$non_wikiword_check$explicit_start \s* ($explicit_path) \s* (?: $explicit_separator \s* $explicit_text \s* )? $explicit_end/x;
while ( $$content =~ /$explicit_regex/g ) {
my $link = $1;
$link =~ s/\s/_/g;
# convert relative paths to absolute paths
if ( $link !~ m|^/| ) {
$link = URI->new_abs( $link, ( $page->path || '' ) . "/" );
}
# use the normalized path string returned by path_pages:
my ( $path_pages, $proto_pages ) =
$page->result_source->resultset->path_pages($link);
if ( defined $proto_pages && @$proto_pages ) {
push @wanted_pages, pop @$proto_pages;
}
else {
push @linked_pages, pop @$path_pages;
}
}
$$content = reinsert_pre( $content, @parts );
return ( \@linked_pages, \@wanted_pages );
}
=back
=head1 SEE ALSO
L,L
=head1 AUTHORS
Marcus Ramberg
=head1 LICENSE
This module is licensed under the same terms as Perl itself.
=cut
1;