summaryrefslogtreecommitdiff
path: root/IkiWiki/Setup/Standard.pm
blob: 22bfc38a40440df88915375e2d02dd307aebe0e1 (plain)
  1. #!/usr/bin/perl
  2. # Standard ikiwiki setup module.
  3. # Parameters to import should be all the standard ikiwiki config stuff,
  4. # plus an array of wrappers to set up.
  5. package IkiWiki::Setup::Standard;
  6. use warnings;
  7. use strict;
  8. use IkiWiki;
  9. sub import { #{{{
  10. $IkiWiki::Setup::raw_setup=$_[1];
  11. } #}}}
  12. sub dumpline ($$$$) { #{{{
  13. my $key=shift;
  14. my $value=shift;
  15. my $type=shift;
  16. my $prefix=shift;
  17. eval q{use Data::Dumper};
  18. error($@) if $@;
  19. local $Data::Dumper::Terse=1;
  20. local $Data::Dumper::Indent=1;
  21. local $Data::Dumper::Pad="\t";
  22. local $Data::Dumper::Sortkeys=1;
  23. local $Data::Dumper::Quotekeys=0;
  24. my $dumpedvalue;
  25. if ($type eq 'boolean' || $type eq 'integer') {
  26. # avoid quotes
  27. $dumpedvalue=$value;
  28. }
  29. elsif ($type eq 'string' && ref $value eq 'ARRAY' && @$value &&
  30. ! grep { /[^-A-Za-z0-9_]/ } @$value) {
  31. # dump simple array as qw{}
  32. $dumpedvalue="[qw{ ".join(" ", @$value)." }]";
  33. }
  34. else {
  35. $dumpedvalue=Dumper($value);
  36. chomp $dumpedvalue;
  37. $dumpedvalue=~s/^\t//;
  38. }
  39. return "\t$prefix$key => $dumpedvalue,";
  40. } #}}}
  41. sub dumpvalues ($@) { #{{{
  42. my $setup=shift;
  43. my @ret;
  44. while (@_) {
  45. my $key=shift;
  46. my %info=%{shift()};
  47. next if $info{type} eq "internal";
  48. push @ret, "\t# ".$info{description} if exists $info{description};
  49. if (exists $setup->{$key} && defined $setup->{$key}) {
  50. push @ret, dumpline($key, $setup->{$key}, $info{type}, "");
  51. delete $setup->{$key};
  52. }
  53. elsif (exists $info{default} && defined $info{default}) {
  54. push @ret, dumpline($key, $info{default}, $info{type}, "#");
  55. }
  56. elsif (exists $info{example}) {
  57. push @ret, dumpline($key, $info{example}, $info{type}, "#");
  58. }
  59. }
  60. return @ret;
  61. } #}}}
  62. sub gendump () { #{{{
  63. my %setup=(%config);
  64. my @ret;
  65. push @ret, "\t# basic setup";
  66. push @ret, dumpvalues(\%setup, IkiWiki::getsetup());
  67. push @ret, "";
  68. foreach my $id (sort keys %{$IkiWiki::hooks{getsetup}}) {
  69. # use an array rather than a hash, to preserve order
  70. my @s=$IkiWiki::hooks{getsetup}{$id}{call}->();
  71. return unless @s;
  72. push @ret, "\t# $id plugin";
  73. push @ret, dumpvalues(\%setup, @s);
  74. push @ret, "";
  75. }
  76. unshift @ret, "#!/usr/bin/perl
  77. # Setup file for ikiwiki.
  78. # Passing this to ikiwiki --setup will make ikiwiki generate wrappers and
  79. # build the wiki.
  80. #
  81. # Remember to re-run ikiwiki --setup any time you edit this file.
  82. use IkiWiki::Setup::Standard {";
  83. push @ret, "}";
  84. return @ret;
  85. } #}}}
  86. 1