summaryrefslogtreecommitdiff
path: root/IkiWiki/Setup/Standard.pm
blob: f7a322317f549b9affb08730c53e685710f5930f (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. eval q{use Text::Wrap};
  80. die $@ if $@;
  81. my %section_plugins;
  82. push @ret, dumpvalues(\%setup, IkiWiki::getsetup());
  83. foreach my $pair (IkiWiki::Setup::getsetup()) {
  84. my $plugin=$pair->[0];
  85. my $setup=$pair->[1];
  86. my %s=@{$setup};
  87. my $section=$s{plugin}->{section};
  88. push @{$section_plugins{$section}}, $plugin;
  89. if (@{$section_plugins{$section}} == 1) {
  90. push @ret, "", "\t".("#" x 70), "\t# $section plugins",
  91. sub {
  92. wrap("\t# (", "\t# ",
  93. join(", ", @{$section_plugins{$section}})).")"
  94. },
  95. "\t".("#" x 70);
  96. }
  97. my @values=dumpvalues(\%setup, @{$setup});
  98. if (@values) {
  99. push @ret, "", "\t# $plugin plugin", @values;
  100. }
  101. }
  102. unshift @ret,
  103. "#!/usr/bin/perl",
  104. "# $description",
  105. "#",
  106. "# Passing this to ikiwiki --setup will make ikiwiki generate",
  107. "# wrappers and build the wiki.",
  108. "#",
  109. "# Remember to re-run ikiwiki --setup any time you edit this file.",
  110. "use IkiWiki::Setup::Standard {";
  111. push @ret, "}";
  112. return map { ref $_ ? $_->() : $_ } @ret;
  113. }
  114. 1