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