]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Setup/Standard.pm
(no commit message)
[git.ikiwiki.info.git] / IkiWiki / Setup / Standard.pm
1 #!/usr/bin/perl
3 package IkiWiki::Setup::Standard;
5 use warnings;
6 use strict;
7 use IkiWiki;
9 # Parameters to import should be all the standard ikiwiki config, in a hash.
10 sub import {
11         IkiWiki::Setup::merge($_[1]);
12 }
14 sub gendump ($@) {
15         my $class=shift;
17         "#!/usr/bin/perl",
18         "#",
19         (map { "# $_" } @_),
20         "use IkiWiki::Setup::Standard {",
21         IkiWiki::Setup::commented_dump(\&dumpline, "\t"),
22         "}"
23 }
25 sub dumpline ($$$$) {
26         my $key=shift;
27         my $value=shift;
28         my $type=shift;
29         my $prefix=shift;
30         
31         eval q{use Data::Dumper};
32         error($@) if $@;
33         local $Data::Dumper::Terse=1;
34         local $Data::Dumper::Indent=1;
35         local $Data::Dumper::Pad="\t";
36         local $Data::Dumper::Sortkeys=1;
37         local $Data::Dumper::Quotekeys=0;
38         # only the perl version preserves utf-8 in output
39         local $Data::Dumper::Useperl=1;
40         
41         my $dumpedvalue;
42         if (($type eq 'boolean' || $type eq 'integer') && $value=~/^[0-9]+$/) {
43                 # avoid quotes
44                 $dumpedvalue=$value;
45         }
46         elsif (ref $value eq 'ARRAY' && @$value && ! grep { /[^\S]/ } @$value) {
47                 # dump simple array as qw{}
48                 $dumpedvalue="[qw{".join(" ", @$value)."}]";
49         }
50         else {
51                 $dumpedvalue=Dumper($value);
52                 chomp $dumpedvalue;
53                 if (length $prefix) {
54                         # add to second and subsequent lines
55                         my @lines=split(/\n/, $dumpedvalue);
56                         $dumpedvalue="";
57                         for (my $x=0; $x <= $#lines; $x++) {
58                                 $lines[$x] =~ s/^\t//;
59                                 $dumpedvalue.="\t".($x ? $prefix : "").$lines[$x]."\n";
60                         }
61                 }
62                 $dumpedvalue=~s/^\t//;
63                 chomp $dumpedvalue;
64         }
65         
66         return "\t$prefix$key => $dumpedvalue,";
67 }
69 1