From 8322b8c9c82941c4052bcc8a0d1824a92d6426a0 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Sat, 31 Jan 2009 18:07:42 +0000 Subject: CGI: add cgi_page_from_404(), which remaps a path like $REDIRECT_URL to an IkiWiki page name Also add a regression test --- t/cgi_page_from_404.t | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100755 t/cgi_page_from_404.t (limited to 't') diff --git a/t/cgi_page_from_404.t b/t/cgi_page_from_404.t new file mode 100755 index 000000000..adbbdf874 --- /dev/null +++ b/t/cgi_page_from_404.t @@ -0,0 +1,43 @@ +#!/usr/bin/perl +use warnings; +use strict; +use Test::More tests => 18; + +BEGIN { use_ok("IkiWiki"); } +BEGIN { use_ok("IkiWiki::CGI"); } + +sub cgi_page_from_404 { return IkiWiki::cgi_page_from_404(shift, shift, shift); } + +$IkiWiki::config{htmlext} = 'html'; + +is(cgi_page_from_404('/', 'http://example.com', 1), 'index'); +is(cgi_page_from_404('/index.html', 'http://example.com', 0), 'index'); +is(cgi_page_from_404('/', 'http://example.com/', 1), 'index'); +is(cgi_page_from_404('/index.html', 'http://example.com/', 0), 'index'); + +is(cgi_page_from_404('/~user/foo/bar', 'http://example.com/~user', 1), + 'foo/bar'); +is(cgi_page_from_404('/~user/foo/bar/index.html', 'http://example.com/~user', 1), + 'foo/bar'); +is(cgi_page_from_404('/~user/foo/bar/', 'http://example.com/~user', 1), + 'foo/bar'); +is(cgi_page_from_404('/~user/foo/bar.html', 'http://example.com/~user', 0), + 'foo/bar'); + +is(cgi_page_from_404('/~user/foo/bar', 'http://example.com/~user/', 1), + 'foo/bar'); +is(cgi_page_from_404('/~user/foo/bar/index.html', 'http://example.com/~user/', 1), + 'foo/bar'); +is(cgi_page_from_404('/~user/foo/bar/', 'http://example.com/~user/', 1), + 'foo/bar'); +is(cgi_page_from_404('/~user/foo/bar.html', 'http://example.com/~user/', 0), + 'foo/bar'); + +is(cgi_page_from_404('/~user/foo', 'https://example.com/~user', 1), + 'foo'); +is(cgi_page_from_404('/~user/foo/index.html', 'https://example.com/~user', 1), + 'foo'); +is(cgi_page_from_404('/~user/foo/', 'https://example.com/~user', 1), + 'foo'); +is(cgi_page_from_404('/~user/foo.html', 'https://example.com/~user', 0), + 'foo'); -- cgit v1.2.3 From 46b880f8390ac82d746add01de38a05155743374 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Sat, 31 Jan 2009 22:32:10 +0000 Subject: Split apache404 into an independent plugin Also make it ignore the 'do' parameter at Joey's suggestion, to have one less thing to remember when configuring. --- IkiWiki/CGI.pm | 49 +------------------------- IkiWiki/Plugin/apache404.pm | 76 ++++++++++++++++++++++++++++++++++++++++ doc/plugins/apache404.mdwn | 11 ++++++ doc/tips/apache_404_handler.mdwn | 10 ------ t/apache404.t | 45 ++++++++++++++++++++++++ t/cgi_page_from_404.t | 43 ----------------------- 6 files changed, 133 insertions(+), 101 deletions(-) create mode 100644 IkiWiki/Plugin/apache404.pm create mode 100644 doc/plugins/apache404.mdwn delete mode 100644 doc/tips/apache_404_handler.mdwn create mode 100755 t/apache404.t delete mode 100755 t/cgi_page_from_404.t (limited to 't') diff --git a/IkiWiki/CGI.pm b/IkiWiki/CGI.pm index 8734cdd49..e75ebcd27 100644 --- a/IkiWiki/CGI.pm +++ b/IkiWiki/CGI.pm @@ -338,46 +338,6 @@ sub cgi_goto ($;$) { exit; } -sub cgi_page_from_404 ($$$) { - my $path = shift; - my $baseurl = shift; - my $usedirs = shift; - - # fail if missing from environment or whatever - return undef unless defined $path; - return undef unless defined $baseurl; - - # with usedirs on, path is like /~fred/foo/bar/ or /~fred/foo/bar or - # /~fred/foo/bar/index.html - # with usedirs off, path is like /~fred/foo/bar.html - # baseurl is like 'http://people.example.com/~fred' - - # convert baseurl to ~fred - unless ($baseurl =~ s{^https?://[^/]+/?}{}) { - return undef; - } - - # convert path to /~fred/foo/bar - if ($usedirs) { - $path =~ s/\/*(?:index\.$config{htmlext})?$//; - } - else { - $path =~ s/\.$config{htmlext}$//; - } - - # remove /~fred/ - unless ($path =~ s{^/*\Q$baseurl\E/*}{}) { - return undef; - } - - # special case for the index - unless ($path) { - return 'index'; - } - - return $path; -} - sub cgi (;$$) { my $q=shift; my $session=shift; @@ -409,14 +369,7 @@ sub cgi (;$$) { # commenter are for compatibility with any saved URLs if ($do eq 'goto' || $do eq 'recentchanges_link' || $do eq 'commenter') { - my $page = undef; - - if ($ENV{REDIRECT_STATUS} eq '404') { - $page = cgi_page_from_404($ENV{REDIRECT_URL}, - $config{url}, $config{usedirs}); - } - - cgi_goto($q, $page); + cgi_goto($q); } # Need to lock the wiki before getting a session. diff --git a/IkiWiki/Plugin/apache404.pm b/IkiWiki/Plugin/apache404.pm new file mode 100644 index 000000000..3ac6b3af5 --- /dev/null +++ b/IkiWiki/Plugin/apache404.pm @@ -0,0 +1,76 @@ +#!/usr/bin/perl +# Copyright © 2009 Simon McVittie +# Licensed under the GNU GPL, version 2, or any later version published by the +# Free Software Foundation +package IkiWiki::Plugin::apache404; + +use warnings; +use strict; +use IkiWiki 3.00; + +sub import { + hook(type => "cgi", id => 'apache404', call => \&cgi); +} + +sub getsetup () { + return + plugin => { + # not really a matter of safety, but enabling/disabling + # through a web interface is useless - it needs web + # server admin action too + safe => 0, + rebuild => 0, + } +} + +sub cgi_page_from_404 ($$$) { + my $path = shift; + my $baseurl = shift; + my $usedirs = shift; + + # fail if missing from environment or whatever + return undef unless defined $path; + return undef unless defined $baseurl; + + # with usedirs on, path is like /~fred/foo/bar/ or /~fred/foo/bar or + # /~fred/foo/bar/index.html + # with usedirs off, path is like /~fred/foo/bar.html + # baseurl is like 'http://people.example.com/~fred' + + # convert baseurl to ~fred + unless ($baseurl =~ s{^https?://[^/]+/?}{}) { + return undef; + } + + # convert path to /~fred/foo/bar + if ($usedirs) { + $path =~ s/\/*(?:index\.$config{htmlext})?$//; + } + else { + $path =~ s/\.$config{htmlext}$//; + } + + # remove /~fred/ + unless ($path =~ s{^/*\Q$baseurl\E/*}{}) { + return undef; + } + + # special case for the index + unless ($path) { + return 'index'; + } + + return $path; +} + +sub cgi ($) { + my $cgi=shift; + + if ($ENV{REDIRECT_STATUS} eq '404') { + my $page = cgi_page_from_404($ENV{REDIRECT_URL}, + $config{url}, $config{usedirs}); + IkiWiki::cgi_goto($cgi, $page); + } +} + +1; diff --git a/doc/plugins/apache404.mdwn b/doc/plugins/apache404.mdwn new file mode 100644 index 000000000..bab8fb59d --- /dev/null +++ b/doc/plugins/apache404.mdwn @@ -0,0 +1,11 @@ +[[!template id=plugin name=apache404 author="[[Simon_McVittie|smcv]]"]] +[[!tag type/useful]] + +This plugin lets you use the IkiWiki CGI script as an Apache 404 handler, +to give the behaviour of various other wiki engines where visiting a +nonexistent page provides you with a link to create it. + +To achieve this, put something like this in the wiki's Apache configuration +file: + + ErrorDocument 404 /cgi-bin/ikiwiki.cgi diff --git a/doc/tips/apache_404_handler.mdwn b/doc/tips/apache_404_handler.mdwn deleted file mode 100644 index 0fda759e7..000000000 --- a/doc/tips/apache_404_handler.mdwn +++ /dev/null @@ -1,10 +0,0 @@ -[[!meta title="Apache 404 handler"]] - -Sufficiently recent versions of IkiWiki can be used as an Apache 404 handler, -to give the behaviour of various other wiki engines where visiting a -nonexistent page provides you with a link to create it. - -To achieve this, put something like this in the wiki's Apache configuration -file: - - ErrorDocument 404 /cgi-bin/ikiwiki.cgi?do=goto diff --git a/t/apache404.t b/t/apache404.t new file mode 100755 index 000000000..00fc35250 --- /dev/null +++ b/t/apache404.t @@ -0,0 +1,45 @@ +#!/usr/bin/perl +use warnings; +use strict; +use Test::More tests => 17; + +BEGIN { use_ok("IkiWiki::Plugin::apache404"); } + +sub cgi_page_from_404 { + return IkiWiki::Plugin::apache404::cgi_page_from_404(shift, shift, + shift); +} + +$IkiWiki::config{htmlext} = 'html'; + +is(cgi_page_from_404('/', 'http://example.com', 1), 'index'); +is(cgi_page_from_404('/index.html', 'http://example.com', 0), 'index'); +is(cgi_page_from_404('/', 'http://example.com/', 1), 'index'); +is(cgi_page_from_404('/index.html', 'http://example.com/', 0), 'index'); + +is(cgi_page_from_404('/~user/foo/bar', 'http://example.com/~user', 1), + 'foo/bar'); +is(cgi_page_from_404('/~user/foo/bar/index.html', 'http://example.com/~user', 1), + 'foo/bar'); +is(cgi_page_from_404('/~user/foo/bar/', 'http://example.com/~user', 1), + 'foo/bar'); +is(cgi_page_from_404('/~user/foo/bar.html', 'http://example.com/~user', 0), + 'foo/bar'); + +is(cgi_page_from_404('/~user/foo/bar', 'http://example.com/~user/', 1), + 'foo/bar'); +is(cgi_page_from_404('/~user/foo/bar/index.html', 'http://example.com/~user/', 1), + 'foo/bar'); +is(cgi_page_from_404('/~user/foo/bar/', 'http://example.com/~user/', 1), + 'foo/bar'); +is(cgi_page_from_404('/~user/foo/bar.html', 'http://example.com/~user/', 0), + 'foo/bar'); + +is(cgi_page_from_404('/~user/foo', 'https://example.com/~user', 1), + 'foo'); +is(cgi_page_from_404('/~user/foo/index.html', 'https://example.com/~user', 1), + 'foo'); +is(cgi_page_from_404('/~user/foo/', 'https://example.com/~user', 1), + 'foo'); +is(cgi_page_from_404('/~user/foo.html', 'https://example.com/~user', 0), + 'foo'); diff --git a/t/cgi_page_from_404.t b/t/cgi_page_from_404.t deleted file mode 100755 index adbbdf874..000000000 --- a/t/cgi_page_from_404.t +++ /dev/null @@ -1,43 +0,0 @@ -#!/usr/bin/perl -use warnings; -use strict; -use Test::More tests => 18; - -BEGIN { use_ok("IkiWiki"); } -BEGIN { use_ok("IkiWiki::CGI"); } - -sub cgi_page_from_404 { return IkiWiki::cgi_page_from_404(shift, shift, shift); } - -$IkiWiki::config{htmlext} = 'html'; - -is(cgi_page_from_404('/', 'http://example.com', 1), 'index'); -is(cgi_page_from_404('/index.html', 'http://example.com', 0), 'index'); -is(cgi_page_from_404('/', 'http://example.com/', 1), 'index'); -is(cgi_page_from_404('/index.html', 'http://example.com/', 0), 'index'); - -is(cgi_page_from_404('/~user/foo/bar', 'http://example.com/~user', 1), - 'foo/bar'); -is(cgi_page_from_404('/~user/foo/bar/index.html', 'http://example.com/~user', 1), - 'foo/bar'); -is(cgi_page_from_404('/~user/foo/bar/', 'http://example.com/~user', 1), - 'foo/bar'); -is(cgi_page_from_404('/~user/foo/bar.html', 'http://example.com/~user', 0), - 'foo/bar'); - -is(cgi_page_from_404('/~user/foo/bar', 'http://example.com/~user/', 1), - 'foo/bar'); -is(cgi_page_from_404('/~user/foo/bar/index.html', 'http://example.com/~user/', 1), - 'foo/bar'); -is(cgi_page_from_404('/~user/foo/bar/', 'http://example.com/~user/', 1), - 'foo/bar'); -is(cgi_page_from_404('/~user/foo/bar.html', 'http://example.com/~user/', 0), - 'foo/bar'); - -is(cgi_page_from_404('/~user/foo', 'https://example.com/~user', 1), - 'foo'); -is(cgi_page_from_404('/~user/foo/index.html', 'https://example.com/~user', 1), - 'foo'); -is(cgi_page_from_404('/~user/foo/', 'https://example.com/~user', 1), - 'foo'); -is(cgi_page_from_404('/~user/foo.html', 'https://example.com/~user', 0), - 'foo'); -- cgit v1.2.3 From 3b83e520182a83e4ae6c61ab7b360b0eb939469f Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Sat, 31 Jan 2009 19:26:36 -0500 Subject: rename apache404 -> 404 This may already work with other web servers that have copied apache's interface, and it should be easy to add support to it for web servers that use some other interface. So, make the name more general. --- IkiWiki/Plugin/404.pm | 77 +++++++++++++++++++++++++++++++++++++++++++++ IkiWiki/Plugin/apache404.pm | 77 --------------------------------------------- debian/changelog | 2 +- debian/copyright | 2 +- doc/plugins/404.mdwn | 11 +++++++ doc/plugins/apache404.mdwn | 11 ------- doc/plugins/goto.mdwn | 2 +- doc/tips/dot_cgi.mdwn | 9 +++--- t/404.t | 44 ++++++++++++++++++++++++++ t/apache404.t | 45 -------------------------- 10 files changed, 139 insertions(+), 141 deletions(-) create mode 100644 IkiWiki/Plugin/404.pm delete mode 100644 IkiWiki/Plugin/apache404.pm create mode 100644 doc/plugins/404.mdwn delete mode 100644 doc/plugins/apache404.mdwn create mode 100755 t/404.t delete mode 100755 t/apache404.t (limited to 't') diff --git a/IkiWiki/Plugin/404.pm b/IkiWiki/Plugin/404.pm new file mode 100644 index 000000000..5550ea7d1 --- /dev/null +++ b/IkiWiki/Plugin/404.pm @@ -0,0 +1,77 @@ +#!/usr/bin/perl +# Copyright © 2009 Simon McVittie +# Licensed under the GNU GPL, version 2, or any later version published by the +# Free Software Foundation +package IkiWiki::Plugin::404; + +use warnings; +use strict; +use IkiWiki 3.00; + +sub import { + hook(type => "cgi", id => '404', call => \&cgi); + IkiWiki::loadplugin("goto"); +} + +sub getsetup () { + return + plugin => { + # not really a matter of safety, but enabling/disabling + # through a web interface is useless - it needs web + # server admin action too + safe => 0, + rebuild => 0, + } +} + +sub cgi_page_from_404 ($$$) { + my $path = shift; + my $baseurl = shift; + my $usedirs = shift; + + # fail if missing from environment or whatever + return undef unless defined $path; + return undef unless defined $baseurl; + + # with usedirs on, path is like /~fred/foo/bar/ or /~fred/foo/bar or + # /~fred/foo/bar/index.html + # with usedirs off, path is like /~fred/foo/bar.html + # baseurl is like 'http://people.example.com/~fred' + + # convert baseurl to ~fred + unless ($baseurl =~ s{^https?://[^/]+/?}{}) { + return undef; + } + + # convert path to /~fred/foo/bar + if ($usedirs) { + $path =~ s/\/*(?:index\.$config{htmlext})?$//; + } + else { + $path =~ s/\.$config{htmlext}$//; + } + + # remove /~fred/ + unless ($path =~ s{^/*\Q$baseurl\E/*}{}) { + return undef; + } + + # special case for the index + unless ($path) { + return 'index'; + } + + return $path; +} + +sub cgi ($) { + my $cgi=shift; + + if ($ENV{REDIRECT_STATUS} eq '404') { + my $page = cgi_page_from_404($ENV{REDIRECT_URL}, + $config{url}, $config{usedirs}); + IkiWiki::Plugin::goto::cgi_goto($cgi, $page); + } +} + +1; diff --git a/IkiWiki/Plugin/apache404.pm b/IkiWiki/Plugin/apache404.pm deleted file mode 100644 index e7ce70435..000000000 --- a/IkiWiki/Plugin/apache404.pm +++ /dev/null @@ -1,77 +0,0 @@ -#!/usr/bin/perl -# Copyright © 2009 Simon McVittie -# Licensed under the GNU GPL, version 2, or any later version published by the -# Free Software Foundation -package IkiWiki::Plugin::apache404; - -use warnings; -use strict; -use IkiWiki 3.00; - -sub import { - hook(type => "cgi", id => 'apache404', call => \&cgi); - IkiWiki::loadplugin("goto"); -} - -sub getsetup () { - return - plugin => { - # not really a matter of safety, but enabling/disabling - # through a web interface is useless - it needs web - # server admin action too - safe => 0, - rebuild => 0, - } -} - -sub cgi_page_from_404 ($$$) { - my $path = shift; - my $baseurl = shift; - my $usedirs = shift; - - # fail if missing from environment or whatever - return undef unless defined $path; - return undef unless defined $baseurl; - - # with usedirs on, path is like /~fred/foo/bar/ or /~fred/foo/bar or - # /~fred/foo/bar/index.html - # with usedirs off, path is like /~fred/foo/bar.html - # baseurl is like 'http://people.example.com/~fred' - - # convert baseurl to ~fred - unless ($baseurl =~ s{^https?://[^/]+/?}{}) { - return undef; - } - - # convert path to /~fred/foo/bar - if ($usedirs) { - $path =~ s/\/*(?:index\.$config{htmlext})?$//; - } - else { - $path =~ s/\.$config{htmlext}$//; - } - - # remove /~fred/ - unless ($path =~ s{^/*\Q$baseurl\E/*}{}) { - return undef; - } - - # special case for the index - unless ($path) { - return 'index'; - } - - return $path; -} - -sub cgi ($) { - my $cgi=shift; - - if ($ENV{REDIRECT_STATUS} eq '404') { - my $page = cgi_page_from_404($ENV{REDIRECT_URL}, - $config{url}, $config{usedirs}); - IkiWiki::Plugin::goto::cgi_goto($cgi, $page); - } -} - -1; diff --git a/debian/changelog b/debian/changelog index f3927f121..be32d3abf 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,6 +1,6 @@ ikiwiki (3.04) UNRELEASED; urgency=low - * apache404: New plugin which lets you use the IkiWiki CGI script as + * 404: New plugin which lets you use the IkiWiki CGI script as an Apache 404 handler, to give the behaviour of various other wiki engines where visiting a nonexistent page provides you with a link to create it. (smcv) diff --git a/debian/copyright b/debian/copyright index bdfbaa573..f589b4a8f 100644 --- a/debian/copyright +++ b/debian/copyright @@ -118,7 +118,7 @@ Copyright: © 2008 Simon McVittie License: GPL-2+ -Files: apache404.pm +Files: 404.pm Copyright: © 2009 Simon McVittie License: GPL-2+ diff --git a/doc/plugins/404.mdwn b/doc/plugins/404.mdwn new file mode 100644 index 000000000..8d36279c8 --- /dev/null +++ b/doc/plugins/404.mdwn @@ -0,0 +1,11 @@ +[[!template id=plugin name=404 author="[[Simon_McVittie|smcv]]"]] +[[!tag type/useful]] + +This plugin lets you use the IkiWiki CGI script as an Apache 404 handler, +to give the behaviour of various other wiki engines where visiting a +nonexistent page provides you with a link to create it. + +To achieve this, put something like this in the wiki's Apache configuration +file: + + ErrorDocument 404 /cgi-bin/ikiwiki.cgi diff --git a/doc/plugins/apache404.mdwn b/doc/plugins/apache404.mdwn deleted file mode 100644 index bab8fb59d..000000000 --- a/doc/plugins/apache404.mdwn +++ /dev/null @@ -1,11 +0,0 @@ -[[!template id=plugin name=apache404 author="[[Simon_McVittie|smcv]]"]] -[[!tag type/useful]] - -This plugin lets you use the IkiWiki CGI script as an Apache 404 handler, -to give the behaviour of various other wiki engines where visiting a -nonexistent page provides you with a link to create it. - -To achieve this, put something like this in the wiki's Apache configuration -file: - - ErrorDocument 404 /cgi-bin/ikiwiki.cgi diff --git a/doc/plugins/goto.mdwn b/doc/plugins/goto.mdwn index 21dda16b2..9c401c5d2 100644 --- a/doc/plugins/goto.mdwn +++ b/doc/plugins/goto.mdwn @@ -2,7 +2,7 @@ [[!tag type/useful]] This plugin adds a `do=goto` mode for the IkiWiki CGI script. It's mainly -for internal use by the [[apache404]], [[comments]] and [[recentchanges]] +for internal use by the [[404]], [[comments]] and [[recentchanges]] plugins, which enable it automatically. With this plugin enabled you can link to `ikiwiki.cgi?do=goto&page=some/where` diff --git a/doc/tips/dot_cgi.mdwn b/doc/tips/dot_cgi.mdwn index ffcbf95d4..04d7a9721 100644 --- a/doc/tips/dot_cgi.mdwn +++ b/doc/tips/dot_cgi.mdwn @@ -26,11 +26,10 @@ configuration changes should work anywhere. Or, if you've put it in a `~/public_html`, edit `/etc/apache2/mods-available/userdir.conf`. -You may also want to enable the [[plugins/apache404]] -plugin. To make apache use it, the apache config file -will need a further modification to make it use ikiwiki's CGI -as the apache 404 handler. Something like this, with -the path adjusted to where you've put the CGI: +* You may also want to enable the [[plugins/404]] plugin. + To make apache use it, the apache config file will need a further + modification to make it use ikiwiki's CGI as the apache 404 handler. + Something like this, with the path adjusted to where you've put the CGI: ErrorDocument 404 /cgi-bin/ikiwiki.cgi diff --git a/t/404.t b/t/404.t new file mode 100755 index 000000000..0bb3c6063 --- /dev/null +++ b/t/404.t @@ -0,0 +1,44 @@ +#!/usr/bin/perl +use warnings; +use strict; +use Test::More tests => 17; + +BEGIN { use_ok("IkiWiki::Plugin::404"); } + +sub cgi_page_from_404 { + return IkiWiki::Plugin::404::cgi_page_from_404(shift, shift, shift); +} + +$IkiWiki::config{htmlext} = 'html'; + +is(cgi_page_from_404('/', 'http://example.com', 1), 'index'); +is(cgi_page_from_404('/index.html', 'http://example.com', 0), 'index'); +is(cgi_page_from_404('/', 'http://example.com/', 1), 'index'); +is(cgi_page_from_404('/index.html', 'http://example.com/', 0), 'index'); + +is(cgi_page_from_404('/~user/foo/bar', 'http://example.com/~user', 1), + 'foo/bar'); +is(cgi_page_from_404('/~user/foo/bar/index.html', 'http://example.com/~user', 1), + 'foo/bar'); +is(cgi_page_from_404('/~user/foo/bar/', 'http://example.com/~user', 1), + 'foo/bar'); +is(cgi_page_from_404('/~user/foo/bar.html', 'http://example.com/~user', 0), + 'foo/bar'); + +is(cgi_page_from_404('/~user/foo/bar', 'http://example.com/~user/', 1), + 'foo/bar'); +is(cgi_page_from_404('/~user/foo/bar/index.html', 'http://example.com/~user/', 1), + 'foo/bar'); +is(cgi_page_from_404('/~user/foo/bar/', 'http://example.com/~user/', 1), + 'foo/bar'); +is(cgi_page_from_404('/~user/foo/bar.html', 'http://example.com/~user/', 0), + 'foo/bar'); + +is(cgi_page_from_404('/~user/foo', 'https://example.com/~user', 1), + 'foo'); +is(cgi_page_from_404('/~user/foo/index.html', 'https://example.com/~user', 1), + 'foo'); +is(cgi_page_from_404('/~user/foo/', 'https://example.com/~user', 1), + 'foo'); +is(cgi_page_from_404('/~user/foo.html', 'https://example.com/~user', 0), + 'foo'); diff --git a/t/apache404.t b/t/apache404.t deleted file mode 100755 index 00fc35250..000000000 --- a/t/apache404.t +++ /dev/null @@ -1,45 +0,0 @@ -#!/usr/bin/perl -use warnings; -use strict; -use Test::More tests => 17; - -BEGIN { use_ok("IkiWiki::Plugin::apache404"); } - -sub cgi_page_from_404 { - return IkiWiki::Plugin::apache404::cgi_page_from_404(shift, shift, - shift); -} - -$IkiWiki::config{htmlext} = 'html'; - -is(cgi_page_from_404('/', 'http://example.com', 1), 'index'); -is(cgi_page_from_404('/index.html', 'http://example.com', 0), 'index'); -is(cgi_page_from_404('/', 'http://example.com/', 1), 'index'); -is(cgi_page_from_404('/index.html', 'http://example.com/', 0), 'index'); - -is(cgi_page_from_404('/~user/foo/bar', 'http://example.com/~user', 1), - 'foo/bar'); -is(cgi_page_from_404('/~user/foo/bar/index.html', 'http://example.com/~user', 1), - 'foo/bar'); -is(cgi_page_from_404('/~user/foo/bar/', 'http://example.com/~user', 1), - 'foo/bar'); -is(cgi_page_from_404('/~user/foo/bar.html', 'http://example.com/~user', 0), - 'foo/bar'); - -is(cgi_page_from_404('/~user/foo/bar', 'http://example.com/~user/', 1), - 'foo/bar'); -is(cgi_page_from_404('/~user/foo/bar/index.html', 'http://example.com/~user/', 1), - 'foo/bar'); -is(cgi_page_from_404('/~user/foo/bar/', 'http://example.com/~user/', 1), - 'foo/bar'); -is(cgi_page_from_404('/~user/foo/bar.html', 'http://example.com/~user/', 0), - 'foo/bar'); - -is(cgi_page_from_404('/~user/foo', 'https://example.com/~user', 1), - 'foo'); -is(cgi_page_from_404('/~user/foo/index.html', 'https://example.com/~user', 1), - 'foo'); -is(cgi_page_from_404('/~user/foo/', 'https://example.com/~user', 1), - 'foo'); -is(cgi_page_from_404('/~user/foo.html', 'https://example.com/~user', 0), - 'foo'); -- cgit v1.2.3 From 8c8b18935b84bcfbf06649c9683e236e39f75df3 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 19 Feb 2009 18:22:51 -0500 Subject: fix pagetype test File had wrong name, and made wrong assumption about what pagetype does for bare files. --- t/pagetype.mdwn | 14 -------------- t/pagetype.t | 17 +++++++++++++++++ 2 files changed, 17 insertions(+), 14 deletions(-) delete mode 100755 t/pagetype.mdwn create mode 100755 t/pagetype.t (limited to 't') diff --git a/t/pagetype.mdwn b/t/pagetype.mdwn deleted file mode 100755 index 76cacd8f7..000000000 --- a/t/pagetype.mdwn +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/perl -use warnings; -use strict; -use Test::More tests => 5; - -BEGIN { use_ok("IkiWiki"); } - -# Used internally. -$IkiWiki::hooks{htmlize}{mdwn}=1; - -is(pagetype("foo.mdwn"), "mdwn"); -is(pagetype("foo/bar.mdwn"), "mdwn"); -is(pagename("foo.png"), undef); -is(pagename("foo"), undef); diff --git a/t/pagetype.t b/t/pagetype.t new file mode 100755 index 000000000..2df59387a --- /dev/null +++ b/t/pagetype.t @@ -0,0 +1,17 @@ +#!/usr/bin/perl +use warnings; +use strict; +use Test::More tests => 6; + +BEGIN { use_ok("IkiWiki"); } + +# Used internally. +$IkiWiki::hooks{htmlize}{mdwn}=1; + +is(pagetype("foo.mdwn"), "mdwn"); +is(pagetype("foo/bar.mdwn"), "mdwn"); + +# bare files get the full filename as page name +is(pagename("foo.png"), "foo.png"); +is(pagename("foo/bar.png"), "foo/bar.png"); +is(pagename("foo"), "foo"); -- cgit v1.2.3 From c1907ded879f75ef07c5bbab26b84042bafc1b17 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 19 Feb 2009 18:28:43 -0500 Subject: fix pagename, pagetype tests Put tests in right file. Set internal variable to hash, the functions expect that. --- t/pagename.t | 5 ++++- t/pagetype.t | 10 ++++------ 2 files changed, 8 insertions(+), 7 deletions(-) (limited to 't') diff --git a/t/pagename.t b/t/pagename.t index c7f1ce180..43f574e98 100755 --- a/t/pagename.t +++ b/t/pagename.t @@ -1,7 +1,7 @@ #!/usr/bin/perl use warnings; use strict; -use Test::More tests => 5; +use Test::More tests => 6; BEGIN { use_ok("IkiWiki"); } @@ -10,5 +10,8 @@ $IkiWiki::hooks{htmlize}{mdwn}{call}=sub {}; is(pagename("foo.mdwn"), "foo"); is(pagename("foo/bar.mdwn"), "foo/bar"); + +# bare files get the full filename as page name is(pagename("foo.png"), "foo.png"); +is(pagename("foo/bar.png"), "foo/bar.png"); is(pagename("foo"), "foo"); diff --git a/t/pagetype.t b/t/pagetype.t index 2df59387a..bb06a1568 100755 --- a/t/pagetype.t +++ b/t/pagetype.t @@ -6,12 +6,10 @@ use Test::More tests => 6; BEGIN { use_ok("IkiWiki"); } # Used internally. -$IkiWiki::hooks{htmlize}{mdwn}=1; +$IkiWiki::hooks{htmlize}{mdwn}={}; is(pagetype("foo.mdwn"), "mdwn"); is(pagetype("foo/bar.mdwn"), "mdwn"); - -# bare files get the full filename as page name -is(pagename("foo.png"), "foo.png"); -is(pagename("foo/bar.png"), "foo/bar.png"); -is(pagename("foo"), "foo"); +is(pagetype("foo.png"), undef); +is(pagetype("foo/bar.png"), undef); +is(pagetype("foo"), undef); -- cgit v1.2.3 From 9ecb0036a32c6930d9400040161c3b9e41ef9b1f Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 19 Feb 2009 18:31:57 -0500 Subject: add keepextension tests --- t/pagename.t | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 't') diff --git a/t/pagename.t b/t/pagename.t index 43f574e98..488e341fa 100755 --- a/t/pagename.t +++ b/t/pagename.t @@ -1,12 +1,13 @@ #!/usr/bin/perl use warnings; use strict; -use Test::More tests => 6; +use Test::More tests => 8; BEGIN { use_ok("IkiWiki"); } # Used internally. -$IkiWiki::hooks{htmlize}{mdwn}{call}=sub {}; +$IkiWiki::hooks{htmlize}{mdwn}={}; +$IkiWiki::hooks{htmlize}{txt}={keepextension => 1}; is(pagename("foo.mdwn"), "foo"); is(pagename("foo/bar.mdwn"), "foo/bar"); @@ -15,3 +16,7 @@ is(pagename("foo/bar.mdwn"), "foo/bar"); is(pagename("foo.png"), "foo.png"); is(pagename("foo/bar.png"), "foo/bar.png"); is(pagename("foo"), "foo"); + +# keepextension preserves the extension in the page name +is(pagename("foo.txt"), "foo.txt"); +is(pagename("foo/bar.txt"), "foo/bar.txt"); -- cgit v1.2.3 From 66dc253437e7ce2e3e8984513b3ecf96603d6670 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Thu, 19 Feb 2009 18:38:45 -0500 Subject: Add noextension parameter to htmlize hooks to support, eg, Makefile. --- IkiWiki.pm | 23 ++++++++++++++--------- debian/changelog | 1 + doc/plugins/write.mdwn | 7 ++++++- doc/todo/Allow_filenames_that_are_all_type.mdwn | 4 ++++ t/pagename.t | 23 ++++++++++++++++++----- t/pagetype.t | 15 --------------- 6 files changed, 43 insertions(+), 30 deletions(-) delete mode 100755 t/pagetype.t (limited to 't') diff --git a/IkiWiki.pm b/IkiWiki.pm index ce1ceb351..f580d1f0d 100644 --- a/IkiWiki.pm +++ b/IkiWiki.pm @@ -627,27 +627,32 @@ sub dirname ($) { return $file; } -sub pagetype ($) { +sub isinternal ($) { my $page=shift; + return exists $pagesources{$page} && + $pagesources{$page} =~ /\._([^.]+)$/; +} + +sub pagetype ($) { + my $file=shift; - if ($page =~ /\.([^.]+)$/) { + if ($file =~ /\.([^.]+)$/) { return $1 if exists $hooks{htmlize}{$1}; } + elsif ($hooks{htmlize}{basename($file)}{noextension}) { + return basename($file); + } return; } -sub isinternal ($) { - my $page=shift; - return exists $pagesources{$page} && - $pagesources{$page} =~ /\._([^.]+)$/; -} - sub pagename ($) { my $file=shift; my $type=pagetype($file); my $page=$file; - $page=~s/\Q.$type\E*$// if defined $type && !$hooks{htmlize}{$type}{keepextension}; + $page=~s/\Q.$type\E*$// + if defined $type && !$hooks{htmlize}{$type}{keepextension} + && !$hooks{htmlize}{$type}{noextension}; if ($config{indexpages} && $page=~/(.*)\/index$/) { $page=$1; } diff --git a/debian/changelog b/debian/changelog index b644ac99c..810c59f4e 100644 --- a/debian/changelog +++ b/debian/changelog @@ -6,6 +6,7 @@ ikiwiki (3.05) UNRELEASED; urgency=low directives, and other links on templates affect the page using the template reliably. * goto: Fix redirect to comments. + * Add noextension parameter to htmlize hooks to support, eg, Makefile. -- Joey Hess Sun, 15 Feb 2009 20:11:57 -0500 diff --git a/doc/plugins/write.mdwn b/doc/plugins/write.mdwn index 2e907938f..696bc6bc3 100644 --- a/doc/plugins/write.mdwn +++ b/doc/plugins/write.mdwn @@ -189,9 +189,14 @@ The function is passed named parameters: "page" and "content" and should return the htmlized content. If `hook` is passed an optional "keepextension" parameter, set to a true -value, then this extension will not be stripped from the source filename when +value, then the extension will not be stripped from the source filename when generating the page. +If `hook` is passed an optional "noextension" parameter, set to a true +value, then the id parameter specifies not a filename extension, but +a whole filename that can be htmlized. This is useful for files +like `Makefile` that have no extension. + ### pagetemplate hook(type => "pagetemplate", id => "foo", call => \&pagetemplate); diff --git a/doc/todo/Allow_filenames_that_are_all_type.mdwn b/doc/todo/Allow_filenames_that_are_all_type.mdwn index e165da7dc..bebbcafa8 100644 --- a/doc/todo/Allow_filenames_that_are_all_type.mdwn +++ b/doc/todo/Allow_filenames_that_are_all_type.mdwn @@ -22,6 +22,10 @@ lost because it didn't have its own bug to track it. Now it does :). -- [[Will >> >> So, yeah, I think this patch is complete. :) -- [[Will]] +>>> Thanks, [[applied|done]], but I added a noextension parameter, +>>> since having keepextension allow files with no extension didn't make +>>> sense. Also, made it work for pages in subdirs.. --[[Joey]] + diff --git a/IkiWiki.pm b/IkiWiki.pm index 8d728c9..1bd46a9 100644 --- a/IkiWiki.pm diff --git a/t/pagename.t b/t/pagename.t index 488e341fa..540d10f4c 100755 --- a/t/pagename.t +++ b/t/pagename.t @@ -1,22 +1,35 @@ #!/usr/bin/perl use warnings; use strict; -use Test::More tests => 8; +use Test::More tests => 19; BEGIN { use_ok("IkiWiki"); } -# Used internally. +# define mdwn as an extension $IkiWiki::hooks{htmlize}{mdwn}={}; -$IkiWiki::hooks{htmlize}{txt}={keepextension => 1}; - +is(pagetype("foo.mdwn"), "mdwn"); is(pagename("foo.mdwn"), "foo"); +is(pagetype("foo/bar.mdwn"), "mdwn"); is(pagename("foo/bar.mdwn"), "foo/bar"); -# bare files get the full filename as page name +# bare files get the full filename as page name, undef type +is(pagetype("foo.png"), undef); is(pagename("foo.png"), "foo.png"); +is(pagetype("foo/bar.png"), undef); is(pagename("foo/bar.png"), "foo/bar.png"); +is(pagetype("foo"), undef); is(pagename("foo"), "foo"); # keepextension preserves the extension in the page name +$IkiWiki::hooks{htmlize}{txt}={keepextension => 1}; is(pagename("foo.txt"), "foo.txt"); +is(pagetype("foo.txt"), "txt"); is(pagename("foo/bar.txt"), "foo/bar.txt"); +is(pagetype("foo/bar.txt"), "txt"); + +# noextension makes extensionless files be treated as first-class pages +$IkiWiki::hooks{htmlize}{Makefile}={noextension =>1}; +is(pagetype("Makefile"), "Makefile"); +is(pagename("Makefile"), "Makefile"); +is(pagetype("foo/Makefile"), "Makefile"); +is(pagename("foo/Makefile"), "foo/Makefile"); diff --git a/t/pagetype.t b/t/pagetype.t deleted file mode 100755 index bb06a1568..000000000 --- a/t/pagetype.t +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/perl -use warnings; -use strict; -use Test::More tests => 6; - -BEGIN { use_ok("IkiWiki"); } - -# Used internally. -$IkiWiki::hooks{htmlize}{mdwn}={}; - -is(pagetype("foo.mdwn"), "mdwn"); -is(pagetype("foo/bar.mdwn"), "mdwn"); -is(pagetype("foo.png"), undef); -is(pagetype("foo/bar.png"), undef); -is(pagetype("foo"), undef); -- cgit v1.2.3