summaryrefslogtreecommitdiff
path: root/perl/IkiWiki/Plugin/varioki.pm
blob: 0132aad326c61a29d0ead1eb1f1c8c7cc2e250d0 (plain)
  1. #!/usr/bin/perl
  2. #                              -*- Mode: Cperl -*- 
  3. # varioki.pm --- 
  4. # Author           : Manoj Srivastava ( srivasta@glaurung.internal.golden-gryphon.com ) 
  5. # Created On       : Wed Dec  6 22:25:44 2006
  6. # Created On Node  : glaurung.internal.golden-gryphon.com
  7. # Last Modified By : Manoj Srivastava
  8. # Last Modified On : Thu Dec  7 13:07:36 2006
  9. # Last Machine Used: glaurung.internal.golden-gryphon.com
  10. # Update Count     : 127
  11. # Status           : Unknown, Use with caution!
  12. # HISTORY          : 
  13. # Description      : 
  14. # arch-tag: 6961717b-156f-4ab2-980f-0d6a973aea21
  15. #
  16. # Copyright (c) 2006 Manoj Srivastava 
  17. #
  18. # This program is free software; you can redistribute it and/or modify
  19. # it under the terms of the GNU General Public License as published by
  20. # the Free Software Foundation; either version 2 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  26. # GNU General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU General Public License
  29. # along with this program; if not, write to the Free Software
  30. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  31. #
  32. require 5.002;
  33. package IkiWiki::Plugin::varioki;
  34. use warnings;
  35. use strict;
  36. use IkiWiki '1.00';
  37. our $VERSION "0.1";
  38. my $file __FILE__;
  39. =head1 NAME
  40. varioki - Add variables for use in ikiwiki templates
  41. =cut
  42. =head1 DESCRIPTION
  43. This plugin attempts to provide a means to add templates for use in
  44. ikiwiki templates, based on a hash variable set in the ikiwiki
  45. configuration file. The motivation for this plugin was to provide an
  46. easy way for end users to add information to be used in templates --
  47. for example, my C blog entry template does fancy things with
  48. the date components of the entry, and there was no easy way to get
  49. that information into the template. Or if one wants to have a
  50. different page template for the top level index page than for the rest
  51. of the pages in the wiki (for example, to only put special content,
  52. like, say, C play lists, only on the front page).
  53. This plugin hooks itsef into the C hook, and adds
  54. parameters to the appropriate templates based on the type. For
  55. example, the following inseted into C creates
  56. C, C, C and C which can
  57. then be used in your templates. The array and hash variables are only
  58. for completeness; I suspect that the first two forms are all that are
  59. really required.
  60.  varioki => {
  61.    'motto'    => '"Manoj\'s musings"',
  62.    'toplvl'   => 'sub {return $page eq "index"}',
  63.    'arrayvar' => '[0, 1, 2, 3]',
  64.    'hashvar'  => '{1, 1, 2, 2}'
  65.  },
  66. Please note that the values in the hash must be simple strings which
  67. are then eval'd, so a string value has to be double quoted, as above
  68. (the eval strips off the outer quotes).  
  69. =cut
  70. sub import #{{{
  71.    hook(type => "pagetemplate"id => "varioki"call => \&pagetemplate);
  72. # }}}
  73. =pod
  74. For every key in the configured hash, the corresponding value is
  75. evaluated.  Based on whether the value was a stringified scalar, code,
  76. array, or hash, the value of the template parameter is generated on
  77. the fly.  The available variables are whatever is available to
  78. C hook scripts, namely, C<$page>, C<$destpage>, and
  79. C<$template>.  Additionally, the global variables and functions as
  80. defined in the Ikiwiki documentation
  81. (L) may be used.
  82. =cut
  83. sub pagetemplate (@) { #{{{
  84.    my %params=@_;
  85.    my $page=$params{page};
  86.    my $template=$params{template};
  87.         
  88.         return unless defined $config{varioki};
  89.          for my $var (keys %{$config{varioki}}) {
  90.            my $value;
  91.            my $foo;
  92.            eval "\$foo=$config{varioki}{$var}";
  93.            if (ref($fooeq "CODE") {
  94.              $value $foo->();
  95.            }
  96.            elsif (ref($fooeq "SCALAR") {
  97.              $value $foo;
  98.            }
  99.            elsif (ref($fooeq "ARRAY") {
  100.              $value join ' '@$foo;
  101.            }
  102.            elsif (ref($fooeq "HASH") {
  103.              for my $i (values %$foo ) {
  104.                $value .= ' ' "$i";
  105.              }
  106.            }
  107.            else {
  108.              $value $foo;
  109.            }
  110. #           warn "$page $var $value\n";
  111.            if ($template->query(name => "$var")) {
  112.              $template->param("$var" =>"$value");
  113.            }
  114.         }
  115. # }}}
  116. 1;
  117. =head1 CAVEATS
  118. This is very inchoate, at the moment, and needs testing. Also, there
  119. is no good way to determine how to handle hashes as values --
  120. currently, the code just joins all hash values with spaces, but it
  121. would be easier for the user to just use an anonymous sub instead of
  122. passing in a hash or an array.
  123. =cut
  124. =head1 BUGS
  125. Since C evals the configuration file, the values have to all
  126. on a single physical line. This is the reason we need to use strings
  127. and eval, instead of just passing in real anonymous sub references,
  128. since the eval pass converts the coderef into a string of the form
  129. "(CODE 12de345657)" which can't be dereferenced.
  130. =cut
  131. =head1 AUTHOR
  132. Manoj Srivastava 
  133. =head1 COPYRIGHT AND LICENSE
  134. This script is a part of the Devotee package, and is 
  135. Copyright (c) 2002 Manoj Srivastava 
  136. This program is free software; you can redistribute it and/or modify
  137. it under the terms of the GNU General Public License as published by
  138. the Free Software Foundation; either version 2 of the License, or
  139. (at your option) any later version.
  140. This program is distributed in the hope that it will be useful,
  141. but WITHOUT ANY WARRANTY; without even the implied warranty of
  142. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  143. GNU General Public License for more details.
  144. You should have received a copy of the GNU General Public License
  145. along with this program; if not, write to the Free Software
  146. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  147. =cut
  148. 1;
  149. __END__