aboutsummaryrefslogtreecommitdiff
path: root/doc/forum/inject__95__preprocess__95__tag.mdwn
blob: 2b41b5b762dbd5e39d6533bed02a64f4a92ae014 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
[[!meta title="Cannot manage to inject preprocess_tag"]]

Hello,    
I am trying to write a plugin that changes the way the
[[ikiwiki/directive/tag]] [[ikiwiki/directive]] works, and I am trying to do so
by using the [[inject|plugins/write/#index81h3]] function. The piece of code
that should (if I understood well the `inject` function) do the trick is :

      sub import {
        inject(
          name => 'IkiWiki::Plugin::tag::preprocess_tag',
          call => \&my_preprocess_tag
        );
      }

Howere, this does not change anything about the effect of the `tag` directive.

I have tried some variants, like calling `inject` outside the `import`
function, or calling `IkiWiki::loadplugin("tag");` to ensure that the
[[plugins/tag]] is loaded, but none of these things work. Any idea?

*Disclaimer:* although proficient in several languages, I am a beginner in Perl.

Here is the full code of (a very early version of) my plugin.

    #! /usr/bin/perl
    require 5.002;
    package IkiWiki::Plugin::parenttag;

    use warnings;
    use strict;
    use IkiWiki 3.00;

    my $orig_preprocess_tag=\&preprocess_tag;

    sub import {
      inject(
        name => 'IkiWiki::Plugin::tag::preprocess_tag',
        call => \&my_preprocess_tag
      );
    }

    sub my_preprocess_tag(@) {
      print "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\nWorking!\n";
      return "TODO";
    }

    1

-- [[Louis|spalax]]

> Hello,    
> I managed to replace the tag original `preprocess_tag` function, using a different approach than using `inject`:
> 
>     my $orig_preprocess_tag;
> 
>     sub import {
>       IkiWiki::loadplugin("tag");
>       $orig_preprocess_tag = \&{$IkiWiki::hooks{preprocess}{tag}{call}};
>       hook(type => "preprocess", id => "tag", call => \&my_preprocess_tag);
>     }
> 
> And later on, I can call the original `preprocess_tag` function using:
> 
>     $orig_preprocess_tag->(...)
> 
> 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`.
> 
> Two questions:
> 
> - how ugly is my solution?
> - is it possible to use `inject` to replace the `IkiWiki::Plugin::tag::preprocess_tag` function?
> 
> -- [[Louis|spalax]]