summaryrefslogtreecommitdiff
path: root/IkiWiki/Setup/Standard.pm
blob: c99dbb620d9473cca44bbec527c115d47a5574d5 (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. my $curr_section;
  80. push @ret, dumpvalues(\%setup, IkiWiki::getsetup());
  81. foreach my $pair (IkiWiki::Setup::getsetup()) {
  82. my $plugin=$pair->[0];
  83. my $setup=$pair->[1];
  84. my %s=@{$setup};
  85. my $section=$s{plugin}->{section};
  86. if (! defined $curr_section || $curr_section ne $section) {
  87. $curr_section=$section;
  88. push @ret, "", "\t#", "\t# $section plugins", "\t#";
  89. }
  90. my @values=dumpvalues(\%setup, @{$setup});
  91. if (@values) {
  92. push @ret, "", "\t# $plugin plugin", @values;
  93. }
  94. }
  95. unshift @ret,
  96. "#!/usr/bin/perl",
  97. "# $description",
  98. "#",
  99. "# Passing this to ikiwiki --setup will make ikiwiki generate",
  100. "# wrappers and build the wiki.",
  101. "#",
  102. "# Remember to re-run ikiwiki --setup any time you edit this file.",
  103. "use IkiWiki::Setup::Standard {";
  104. push @ret, "}";
  105. return @ret;
  106. }
  107. 1