]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Setup/Standard.pm
can now dump fully functional setup files
[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 $type=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;
31         if ($type eq 'boolean' || $type eq 'integer') {
32                 # avoid quotes
33                 $dumpedvalue=$value;
34         }
35         elsif ($type eq 'string' && ref $value eq 'ARRAY' && @$value &&
36             ! grep { /[^-A-Za-z0-9_]/ } @$value) {
37                 # dump simple array as qw{}
38                 $dumpedvalue="[qw{ ".join(" ", @$value)." }]";
39         }
40         else {
41                 $dumpedvalue=Dumper($value);
42                 chomp $dumpedvalue;
43                 $dumpedvalue=~s/^\t//;
44         }
45         
46         return "\t$prefix$key => $dumpedvalue,";
47 } #}}}
48
49 sub dumpvalues ($@) { #{{{
50         my $setup=shift;
51         my @ret;
52         while (@_) {
53                 my $key=shift;
54                 my %info=%{shift()};
55
56                 next if $info{type} eq "internal";
57                 
58                 push @ret, "\t# ".$info{description} if exists $info{description};
59                 
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{default} && defined $info{default}) {
65                         push @ret, dumpline($key, $info{default}, $info{type}, "#");
66                 }
67                 elsif (exists $info{example}) {
68                         push @ret, dumpline($key, $info{example}, $info{type}, "#");
69                 }
70         }
71         return @ret;
72 } #}}}
73
74 sub dump ($) { #{{{
75         my $file=IkiWiki::possibly_foolish_untaint(shift);
76         
77         my %setup=(%config);
78         my @ret;
79         
80         push @ret, "\t# basic setup";
81         push @ret, dumpvalues(\%setup, IkiWiki::getsetup());
82         push @ret, "";
83
84         foreach my $id (sort keys %{$IkiWiki::hooks{getsetup}}) {
85                 # use an array rather than a hash, to preserve order
86                 my @s=$IkiWiki::hooks{getsetup}{$id}{call}->();
87                 return unless @s;
88                 push @ret, "\t# $id plugin";
89                 push @ret, dumpvalues(\%setup, @s);
90                 push @ret, "";
91         }
92         
93         unshift @ret, "#!/usr/bin/perl
94 # Setup file for ikiwiki.
95 # Passing this to ikiwiki --setup will make ikiwiki generate wrappers and
96 # build the wiki.
97 #
98 # Remember to re-run ikiwiki --setup any time you edit this file.
99
100 use IkiWiki::Setup::Standard {";
101         push @ret, "}";
102
103         open (OUT, ">", $file) || die "$file: $!";
104         print OUT "$_\n" foreach @ret;
105         close OUT;
106 } #}}}
107
108 1