summaryrefslogtreecommitdiff
path: root/IkiWiki/Setup.pm
blob: 262d494797639a43f62b9a5c89c34f34fec03b04 (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. if (exists $setup{add_plugins}) {
  33. push @{$setup{add_plugins}}, @{$config{add_plugins}};
  34. }
  35. if (exists $setup{exclude}) {
  36. push @{$config{wiki_file_prune_regexps}}, $setup{exclude};
  37. }
  38. foreach my $c (keys %setup) {
  39. if (defined $setup{$c}) {
  40. if (! ref $setup{$c} || ref $setup{$c} eq 'Regexp') {
  41. $config{$c}=IkiWiki::possibly_foolish_untaint($setup{$c});
  42. }
  43. elsif (ref $setup{$c} eq 'ARRAY') {
  44. $config{$c}=[map { IkiWiki::possibly_foolish_untaint($_) } @{$setup{$c}}]
  45. }
  46. elsif (ref $setup{$c} eq 'HASH') {
  47. foreach my $key (keys %{$setup{$c}}) {
  48. $config{$c}{$key}=IkiWiki::possibly_foolish_untaint($setup{$c}{$key});
  49. }
  50. }
  51. }
  52. else {
  53. $config{$c}=undef;
  54. }
  55. }
  56. } #}}}
  57. sub dump ($) { #{{{
  58. my $file=IkiWiki::possibly_foolish_untaint(shift);
  59. require IkiWiki::Setup::Standard;
  60. my @dump=IkiWiki::Setup::Standard::gendump("Setup file for ikiwiki.");
  61. open (OUT, ">", $file) || die "$file: $!";
  62. print OUT "$_\n" foreach @dump;
  63. close OUT;
  64. }
  65. 1