summaryrefslogtreecommitdiff
path: root/IkiWiki/Setup.pm
blob: b67d1a45417149758f7b6ce2cfc882ba02ed51af (plain)
  1. #!/usr/bin/perl
  2. # Ikiwiki setup files are perl files that 'use IkiWiki::Setup::foo',
  3. # passing it some sort of configuration data.
  4. package IkiWiki::Setup;
  5. use warnings;
  6. use strict;
  7. use IkiWiki;
  8. use open qw{:utf8 :std};
  9. # There can be multiple modules, with different configuration styles.
  10. # The setup modules each convert the data into the hashes used by ikiwiki
  11. # internally (if it's not already in that format), and store it in
  12. # IkiWiki::Setup::$raw_setup, to pass it back to this module.
  13. our $raw_setup;
  14. sub load ($) { # {{{
  15. my $setup=IkiWiki::possibly_foolish_untaint(shift);
  16. delete $config{setup};
  17. #translators: The first parameter is a filename, and the second
  18. #translators: is a (probably not translated) error message.
  19. open (IN, $setup) || error(sprintf(gettext("cannot read %s: %s"), $setup, $!));
  20. my $code;
  21. {
  22. local $/=undef;
  23. $code=<IN>;
  24. }
  25. ($code)=$code=~/(.*)/s;
  26. close IN;
  27. eval $code;
  28. error("$setup: ".$@) if $@;
  29. my %setup=%{$raw_setup};
  30. $raw_setup=undef;
  31. # Merge setup into existing config and untaint.
  32. $setup{plugin}=$config{plugin};
  33. if (exists $setup{add_plugins}) {
  34. push @{$setup{plugin}}, @{$setup{add_plugins}};
  35. delete $setup{add_plugins};
  36. }
  37. if (exists $setup{exclude}) {
  38. push @{$config{wiki_file_prune_regexps}}, $setup{exclude};
  39. }
  40. foreach my $c (keys %setup) {
  41. if (defined $setup{$c}) {
  42. if (! ref $setup{$c}) {
  43. $config{$c}=IkiWiki::possibly_foolish_untaint($setup{$c});
  44. }
  45. elsif (ref $setup{$c} eq 'ARRAY') {
  46. $config{$c}=[map { IkiWiki::possibly_foolish_untaint($_) } @{$setup{$c}}]
  47. }
  48. elsif (ref $setup{$c} eq 'HASH') {
  49. foreach my $key (keys %{$setup{$c}}) {
  50. $config{$c}{$key}=IkiWiki::possibly_foolish_untaint($setup{$c}{$key});
  51. }
  52. }
  53. }
  54. else {
  55. $config{$c}=undef;
  56. }
  57. }
  58. } #}}}
  59. 1