]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - doc/forum/inject__95__preprocess__95__tag.mdwn
(no commit message)
[git.ikiwiki.info.git] / doc / forum / inject__95__preprocess__95__tag.mdwn
1 [[!meta title="Cannot manage to inject preprocess_tag"]]
3 Hello,    
4 I am trying to write a plugin that changes the way the
5 [[ikiwiki/directive/tag]] [[ikiwiki/directive]] works, and I am trying to do so
6 by using the [[inject|plugins/write/#index81h3]] function. The piece of code
7 that should (if I understood well the `inject` function) do the trick is :
9       sub import {
10         inject(
11           name => 'IkiWiki::Plugin::tag::preprocess_tag',
12           call => \&my_preprocess_tag
13         );
14       }
16 Howere, this does not change anything about the effect of the `tag` directive.
18 I have tried some variants, like calling `inject` outside the `import`
19 function, or calling `IkiWiki::loadplugin("tag");` to ensure that the
20 [[plugins/tag]] is loaded, but none of these things work. Any idea?
22 *Disclaimer:* although proficient in several languages, I am a beginner in Perl.
24 Here is the full code of (a very early version of) my plugin.
26     #! /usr/bin/perl
27     require 5.002;
28     package IkiWiki::Plugin::parenttag;
30     use warnings;
31     use strict;
32     use IkiWiki 3.00;
34     my $orig_preprocess_tag=\&preprocess_tag;
36     sub import {
37       inject(
38         name => 'IkiWiki::Plugin::tag::preprocess_tag',
39         call => \&my_preprocess_tag
40       );
41     }
43     sub my_preprocess_tag(@) {
44       print "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nWorking!\n";
45       return "TODO";
46     }
48     1
50 -- [[Louis|spalax]]
52 > Hello,    
53 > I managed to replace the tag original `preprocess_tag` function, using a different approach than using `inject`:
54
55 >     my $orig_preprocess_tag;
56
57 >     sub import {
58 >       IkiWiki::loadplugin("tag");
59 >       $orig_preprocess_tag = \&{$IkiWiki::hooks{preprocess}{tag}{call}};
60 >       hook(type => "preprocess", id => "tag", call => \&my_preprocess_tag);
61 >     }
62
63 > And later on, I can call the original `preprocess_tag` function using:
64
65 >     $orig_preprocess_tag->(...)
66
67 > The problem is that I am digging into `IkiWiki.pm` package to extract data from `IkiWiki::hooks`, which is not guaranteed to work in the future, contrary to `inject`.
68
69 > Two questions:
70
71 > - how ugly is my solution?
72 > - is it possible to use `inject` to replace the `IkiWiki::Plugin::tag::preprocess_tag` function?
73
74 > -- [[Louis|spalax]]