summaryrefslogtreecommitdiff
path: root/IkiWiki/Setup/Standard.pm
blob: 9d8ec38515e5ccb3f5cf280f493cf7495698750b (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::merge($_[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. if (length $prefix) {
  38. # add to second and subsequent lines
  39. my @lines=split(/\n/, $dumpedvalue);
  40. $dumpedvalue="";
  41. for (my $x=0; $x <= $#lines; $x++) {
  42. $lines[$x] =~ s/^\t//;
  43. $dumpedvalue.="\t".($x ? $prefix : "").$lines[$x]."\n";
  44. }
  45. }
  46. $dumpedvalue=~s/^\t//;
  47. chomp $dumpedvalue;
  48. }
  49. return "\t$prefix$key => $dumpedvalue,";
  50. } #}}}
  51. sub dumpvalues ($@) { #{{{
  52. my $setup=shift;
  53. my @ret;
  54. while (@_) {
  55. my $key=shift;
  56. my %info=%{shift()};
  57. next if $info{type} eq "internal";
  58. push @ret, "\t# ".$info{description} if exists $info{description};
  59. if (exists $setup->{$key} && defined $setup->{$key}) {
  60. push @ret, dumpline($key, $setup->{$key}, $info{type}, "");
  61. delete $setup->{$key};
  62. }
  63. elsif (exists $info{example}) {
  64. push @ret, dumpline($key, $info{example}, $info{type}, "#");
  65. }
  66. }
  67. return @ret;
  68. } #}}}
  69. sub gendump ($) { #{{{
  70. my $description=shift;
  71. my %setup=(%config);
  72. my @ret;
  73. # disable logging to syslog while dumping
  74. $config{syslog}=0;
  75. push @ret, "\t# basic setup";
  76. push @ret, dumpvalues(\%setup, IkiWiki::getsetup());
  77. # Load all plugins, so that all setup options are available.
  78. # (But skip a few problematic external demo plugins.)
  79. my @plugins=grep { ! /^(externaldemo|pythondemo|\Q$config{rcs}\E)$/ }
  80. sort(IkiWiki::listplugins());
  81. unshift @plugins, $config{rcs} if $config{rcs}; # rcs plugin 1st
  82. foreach my $plugin (@plugins) {
  83. eval { IkiWiki::loadplugin($plugin) };
  84. if (exists $IkiWiki::hooks{checkconfig}{$plugin}{call}) {
  85. my @s=eval { $IkiWiki::hooks{checkconfig}{$plugin}{call}->() };
  86. }
  87. }
  88. foreach my $id (@plugins) {
  89. if (exists $IkiWiki::hooks{getsetup}{$id}{call}) {
  90. # use an array rather than a hash, to preserve order
  91. my @s=eval { $IkiWiki::hooks{getsetup}{$id}{call}->() };
  92. next unless @s;
  93. push @ret, "", "\t# $id plugin";
  94. push @ret, dumpvalues(\%setup, @s);
  95. }
  96. }
  97. unshift @ret,
  98. "#!/usr/bin/perl",
  99. "# $description",
  100. "#",
  101. "# Passing this to ikiwiki --setup will make ikiwiki generate",
  102. "# wrappers and build the wiki.",
  103. "#",
  104. "# Remember to re-run ikiwiki --setup any time you edit this file.",
  105. "use IkiWiki::Setup::Standard {";
  106. push @ret, "}";
  107. return @ret;
  108. } #}}}
  109. 1