summaryrefslogtreecommitdiff
path: root/IkiWiki/Setup/Standard.pm
blob: d1c53c0645c242dd5c3c4974156d66c00607f819 (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::raw_setup=$_[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 ($type eq 'string' && ref $value eq 'ARRAY' && @$value &&
  30. ! grep { /[^-A-Za-z0-9_]/ } @$value) {
  31. # dump simple array as qw{}
  32. $dumpedvalue="[qw{ ".join(" ", @$value)." }]";
  33. }
  34. else {
  35. $dumpedvalue=Dumper($value);
  36. chomp $dumpedvalue;
  37. if (length $prefix) {
  38. # add to second and subsequent lines
  39. my @lines=split(/\n/, $dumpedvalue);
  40. $dumpedvalue="";
  41. for (my $x=0; $x <= $#lines; $x++) {
  42. $lines[$x] =~ s/^\t//;
  43. $dumpedvalue.="\t".($x ? $prefix : "").$lines[$x]."\n";
  44. }
  45. }
  46. $dumpedvalue=~s/^\t//;
  47. chomp $dumpedvalue;
  48. }
  49. return "\t$prefix$key => $dumpedvalue,";
  50. } #}}}
  51. sub dumpvalues ($@) { #{{{
  52. my $setup=shift;
  53. my @ret;
  54. while (@_) {
  55. my $key=shift;
  56. my %info=%{shift()};
  57. next if $info{type} eq "internal";
  58. push @ret, "\t# ".$info{description} if exists $info{description};
  59. if (exists $setup->{$key} && defined $setup->{$key}) {
  60. push @ret, dumpline($key, $setup->{$key}, $info{type}, "");
  61. delete $setup->{$key};
  62. }
  63. elsif (exists $info{default} && defined $info{default}) {
  64. push @ret, dumpline($key, $info{default}, $info{type}, "#");
  65. }
  66. elsif (exists $info{example}) {
  67. push @ret, dumpline($key, $info{example}, $info{type}, "#");
  68. }
  69. }
  70. return @ret;
  71. } #}}}
  72. sub gendump ($) { #{{{
  73. my $description=shift;
  74. my %setup=(%config);
  75. my @ret;
  76. push @ret, "\t# basic setup";
  77. push @ret, dumpvalues(\%setup, IkiWiki::getsetup());
  78. # Load all plugins, so that all setup options are available.
  79. my @plugins=grep { ! /externaldemo|pythondemo/ } sort(IkiWiki::listplugins());
  80. foreach my $plugin (@plugins) {
  81. eval { IkiWiki::loadplugin($plugin) };
  82. if (exists $IkiWiki::hooks{checkconfig}{$plugin}{call}) {
  83. my @s=eval { $IkiWiki::hooks{checkconfig}{$plugin}{call}->() };
  84. }
  85. }
  86. unshift @plugins, $config{rcs} if $config{rcs};
  87. foreach my $id (@plugins) {
  88. my $title="\t# $id".($id ne $config{rcs} ? " plugin" : "");
  89. if (exists $IkiWiki::hooks{getsetup}{$id}{call}) {
  90. # use an array rather than a hash, to preserve order
  91. my @s=eval { $IkiWiki::hooks{getsetup}{$id}{call}->() };
  92. next unless @s;
  93. push @ret, "", $title;
  94. push @ret, dumpvalues(\%setup, @s);
  95. }
  96. }
  97. unshift @ret,
  98. "#!/usr/bin/perl",
  99. "# $description",
  100. "#",
  101. "# Passing this to ikiwiki --setup will make ikiwiki generate",
  102. "# wrappers and build the wiki.",
  103. "#",
  104. "# Remember to re-run ikiwiki --setup any time you edit this file.",
  105. "use IkiWiki::Setup::Standard {";
  106. push @ret, "}";
  107. return @ret;
  108. } #}}}
  109. 1