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