]> sipb.mit.edu Git - ikiwiki.git/blob - doc/todo/missingparents.pm.mdwn
web commit by http://madduck.net/
[ikiwiki.git] / doc / todo / missingparents.pm.mdwn
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,
4 something like this:
5
6 <pre>
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"]]"""]]
10 </pre>
11
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.
21
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.
28
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
32
33 <pre>
34 Index: IkiWiki/Render.pm
35 ===================================================================
36 --- IkiWiki/Render.pm   (revision 3926)
37 +++ IkiWiki/Render.pm   (working copy)
38 @@ -322,17 +322,7 @@
39                 if (! $exists{$page}) {
40                         debug(sprintf(gettext("removing old page %s"), $page));
41                         push @del, $pagesources{$page};
42 -                       $links{$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{$_};
51 -                               }
52 -                       }
53 +                       delpage($page);
54                 }
55         }
56  
57 @@ -377,6 +367,10 @@
58                 }
59         }
60  
61 +       if (@del) {
62 +               run_hooks(needsdelete => sub { shift->(\@del) });
63 +       }
64 +
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)
72 @@ -0,0 +1,142 @@
73 +#!/usr/bin/perl
74 +# missingparents plugin: detect missing parents of pages and create them
75 +package IkiWiki::Plugin::missingparents;
76 +
77 +use warnings;
78 +use strict;
79 +use IkiWiki 2.00;
80 +use IkiWiki::Plugin::relative;
81 +
82 +my %ownfiles;
83 +my @pagespecs;
84 +
85 +sub import { #{{{
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);
91 +} # }}}
92 +
93 +sub checkconfig () { #{{{
94 +       IkiWiki::preprocess("missingparents", "missingparents",
95 +               readfile(srcfile("missingparents.mdwn")));
96 +       loadstate();
97 +       if ($config{rebuild}){
98 +               foreach my $file (keys %ownfiles) {
99 +                       unlink $config{srcdir}.'/'.$file;
100 +               }
101 +       }
102 +} #}}}
103 +
104 +sub preprocess_missingparents (@) { #{{{
105 +       my %params=@_;
106 +
107 +       if (! defined $params{pages} || ! defined $params{generate}) {
108 +               return "[[missingparents ".gettext("missing pages or generate parameter")."]]";
109 +       }
110 +
111 +       push @pagespecs, \%params;
112 +
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}.'`');
118 +} # }}}
119 +
120 +my $state_loaded=0;
121 +sub loadstate() { #{{{
122 +       my $filename = "$config{wikistatedir}/missingparents";
123 +       if (-e $filename) {
124 +               open (IN, $filename) ||
125 +                     die "$filename: $!";
126 +               while (<IN>) {
127 +                       chomp;
128 +                       $ownfiles{$_} = 1;
129 +               }
130 +
131 +               close IN;
132 +
133 +               $state_loaded=1;
134 +       }
135 +} #}}}
136 +
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};
143 +       }
144 +       rename($filename, "$config{wikistatedir}/missingparents") ||
145 +               error("rename $filename: $!", $cleanup);
146 +} #}}}
147 +
148 +sub needsdelete (@) { #{{{
149 +       my $files=shift;
150 +       
151 +       my @mydel;
152 +       my $pruned = 1;
153 +       do {
154 +               $pruned = 0;
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;
163 +                               $pruned = 1;
164 +                       }
165 +               }
166 +       } while($pruned);
167 +       foreach my $page (@mydel){
168 +               push @{$files}, $page;
169 +       }
170 +} #}}}
171 +
172 +sub check_matches($) { #{{{
173 +       my $page = shift;
174 +       return if $IkiWiki::pagesources{$page};
175 +
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);
183 +               return $output;
184 +       }
185 +       return "";
186 +} #}}}
187 +
188 +sub needsbuild ($) { #{{{
189 +       my $files=shift;
190 +       my @new;
191 +
192 +       foreach my $file (@{$files}) {
193 +               if ($ownfiles{$file}) {
194 +                       # someone edited our file, making it the
195 +                       # user's problem
196 +                       delete $ownfiles{$file};
197 +                       next;
198 +               }
199 +               my $page = pagename $file;
200 +               my $newfile = "";
201 +               foreach my $parent (split '/', $page) {
202 +                       $newfile .= $parent;
203 +                       my $output = check_matches($newfile);
204 +                       push @new, $output if $output;
205 +                       $newfile .= "/";
206 +               }
207 +       }
208 +       foreach my $file (@new) {
209 +               $ownfiles{$file} = 1;
210 +               push @{$files}, $file;
211 +       }
212 +} #}}}
213 +
214 +1
215 Index: IkiWiki.pm
216 ===================================================================
217 --- IkiWiki.pm  (revision 3926)
218 +++ IkiWiki.pm  (working copy)
219 @@ -16,7 +16,7 @@
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
228 @@ -330,6 +336,30 @@
229                 error("failed renaming $newfile to $destdir/$file: $!", $cleanup);
230  } #}}}
231  
232 +sub newpage($$) { #{{{
233 +       my $file=shift;
234 +       my $page=shift;
235 +
236 +       $pagemtime{$page} = $pagectime{$page} = time;
237 +       $pagesources{$page} = $file;
238 +       $pagecase{lc $page} = $page;
239 +} #}}}
240 +
241 +sub delpage($) { #{{{
242 +       my $page=shift;
243 +       $links{$page}=[];
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{$_};
252 +                       }
253 +               }
254 +} #}}}
255 +
256  my %cleared;
257  sub will_render ($$;$) { #{{{
258         my $page=shift;
259 </pre>
260
261 [[tag patch]]