1 Instead of having files foo.html "in front of" foo/, I prefer to have
2 foo/index.html. This patch allows that. Specifically, foo/index.type
3 is translated to $links{'foo/'}, and bestlink looks for either "foo" or
4 "foo/" when linking to pages. There are other miscellaneous changes that
7 1. change the `cgi_editpage` `@page_locs` code so that creating foo from
8 a/b/c prefers a/b/foo and then a/b/c/foo, but if creating foo from a/b/c/,
9 then prefer a/b/c/foo. I'm not really sure why the original was doing what
10 it did (why trim terminal `/` if no pages end in `/`?), so this part might
12 2. tweak things so that index.rss and index.atom are generated if inlining
14 2. backlinks from "foo/bar" to "foo/" trim common prefixes as long as there
15 would be something left when the trimming is done (i.e. don't trim "foo/")
16 3. parentlinks for "foo/" are the same as for "foo", except one directory
18 4. rewrite parentlinks so that bestlink is called at each level
19 5. basename("foo/") => basename("foo")
20 6. links to "foo/" are translated to "foo/index.html" rather than "foo/.html".
21 (Links to "foo/" might be preferred, but that causes an infinite loop in
22 writefile, because apparently dirname("foo/") == "foo/" on my system for
23 reasons that aren't clear to me.)
24 7. pagetitle("foo/") => pagetitle("foo")
25 8. clip the final slash when matching a relative pagespec, even if there are
26 no characters after it (otherwise inlining "./a" from "foo/" gets
27 translated to "foo//a")
29 In case whitespace gets garbled, I'm also leaving a copy of the patch on
30 [my site](http://ikidev.betacantrips.com/patches/index.patch). It should apply
31 cleanly to a freshly unpacked ikiwiki-1.42. You can also see it in action
32 [here](http://ikidev.betacantrips.com/one/). --Ethan
34 diff -urX ignorepats ikiclean/IkiWiki/CGI.pm ikidev/IkiWiki/CGI.pm
35 --- ikiclean/IkiWiki/CGI.pm 2007-02-11 21:40:32.419641000 -0800
36 +++ ikidev/IkiWiki/CGI.pm 2007-02-11 21:54:36.252357000 -0800
38 @page_locs=$best_loc=$page;
46 if ((defined $form->field('subpage') && length $form->field('subpage')) ||
47 $page eq gettext('discussion')) {
51 push @page_locs, $dir.$page;
52 - push @page_locs, "$from/$page";
53 + if ($dir ne $from){ # i.e. $from not a directory
54 + push @page_locs, "$from/$page";
58 push @page_locs, $dir.$page;
59 diff -urX ignorepats ikiclean/IkiWiki/Plugin/inline.pm ikidev/IkiWiki/Plugin/inline.pm
60 --- ikiclean/IkiWiki/Plugin/inline.pm 2007-02-11 21:40:31.996007000 -0800
61 +++ ikidev/IkiWiki/Plugin/inline.pm 2007-02-11 21:54:36.008358000 -0800
64 add_depends($params{page}, $params{pages});
66 - my $rssurl=rsspage(basename($params{page}));
67 - my $atomurl=atompage(basename($params{page}));
68 + my $rssurl=basename(rsspage($params{page}));
69 + my $atomurl=basename(atompage($params{page}));
72 if (exists $params{rootpage} && $config{cgiurl}) {
75 sub rsspage ($) { #{{{
77 + $page = htmlpage($page);
78 + $page =~s/\.html$/.rss/;
80 - return $page.".rss";
84 sub atompage ($) { #{{{
86 + $page = htmlpage($page);
87 + $page =~s/\.html$/.atom/;
89 - return $page.".atom";
93 sub genfeed ($$$$@) { #{{{
94 diff -urX ignorepats ikiclean/IkiWiki/Render.pm ikidev/IkiWiki/Render.pm
95 --- ikiclean/IkiWiki/Render.pm 2007-02-11 21:40:32.413641000 -0800
96 +++ ikidev/IkiWiki/Render.pm 2007-02-11 21:54:36.246356000 -0800
99 1 while (($dir)=$page_trimmed=~m!^([^/]+/)!) &&
101 + $p_trimmed=~m/^\Q$dir\E(?:.)/ &&
102 $p_trimmed=~s/^\Q$dir\E// &&
103 $page_trimmed=~s/^\Q$dir\E//;
108 return if $page eq 'index'; # toplevel
109 - foreach my $dir (reverse split("/", $page)) {
110 + if ($page =~ m{/$}){
115 + while ($page =~ m!([^/]+)$!) {
117 + $page =~ s!/?[^/]+$!!;
120 - unshift @ret, { url => $path.htmlpage($dir), page => pagetitle($dir) };
121 + my $target = abs2rel(htmlpage(bestlink($page, $last)), $page);
122 + unshift @ret, { url => $path.$target, page => pagetitle($last) };
126 diff -urX ignorepats ikiclean/IkiWiki.pm ikidev/IkiWiki.pm
127 --- ikiclean/IkiWiki.pm 2007-02-11 21:40:35.118406000 -0800
128 +++ ikidev/IkiWiki.pm 2007-02-11 22:22:49.146071000 -0800
130 sub basename ($) { #{{{
137 @@ -214,12 +215,14 @@
138 my $type=pagetype($file);
140 $page=~s/\Q.$type\E*$// if defined $type;
141 + $page=~s#index$## if $page=~m{/index$};
145 sub htmlpage ($) { #{{{
148 + return $page."index.html" if $page=~m{/$};
149 return $page.".html";
158 if ($link=~s/^\/+//) {
161 if (exists $links{$l}) {
164 + if (exists $links{$l.'/'}){
167 elsif (exists $pagecase{lc $l}) {
168 return $pagecase{lc $l};
171 $page=~s/__(\d+)__/&#$1;/g;
181 if ($glob =~ m!^\./!) {
182 - $from=~s!/?[^/]+$!!;
183 + $from=~s!/?[^/]*$!!;
185 $glob="$from/$glob" if length $from;
188 I independently implemented a similar, but smaller patch.
189 (It's smaller because I only care about rendering; not CGI, for example.)
190 The key to this patch is that "A/B/C" is treated as equivalent
192 Here it is: --Per Bothner
194 --- IkiWiki/Render.pm~ 2007-01-11 15:01:51.000000000 -0800
195 +++ IkiWiki/Render.pm 2007-02-02 22:24:12.000000000 -0800
197 foreach my $dir (reverse split("/", $page)) {
200 - unshift @ret, { url => $path.htmlpage($dir), page => pagetitle($dir) };
201 + unshift @ret, { url => abs2rel(htmlpage(bestlink($page, $dir)), dirname($page)), page => pagetitle($dir) };
204 + elsif ($dir ne "index") {
209 --- IkiWiki.pm~ 2007-01-12 12:47:09.000000000 -0800
210 +++ IkiWiki.pm 2007-02-02 18:02:16.000000000 -0800
212 elsif (exists $pagecase{lc $l}) {
213 return $pagecase{lc $l};
216 + my $lindex = $l . "/index";
217 + if (exists $links{$lindex}) {
221 } while $cwd=~s!/?[^/]+$!!;
223 if (length $config{userdir} && exists $links{"$config{userdir}/".lc($link)}) {
225 Note I handle setting the url; slightly differently.
226 Also note that an initial "index" is ignored. I.e. a
227 page "A/B/index.html" is treated as "A/B".
229 > This is actually a pretty cool hack. I'll have to think about
230 > whether I like it better than my way though :) --Ethan
234 How about doing the index stuff only on the output side? (Or does the latter patch do it? I haven't tried them.) That is, render every `foo.type` for the rendered types (mdwn etc.) as `foo/index.html`, generating links to `foo/` instead of `foo.html`, but not earlier than the point where the .html as presently appended to the page name. Then you just flip a build time option on an existing wiki without any changes to that, and the pages appear elsewhere. The `index.type` files might be left out of this scheme, though (and the top-level one, of course, has to). --[[tuomov]]
236 > Well, get around to wasting time on it after all, and [here's the patch](http://iki.fi/tuomov/use_dirs.diff). The `-use_dirs` option will cause everything to be rendered inside directories. There may still be some problems with it, that need looking into (it doesn't e.g. check for conflicts between foo/index.mdwn and foo.mdwn), but seems to work well enough for me... The patch also improves, I think, the parentlinks code a little, as it uses generic routines to actually find the target location now. The only places where the `use_dirs` option is used is `htmlpage`, in fact, although other specific kludges needed to be removed from other points in the code.
238 >> FWIW, [use_dirs.diff](http://iki.fi/tuomov/use_dirs.diff) applies cleanly, and works well for me. Given that it makes this behaviour optional, how about merging it? I have some follow-up patches which I'm sitting on for now. ;-) -- Ben
240 >>> How do you apply a patch created by svn diff? I've been curious about this for a long time. The use_dirs patch looks OK but I'd like to play with it. --Ethan
242 >>>> Just do `svn co svn://ikiwiki.kitenet.net/ikiwiki/trunk ikiwiki` then `cd ikiwiki && patch -p0 <use_dirs.diff`. :-) Same would work with a tarball as well.
244 >>>>> Sorry, I'm dumb. I'm so used to doing -p1 that doing -p0 never occurred to me; I thought the patch format generated by svn diff was just "wrong". --Ethan
248 First pass over Tumov's patch -- which doesn't cleanly apply anymore, so
249 I'll attach an updated and slightly modified version below.
251 * `urlto()` is O(N) to the number of pages in the wiki, which leads to
252 O(N^2) behavior, which could be a scalability problem. This happens because
253 of the lookup for `$to` in `%renderedfiles`, which shouldn't be necessary
254 most of the time. Couldn't it just be required that `$to` be a html page
255 name on input? Or require it be a non-html page name and always run
258 > Perhaps it would be possible to require that, but it seems like a
259 > very artificial restriction. The renderedfiles search is just a
260 > copy-paste from htmllink, and I'm no perl (or ikiwiki internals)
261 > expert... maybe there would be a faster way to do the check whether
262 > name translation is needed? No more than O(log n) steps should be
263 > needed for a simple search, after all, and maybe there would be shortcuts
264 > for even constant-time (in n) checks. --[[tuomov]]
266 * As we discussed in email, this will break handling of `foo/index.mdwn`
267 pages. Needs to be changed to generate `foo/index/index.html` for such
268 pages (though not for the toplevel `index`).
270 >> Can someone elaborate on this? What's broken about it? Will pages
271 >> foo/index/index.html include foo/index in their parentlinks? --Ethan
273 * This does make the resulting wikis much less browsable directly on the
274 filesystem, since `dir` to `dir/index.html` conversion is only handled by web
275 servers and so you end up browsing to a directory index all the time.
276 Wouldn't it be better to make the links themselves include the index.html?
277 (Although that would mean that [[bugs/broken_parentlinks]] would not be
278 fixed en passant by this patch..)
280 > Yes, the sites are not that browsable on the FS (I blame the browsers
281 > for being stupid!), but linking to the directory produces so much
282 > cleaner URLs for the Web, that I specifically want it. This is,
283 > after all, an optional arrangement.
285 * Some of the generated links are missing the trailing / , which is
286 innefficient since it leads to a http redirect when clicking on that
287 link. Seems to be limited to ".." links, and possibly only to
288 parentlinks. (Already fixed it for "." links.)
289 * It calles abs2rel about 16% more often with the patch, which makes it
290 a bit slower, since abs2rel is not very efficient. (This omits abs2rel
291 calls that might be memoized away already.) This seems to be due to one
292 extra abs2rel for the toplevel wiki page due to the nicely cleaned up code
293 in `parentlinks` -- so I'm not really complaining.. Especially since the
294 patch adds a new nice memoizable `urlto`.
295 * The rss page name generation code seems unnecesarily roundabout, I'm sure
296 that can be cleaned up somehow, perhaps by making `htmlpage` more
299 > Something like `targetpage(basename, extension)`?
301 * `aggregate.pm` uses htmlpage in a way that breaks with its new behavior.
302 It will need to be changed as follows:
306 ===================================================================
307 --- aggregate.pm (revision 2700)
308 +++ aggregate.pm (working copy)
310 # NB: This doesn't check for path length limits.
312 my $max=POSIX::pathconf($config{srcdir}, &POSIX::_PC_NAME_MAX);
313 - if (defined $max && length(htmlpage($page)) >= $max) {
314 + if (defined $max && length(htmlfn($page)) >= $max) {
316 $page=$feed->{dir}."/item";
317 while (exists $IkiWiki::pagecase{lc $page.$c} ||
319 if (ref $feed->{tags}) {
320 $template->param(tags => [map { tag => $_ }, @{$feed->{tags}}]);
322 - writefile(htmlpage($guid->{page}), $config{srcdir},
323 + writefile(htmlfn($guid->{page}), $config{srcdir},
326 # Set the mtime, this lets the build process get the right creation
328 return "$config{srcdir}/".htmlpage($page);
331 +sub htmlfn ($) { #{{{
332 + return shift().".html";
338 * `linkmap.pm` uses `htmlpage` to construct a link and should probably be
339 changed like this (untested):
343 ===================================================================
344 --- linkmap.pm (revision 2700)
345 +++ linkmap.pm (working copy)
347 foreach my $item (keys %links) {
348 if (pagespec_match($item, $params{pages}, $params{page})) {
349 my $link=htmlpage($item);
350 - $link=IkiWiki::abs2rel($link, IkiWiki::dirname($params{page}));
351 - $mapitems{$item}=$link;
352 + $mapitems{$item}=urlto($link, $params{destpage});
357 * `inline.pm` uses htmlpage and `abs2rel` to generate a link, and probably
358 needs to be changed to either use `urlto` or to call `beautify_url` like
359 htmllink does. This might work:
363 ===================================================================
364 --- inline.pm (revision 2700)
365 +++ inline.pm (working copy)
367 # Don't use htmllink because this way the
368 # title is separate and can be overridden by
370 - my $link=bestlink($params{page}, $page);
371 - $link=htmlpage($link) if defined $type;
372 - $link=abs2rel($link, dirname($params{destpage}));
373 - $template->param(pageurl => $link);
374 + $template->param(pageurl => urlto(bestlink($params{page}, $page), $params{destpage}));
375 $template->param(title => pagetitle(basename($page)));
376 $template->param(ctime => displaytime($pagectime{$page}));
379 * `img.pm` makes some assumptions about name of the page that will be
380 linking to the image, which are probably broken.
382 * The changes to htmlpage's behavior probably call for the plugin
383 interface version number to be changed.
387 Updated version of Tumov's patch follows:
390 Index: IkiWiki/Render.pm
391 ===================================================================
392 --- IkiWiki/Render.pm (revision 2700)
393 +++ IkiWiki/Render.pm (working copy)
396 return unless $backlinks{$page};
397 foreach my $p (keys %{$backlinks{$page}}) {
398 - my $href=abs2rel(htmlpage($p), dirname($page));
400 + my $href=urlto($p, $page);
402 # Trim common dir prefixes from both pages.
404 my $page_trimmed=$page;
410 + my $title=$config{wikiname};
412 return if $page eq 'index'; # toplevel
413 - foreach my $dir (reverse split("/", $page)) {
416 - unshift @ret, { url => $path.htmlpage($dir), page => pagetitle($dir) };
421 + foreach my $dir (split("/", $page)) {
422 + push @ret, { url => urlto($path, $page), page => $title };
424 + $title=pagetitle($dir);
426 - unshift @ret, { url => length $path ? $path : ".", page => $config{wikiname} };
430 Index: IkiWiki/Plugin/inline.pm
431 ===================================================================
432 --- IkiWiki/Plugin/inline.pm (revision 2700)
433 +++ IkiWiki/Plugin/inline.pm (working copy)
436 add_depends($params{page}, $params{pages});
438 - my $rssurl=rsspage(basename($params{page}));
439 - my $atomurl=atompage(basename($params{page}));
440 + my $rssurl=basename(rsspage($params{page}));
441 + my $atomurl=basename(atompage($params{page}));
444 if (exists $params{rootpage} && $config{cgiurl}) {
446 # title is separate and can be overridden by
448 my $link=bestlink($params{page}, $page);
449 - $link=htmlpage($link) if defined $type;
450 - $link=abs2rel($link, dirname($params{destpage}));
451 + $link=urlto($link, $params{destpage});
452 $template->param(pageurl => $link);
453 $template->param(title => pagetitle(basename($page)));
454 $template->param(ctime => displaytime($pagectime{$page}));
455 @@ -205,15 +204,17 @@
459 - will_render($params{page}, rsspage($params{page}));
460 - writefile(rsspage($params{page}), $config{destdir},
461 + my $rssp=rsspage($params{page});
462 + will_render($params{page}, $rssp);
463 + writefile($rssp, $config{destdir},
464 genfeed("rss", $rssurl, $desc, $params{page}, @list));
465 $toping{$params{page}}=1 unless $config{rebuild};
466 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
469 - will_render($params{page}, atompage($params{page}));
470 - writefile(atompage($params{page}), $config{destdir},
471 + my $atomp=atompage($params{page});
472 + will_render($params{page}, $atomp);
473 + writefile($atomp, $config{destdir},
474 genfeed("atom", $atomurl, $desc, $params{page}, @list));
475 $toping{$params{page}}=1 unless $config{rebuild};
476 $feedlinks{$params{destpage}}=qq{<link rel="alternate" type="application/atom+xml" title="Atom" href="$atomurl" />};
477 @@ -288,16 +289,25 @@
481 +sub basepage ($) { #{{{
484 + $page=htmlpage($page);
485 + $page =~ s/\.html$//;
490 sub rsspage ($) { #{{{
493 - return $page.".rss";
494 + return basepage($page).".rss";
497 sub atompage ($) { #{{{
500 - return $page.".atom";
501 + return basepage($page).".atom";
504 sub genfeed ($$$$@) { #{{{
506 ===================================================================
507 --- ikiwiki.in (revision 2700)
508 +++ ikiwiki.in (working copy)
510 "sslcookie!" => \$config{sslcookie},
511 "httpauth!" => \$config{httpauth},
512 "userdir=s" => \$config{userdir},
513 + "usedirs!" => \$config{usedirs},
514 "exclude=s@" => sub {
515 push @{$config{wiki_file_prune_regexps}}, $_[1];
517 Index: doc/usage.mdwn
518 ===================================================================
519 --- doc/usage.mdwn (revision 2700)
520 +++ doc/usage.mdwn (working copy)
527 + Create output files named page/index.html instead of page.html.
529 * --w3mmode, --no-w3mmode
531 Enable [[w3mmode]], which allows w3m to use ikiwiki as a local CGI script,
532 Index: doc/plugins/write.mdwn
533 ===================================================================
534 --- doc/plugins/write.mdwn (revision 2700)
535 +++ doc/plugins/write.mdwn (working copy)
538 This is the standard gettext function, although slightly optimised.
542 +Construct a relative url to the first parameter from the second.
546 ikiwiki's support for revision control systems also uses pluggable perl
547 Index: doc/ikiwiki.setup
548 ===================================================================
549 --- doc/ikiwiki.setup (revision 2700)
550 +++ doc/ikiwiki.setup (working copy)
553 # To link to user pages in a subdirectory of the wiki.
555 + # To enable alternate output filenames.
558 # To add plugins, list them here.
559 #add_plugins => [qw{goodstuff openid search wikitext camelcase
561 ===================================================================
562 --- IkiWiki.pm (revision 2700)
563 +++ IkiWiki.pm (working copy)
565 use Exporter q{import};
566 our @EXPORT = qw(hook debug error template htmlpage add_depends pagespec_match
567 bestlink htmllink readfile writefile pagetype srcfile pagename
568 - displaytime will_render gettext
569 + displaytime will_render gettext urlto
570 %config %links %renderedfiles %pagesources);
571 our $VERSION = 1.02; # plugin interface version, next is ikiwiki version
572 our $version='unknown'; # VERSION_AUTOREPLACE done by Makefile, DNE
580 sub checkconfig () { #{{{
582 sub htmlpage ($) { #{{{
585 - return $page.".html";
586 + if (! $config{usedirs} || $page =~ /^index$/ || $page =~ /\/index$/) {
587 + return $page.".html";
589 + return $page."/index.html";
593 sub srcfile ($) { #{{{
596 return "$config{url}/" if ! defined $page;
598 + $page=htmlpage($page);
600 $page=~s/[^\/]+\//..\//g;
603 $config{timeformat}, localtime($time)));
606 +sub beautify_url ($) { #{{{
609 + $url =~ s!/index.html$!/!;
610 + $url =~ s!^$!./!; # Browsers don't like empty links...
615 +sub urlto ($$) { #{{{
620 + ! grep { $_ eq $to } map { @{$_} } values %renderedfiles) {
624 + my $link = abs2rel($to, dirname(htmlpage($from)));
626 + return beautify_url($link);
629 sub htmllink ($$$;@) { #{{{
630 my $lpage=shift; # the page doing the linking
631 my $page=shift; # the page that will contain the link (different for inline)
633 "\">?</a>$linktext</span>"
636 - $bestlink=abs2rel($bestlink, dirname($page));
637 + $bestlink=abs2rel($bestlink, dirname(htmlpage($page)));
638 + $bestlink=beautify_url($bestlink);
640 if (! $opts{noimageinline} && isinlinableimage($bestlink)) {
641 return "<img src=\"$bestlink\" alt=\"$linktext\" />";