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