summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--IkiWiki.pm32
-rw-r--r--IkiWiki/Plugin/tag.pm58
-rw-r--r--IkiWiki/Render.pm109
-rw-r--r--doc/plugins/write.mdwn17
-rw-r--r--templates/autotag.tmpl3
5 files changed, 163 insertions, 56 deletions
diff --git a/IkiWiki.pm b/IkiWiki.pm
index 4084d4997..5355b838d 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 7a85874f6..d2a3d4dfd 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,37 @@ 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 {
+ debug(sprintf(gettext("creating tag page %s"), $tag));
+
+ my $template=template("autotag.tmpl");
+ $template->param(tag => $tag);
+ writefile($tagfile, $config{srcdir}, $template->output);
+ });
+ }
}
sub preprocess_tag (@) {
@@ -69,8 +100,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 +118,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 +145,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");
@@ -125,7 +161,7 @@ sub pagetemplate (@) {
package IkiWiki::PageSpec;
sub match_tagged ($$;@) {
- return match_link($_[0], IkiWiki::Plugin::tag::tagpage($_[1]), linktype => 'tag');
+ return match_link($_[0], IkiWiki::Plugin::tag::taglink($_[1]), linktype => 'tag');
}
1
diff --git a/IkiWiki/Render.pm b/IkiWiki/Render.pm
index bbf8f915e..03b2910fd 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;
@@ -281,6 +281,26 @@ sub srcdir_check () {
}
+sub verify_src_file ($$) {
+ my $file=shift;
+ my $dir=shift;
+
+ return if -l $file || -d _;
+ $file=~s/^\Q$dir\E\/?//;
+ return if ! length $file;
+ my $page = pagename($file);
+ if (! exists $pagesources{$page} &&
+ file_pruned($file)) {
+ return;
+ }
+
+ my ($file_untainted) = $file =~ /$config{wiki_file_regexp}/; # untaint
+ if (! defined $file_untainted) {
+ warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
+ }
+ return ($file_untainted, $page);
+}
+
sub find_src_files () {
my @files;
my %pages;
@@ -289,58 +309,38 @@ sub find_src_files () {
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;
+ my ($file, $page) = verify_src_file(decode_utf8($_), $config{srcdir});
+ if (defined $file) {
+ push @files, $file;
if ($pages{$page}) {
debug(sprintf(gettext("%s has multiple possible source pages"), $page));
}
$pages{$page}=1;
}
+ else {
+ $File::Find::prune=1;
+ }
},
}, $config{srcdir});
foreach my $dir (@{$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 {
+ my ($file, $page) = verify_src_file(decode_utf8($_), $dir);
+ if (defined $file) {
# avoid underlaydir override
# attacks; see security.mdwn
- if (! -l "$config{srcdir}/$f" &&
+ if (! -l "$config{srcdir}/$file" &&
! -e _) {
if (! $pages{$page}) {
- push @files, $f;
+ push @files, $file;
$pages{$page}=1;
}
}
}
+ else {
+ $File::Find::prune=1;
+ }
},
}, $dir);
};
@@ -686,6 +686,37 @@ sub render_backlinks ($) {
}
}
+sub gen_autofile ($$$) {
+ my $autofile=shift;
+ my $pages=shift;
+ my $del=shift;
+
+ if (srcfile($autofile, 1)) {
+ return 0;
+ }
+
+ my ($file, $page) = verify_src_file("$config{srcdir}/$autofile", $config{srcdir});
+
+ if ((!defined $file) ||
+ (exists $wikistate{$autofiles{$autofile}{plugin}}{autofile_deleted})) {
+ return 0;
+ }
+
+ if ($pages->{$page}) {
+ return 0;
+ }
+
+ if (grep { $_ eq $file } @$del) {
+ $wikistate{$autofiles{$autofile}{generator}}{autofile_deleted}=1;
+ return 0;
+ }
+
+ $autofiles{$autofile}{generator}->();
+ $pages->{$page}=1;
+ return 1;
+}
+
+
sub refresh () {
srcdir_check();
run_hooks(refresh => sub { shift->() });
@@ -700,6 +731,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/doc/plugins/write.mdwn b/doc/plugins/write.mdwn
index 0bf6fcf48..5190a26ed 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 add, 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/templates/autotag.tmpl b/templates/autotag.tmpl
new file mode 100644
index 000000000..a8824171b
--- /dev/null
+++ b/templates/autotag.tmpl
@@ -0,0 +1,3 @@
+## Pages tagged <TMPL_VAR TAG> ##
+
+[[!inline pages="tagged(<TMPL_VAR TAG>)" actions="no" archive="yes"]]