summaryrefslogtreecommitdiff
path: root/IkiWiki/Setup/Standard.pm
blob: b8ad091448c025d3b3400b0e38b883e84754d6fa (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 $description=shift;
  64. my %setup=(%config);
  65. my @ret;
  66. push @ret, "\t# basic setup";
  67. push @ret, dumpvalues(\%setup, IkiWiki::getsetup());
  68. push @ret, "";
  69. # sort rcs plugin first
  70. my @plugins=sort {
  71. ($a eq $config{rcs}) <=> ($b eq $config{rcs})
  72. ||
  73. $a cmp $b
  74. } keys %{$IkiWiki::hooks{getsetup}};
  75. foreach my $id (sort keys %{$IkiWiki::hooks{getsetup}}) {
  76. # use an array rather than a hash, to preserve order
  77. my @s=$IkiWiki::hooks{getsetup}{$id}{call}->();
  78. return unless @s;
  79. push @ret, "\t# $id".($id ne $config{rcs} ? " plugin" : "");
  80. push @ret, dumpvalues(\%setup, @s);
  81. push @ret, "";
  82. }
  83. unshift @ret,
  84. "#!/usr/bin/perl",
  85. "# $description",
  86. "#",
  87. "# Passing this to ikiwiki --setup will make ikiwiki generate",
  88. "# wrappers and build the wiki.",
  89. "#",
  90. "# Remember to re-run ikiwiki --setup any time you edit this file.",
  91. "use IkiWiki::Setup::Standard {";
  92. push @ret, "}";
  93. return @ret;
  94. } #}}}
  95. 1