]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Setup/Standard.pm
Merge branch 'master' into sipb
[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         # only the perl version preserves utf-8 in output
30         local $Data::Dumper::Useperl=1;
31         
32         my $dumpedvalue;
33         if (($type eq 'boolean' || $type eq 'integer') && $value=~/^[0-9]+$/) {
34                 # avoid quotes
35                 $dumpedvalue=$value;
36         }
37         elsif (ref $value eq 'ARRAY' && @$value && ! grep { /[^\S]/ } @$value) {
38                 # dump simple array as qw{}
39                 $dumpedvalue="[qw{".join(" ", @$value)."}]";
40         }
41         else {
42                 $dumpedvalue=Dumper($value);
43                 chomp $dumpedvalue;
44                 if (length $prefix) {
45                         # add to second and subsequent lines
46                         my @lines=split(/\n/, $dumpedvalue);
47                         $dumpedvalue="";
48                         for (my $x=0; $x <= $#lines; $x++) {
49                                 $lines[$x] =~ s/^\t//;
50                                 $dumpedvalue.="\t".($x ? $prefix : "").$lines[$x]."\n";
51                         }
52                 }
53                 $dumpedvalue=~s/^\t//;
54                 chomp $dumpedvalue;
55         }
56         
57         return "\t$prefix$key => $dumpedvalue,";
58 }
59
60 sub dumpvalues ($@) {
61         my $setup=shift;
62         my @ret;
63         while (@_) {
64                 my $key=shift;
65                 my %info=%{shift()};
66
67                 next if $key eq "plugin" || $info{type} eq "internal";
68                 
69                 push @ret, "\t# ".$info{description} if exists $info{description};
70                 
71                 if (exists $setup->{$key} && defined $setup->{$key}) {
72                         push @ret, dumpline($key, $setup->{$key}, $info{type}, "");
73                         delete $setup->{$key};
74                 }
75                 elsif (exists $info{example}) {
76                         push @ret, dumpline($key, $info{example}, $info{type}, "#");
77                 }
78                 else {
79                         push @ret, dumpline($key, "", $info{type}, "#");
80                 }
81         }
82         return @ret;
83 }
84
85 sub gendump ($) {
86         my $description=shift;
87         my %setup=(%config);
88         my @ret;
89         
90         # disable logging to syslog while dumping
91         $config{syslog}=undef;
92
93         eval q{use Text::Wrap};
94         die $@ if $@;
95
96         my %section_plugins;
97         push @ret, dumpvalues(\%setup, IkiWiki::getsetup());
98         foreach my $pair (IkiWiki::Setup::getsetup()) {
99                 my $plugin=$pair->[0];
100                 my $setup=$pair->[1];
101                 my %s=@{$setup};
102                 my $section=$s{plugin}->{section};
103                 push @{$section_plugins{$section}}, $plugin;
104                 if (@{$section_plugins{$section}} == 1) {
105                         push @ret, "", "\t".("#" x 70), "\t# $section plugins",
106                                 sub {
107                                         wrap("\t#   (", "\t#    ",
108                                                 join(", ", @{$section_plugins{$section}})).")"
109                                 },
110                                 "\t".("#" x 70);
111                 }
112
113                 my @values=dumpvalues(\%setup, @{$setup});
114                 if (@values) {
115                         push @ret, "", "\t# $plugin plugin", @values;
116                 }
117         }
118
119         unshift @ret,
120                 "#!/usr/bin/perl",
121                 "# $description",
122                 "#",
123                 "# Passing this to ikiwiki --setup will make ikiwiki generate",
124                 "# wrappers and build the wiki.",
125                 "#",
126                 "# Remember to re-run ikiwiki --setup any time you edit this file.",
127                 "use IkiWiki::Setup::Standard {";
128         push @ret, "}";
129
130         return map { ref $_ ? $_->() : $_ } @ret;
131 }
132
133 1