Merge branch 'tova' into autoconfig
[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') {
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                 if (length $prefix) {
44                         # add to second and subsequent lines
45                         my @lines=split(/\n/, $dumpedvalue);
46                         $dumpedvalue="";
47                         for (my $x=0; $x <= $#lines; $x++) {
48                                 $lines[$x] =~ s/^\t//;
49                                 $dumpedvalue.="\t".($x ? $prefix : "").$lines[$x]."\n";
50                         }
51                 }
52                 $dumpedvalue=~s/^\t//;
53                 chomp $dumpedvalue;
54         }
55         
56         return "\t$prefix$key => $dumpedvalue,";
57 } #}}}
58
59 sub dumpvalues ($@) { #{{{
60         my $setup=shift;
61         my @ret;
62         while (@_) {
63                 my $key=shift;
64                 my %info=%{shift()};
65
66                 next if $info{type} eq "internal";
67                 
68                 push @ret, "\t# ".$info{description} if exists $info{description};
69                 
70                 if (exists $setup->{$key} && defined $setup->{$key}) {
71                         push @ret, dumpline($key, $setup->{$key}, $info{type}, "");
72                         delete $setup->{$key};
73                 }
74                 elsif (exists $info{example}) {
75                         push @ret, dumpline($key, $info{example}, $info{type}, "#");
76                 }
77                 else {
78                         push @ret, dumpline($key, "", $info{type}, "#");
79                 }
80         }
81         return @ret;
82 } #}}}
83
84 sub gendump ($) { #{{{
85         my $description=shift;
86         my %setup=(%config);
87         my @ret;
88         
89         # disable logging to syslog while dumping
90         $config{syslog}=0;
91
92         push @ret, "\t# basic setup";
93         push @ret, dumpvalues(\%setup, IkiWiki::getsetup());
94
95         # Load all plugins, so that all setup options are available.
96         # (But skip a few problematic external demo plugins.)
97         my @plugins=grep { ! /^(externaldemo|pythondemo|\Q$config{rcs}\E)$/ }
98                 sort(IkiWiki::listplugins());
99         unshift @plugins, $config{rcs} if $config{rcs}; # rcs plugin 1st
100         foreach my $plugin (@plugins) {
101                 eval { IkiWiki::loadplugin($plugin) };
102                 if (exists $IkiWiki::hooks{checkconfig}{$plugin}{call}) {
103                         my @s=eval { $IkiWiki::hooks{checkconfig}{$plugin}{call}->() };
104                 }
105         }
106
107         foreach my $id (@plugins) {
108                 if (exists $IkiWiki::hooks{getsetup}{$id}{call}) {
109                         # use an array rather than a hash, to preserve order
110                         my @s=eval { $IkiWiki::hooks{getsetup}{$id}{call}->() };
111                         next unless @s;
112                         push @ret, "", "\t# $id plugin";
113                         push @ret, dumpvalues(\%setup, @s);
114                 }
115         }
116         
117         unshift @ret,
118                 "#!/usr/bin/perl",
119                 "# $description",
120                 "#",
121                 "# Passing this to ikiwiki --setup will make ikiwiki generate",
122                 "# wrappers and build the wiki.",
123                 "#",
124                 "# Remember to re-run ikiwiki --setup any time you edit this file.",
125                 "use IkiWiki::Setup::Standard {";
126         push @ret, "}";
127
128         return @ret;
129 } #}}}
130
131 1