summaryrefslogtreecommitdiff
path: root/IkiWiki/Setup/Standard.pm
blob: 8a11b2ec6ba366dd49049add8b9ad320d78faaf9 (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') && $value=~/^[0-9]+$/) {
  26. # avoid quotes
  27. $dumpedvalue=$value;
  28. }
  29. elsif (ref $value eq 'ARRAY' && @$value && ! grep { /[^\S]/ } @$value) {
  30. # dump simple array as qw{}
  31. $dumpedvalue="[qw{".join(" ", @$value)."}]";
  32. }
  33. else {
  34. $dumpedvalue=Dumper($value);
  35. chomp $dumpedvalue;
  36. if (length $prefix) {
  37. # add to second and subsequent lines
  38. my @lines=split(/\n/, $dumpedvalue);
  39. $dumpedvalue="";
  40. for (my $x=0; $x <= $#lines; $x++) {
  41. $lines[$x] =~ s/^\t//;
  42. $dumpedvalue.="\t".($x ? $prefix : "").$lines[$x]."\n";
  43. }
  44. }
  45. $dumpedvalue=~s/^\t//;
  46. chomp $dumpedvalue;
  47. }
  48. return "\t$prefix$key => $dumpedvalue,";
  49. } #}}}
  50. sub dumpvalues ($@) { #{{{
  51. my $setup=shift;
  52. my @ret;
  53. while (@_) {
  54. my $key=shift;
  55. my %info=%{shift()};
  56. next if $key eq "plugin" || $info{type} eq "internal";
  57. push @ret, "\t# ".$info{description} if exists $info{description};
  58. if (exists $setup->{$key} && defined $setup->{$key}) {
  59. push @ret, dumpline($key, $setup->{$key}, $info{type}, "");
  60. delete $setup->{$key};
  61. }
  62. elsif (exists $info{example}) {
  63. push @ret, dumpline($key, $info{example}, $info{type}, "#");
  64. }
  65. else {
  66. push @ret, dumpline($key, "", $info{type}, "#");
  67. }
  68. }
  69. return @ret;
  70. } #}}}
  71. sub gendump ($) { #{{{
  72. my $description=shift;
  73. my %setup=(%config);
  74. my @ret;
  75. # disable logging to syslog while dumping
  76. $config{syslog}=0;
  77. push @ret, dumpvalues(\%setup, IkiWiki::getsetup());
  78. foreach my $pair (IkiWiki::Setup::getsetup()) {
  79. my $plugin=$pair->[0];
  80. my $setup=$pair->[1];
  81. my @values=dumpvalues(\%setup, @{$setup});
  82. if (@values) {
  83. push @ret, "", "\t# $plugin plugin", @values;
  84. }
  85. }
  86. unshift @ret,
  87. "#!/usr/bin/perl",
  88. "# $description",
  89. "#",
  90. "# Passing this to ikiwiki --setup will make ikiwiki generate",
  91. "# wrappers and build the wiki.",
  92. "#",
  93. "# Remember to re-run ikiwiki --setup any time you edit this file.",
  94. "use IkiWiki::Setup::Standard {";
  95. push @ret, "}";
  96. return @ret;
  97. } #}}}
  98. 1