summaryrefslogtreecommitdiff
path: root/IkiWiki/Setup/Standard.pm
blob: 0e640f8ac85b4b1ae9b21df12b9a574090785f94 (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 (ref $value eq 'ARRAY' && @$value && ! grep { /[^-A-Za-z0-9_]/ } @$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 $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. push @ret, "", "\t# $plugin plugin";
  82. push @ret, dumpvalues(\%setup, @{$setup});
  83. }
  84. unshift @ret,
  85. "#!/usr/bin/perl",
  86. "# $description",
  87. "#",
  88. "# Passing this to ikiwiki --setup will make ikiwiki generate",
  89. "# wrappers and build the wiki.",
  90. "#",
  91. "# Remember to re-run ikiwiki --setup any time you edit this file.",
  92. "use IkiWiki::Setup::Standard {";
  93. push @ret, "}";
  94. return @ret;
  95. } #}}}
  96. 1