]> scripts.mit.edu Git - www/ikiwiki.git/blob - doc/todo/Add_a_plugin_to_list_available_pre-processor_commands.mdwn
response
[www/ikiwiki.git] / doc / todo / Add_a_plugin_to_list_available_pre-processor_commands.mdwn
1 I've found myself wanting to know which [[plugins]] are switched on so I know which pre-processor commands I can use.  The attached [[patch]] adds a new plugin that generates the list of available plugins. -- [[Will]]
2
3 > Good idea, I do see a few problems:
4
5 > - preprocessor directives do not necessarily have the same name as the
6 >   plugin that contains them (for example, the graphviz plugin adds a graph
7 >   directive). Won't keys `%{IkiWiki::hooks{preprocess}}` work?
8
9 >>> Er, yeah - that's a much better solution. :) -- and done
10
11 > - "listplugins" is a bit misnamed since it only does preprocessor directives.
12
13 >>> Yes.  Initially this was going to list all enabled plugins.  Then when searching
14 >>> for enabled plugins I changed my mind and decided that a list of pre-processor
15 >>> directives was more useful.  I'll fix that too. -- changed to `listpreprocessors`
16
17 > - comment was copied from version plugin and still mentions version :-)
18
19 >>> :-) -- fixed
20
21 > - Seems like [[ikiwiki/formatting]] could benefit from including the
22 >   list.. however, just a list of preprocessor directive names is not
23 >   the most user-friendly thing that could be put on that page. It would
24 >   be nice if there were also a short description and maybe an example of
25 >   use. Seems like the place to include that info would be in the call
26 >   to `hook()`.
27 >   (Maybe adding that is more involved than you want to go though..)
28
29 > --[[Joey]]
30
31 >> Adding a whole new hook for a usage example is more effort than I
32 >> wanted to go to.  I was thinking of either:
33
34 >>> Just to clarify, I meant adding new parameters to the same hook call
35 >>> that registers the plugin. --[[Joey]]
36
37 >>    - Adding a configuration for a wiki directory.  If a matching page is in the
38 >>      specified wiki directory then the plugin name gets turned into a link to that
39 >>      page
40 >>    - Adding configuration for an external URL.  Each plugin name is added as
41 >>       a link to the plugin name appended to the URL.
42
43 >>The first option is easier to navigate and wouldn't produce broken links,
44 >>but requires all the plugin documentation to be local.  The second option
45 >>can link back to the main IkiWiki site, but if you have any non-standard
46 >>plugins then you'll get broken links.
47 >>
48 >>Hrm.  After listing all of that, maybe your idea with the hooks is the better
49 >>solution.  I'll think about it some more. -- [[Will]]
50
51 >>> I've also run into this problem with the websetup plugin, and
52 >>> considered those ideas too. I don't like the external url, because
53 >>> ikiwiki.info may be out of sync with the version of ikiwiki being used.
54 >>> (Or maybe it's gone! :-) The first idea is fine, except for the bloat
55 >>> issue. If turning on listpreprocessors and/or websetup means adding
56 >>> hundreds of pages (and of kilobytes) to your wiki, that could be an
57 >>> incentive to not turn them on..
58 >>>
59 >>> Hmm.. maybe the thing to do is to use _internal pages for the plugins;
60 >>> then the individual pages would not be rendered, and your inlines would
61 >>> still work. Although I don't know how websetup would use it then, and
62 >>> also they would have to be non-internal for ikiwiki's own docwiki. Hmm.
63 >>> Maybe these are two different things; one is a set of pages describing
64 >>> preprocessor directives, and the second a set of pages describing
65 >>> plugins. They're so closely related though it seems a shame to keep
66 >>> them separate..
67 >>> --[[Joey]]
68
69 >>> I started implementing the hook based solution, and decided I didn't like
70 >>> it because there was no nice way to rebuild pages when the preprocessor
71 >>> descriptions changed.  So instead I assumed that the the [[plugins]] pages
72 >>> would be moved into the underlay directory.  This plugin then uses an
73 >>> `inline` directive to include those pages.  You can use the `inline`
74 >>> parameter to decide if you want to include all the descriptions or
75 >>> just the titles.  There is also an option to auto-create default/blank
76 >>> description pages if they are missing (from a template).  As preprocessor
77 >>> commands don't list unless they have a description page, auto-creation
78 >>> is enabled by default.
79 >>>
80 >>>  There are three new templates that are needed.  These are for:
81 >>>
82 >>>  - The auto-created description pages are generated from `preprocessor-description.tmpl`.
83 >>>  - When only pre-processor names are listed, the `listpreprocessors-listonly.tmpl` template is used.
84 >>>  - When pre-processor descriptions are included inline, the `listpreprocessors-inline.tmpl` template is used.
85 >>>
86 >>> -- [[Will]]
87
88 >>>> Just a quick note: pages are only created for pre-processor commands
89 >>>> that exist when the `refresh` hook is called.  This is before the [[shortcuts]] are
90 >>>> processed.  However, the list of available pre-processor commands will include
91 >>>> shortcuts if they have description pages (the list is generated later, after the
92 >>>> shortcuts have been added).  While this was unplanned, it seems a reasonable
93 >>>> tradeoff between including all the large number of shortcuts and including none. -- [[Will]]
94
95 >>>>>> I think that using an inline is elegant! However, I don't understand
96 >>>>>> why it has to create stub description pages? I doubt that, if a
97 >>>>>> directive is missing a page, the stub will be filled out in many
98 >>>>>> wikis. And it adds a lot of complexity, particularly committing a
99 >>>>>> bunch of generated pages to revision control when the user just
100 >>>>>> wants a plugin list seems undesirable.
101 >>>>>>
102 >>>>>> Seems to me it could use the inline for pages that exist, and append
103 >>>>>> to the bottom a generated text for anything that is currently missing.
104 >>>>>> The generated text could even have a page creation link in it if
105 >>>>>> you wanted.
106 >>>>>> --[[Joey]]
107
108 >>>>>>> I kinda agree about the page generation.  I don't like mixing an
109 >>>>>>> inlined and a list though.  Besides which, that ends
110 >>>>>>> up keeping much of complexity of the page generation because
111 >>>>>>> the code still has to detect which pages are missing.  I've added
112 >>>>>>> a patch that uses a list of wikilinks instead.  This way available
113 >>>>>>> pages get linked correctly, and missing pages get normal creation
114 >>>>>>> links.  The old patch is still here if you decide you prefer that. -- [[Will]]
115
116 >>>>>>>> Can you explain the full/early list (why track both?) and generated parameter?
117 >>>>>>>> Only code change I'd suggest is using `htmllink` rather than 
118 >>>>>>>> generating a wikilink.
119
120     #!/usr/bin/perl
121     # Ikiwiki listpreprocessors plugin.
122     package IkiWiki::Plugin::listpreprocessors;
123     
124     use warnings;
125     use strict;
126     use IkiWiki 2.00;
127     
128     sub import { #{{{
129         hook(type => "getsetup", id => "listpreprocessors", call => \&getsetup);
130         hook(type => "checkconfig", id => "listpreprocessors", call => \&checkconfig);
131         hook(type => "needsbuild", id => "listpreprocessors", call => \&needsbuild);
132         hook(type => "preprocess", id => "listpreprocessors", call => \&preprocess);
133     } # }}}
134     
135     sub getsetup () { #{{{
136         return
137                 plugin => {
138                         safe => 1,
139                         rebuild => undef,
140                 },
141                 preprocessor_description_dir => {
142                         type => "string",
143                         description => "The ikiwiki directory that contains plugin descriptions.",
144                         safe => 1,
145                         rebuild => 1,
146                 },
147     } #}}}
148     
149     my @fullPluginList;
150     my @earlyPluginList;
151     my $pluginString;
152     
153     sub checkconfig () { #{{{
154         if (!defined $config{plugin_description_dir}) {
155             $config{plugin_description_dir} = "ikiwiki/plugin/";
156         }
157     
158         @earlyPluginList = sort( keys %{ $IkiWiki::hooks{preprocess} } );
159     } #}}}
160     
161     sub needsbuild (@) { #{{{
162         my $needsbuild=shift;
163     
164         @fullPluginList = sort( keys %{ $IkiWiki::hooks{preprocess} } );
165         $pluginString = join (' ', @earlyPluginList) . " : ". join (' ', @fullPluginList);
166     
167         foreach my $page (keys %pagestate) {
168                 if (exists $pagestate{$page}{listpreprocessors}{shown}) {
169                         if ($pagestate{$page}{listpreprocessors}{shown} ne $pluginString) {
170                                 push @$needsbuild, $pagesources{$page};
171                         }
172                         if (exists $pagesources{$page} &&
173                                         grep { $_ eq $pagesources{$page} } @$needsbuild) {
174                                 # remove state, will be re-added if
175                                 # the [[!listpreprocessors]] is still there during the
176                                 # rebuild
177                                 delete $pagestate{$page}{listpreprocessors}{shown};
178                         }
179                 }
180         }
181     } # }}}
182     
183     sub preprocess (@) { #{{{
184         my %params=@_;
185         
186         $pagestate{$params{destpage}}{listpreprocessors}{shown}=$pluginString;
187         
188         my @pluginlist;
189         
190         if (! defined $params{generated}) {
191                 @pluginlist = @fullPluginList;
192         } else {
193                 @pluginlist = @earlyPluginList;
194         }
195         
196         my $result = '<ul class="listpreprocessors">';
197         
198         foreach my $plugin (@pluginlist) {
199                 $result .= '<li class="listpreprocessors">[[' . $config{plugin_description_dir} . $plugin . ']]</li>';
200         }
201         
202         $result .= "</ul>";
203         
204         return IkiWiki::preprocess($params{page}, $params{destpage}, 
205                 IkiWiki::filter($params{page}, $params{destpage}, $result));
206     } # }}}
207     
208     1
209
210 ----
211
212 Here is the main listpreprocessors plugin. (Note, because this has double
213 square brackets in the source, it isn't quite displaying correctly - look
214 at the page source for details.)  New template files follow:
215
216     #!/usr/bin/perl
217     # Ikiwiki listpreprocessors plugin.
218     package IkiWiki::Plugin::listpreprocessors;
219     
220     use warnings;
221     use strict;
222     use Encode;
223     use IkiWiki 2.00;
224     
225     sub import { #{{{
226         hook(type => "getsetup", id => "listpreprocessors", call => \&getsetup);
227         hook(type => "preprocess", id => "listpreprocessors", call => \&preprocess);
228         hook(type => "refresh", id => "listpreprocessors", call => \&refresh);
229     } # }}}
230     
231     sub getsetup () { #{{{
232         return
233                 plugin => {
234                         safe => 1,
235                         rebuild => undef,
236                 },
237                 preprocessor_description_dir => {
238                         type => "string",
239                         description => "The ikiwiki directory that contains plugin descriptions.",
240                         safe => 1,
241                         rebuild => 1,
242                 },
243                 preprocessor_description_autocreate => {
244                         type => "boolean",
245                         description => "Should pre-processor command descriptions be automatically created from a template.",
246                         safe => 1,
247                         rebuild => 1,
248                 },
249     } #}}}
250     
251     sub gendescription ($$) { #{{{
252         my $plugin=shift;
253         my $page=shift;
254         my $file=$page.".".$config{default_pageext};
255         my $template=template("preprocessor-description.tmpl");
256         $template->param(page => $page, plugin => $plugin);
257         writefile($file, $config{srcdir}, $template->output);
258         if ($config{rcs}) {
259                 IkiWiki::rcs_add($file);
260         }
261     } #}}}
262     
263     sub refresh () { #{{{
264         eval q{use File::Find};
265         error($@) if $@;
266     
267         if (defined $config{preprocessor_description_autocreate} && ! $config{preprocessor_description_autocreate}) {
268                 return; # create pages unless they explicitly ask us not to
269         }
270     
271         if (!defined $config{preprocessor_description_dir}) {
272                 $config{preprocessor_description_dir} = "ikiwiki/plugin/";
273         }
274         
275         my @pluginlist = sort( keys %{ $IkiWiki::hooks{preprocess} } );
276         my %pluginpages;
277     
278         if (@pluginlist) {
279                 my ($plugin,$page);
280                 
281                 foreach $plugin (@pluginlist) {
282                         $pluginpages{$plugin} = $config{preprocessor_description_dir} . $plugin;
283                 }
284     
285                 my %pages;
286                 foreach my $dir ($config{srcdir}, @{$config{underlaydirs}}, $config{underlaydir}) {
287                         find({
288                                 no_chdir => 1,
289                                 wanted => sub {
290                                         $_=decode_utf8($_);
291                                         if (IkiWiki::file_pruned($_, $dir)) {
292                                                 $File::Find::prune=1;
293                                         }
294                                         elsif (! -l $_) {
295                                                 my ($f)=/$config{wiki_file_regexp}/; # untaint
296                                                 return unless defined $f;
297                                                 $f=~s/^\Q$dir\E\/?//;
298                                                 return unless length $f;
299                                                 return if $f =~ /\._([^.]+)$/; # skip internal page
300                                                 if (! -d _) {
301                                                         $pages{pagename($f)}=$f;
302                                                 }
303                                         }
304                                 }
305                         }, $dir);
306                 }
307     
308                 if ($config{rcs}) {
309                         IkiWiki::disable_commit_hook();
310                 }
311                 
312                 my $needcommit = 0;
313                 
314                 while (($plugin,$page) = each %pluginpages) {
315                         if (! exists $pages{$page}) {
316                                 $needcommit = 1;
317                                 gendescription($plugin,$page);
318                         }
319                 }
320                 
321                 if ($config{rcs}) {
322                         if ($needcommit) {
323                                 IkiWiki::rcs_commit_staged(
324                                         gettext("automatic pre-processor description generation"),
325                                         undef, undef);
326                         }
327                         IkiWiki::enable_commit_hook();
328                 }
329         }
330     } #}}}
331     
332     sub preprocess (@) { #{{{
333         my %params=@_;
334         
335         if (!defined $config{plugin_description_dir}) {
336                 $config{plugin_description_dir} = "ikiwiki/plugin/";
337         }
338         
339         my @pluginlist = sort( keys %{ $IkiWiki::hooks{preprocess} } );
340         foreach my $plugin (@pluginlist) {
341                 $plugin = $config{plugin_description_dir} . $plugin;
342         }
343         my $pluginString = join (' or ', @pluginlist);
344         
345         my $result = "[[!inline pages=\"$pluginString\" feeds=\"no\" show=0 sort=\"title\"";
346         
347         if (defined $params{inline}) {
348                 $result .= ' template=\"listpreprocessors-listonly\" archive="yes"';
349         } else {
350                 $result .= ' template=\"listpreprocessors-inline\" archive="no"';
351         }
352         
353         $result .= "]]";
354         
355         return IkiWiki::preprocess($params{page}, $params{destpage}, 
356                 IkiWiki::filter($params{page}, $params{destpage}, $result));
357     } # }}}
358     
359     1
360
361 --------
362
363 This is what I was using for `listpreprocessors-inline.tmpl`:
364
365     <div class="listpreprocessorsinline">
366     
367     <div class="inlineheader">
368     
369     <span class="header">
370     <a href="<TMPL_VAR PAGEURL>"><TMPL_VAR TITLE></a>
371     </span>
372     
373     </div><!--.inlineheader-->
374     
375     <div class="inlinecontent">
376     <TMPL_VAR CONTENT>
377     </div><!--.inlinecontent-->
378     
379     </div><!--.listpreprocessorsinline-->
380
381 --------
382
383 This is what I was using for `listpreprocessors-listonly.tmpl`:
384
385     <p class="listpreprocessors"><a href="<TMPL_VAR PAGEURL>"><TMPL_VAR TITLE></a></p>
386
387 --------
388
389 This is what I was using for `preprocessor-description.tmpl`:
390
391     The <TMPL_VAR plugin> preprocessor command currently has no description.
392     
393     Maybe you should edit this page to add one.