]> sipb.mit.edu Git - ikiwiki.git/blob - doc/todo/auto-create_tag_pages_according_to_a_template.mdwn
tagging (to find this page easier when studying ikiwiki)
[ikiwiki.git] / doc / todo / auto-create_tag_pages_according_to_a_template.mdwn
1 [[!tag tags]]
2
3 It would be great if I could tell ikiwiki to automatically instantiate pages for each [[tag|directive/tag]], according to a template, especially when `$tagbase` is set.
4
5 Tags are mainly specific to the object to which they’re stuck. However, I often use them the other way around, too: as concepts. And sometimes I’d like to see all pages related to a given concept (“tagged with a given tag”). The only way to do this with ikiwiki is to instantiate a page for each tag and slap a map on it. This is quite tedious and I’d really love to see Ikiwiki do so by default for all tags.
6
7 Also see: <http://madduck.net/blog/2008.01.06:new-blog/> and <http://users.itk.ppke.hu/~cstamas/code/ikiwiki/autocreatetagpage/>
8
9 [[!tag wishlist]]
10
11 I would love to see this as well. -- dato
12
13 ---
14
15 I have create a patch to [[tag.pm|plugins/tag]] for add the option for auto create tag pages.
16 A new setting is used to enable or disable auto-create tag pages, `tag_autocreate`.
17 The new tag file is created during the preprocess phase. 
18 The new tag file is then complied during the change phase.
19
20 _tag.pm from version 3.01_
21
22
23         --- tag.pm      2009-02-06 10:26:03.000000000 -0700
24         +++ tag_new.pm  2009-02-06 12:17:19.000000000 -0700
25         @@ -14,6 +14,7 @@
26                         hook(type => "preprocess", id => "tag", call => \&preprocess_tag, scan => 1);
27                         hook(type => "preprocess", id => "taglink", call => \&preprocess_taglink, scan => 1);
28                         hook(type => "pagetemplate", id => "tag", call => \&pagetemplate);
29         +       hook(type => "change", id => "tag", call => \&change);
30          }
31          
32          sub getopt () {
33         @@ -36,6 +37,36 @@
34                                                         safe => 1,
35                                                         rebuild => 1,
36                                         },
37         +               tag_autocreate => {
38         +                       type => "boolean",
39         +                       example => 0,
40         +                       description => "Auto-create the new tag pages, uses autotagpage.tmpl ",
41         +                       safe => 1,
42         +                       rebulid => 1,
43         +               },
44         +}
45         +
46         +my $autocreated_page = 0;
47         +
48         +sub gen_tag_page($)    {
49         +       my $tag=shift;
50         +
51         +       my $tag_file=$tag.'.'.$config{default_pageext};
52         +       return if (-f $config{srcdir}.$tag_file);
53         +
54         +       my $template=template("autotagpage.tmpl");
55         +       $template->param(tag => $tag);
56         +       writefile($tag_file, $config{srcdir}, $template->output);
57         +       $autocreated_page = 1;
58         +
59         +       if ($config{rcs}) {
60         +               IkiWiki::disable_commit_hook();
61         +               IkiWiki::rcs_add($tag_file);
62         +               IkiWiki::rcs_commit_staged(
63         +                       gettext("Automatic tag page generation"),
64         +                       undef, undef);
65         +               IkiWiki::enable_commit_hook();
66         +       }
67          }
68          
69          sub tagpage ($) {
70         @@ -47,6 +78,10 @@
71                                         $tag=~y#/#/#s; # squash dups
72                         }
73          
74         +       if (defined $config{tag_autocreate} && $config{tag_autocreate} ) {
75         +               gen_tag_page($tag);
76         +       }
77         +
78                         return $tag;
79          }
80          
81         @@ -125,4 +160,18 @@
82                         }
83          }
84          
85         +sub change(@) {
86         +       return unless($autocreated_page);
87         +       $autocreated_page = 0;
88         +
89         +       # This refresh/saveindex is to complie the autocreated tag pages
90         +       IkiWiki::refresh();
91         +       IkiWiki::saveindex();
92         +
93         +       # This refresh/saveindex is to fix the Tags link
94         +       # With out this additional refresh/saveindex the tag link displays ?tag
95         +       IkiWiki::refresh();
96         +       IkiWiki::saveindex();
97         +}
98         +
99
100
101 This uses a template called `autotagpage.tmpl`, here is my template file:
102
103     \[[!inline pages="link(<TMPL_VAR TAG>)" archive="yes"]]
104
105
106 A quirk I have not figured out is during the `sub change`, see my comments in the code.
107 I am not sure if that is the best way to handle it.
108
109 [[!tag patch]]
110 -- Jeremy Schultz <jeremy.schultz@uleth.ca>