summaryrefslogtreecommitdiff
path: root/IkiWiki/Setup.pm
blob: c2fd59b21479f96bc4d70d68981a36e0edfed593 (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. sub load ($) { # {{{
  10. my $setup=IkiWiki::possibly_foolish_untaint(shift);
  11. delete $config{setup};
  12. #translators: The first parameter is a filename, and the second
  13. #translators: is a (probably not translated) error message.
  14. open (IN, $setup) || error(sprintf(gettext("cannot read %s: %s"), $setup, $!));
  15. my $code;
  16. {
  17. local $/=undef;
  18. $code=<IN>;
  19. }
  20. ($code)=$code=~/(.*)/s;
  21. close IN;
  22. eval $code;
  23. error("$setup: ".$@) if $@;
  24. } #}}}
  25. sub merge ($) {
  26. my %setup=%{shift()};
  27. # Merge setup into existing config and untaint.
  28. if (exists $setup{add_plugins}) {
  29. push @{$setup{add_plugins}}, @{$config{add_plugins}};
  30. }
  31. if (exists $setup{exclude}) {
  32. push @{$config{wiki_file_prune_regexps}}, $setup{exclude};
  33. }
  34. foreach my $c (keys %setup) {
  35. if (defined $setup{$c}) {
  36. if (! ref $setup{$c} || ref $setup{$c} eq 'Regexp') {
  37. $config{$c}=IkiWiki::possibly_foolish_untaint($setup{$c});
  38. }
  39. elsif (ref $setup{$c} eq 'ARRAY') {
  40. if ($c eq 'wrappers') {
  41. # backwards compatability code
  42. $config{$c}=$setup{$c};
  43. }
  44. else {
  45. $config{$c}=[map { IkiWiki::possibly_foolish_untaint($_) } @{$setup{$c}}]
  46. }
  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. if (length $config{cgi_wrapper}) {
  59. push @{$config{wrappers}}, {
  60. cgi => 1,
  61. wrapper => $config{cgi_wrapper},
  62. wrappermode => (defined $config{cgi_wrappermode} ? $config{cgi_wrappermode} : "06755"),
  63. };
  64. }
  65. } #}}}
  66. sub dump ($) { #{{{
  67. my $file=IkiWiki::possibly_foolish_untaint(shift);
  68. require IkiWiki::Setup::Standard;
  69. my @dump=IkiWiki::Setup::Standard::gendump("Setup file for ikiwiki.");
  70. open (OUT, ">", $file) || die "$file: $!";
  71. print OUT "$_\n" foreach @dump;
  72. close OUT;
  73. }
  74. 1