1 This is another blogging support thing, and it relies on
2 [[pagespec_relative_to_a_target]] (but only to figure out whether a given page
3 has a child). Basically, you give it a page called missingparents.mdwn,
7 [[missingparents pages="posts/* and !posts/*/*" generate="""[[template id=year text="$page"]]"""]]
8 [[missingparents pages="posts/*/* and !posts/*/*/*" generate="""[[template id=month text="$page"]]"""]]
9 [[missingparents pages="posts/*/*/* and !posts/*/*/*/*" generate="""[[template id=day text="$page"]]"""]]
12 And it scans the whole wiki for pages that match the pagespecs but are missing
13 parents. If any are found, they are generated automatically using the text in
14 the "generate" parameter (except $page is substituted for the page title).
15 *These generated pages aren't kept in version control*, but of course they're
16 ordinary wiki pages and can be edited by the web form or otherwise added, at
17 which point the missingparents plugin lets go of them. (TODO: CGI.pm needs to
18 know to rcs_add these pages if they are edited, and it doesn't.) If all of the
19 children of a missingparent page goes away, the missingparent itself is
20 unlinked automatically, and all missingparents are deleted on wiki rebuild.
22 To implement this, I needed to tell ikiwiki that pages were being added and
23 removed in a non-standard way, and so created functions newpage and delpage
24 in the IkiWiki namespace to do these things. delpage is modeled on the
25 Render.pm code that deletes pages, so I re-used it in Render.pm. I also
26 needed a way to add files to be deleted on a refresh(), so I added a
27 needsdelete hook, parallel in form to needsbuild.
29 This patch, or one like it, would enable better blogging support, by adding
30 the ability to hierarchically organize blog posts and automatically generate
31 structural pages for year, month, or day. Please apply. --Ethan
34 Index: IkiWiki/Render.pm
35 ===================================================================
36 --- IkiWiki/Render.pm (revision 3926)
37 +++ IkiWiki/Render.pm (working copy)
39 if (! $exists{$page}) {
40 debug(sprintf(gettext("removing old page %s"), $page));
41 push @del, $pagesources{$page};
43 - $renderedfiles{$page}=[];
44 - $pagemtime{$page}=0;
45 - prune($config{destdir}."/".$_)
46 - foreach @{$oldrenderedfiles{$page}};
47 - delete $pagesources{$page};
48 - foreach (keys %destsources) {
49 - if ($destsources{$_} eq $page) {
50 - delete $destsources{$_};
62 + run_hooks(needsdelete => sub { shift->(\@del) });
65 if (%rendered || @del) {
66 # rebuild dependant pages
67 foreach my $f (@files) {
68 Index: IkiWiki/Plugin/missingparents.pm
69 ===================================================================
70 --- IkiWiki/Plugin/missingparents.pm (revision 0)
71 +++ IkiWiki/Plugin/missingparents.pm (revision 0)
74 +# missingparents plugin: detect missing parents of pages and create them
75 +package IkiWiki::Plugin::missingparents;
80 +use IkiWiki::Plugin::relative;
86 + hook(type => "checkconfig", id => "missingparents", call => \&checkconfig);
87 + hook(type => "needsdelete", id => "missingparents", call => \&needsdelete);
88 + hook(type => "needsbuild", id => "missingparents", call => \&needsbuild);
89 + hook(type => "savestate", id => "missingparents", call => \&savestate);
90 + hook(type => "preprocess", id => "missingparents", call => \&preprocess_missingparents);
93 +sub checkconfig () { #{{{
94 + IkiWiki::preprocess("missingparents", "missingparents",
95 + readfile(srcfile("missingparents.mdwn")));
97 + if ($config{rebuild}){
98 + foreach my $file (keys %ownfiles) {
99 + unlink $config{srcdir}.'/'.$file;
104 +sub preprocess_missingparents (@) { #{{{
107 + if (! defined $params{pages} || ! defined $params{generate}) {
108 + return "[[missingparents ".gettext("missing pages or generate parameter")."]]";
111 + push @pagespecs, \%params;
113 + #translators: This is used to display what missingparents are defined.
114 + #translators: First parameter is a pagespec, the second
115 + #translators: is text for pages that match that pagespec.
116 + return sprintf(gettext("missingparents in %s will be %s"),
117 + '`'.$params{pages}.'`', '`\\'.$params{generate}.'`');
121 +sub loadstate() { #{{{
122 + my $filename = "$config{wikistatedir}/missingparents";
123 + if (-e $filename) {
124 + open (IN, $filename) ||
125 + die "$filename: $!";
137 +sub savestate() { #{{{
138 + my $filename = "$config{wikistatedir}/missingparents.new";
139 + my $cleanup = sub { unlink ($filename) };
140 + open (OUT, ">$filename") || error("open $filename: $!", $cleanup);
141 + foreach my $data (keys %ownfiles) {
142 + print OUT "$data\n" if $ownfiles{$data};
144 + rename($filename, "$config{wikistatedir}/missingparents") ||
145 + error("rename $filename: $!", $cleanup);
148 +sub needsdelete (@) { #{{{
155 + foreach my $file (keys %ownfiles) {
156 + my $page = pagename($file);
157 + if (! IkiWiki::PageSpec::match_has_child($page, "")) {
158 + # No children -- get rid of it
159 + push @mydel, $page;
160 + delete $ownfiles{$file};
161 + IkiWiki::delpage($page);
162 + unlink $config{srcdir}."/".$file;
167 + foreach my $page (@mydel){
168 + push @{$files}, $page;
172 +sub check_matches($) { #{{{
174 + return if $IkiWiki::pagesources{$page};
176 + foreach my $miss (@pagespecs) {
177 + next unless pagespec_match($page, $miss->{pages});
178 + my $text = $miss->{generate};
179 + $text =~ s/\$page/$page/;
180 + my $output = $page.".mdwn";
181 + writefile($output, "$config{srcdir}/", $text);
182 + IkiWiki::newpage($output, $page);
188 +sub needsbuild ($) { #{{{
192 + foreach my $file (@{$files}) {
193 + if ($ownfiles{$file}) {
194 + # someone edited our file, making it the
196 + delete $ownfiles{$file};
199 + my $page = pagename $file;
201 + foreach my $parent (split '/', $page) {
202 + $newfile .= $parent;
203 + my $output = check_matches($newfile);
204 + push @new, $output if $output;
208 + foreach my $file (@new) {
209 + $ownfiles{$file} = 1;
210 + push @{$files}, $file;
216 ===================================================================
217 --- IkiWiki.pm (revision 3926)
218 +++ IkiWiki.pm (working copy)
220 use Exporter q{import};
221 our @EXPORT = qw(hook debug error template htmlpage add_depends pagespec_match
222 bestlink htmllink readfile writefile pagetype srcfile pagename
223 - displaytime will_render gettext urlto targetpage
224 + displaytime will_render gettext urlto targetpage newpage delpage
225 %config %links %renderedfiles %pagesources %destsources);
226 our $VERSION = 2.00; # plugin interface version, next is ikiwiki version
227 our $version='unknown'; # VERSION_AUTOREPLACE done by Makefile, DNE
229 error("failed renaming $newfile to $destdir/$file: $!", $cleanup);
232 +sub newpage($$) { #{{{
236 + $pagemtime{$page} = $pagectime{$page} = time;
237 + $pagesources{$page} = $file;
238 + $pagecase{lc $page} = $page;
241 +sub delpage($) { #{{{
244 + $renderedfiles{$page}=[];
245 + $pagemtime{$page}=0;
246 + prune($config{destdir}."/".$_)
247 + foreach @{$oldrenderedfiles{$page}};
248 + delete $pagesources{$page};
249 + foreach (keys %destsources) {
250 + if ($destsources{$_} eq $page) {
251 + delete $destsources{$_};
257 sub will_render ($$;$) { #{{{