summaryrefslogtreecommitdiff
path: root/IkiWiki/Setup/Standard.pm
blob: 9d4732b6f87825c3594f061ec6a41dcee6c8203e (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. else {
  67. push @ret, dumpline($key, "", $info{type}, "#");
  68. }
  69. }
  70. return @ret;
  71. } #}}}
  72. sub gendump ($) { #{{{
  73. my $description=shift;
  74. my %setup=(%config);
  75. my @ret;
  76. # disable logging to syslog while dumping
  77. $config{syslog}=0;
  78. push @ret, "\t# basic setup";
  79. push @ret, dumpvalues(\%setup, IkiWiki::getsetup());
  80. # Load all plugins, so that all setup options are available.
  81. # (But skip a few problematic external demo plugins.)
  82. my @plugins=grep { ! /^(externaldemo|pythondemo|\Q$config{rcs}\E)$/ }
  83. sort(IkiWiki::listplugins());
  84. unshift @plugins, $config{rcs} if $config{rcs}; # rcs plugin 1st
  85. foreach my $plugin (@plugins) {
  86. eval { IkiWiki::loadplugin($plugin) };
  87. if (exists $IkiWiki::hooks{checkconfig}{$plugin}{call}) {
  88. my @s=eval { $IkiWiki::hooks{checkconfig}{$plugin}{call}->() };
  89. }
  90. }
  91. foreach my $id (@plugins) {
  92. if (exists $IkiWiki::hooks{getsetup}{$id}{call}) {
  93. # use an array rather than a hash, to preserve order
  94. my @s=eval { $IkiWiki::hooks{getsetup}{$id}{call}->() };
  95. next unless @s;
  96. push @ret, "", "\t# $id plugin";
  97. push @ret, dumpvalues(\%setup, @s);
  98. }
  99. }
  100. unshift @ret,
  101. "#!/usr/bin/perl",
  102. "# $description",
  103. "#",
  104. "# Passing this to ikiwiki --setup will make ikiwiki generate",
  105. "# wrappers and build the wiki.",
  106. "#",
  107. "# Remember to re-run ikiwiki --setup any time you edit this file.",
  108. "use IkiWiki::Setup::Standard {";
  109. push @ret, "}";
  110. return @ret;
  111. } #}}}
  112. 1