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. #
  15. # arch-tag: 6961717b-156f-4ab2-980f-0d6a973aea21
  16. #
  17. # Copyright (c) 2006 Manoj Srivastava
  18. #
  19. # This program is free software; you can redistribute it and/or modify
  20. # it under the terms of the GNU General Public License as published by
  21. # the Free Software Foundation; either version 2 of the License, or
  22. # (at your option) any later version.
  23. #
  24. # This program is distributed in the hope that it will be useful,
  25. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27. # GNU General Public License for more details.
  28. #
  29. # You should have received a copy of the GNU General Public License
  30. # along with this program; if not, write to the Free Software
  31. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  32. #
  33. require 5.002;
  34. package IkiWiki::Plugin::varioki;
  35. use warnings;
  36. use strict;
  37. use IkiWiki '1.00';
  38. our $VERSION = "0.1";
  39. my $file = __FILE__;
  40. =head1 NAME
  41. varioki - Add variables for use in ikiwiki templates
  42. =cut
  43. =head1 DESCRIPTION
  44. This plugin attempts to provide a means to add templates for use in
  45. ikiwiki templates, based on a hash variable set in the ikiwiki
  46. configuration file. The motivation for this plugin was to provide an
  47. easy way for end users to add information to be used in templates --
  48. for example, my C blog entry template does fancy things with
  49. the date components of the entry, and there was no easy way to get
  50. that information into the template. Or if one wants to have a
  51. different page template for the top level index page than for the rest
  52. of the pages in the wiki (for example, to only put special content,
  53. like, say, C play lists, only on the front page).
  54. This plugin hooks itsef into the C hook, and adds
  55. parameters to the appropriate templates based on the type. For
  56. example, the following inseted into C creates
  57. C, C, C and C which can
  58. then be used in your templates. The array and hash variables are only
  59. for completeness; I suspect that the first two forms are all that are
  60. really required.
  61. varioki => {
  62. 'motto' => '"Manoj\'s musings"',
  63. 'toplvl' => 'sub {return $page eq "index"}',
  64. 'arrayvar' => '[0, 1, 2, 3]',
  65. 'hashvar' => '{1, 1, 2, 2}'
  66. },
  67. Please note that the values in the hash must be simple strings which
  68. are then eval'd, so a string value has to be double quoted, as above
  69. (the eval strips off the outer quotes).
  70. =cut
  71. sub import { #{{{
  72. hook(type => "pagetemplate", id => "varioki", call => \&pagetemplate);
  73. } # }}}
  74. =pod
  75. For every key in the configured hash, the corresponding value is
  76. evaluated. Based on whether the value was a stringified scalar, code,
  77. array, or hash, the value of the template parameter is generated on
  78. the fly. The available variables are whatever is available to
  79. C hook scripts, namely, C<$page>, C<$destpage>, and
  80. C<$template>. Additionally, the global variables and functions as
  81. defined in the Ikiwiki documentation
  82. (L) may be used.
  83. =cut
  84. sub pagetemplate (@) { #{{{
  85. my %params=@_;
  86. my $page=$params{page};
  87. my $template=$params{template};
  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($foo) eq "CODE") {
  94. $value = $foo->();
  95. }
  96. elsif (ref($foo) eq "SCALAR") {
  97. $value = $foo;
  98. }
  99. elsif (ref($foo) eq "ARRAY") {
  100. $value = join ' ', @$foo;
  101. }
  102. elsif (ref($foo) eq "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__