add dumpsetup option; refactor
[ikiwiki.git] / IkiWiki / Setup / Standard.pm
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
6 package IkiWiki::Setup::Standard;
7
8 use warnings;
9 use strict;
10
11 sub import { #{{{
12         $IkiWiki::Setup::raw_setup=$_[1];
13 } #}}}
14
15 package IkiWiki::Setup;
16
17 sub dumpline ($$$) { #{{{
18         my $key=shift;
19         my $value=shift;
20         my $prefix=shift;
21         
22         eval q{use Data::Dumper};
23         error($@) if $@;
24         local $Data::Dumper::Terse=1;
25         local $Data::Dumper::Indent=1;
26         local $Data::Dumper::Pad="\t";
27         local $Data::Dumper::Sortkeys=1;
28         local $Data::Dumper::Quotekeys=0;
29         
30         my $dumpedvalue=Dumper($value);
31         chomp $dumpedvalue;
32         $dumpedvalue=~s/^\t//;
33         
34         return "\t$prefix$key=$dumpedvalue,";
35 } #}}}
36
37 sub dumpvalues ($@) { #{{{
38         my $setup=shift;
39         my @ret;
40         while (@_) {
41                 my $key=shift;
42                 my %info=%{shift()};
43                 
44                 push @ret, "\t# ".$info{description} if exists $info{description};
45                 
46                 if (exists $setup->{$key} && defined $setup->{$key}) {
47                         push @ret, dumpline($key, $setup->{$key}, "");
48                         delete $setup->{$key};
49                 }
50                 elsif (exists $info{default}) {
51                         push @ret, dumpline($key, $info{default}, "#");
52                 }
53                 elsif (exists $info{example}) {
54                         push @ret, dumpline($key, $info{example}, "#");
55                 }
56         }
57         return @ret;
58 } #}}}
59
60 sub dump ($) { #{{{
61         my $file=shift;
62         
63         my %setup=(%config);
64         my @ret;
65
66         foreach my $id (sort keys %{$IkiWiki::hooks{getsetup}}) {
67                 # use an array rather than a hash, to preserve order
68                 my @s=$IkiWiki::hooks{getsetup}{$id}{call}->();
69                 return unless @s;
70                 push @ret, "\t# $id plugin";
71                 push @ret, dumpvalues(\%setup, @s);
72                 push @ret, "";
73         }
74         
75         if (%setup) {
76                 push @ret, "\t# other";
77                 foreach my $key (sort keys %setup) {
78                         push @ret, dumpline($key, $setup{$key}, "");
79                 }
80         }
81         
82         unshift @ret, "#!/usr/bin/perl
83 # Setup file for ikiwiki.
84 # Passing this to ikiwiki --setup will make ikiwiki generate wrappers and
85 # build the wiki.
86 #
87 # Remember to re-run ikiwiki --setup any time you edit this file.
88
89 use IkiWiki::Setup::Standard {";
90         push @ret, "}";
91
92         open (OUT, ">", $file) || die "$file: $!";
93         print OUT "$_\n" foreach @ret;
94         close OUT;
95 } #}}}
96
97 1