From: Joey Hess Date: Thu, 22 Apr 2010 00:18:44 +0000 (-0400) Subject: Merge branch 'master' into autotag X-Git-Url: https://sipb.mit.edu/gitweb.cgi/ikiwiki.git/commitdiff_plain/20052f888277f270ab846bf8b1eb44714c1009d6?hp=129cd00bdb03e32df8645b12138cb4c79952775e Merge branch 'master' into autotag Conflicts: IkiWiki/Plugin/tag.pm --- diff --git a/IkiWiki.pm b/IkiWiki.pm index 8af290745..dbf50feb4 100644 --- a/IkiWiki.pm +++ b/IkiWiki.pm @@ -12,19 +12,20 @@ use Storable; use open qw{:utf8 :std}; use vars qw{%config %links %oldlinks %pagemtime %pagectime %pagecase - %pagestate %wikistate %renderedfiles %oldrenderedfiles - %pagesources %destsources %depends %depends_simple %hooks - %forcerebuild %loaded_plugins %typedlinks %oldtypedlinks}; + %pagestate %wikistate %renderedfiles %oldrenderedfiles + %pagesources %destsources %depends %depends_simple %hooks + %forcerebuild %loaded_plugins %typedlinks %oldtypedlinks + %autofiles}; use Exporter q{import}; our @EXPORT = qw(hook debug error template htmlpage deptype - add_depends pagespec_match pagespec_match_list bestlink - htmllink readfile writefile pagetype srcfile pagename - displaytime will_render gettext ngettext urlto targetpage - add_underlay pagetitle titlepage linkpage newpagefile - inject add_link - %config %links %pagestate %wikistate %renderedfiles - %pagesources %destsources %typedlinks); + add_depends pagespec_match pagespec_match_list bestlink + htmllink readfile writefile pagetype srcfile pagename + displaytime will_render gettext ngettext urlto targetpage + add_underlay pagetitle titlepage linkpage newpagefile + inject add_link add_autofile + %config %links %pagestate %wikistate %renderedfiles + %pagesources %destsources %typedlinks); our $VERSION = 3.00; # plugin interface version, next is ikiwiki version our $version='unknown'; # VERSION_AUTOREPLACE done by Makefile, DNE our $installdir='/usr'; # INSTALLDIR_AUTOREPLACE done by Makefile, DNE @@ -1885,7 +1886,7 @@ sub define_gettext () { return shift; } }; - *ngettext=sub { + *ngettext=sub { $getobj->() if $getobj; if ($gettext_obj) { $gettext_obj->nget(@_); @@ -1950,6 +1951,15 @@ sub add_link ($$;$) { } } +sub add_autofile ($$$) { + my $file=shift; + my $plugin=shift; + my $generator=shift; + + $autofiles{$file}{plugin}=$plugin; + $autofiles{$file}{generator}=$generator; +} + sub sortspec_translate ($$) { my $spec = shift; my $reverse = shift; diff --git a/IkiWiki/Plugin/tag.pm b/IkiWiki/Plugin/tag.pm index 8ec08e936..cd7ecc212 100644 --- a/IkiWiki/Plugin/tag.pm +++ b/IkiWiki/Plugin/tag.pm @@ -34,11 +34,18 @@ sub getsetup () { safe => 1, rebuild => 1, }, + tag_autocreate => { + type => "boolean", + example => 0, + description => "autocreate new tag pages?", + safe => 1, + rebuild => undef, + }, } -sub tagpage ($) { +sub taglink ($) { my $tag=shift; - + if ($tag !~ m{^/} && defined $config{tagbase}) { $tag="/".$config{tagbase}."/".$tag; @@ -48,13 +55,45 @@ sub tagpage ($) { return $tag; } -sub taglink ($$$;@) { +sub htmllink_tag ($$$;@) { my $page=shift; my $destpage=shift; my $tag=shift; my %opts=@_; - return htmllink($page, $destpage, tagpage($tag), %opts); + return htmllink($page, $destpage, taglink($tag), %opts); +} + +sub gentag ($) { + my $tag=shift; + + if ($config{tag_autocreate}) { + my $tagpage=taglink($tag); + if ($tagpage=~/^\.\/(.*)/) { + $tagpage=$1; + } + else { + $tagpage=~s/^\///; + } + + my $tagfile = newpagefile($tagpage, $config{default_pageext}); + + add_autofile($tagfile, "tag", sub { + my $message=sprintf(gettext("creating tag page %s"), $tagpage); + debug($message); + + my $template=template("autotag.tmpl"); + $template->param(tagname => IkiWiki::basename($tag)); + $template->param(tag => $tag); + writefile($tagfile, $config{srcdir}, $template->output); + if ($config{rcs}) { + IkiWiki::disable_commit_hook(); + IkiWiki::rcs_add($tagfile); + IkiWiki::rcs_commit_staged($message, undef, undef); + IkiWiki::enable_commit_hook(); + } + }); + } } sub preprocess_tag (@) { @@ -69,8 +108,11 @@ sub preprocess_tag (@) { foreach my $tag (keys %params) { $tag=linkpage($tag); + # hidden WikiLink - add_link($page, tagpage($tag), 'tag'); + add_link($page, taglink($tag), 'tag'); + + gentag($tag); } return ""; @@ -84,14 +126,16 @@ sub preprocess_taglink (@) { return join(" ", map { if (/(.*)\|(.*)/) { my $tag=linkpage($2); - add_link($params{page}, tagpage($tag), 'tag'); - return taglink($params{page}, $params{destpage}, $tag, + add_link($params{page}, taglink($tag), 'tag'); + gentag($tag); + return htmllink_tag($params{page}, $params{destpage}, $tag, linktext => pagetitle($1)); } else { my $tag=linkpage($_); - add_link($params{page}, tagpage($tag), 'tag'); - return taglink($params{page}, $params{destpage}, $tag); + add_link($params{page}, taglink($tag), 'tag'); + gentag($tag); + return htmllink_tag($params{page}, $params{destpage}, $tag); } } grep { @@ -109,7 +153,7 @@ sub pagetemplate (@) { $template->param(tags => [ map { - link => taglink($page, $destpage, $_, rel => "tag") + link => htmllink_tag($page, $destpage, $_, rel => "tag") }, sort keys %$tags ]) if defined $tags && %$tags && $template->query(name => "tags"); @@ -126,8 +170,8 @@ package IkiWiki::PageSpec; sub match_tagged ($$;@) { my $page=shift; - my $glob=shift; - return match_link($page, IkiWiki::Plugin::tag::tagpage($glob), linktype => 'tag', @_); + my $glob=IkiWiki::Plugin::tag::taglink(shift); + return match_link($page, $glob, linktype => 'tag', @_); } 1 diff --git a/IkiWiki/Render.pm b/IkiWiki/Render.pm index 0e5336f22..49d080c16 100644 --- a/IkiWiki/Render.pm +++ b/IkiWiki/Render.pm @@ -43,7 +43,7 @@ sub backlinks ($) { my @links; foreach my $p (backlink_pages($page)) { my $href=urlto($p, $page); - + # Trim common dir prefixes from both pages. my $p_trimmed=$p; my $page_trimmed=$page; @@ -286,63 +286,54 @@ sub find_src_files () { my %pages; eval q{use File::Find}; error($@) if $@; - find({ - no_chdir => 1, - wanted => sub { - my $file=decode_utf8($_); - $file=~s/^\Q$config{srcdir}\E\/?//; - return if -l $_ || -d _ || ! length $file; - my $page = pagename($file); - if (! exists $pagesources{$page} && - file_pruned($file)) { - $File::Find::prune=1; - return; - } - my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint - if (! defined $f) { - warn(sprintf(gettext("skipping bad filename %s"), $file)."\n"); - } - else { - push @files, $f; - if ($pages{$page}) { - debug(sprintf(gettext("%s has multiple possible source pages"), $page)); + my ($page, $dir, $underlay); + my $helper=sub { + my $file=decode_utf8($_); + + return if -l $file || -d _; + $file=~s/^\Q$dir\E\/?//; + return if ! length $file; + $page = pagename($file); + if (! exists $pagesources{$page} && + file_pruned($file)) { + $File::Find::prune=1; + return; + } + + my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint + if (! defined $f) { + warn(sprintf(gettext("skipping bad filename %s"), $file)."\n"); + } + + if ($underlay) { + # avoid underlaydir override attacks; see security.mdwn + if (! -l "$config{srcdir}/$f" && ! -e _) { + if (! $pages{$page}) { + push @files, $f; + $pages{$page}=1; } - $pages{$page}=1; } - }, - }, $config{srcdir}); - foreach my $dir (@{$config{underlaydirs}}, $config{underlaydir}) { + } + else { + push @files, $f; + if ($pages{$page}) { + debug(sprintf(gettext("%s has multiple possible source pages"), $page)); + } + $pages{$page}=1; + } + }; + + find({ + no_chdir => 1, + wanted => $helper, + }, $dir=$config{srcdir}); + $underlay=1; + foreach (@{$config{underlaydirs}}, $config{underlaydir}) { find({ no_chdir => 1, - wanted => sub { - my $file=decode_utf8($_); - $file=~s/^\Q$dir\E\/?//; - return if -l $_ || -d _ || ! length $file; - my $page=pagename($file); - if (! exists $pagesources{$page} && - file_pruned($file)) { - $File::Find::prune=1; - return; - } - - my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint - if (! defined $f) { - warn(sprintf(gettext("skipping bad filename %s"), $file)."\n"); - } - else { - # avoid underlaydir override - # attacks; see security.mdwn - if (! -l "$config{srcdir}/$f" && - ! -e _) { - if (! $pages{$page}) { - push @files, $f; - $pages{$page}=1; - } - } - } - }, - }, $dir); + wanted => $helper, + }, $dir=$_); }; return \@files, \%pages; } @@ -686,6 +677,49 @@ sub render_backlinks ($) { } } +sub gen_autofile ($$$) { + my $autofile=shift; + my $pages=shift; + my $del=shift; + + if (file_pruned($autofile)) { + return; + } + + my ($file)="$config{srcdir}/$autofile" =~ /$config{wiki_file_regexp}/; # untaint + if (! defined $file) { + return; + } + + # Remember autofiles that were tried, and never try them again later. + if (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}) { + return; + } + $wikistate{$autofiles{$autofile}{plugin}}{autofile}{$autofile}=1; + + if (srcfile($autofile, 1) || file_pruned($autofile)) { + return; + } + + if (-l $file || -d _ || -e _) { + return; + } + + my $page = pagename($file); + if ($pages->{$page}) { + return; + } + + if (grep { $_ eq $autofile } @$del) { + return; + } + + $autofiles{$autofile}{generator}->(); + $pages->{$page}=1; + return 1; +} + + sub refresh () { srcdir_check(); run_hooks(refresh => sub { shift->() }); @@ -700,6 +734,16 @@ sub refresh () { scan($file); } + foreach my $autofile (keys %autofiles) { + if (gen_autofile($autofile, $pages, $del)) { + push @{$files}, $autofile; + push @{$new}, $autofile if find_new_files([$autofile]); + push @{$changed}, $autofile if find_changed([$autofile]); + + scan($autofile); + } + } + calculate_links(); remove_del(@$del, @$internal_del); diff --git a/debian/changelog b/debian/changelog index 6b91fd3e6..a63860088 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,6 +1,8 @@ ikiwiki (3.20100415) UNRELEASED; urgency=low [ Joey Hess ] + * tag: Automatic creation of tag pages can now be enabled using + the tag_autocreate setting. (David Riebenbauer) * bzr: Fix bzr log parsing to work with bzr 2.0. (liw) * comments: Fix missing entity encoding in title. * txt: Add a special case for robots.txt. diff --git a/doc/plugins/tag.mdwn b/doc/plugins/tag.mdwn index 8ff70a069..bdf39d7e8 100644 --- a/doc/plugins/tag.mdwn +++ b/doc/plugins/tag.mdwn @@ -8,6 +8,9 @@ These directives allow tagging pages. It also provides the `tagged()` [[ikiwiki/PageSpec]], which can be used to match pages that are tagged with a specific tag. +If the `tag_autocreate` setting is enabled, tag pages will automatically be +created as needed. + [[!if test="enabled(tag)" then=""" This wiki has the tag plugin enabled, so you'll see a note below that this page is tagged with the "tags" tag. diff --git a/doc/plugins/write.mdwn b/doc/plugins/write.mdwn index 0bf6fcf48..404c3b44f 100644 --- a/doc/plugins/write.mdwn +++ b/doc/plugins/write.mdwn @@ -966,6 +966,23 @@ added. Pass it the page that contains the link, and the link text. An optional third parameter sets the link type. If not specified, it is an ordinary [[ikiwiki/WikiLink]]. +### `add_autofile($$$)` + +Sometimes you may want to add a file to the `srcdir` as a result of content +of other pages. For example, [[plugins/tag]] pages can be automatically +created as needed. This function can be used to do that. + +The three parameters are the filename to create (relative to the `srcdir`), +the name of the plugin, and a callback function. The callback will be +called if it is appropriate to automatically add the file, and should then +take care of creating it, and doing anything else it needs to (such as +checking it into revision control). Note that the callback may not always +be called. For example, if an automatically added file is deleted by the +user, ikiwiki will avoid re-adding it again. + +This function needs to be called during the scan hook, or earlier in the +build process, in order to add the file early enough for it to be built. + ## Miscellaneous ### Internal use pages diff --git a/doc/roadmap.mdwn b/doc/roadmap.mdwn index 0c7bf2fa8..8e2a01827 100644 --- a/doc/roadmap.mdwn +++ b/doc/roadmap.mdwn @@ -78,6 +78,7 @@ Probably incomplete list: * Make pagespecs match relative by default? (see [[discussion]]) * Flip wikilinks? (see [[todo/link_plugin_perhaps_too_general?]]) * YADA format setup files per default? +* Enable `tag_autocreate` by default. In general, we try to use [[ikiwiki-transition]] or forced rebuilds on upgrade to deal with changes that break compatability. Some things that diff --git a/templates/autotag.tmpl b/templates/autotag.tmpl new file mode 100644 index 000000000..7b0d4c90f --- /dev/null +++ b/templates/autotag.tmpl @@ -0,0 +1,3 @@ +## Pages tagged ## + +[[!inline pages="tagged()" actions="no" archive="yes"]]