From a3dd70b967a58ff5895faccb3724d2a7feeac2b0 Mon Sep 17 00:00:00 2001 From: "Kyle J. McKay" Date: Tue, 10 Jan 2017 11:25:43 -0800 Subject: [PATCH] Markdown.pl: handle some limited [[wiki style links]] The double bracket links are pervasive in source documents. Recognize them and process them. At this point only links that reference an absolute URL are recognized and turned into clickable links. Everything else is passed on through unchanged. Signed-off-by: Kyle J. McKay --- Markdown.pl | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/Markdown.pl b/Markdown.pl index 8f29969..c7bc468 100755 --- a/Markdown.pl +++ b/Markdown.pl @@ -730,6 +730,25 @@ sub _EscapeSpecialChars { } +sub _ProcessWikiLink { + my ($link_text, $link_loc) = @_; + if (defined($link_loc) && $link_loc =~ m{^(?:http|ftp)s?://\S+$}i) { + # Just rewrite it to [...](...) form + return "[".$link_text."](".$link_loc.")"; + } + if (defined($link_loc)) { + # We don't handle any other kind of "bar" links yet + return undef; + } + if ($link_text =~ m{^(?:http|ftp)s?://\S+$}i) { + # Just rewrite it to [...](...) form + return "[".$link_text."](".$link_text.")"; + } + # We don't handle any other wiki-style links yet + return undef; +} + + sub _DoAnchors { # # Turn Markdown link shortcuts into XHTML tags. @@ -737,7 +756,32 @@ sub _DoAnchors { my $text = shift; # - # First, handle reference-style links: [link text] [id] + # First, handle wiki-style links: [[wiki style link]] + # + $text =~ s{ + ( # wrap whole match in $1 + \[\[ + ($g_nested_brackets) # link text and id = $2 + \]\] + ) + }{ + my $result; + my $whole_match = $1; + my $link_text = $2; + my $link_loc = undef; + + if ($link_text =~ /^(.*)\|(.*)$/s) { + $link_text = $1; + $link_loc = $2; + } + + $result = _ProcessWikiLink($link_text, $link_loc); + defined($result) or $result = $whole_match; + $result; + }xsge; + + # + # Next, handle reference-style links: [link text] [id] # $text =~ s{ ( # wrap whole match in $1 @@ -783,7 +827,7 @@ sub _DoAnchors { }xsge; # - # Next, inline-style links: [link text](url "optional title") + # Finally, inline-style links: [link text](url "optional title") # $text =~ s{ ( # wrap whole match in $1