From b973ed82699903c23b3feeb5e73e1ebd6f587f43 Mon Sep 17 00:00:00 2001
From: joey <joey@0fa5a96a-9a0e-0410-b3b2-a0fd24251071>
Date: Sun, 15 Oct 2006 19:33:52 +0000
Subject: [PATCH] * Add no_override parameter to hook(). * Add a shortcut
 plugin, inspired by Victor Moral's contributed shortcuts   plugin, but
 featuring a more ikiwiki-ish syntax and with shortcuts that   can be
 configured using a page in wiki.

---
 IkiWiki.pm                                    |  2 +
 IkiWiki/Plugin/shortcut.pm                    | 58 +++++++++++++++++++
 basewiki/shortcuts.mdwn                       |  8 +++
 debian/changelog                              |  9 +++
 doc/plugins/contrib/googlemaps.mdwn           |  1 +
 doc/plugins/contrib/img.mdwn                  |  1 +
 doc/plugins/contrib/linguas.mdwn              |  1 +
 doc/plugins/contrib/shortcuts.mdwn            |  5 ++
 doc/plugins/contrib/shortcuts/discussion.mdwn |  3 +
 doc/plugins/shortcut.mdwn                     | 12 ++++
 doc/plugins/write.mdwn                        |  5 ++
 11 files changed, 105 insertions(+)
 create mode 100644 IkiWiki/Plugin/shortcut.pm
 create mode 100644 basewiki/shortcuts.mdwn
 create mode 100644 doc/plugins/shortcut.mdwn

diff --git a/IkiWiki.pm b/IkiWiki.pm
index a39e317f5..7084e9627 100644
--- a/IkiWiki.pm
+++ b/IkiWiki.pm
@@ -641,6 +641,8 @@ sub hook (@) { # {{{
 	if (! exists $param{type} || ! ref $param{call} || ! exists $param{id}) {
 		error "hook requires type, call, and id parameters";
 	}
+
+	return if $param{no_override} && exists $hooks{$param{type}}{$param{id}};
 	
 	$hooks{$param{type}}{$param{id}}=\%param;
 } # }}}
diff --git a/IkiWiki/Plugin/shortcut.pm b/IkiWiki/Plugin/shortcut.pm
new file mode 100644
index 000000000..d09d5879d
--- /dev/null
+++ b/IkiWiki/Plugin/shortcut.pm
@@ -0,0 +1,58 @@
+#!/usr/bin/perl
+package IkiWiki::Plugin::shortcut;
+
+use warnings;
+use strict;
+use IkiWiki;
+
+sub import { #{{{
+	hook(type => "checkconfig", id => "shortcut", call => \&checkconfig);
+	hook(type => "preprocess", id => "shortcut", call => \&preprocess_shortcut);
+} #}}}
+
+sub checkconfig () { #{{{
+	# Preprocess the shortcuts page to get all the available shortcuts
+	# defined before other pages are rendered.
+	IkiWiki::preprocess("shortcuts", "shortcuts",
+		readfile(srcfile("shortcuts.mdwn")));
+} # }}}
+
+sub preprocess_shortcut (@) { #{{{
+	my %params=@_;
+
+	if (! defined $params{name} || ! defined $params{url}) {
+		return "[[shortcut missing name or url parameter]]";
+	}
+
+	hook(type => "preprocess", no_override => 1, id => $params{name},
+		call => sub { shortcut_expand($params{name}, $params{url}, @_) });
+
+	return "shortcut $params{name} points to $params{url}";
+} # }}}
+
+sub shortcut_expand ($$@) { #{{{
+	my $name=shift;
+	my $url=shift;
+	my %params=@_;
+
+	# Get params in original order.
+	my @params;
+	while (@_) {
+		my $key=shift;
+		my $value=shift;
+		push @params, $key if ! length $value;
+	}
+
+	# If the shortcuts page changes, all pages that use shortcuts will
+	# need to be updated.
+	add_depends($params{destpage}, "shortcuts");
+
+	my $text=join(" ", @params);
+	my $encoded_text=$text;
+	$encoded_text=~s/([^A-Za-z0-9])/sprintf("%%%02X", ord($1))/seg;
+	
+	$url=~s/\%s/$encoded_text/g;
+	return "<a href=\"$url\">$text</a>";
+} #}}}
+
+1
diff --git a/basewiki/shortcuts.mdwn b/basewiki/shortcuts.mdwn
new file mode 100644
index 000000000..7dde7919e
--- /dev/null
+++ b/basewiki/shortcuts.mdwn
@@ -0,0 +1,8 @@
+This page is used to control what shortcut links are supported by the wiki.
+
+* [[shortcut name=google url="http://www.google.com/search?q=%s"]]
+* [[shortcut name=wikipedia url="http://en.wikipedia.org/wiki/%s"]]
+* [[shortcut name=debbug url="http://bugs.debian.org/%s"]]
+
+To add a new shortcut, use the "shortcut" preprocessor directive. "%s" in
+the url is replaced by the text of the shortcut.
diff --git a/debian/changelog b/debian/changelog
index 01a63aae1..3aeeccb11 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,12 @@
+ikiwiki (1.30) UNRELEASED; urgency=low
+
+  * Add no_override parameter to hook().
+  * Add a shortcut plugin, inspired by Victor Moral's contributed shortcuts 
+    plugin, but featuring a more ikiwiki-ish syntax and with shortcuts that
+    can be configured using a page in wiki.
+
+ -- Joey Hess <joeyh@debian.org>  Sun, 15 Oct 2006 14:49:28 -0400
+
 ikiwiki (1.29) unstable; urgency=low
 
   * Patch from Paul Tötterman to use CP in the Makefile.
diff --git a/doc/plugins/contrib/googlemaps.mdwn b/doc/plugins/contrib/googlemaps.mdwn
index 2ea4e4fa4..56d2d6031 100644
--- a/doc/plugins/contrib/googlemaps.mdwn
+++ b/doc/plugins/contrib/googlemaps.mdwn
@@ -1,5 +1,6 @@
 [[template id=plugin name=googlemaps author="Christian Mock"]]
 [[tag special-purpose]]
+[[meta title"googlemaps (third-party plugin)"]]
 
 `googlemaps` is a plugin that allows using the [Google Maps API][2]
 from ikiwiki.
diff --git a/doc/plugins/contrib/img.mdwn b/doc/plugins/contrib/img.mdwn
index f2133a38b..72b8579e0 100644
--- a/doc/plugins/contrib/img.mdwn
+++ b/doc/plugins/contrib/img.mdwn
@@ -1,5 +1,6 @@
 [[template id=plugin name=img author="Christian Mock"]]
 [[tag chrome]]
+[[meta title"img (third-party plugin)"]]
 
 `img` is an enhanced image handling plugin.
 
diff --git a/doc/plugins/contrib/linguas.mdwn b/doc/plugins/contrib/linguas.mdwn
index cf7a458c7..c90e11a54 100644
--- a/doc/plugins/contrib/linguas.mdwn
+++ b/doc/plugins/contrib/linguas.mdwn
@@ -1,4 +1,5 @@
 [[template id=plugin name=linguas author="Jordà Polo"]]
+[[meta title"linguas (third-party plugin)"]]
 
 Linguas
 =======
diff --git a/doc/plugins/contrib/shortcuts.mdwn b/doc/plugins/contrib/shortcuts.mdwn
index eb13bb106..81c2cf8dc 100644
--- a/doc/plugins/contrib/shortcuts.mdwn
+++ b/doc/plugins/contrib/shortcuts.mdwn
@@ -1,5 +1,10 @@
 [[template id=plugin name=shortcuts included=0 author="[[VictorMoral]]"]]
 [[tag type/format]]
+[[meta title"shortcuts (third-party plugin)"]]
+
+Note: This plugin is different than the "shortcut" plugin now included in
+ikiwiki. I'm leaving it here in case people prefer how this one works.
+--[[Joey]]
 
 *shortcuts* is a plugin for make external links easy in a ikiwiki page.
 It filter the raw source code, searching *wafl expresions* and replacing it
diff --git a/doc/plugins/contrib/shortcuts/discussion.mdwn b/doc/plugins/contrib/shortcuts/discussion.mdwn
index 367d1b9a3..70f962f1d 100644
--- a/doc/plugins/contrib/shortcuts/discussion.mdwn
+++ b/doc/plugins/contrib/shortcuts/discussion.mdwn
@@ -22,3 +22,6 @@ standard across wikis, this approach seems cleaner and easier to learn for
 ikiwiki users.
 
 --[[Joey]]
+
+Update: I've added a "shortcut" plugin that works as described above.
+--[[Joey]]
diff --git a/doc/plugins/shortcut.mdwn b/doc/plugins/shortcut.mdwn
new file mode 100644
index 000000000..33180b120
--- /dev/null
+++ b/doc/plugins/shortcut.mdwn
@@ -0,0 +1,12 @@
+[[template id=plugin name=shortcut included=1 author="[[Joey]]"]]
+[[tag type/format]]
+
+This plugin allows external links to commonly linked to sites to be made
+more easily using shortcuts. Some examples of using shortcuts include:
+
+	\[[google foo]]
+	\[[wikipedia War of 1812]]
+	\[[debbug 12345]]
+
+The available shortcuts are defined on the [[shortcuts]] page in
+the wiki.
diff --git a/doc/plugins/write.mdwn b/doc/plugins/write.mdwn
index 18aa34dca..1beafa395 100644
--- a/doc/plugins/write.mdwn
+++ b/doc/plugins/write.mdwn
@@ -210,6 +210,11 @@ ikiwiki program.
 
 Hook into ikiwiki's processing. See the discussion of hooks above.
 
+Note that in addition to the named parameters described above, a parameter
+named no_override is supported, If it's set to a true value, then this hook
+will not override any existing hook with the same id. This is useful if
+the id can be controled by the user.
+
 #### `debug($)`
 
 Logs a debugging message. These are supressed unless verbose mode is turned
-- 
2.39.5