1 varioki - Add variables for use in ikiwiki templates
3 This plugin attempts to provide a means to add templates for use in ikiwiki templates, based on a hash variable set in the ikiwiki configuration file. The motivation for this plugin was to provide an easy way for end users to add information to be used in templates -- for example, my "Blosxom" blog entry template does fancy things with the date components of the entry, and there was no easy way to get that information into the template. Or if one wants to have a different page template for the top level index page than for the rest of the pages inthe wiki (for example, to only put special content, like, say, 'last.fm" play lists, only on the front page).
5 This plugin hooks itsef into the "pagetemplate" hook, and adds parameters to the appropriate templates based on the type. For example, the following inserted into "ikiwiki.setup" creates "TMPL_VAR MOTTO" and "TOPLVL" which can then be used in your templates.
8 ’motto’ => ’"Manoj\’s musings"’,
9 ’toplvl’ => ’sub {return $page eq "index"}’
12 For every key in the configured hash, the corresponding value is evaluated. Based on whether the value was a stringified scalar, code, array, or hash, the value of the template parameter is generated on the fly. The available variables are whatever is available to "pagetemplate" hook scripts, namely, $page, $destpage, and $template. Additionally, the global variables and functions as defined in the Ikiwiki documentation (<http://ikiwiki.info/plugins/write/>) may be used.
16 > I think you could now implement "toplvl" using [[conditionals|/plugins/conditional]]:
18 > \[[if test="destpage(/index)" then="""...""" else="""..."""]]
22 > > Right. But how about some more complex stuff, for example, from my varioki settings below? --ManojSrivastava
24 > Here's a dump of the file Manoj sent me, for reference.
26 > My take on this is that simple plugins can do the same sort of things, this is
27 > kind of wanting to avoid the plugin mechanism and just use templates and
28 > stuff in the config file. Not too thrilled about that. --[[Joey]]
30 > > OK. How do you implement something like category I have in my varioki
31 > > settings? As a user, I can just add new stuff to my config and my template;
32 > > with a plugin I'll have to write a plugin, and install it in the ikiwiki plugin
33 > > directory, which is not very easy for a plain ol' user. Not everyone is the
34 > > sysadmin of their own machines with access to system dirs. --ManojSrivastava
37 varioki => {'motto' => '"Manoj\'s musings"',
38 'arrayvar' => '[0, 1, 2, 3]',
39 'hashvar' => '{1, 1, 2, 2}',
40 'toplvl' => 'sub {return $page eq "index"}',
41 'isblog' => 'sub {return $page =~ m,blog/.*,}',
42 'category' => 'sub { return " " unless $page=~ m,^blog/,; my $p=""; my $i="<a href=\"$config{url}/blog\">Top::</a>"; my @a=split ("/",$page); shift @a; pop @a; foreach my $dir (@a) { $p.=$dir; $i.="<a href=\"$config{url}/tag/$p\">$dir</a>::"; $p.="/"; }; return $i }',
43 'date' => 'sub { return POSIX::strftime("%d", gmtime((stat(srcfile($pagesources{$page})))[9])); }',
44 'year' => 'sub { return POSIX::strftime("%Y", gmtime((stat(srcfile($pagesources{$page})))[9])); }',
45 'month' => 'sub { return POSIX::strftime("%B", gmtime((stat(srcfile($pagesources{$page})))[9])); }',
46 'day' => 'sub { return POSIX::strftime("%A", gmtime((stat(srcfile($pagesources{$page})))[9])); }',
53 * looking for srivasta@debian.org--2006-misc/ikiwiki--upstream--1.0--patch-488 to compare with
54 * comparing to srivasta@debian.org--2006-misc/ikiwiki--upstream--1.0--patch-488: ................................................................ done.
59 +++ mod/IkiWiki/Plugin/.arch-ids/varioki.pm.id
61 +Manoj Srivastava <srivasta@debian.org> Thu Dec 7 12:59:07 2006 12659.0
63 +++ mod/IkiWiki/Plugin/varioki.pm
66 +# -*- Mode: Cperl -*-
68 +# Author : Manoj Srivastava ( srivasta@glaurung.internal.golden-gryphon.com )
69 +# Created On : Wed Dec 6 22:25:44 2006
70 +# Created On Node : glaurung.internal.golden-gryphon.com
71 +# Last Modified By : Manoj Srivastava
72 +# Last Modified On : Thu Dec 7 13:07:36 2006
73 +# Last Machine Used: glaurung.internal.golden-gryphon.com
75 +# Status : Unknown, Use with caution!
79 +# arch-tag: 6961717b-156f-4ab2-980f-0d6a973aea21
81 +# Copyright (c) 2006 Manoj Srivastava <srivasta@debian.org>
83 +# This program is free software; you can redistribute it and/or modify
84 +# it under the terms of the GNU General Public License as published by
85 +# the Free Software Foundation; either version 2 of the License, or
86 +# (at your option) any later version.
88 +# This program is distributed in the hope that it will be useful,
89 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
90 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
91 +# GNU General Public License for more details.
93 +# You should have received a copy of the GNU General Public License
94 +# along with this program; if not, write to the Free Software
95 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
100 +package IkiWiki::Plugin::varioki;
106 +our $VERSION = "0.1";
107 +my $file = __FILE__;
112 +varioki - Add variables for use in ikiwiki templates
118 +This plugin attempts to provide a means to add templates for use in
119 +ikiwiki templates, based on a hash variable set in the ikiwiki
120 +configuration file. The motivation for this plugin was to provide an
121 +easy way for end users to add information to be used in templates --
122 +for example, my C<Blosxom> blog entry template does fancy things with
123 +the date components of the entry, and there was no easy way to get
124 +that information into the template. Or if one wants to have a
125 +different page template for the top level index page than for the rest
126 +of the pages in the wiki (for example, to only put special content,
127 +like, say, C<last.fm> play lists, only on the front page).
129 +This plugin hooks itsef into the C<pagetemplate> hook, and adds
130 +parameters to the appropriate templates based on the type. For
131 +example, the following inseted into C<ikiwiki.setup> creates
132 +C<TMPL_VAR MOTTO>, C<ARRAYVAR>, C<HASHVAR> and C<TOPLVL> which can
133 +then be used in your templates. The array and hash variables are only
134 +for completeness; I suspect that the first two forms are all that are
138 + 'motto' => '"Manoj\'s musings"',
139 + 'toplvl' => 'sub {return $page eq "index"}',
140 + 'arrayvar' => '[0, 1, 2, 3]',
141 + 'hashvar' => '{1, 1, 2, 2}'
144 +Please note that the values in the hash must be simple strings which
145 +are then eval'd, so a string value has to be double quoted, as above
146 +(the eval strips off the outer quotes).
152 + hook(type => "pagetemplate", id => "varioki", call => \&pagetemplate);
158 +For every key in the configured hash, the corresponding value is
159 +evaluated. Based on whether the value was a stringified scalar, code,
160 +array, or hash, the value of the template parameter is generated on
161 +the fly. The available variables are whatever is available to
162 +C<pagetemplate> hook scripts, namely, C<$page>, C<$destpage>, and
163 +C<$template>. Additionally, the global variables and functions as
164 +defined in the Ikiwiki documentation
165 +(L<http://ikiwiki.kitenet.net/plugins/write.html>) may be used.
169 +sub pagetemplate (@) { #{{{
171 + my $page=$params{page};
172 + my $template=$params{template};
174 + return unless defined $config{varioki};
175 + for my $var (keys %{$config{varioki}}) {
178 + eval "\$foo=$config{varioki}{$var}";
179 + if (ref($foo) eq "CODE") {
182 + elsif (ref($foo) eq "SCALAR") {
185 + elsif (ref($foo) eq "ARRAY") {
186 + $value = join ' ', @$foo;
188 + elsif (ref($foo) eq "HASH") {
189 + for my $i (values %$foo ) {
190 + $value .= ' ' . "$i";
196 + warn "$page $var $value\n";
197 + if ($template->query(name => "$var")) {
198 + $template->param("$var" =>"$value");
207 +This is very inchoate, at the moment, and needs testing. Also, there
208 +is no good way to determine how to handle hashes as values --
209 +currently, the code just joins all hash values with spaces, but it
210 +would be easier for the user to just use an anonymous sub instead of
211 +passing in a hash or an array.
217 +Since C<ikiwiki> evals the configuration file, the values have to all
218 +on a single physical line. This is the reason we need to use strings
219 +and eval, instead of just passing in real anonymous sub references,
220 +since the eval pass converts the coderef into a string of the form
221 +"(CODE 12de345657)" which can't be dereferenced.
227 +Manoj Srivastava <srivasta@debian.org>
229 +=head1 COPYRIGHT AND LICENSE
231 +This script is a part of the Devotee package, and is
233 +Copyright (c) 2002 Manoj Srivastava <srivasta@debian.org>
235 +This program is free software; you can redistribute it and/or modify
236 +it under the terms of the GNU General Public License as published by
237 +the Free Software Foundation; either version 2 of the License, or
238 +(at your option) any later version.
240 +This program is distributed in the hope that it will be useful,
241 +but WITHOUT ANY WARRANTY; without even the implied warranty of
242 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
243 +GNU General Public License for more details.
245 +You should have received a copy of the GNU General Public License
246 +along with this program; if not, write to the Free Software
247 +Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA