Merge branch 'master' into autotag
authorJoey Hess <joey@kitenet.net>
Thu, 22 Apr 2010 00:32:30 +0000 (20:32 -0400)
committerJoey Hess <joey@kitenet.net>
Thu, 22 Apr 2010 00:32:30 +0000 (20:32 -0400)
IkiWiki.pm
IkiWiki/Plugin/tag.pm
IkiWiki/Render.pm
debian/changelog
doc/plugins/tag.mdwn
doc/plugins/write.mdwn
doc/roadmap.mdwn
templates/autotag.tmpl [new file with mode: 0644]

index 8af290745283a5bc86c45cc4bffb9e156cda5a05..dbf50feb46d31cc1a97f14456527d7cd1ea6a2cd 100644 (file)
@@ -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;
index 8ec08e936d1b021d69d7ad9f546c3427a425920c..cd7ecc212fd7c928560ade98d34366ae9da45999 100644 (file)
@@ -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
index 0e5336f22e83cdd998f95a1aa5038aad03e20528..49d080c161a9c6cc93250b3c6a8dc204a5ee209a 100644 (file)
@@ -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);
index 6b91fd3e6eef16aa7e7d60db52d6d9fd18b94bed..a63860088a16c3a8b24e28bb0be5cedb46a91e0c 100644 (file)
@@ -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.
index 8ff70a069ce666cc9fc909878b2a7e93518d8296..bdf39d7e89cc4bb7d94e48e9414d9992c0ef28e8 100644 (file)
@@ -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.
index 0bf6fcf48ff17b48c60a9cad37a0ded3cd60f0c9..404c3b44f3ace0123194a29c14c5ede32d2be3dc 100644 (file)
@@ -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
index 0c7bf2fa8af454aed9da37865b232bb9e193e7c3..8e2a01827032c7b8c05fa93e601b1c5534efdee4 100644 (file)
@@ -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 (file)
index 0000000..7b0d4c9
--- /dev/null
@@ -0,0 +1,3 @@
+## Pages tagged <TMPL_VAR TAGNAME> ##
+
+[[!inline pages="tagged(<TMPL_VAR TAG>)" actions="no" archive="yes"]]