]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Setup/Standard.pm
fix misc breakage from plugin safe/rebuild data addition
[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::merge($_[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') && $value=~/^[0-9]+$/) {
32                 # avoid quotes
33                 $dumpedvalue=$value;
34         }
35         elsif (ref $value eq 'ARRAY' && @$value && ! grep { /[^-A-Za-z0-9_]/ } @$value) {
36                 # dump simple array as qw{}
37                 $dumpedvalue="[qw{".join(" ", @$value)."}]";
38         }
39         else {
40                 $dumpedvalue=Dumper($value);
41                 chomp $dumpedvalue;
42                 if (length $prefix) {
43                         # add to second and subsequent lines
44                         my @lines=split(/\n/, $dumpedvalue);
45                         $dumpedvalue="";
46                         for (my $x=0; $x <= $#lines; $x++) {
47                                 $lines[$x] =~ s/^\t//;
48                                 $dumpedvalue.="\t".($x ? $prefix : "").$lines[$x]."\n";
49                         }
50                 }
51                 $dumpedvalue=~s/^\t//;
52                 chomp $dumpedvalue;
53         }
54         
55         return "\t$prefix$key => $dumpedvalue,";
56 } #}}}
57
58 sub dumpvalues ($@) { #{{{
59         my $setup=shift;
60         my @ret;
61         while (@_) {
62                 my $key=shift;
63                 my %info=%{shift()};
64
65                 next if $key eq "plugin" || $info{type} eq "internal";
66                 
67                 push @ret, "\t# ".$info{description} if exists $info{description};
68                 
69                 if (exists $setup->{$key} && defined $setup->{$key}) {
70                         push @ret, dumpline($key, $setup->{$key}, $info{type}, "");
71                         delete $setup->{$key};
72                 }
73                 elsif (exists $info{example}) {
74                         push @ret, dumpline($key, $info{example}, $info{type}, "#");
75                 }
76                 else {
77                         push @ret, dumpline($key, "", $info{type}, "#");
78                 }
79         }
80         return @ret;
81 } #}}}
82
83 sub gendump ($) { #{{{
84         my $description=shift;
85         my %setup=(%config);
86         my @ret;
87         
88         # disable logging to syslog while dumping
89         $config{syslog}=0;
90
91         push @ret, dumpvalues(\%setup, IkiWiki::getsetup());
92         foreach my $pair (IkiWiki::Setup::getsetup()) {
93                 my $plugin=$pair->[0];
94                 my $setup=$pair->[1];
95                 my @values=dumpvalues(\%setup, @{$setup});
96                 if (@values) {
97                         push @ret, "", "\t# $plugin plugin", @values;
98                 }
99         }
100
101         unshift @ret,
102                 "#!/usr/bin/perl",
103                 "# $description",
104                 "#",
105                 "# Passing this to ikiwiki --setup will make ikiwiki generate",
106                 "# wrappers and build the wiki.",
107                 "#",
108                 "# Remember to re-run ikiwiki --setup any time you edit this file.",
109                 "use IkiWiki::Setup::Standard {";
110         push @ret, "}";
111
112         return @ret;
113 } #}}}
114
115 1