From a63929f6cc7778ffc4ba57d784cdf2206ec650c7 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 11 Feb 2010 22:24:15 -0500 Subject: Group related plugins into sections in the setup file, and drop unused rcs plugins from the setup file. --- IkiWiki/Plugin/link.pm | 1 + 1 file changed, 1 insertion(+) (limited to 'IkiWiki/Plugin/link.pm') diff --git a/IkiWiki/Plugin/link.pm b/IkiWiki/Plugin/link.pm index 4c1add985..3838aec09 100644 --- a/IkiWiki/Plugin/link.pm +++ b/IkiWiki/Plugin/link.pm @@ -20,6 +20,7 @@ sub getsetup () { plugin => { safe => 1, rebuild => 1, + section => "core", }, } -- cgit v1.2.3 From dd3274ce734e3af0ca6a7940f69201d8d4b84fda Mon Sep 17 00:00:00 2001 From: Bernd Zeimetz Date: Mon, 14 Jun 2010 01:18:33 +0200 Subject: Enhance the link plugin to handle external links. The following ways to create a link are supported now: [[url]] [[text|url]] url can be one of the following: - an internal wikilink: will be handled as before - any other kind of URL, including mailto: proper links will be created: url text - an email address: url text --- IkiWiki/Plugin/link.pm | 89 +++++++++++++++++++++++++++++++++++++------------- doc/plugins/link.mdwn | 2 +- 2 files changed, 68 insertions(+), 23 deletions(-) (limited to 'IkiWiki/Plugin/link.pm') diff --git a/IkiWiki/Plugin/link.pm b/IkiWiki/Plugin/link.pm index 3838aec09..d41965bd3 100644 --- a/IkiWiki/Plugin/link.pm +++ b/IkiWiki/Plugin/link.pm @@ -7,6 +7,9 @@ use IkiWiki 3.00; my $link_regexp; +my $email_regexp = qr/^(?:[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/i; +my $url_regexp = qr/^(?:[^:]+:\/\/|mailto:).*/i; + sub import { hook(type => "getsetup", id => "link", call => \&getsetup); hook(type => "checkconfig", id => "link", call => \&checkconfig); @@ -57,8 +60,42 @@ sub checkconfig () { )? # optional \]\] # end of link - }x, + }x; + } +} + +sub is_externallink ($$) { + my $page = shift; + my $url = shift; + if ($url =~ /$email_regexp/) { + # url looks like an email address, so we assume it + # is supposed to be an external link if there is no + # page with that name. + $url =~ s/#.*//; + return (! (bestlink($page, linkpage($url)))) } + return ($url =~ /$url_regexp/) +} + +sub externallink ($;@) { + my $url = shift; + my $pagetitle = shift; + + # build pagetitle + if (!($pagetitle)) { + $pagetitle = $url; + # use only the email address as title for mailto: urls + if ($pagetitle =~ /^mailto:.*/) { + $pagetitle =~ s/^mailto:([^?]+).*/$1/; + } + } + + # handle email-addresses (without mailto:): + if ($url =~ /$email_regexp/) { + $url = "mailto:" . $url; + } + + return "$pagetitle"; } sub linkify (@) { @@ -70,12 +107,16 @@ sub linkify (@) { defined $2 ? ( $1 ? "[[$2|$3".($4 ? "#$4" : "")."]]" - : htmllink($page, $destpage, linkpage($3), - anchor => $4, linktext => pagetitle($2))) + : is_externallink($page, $3 . ($4 ? "#$4" : "")) + ? externallink("$3" . ($4 ? "#$4" : ""), $2) + : htmllink($page, $destpage, linkpage($3), + anchor => $4, linktext => pagetitle($2))) : ( $1 ? "[[$3".($4 ? "#$4" : "")."]]" - : htmllink($page, $destpage, linkpage($3), - anchor => $4)) + : is_externallink($page, $3 . ($4 ? "#$4" : "")) + ? externallink("$3" . ($4 ? "#$4" : "")) + : htmllink($page, $destpage, linkpage($3), + anchor => $4)) }eg; return $params{content}; @@ -87,7 +128,9 @@ sub scan (@) { my $content=$params{content}; while ($content =~ /(? Date: Wed, 23 Jun 2010 13:40:10 -0400 Subject: avoid needing full email regexp Fully validating the email address is not necessary, all that matters is not matching an url like http://foo@bar/ as an email address. --- IkiWiki/Plugin/link.pm | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'IkiWiki/Plugin/link.pm') diff --git a/IkiWiki/Plugin/link.pm b/IkiWiki/Plugin/link.pm index d41965bd3..7d4692ef0 100644 --- a/IkiWiki/Plugin/link.pm +++ b/IkiWiki/Plugin/link.pm @@ -7,7 +7,7 @@ use IkiWiki 3.00; my $link_regexp; -my $email_regexp = qr/^(?:[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/i; +my $email_regexp = qr/^.+@.+$/; my $url_regexp = qr/^(?:[^:]+:\/\/|mailto:).*/i; sub import { @@ -82,7 +82,7 @@ sub externallink ($;@) { my $pagetitle = shift; # build pagetitle - if (!($pagetitle)) { + if (! $pagetitle) { $pagetitle = $url; # use only the email address as title for mailto: urls if ($pagetitle =~ /^mailto:.*/) { @@ -90,8 +90,8 @@ sub externallink ($;@) { } } - # handle email-addresses (without mailto:): - if ($url =~ /$email_regexp/) { + if ($url !~ /$url_regexp/) { + # handle email addresses (without mailto:) $url = "mailto:" . $url; } -- cgit v1.2.3 From 6e67219eff9ea599e9efa8a846a4c78c76c5b008 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Wed, 23 Jun 2010 13:57:27 -0400 Subject: simplify anchor handling At least two bugfixes in here. First, an old bug; \[[foo#0]] was displayed as [[foo]], losing the anchor as the anchor text was false. Secondly, a new bug; an email like foo#bar@baz should not check bestlink("foo@baz"). --- IkiWiki/Plugin/link.pm | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) (limited to 'IkiWiki/Plugin/link.pm') diff --git a/IkiWiki/Plugin/link.pm b/IkiWiki/Plugin/link.pm index 7d4692ef0..87e06ca89 100644 --- a/IkiWiki/Plugin/link.pm +++ b/IkiWiki/Plugin/link.pm @@ -64,23 +64,33 @@ sub checkconfig () { } } -sub is_externallink ($$) { +sub is_externallink ($$;$) { my $page = shift; my $url = shift; + my $anchor = shift; + + if (defined $anchor) { + $url.="#".$anchor; + } + if ($url =~ /$email_regexp/) { # url looks like an email address, so we assume it # is supposed to be an external link if there is no # page with that name. - $url =~ s/#.*//; return (! (bestlink($page, linkpage($url)))) } return ($url =~ /$url_regexp/) } -sub externallink ($;@) { +sub externallink ($$;$) { my $url = shift; + my $anchor = shift; my $pagetitle = shift; + if (defined $anchor) { + $url.="#".$anchor; + } + # build pagetitle if (! $pagetitle) { $pagetitle = $url; @@ -106,15 +116,15 @@ sub linkify (@) { $params{content} =~ s{(\\?)$link_regexp}{ defined $2 ? ( $1 - ? "[[$2|$3".($4 ? "#$4" : "")."]]" - : is_externallink($page, $3 . ($4 ? "#$4" : "")) - ? externallink("$3" . ($4 ? "#$4" : ""), $2) + ? "[[$2|$3".(defined $4 ? "#$4" : "")."]]" + : is_externallink($page, $3, $4) + ? externallink($3, $4, $2) : htmllink($page, $destpage, linkpage($3), anchor => $4, linktext => pagetitle($2))) : ( $1 - ? "[[$3".($4 ? "#$4" : "")."]]" - : is_externallink($page, $3 . ($4 ? "#$4" : "")) - ? externallink("$3" . ($4 ? "#$4" : "")) + ? "[[$3".(defined $4 ? "#$4" : "")."]]" + : is_externallink($page, $3, $4) + ? externallink($3, $4) : htmllink($page, $destpage, linkpage($3), anchor => $4)) }eg; @@ -128,7 +138,7 @@ sub scan (@) { my $content=$params{content}; while ($content =~ /(? Date: Wed, 23 Jun 2010 14:05:57 -0400 Subject: bugfix: record email-like links as page links This way, an email-like link will be a mailto until a matching page is created, then it will link to the page. And removing the page will convert it back to a mailto. --- IkiWiki/Plugin/link.pm | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'IkiWiki/Plugin/link.pm') diff --git a/IkiWiki/Plugin/link.pm b/IkiWiki/Plugin/link.pm index 87e06ca89..f6c3573f7 100644 --- a/IkiWiki/Plugin/link.pm +++ b/IkiWiki/Plugin/link.pm @@ -64,21 +64,23 @@ sub checkconfig () { } } -sub is_externallink ($$;$) { +sub is_externallink ($$;$$) { my $page = shift; my $url = shift; my $anchor = shift; + my $force = shift; if (defined $anchor) { $url.="#".$anchor; } - if ($url =~ /$email_regexp/) { + if (! $force && $url =~ /$email_regexp/) { # url looks like an email address, so we assume it # is supposed to be an external link if there is no # page with that name. return (! (bestlink($page, linkpage($url)))) } + return ($url =~ /$url_regexp/) } @@ -138,7 +140,7 @@ sub scan (@) { my $content=$params{content}; while ($content =~ /(?