ikiwiki/IkiWiki/Setup/Standard.pm

118 lines
2.6 KiB
Perl
Raw Normal View History

#!/usr/bin/perl
# Standard ikiwiki setup module.
# Parameters to import should be all the standard ikiwiki config stuff,
2006-04-21 18:39:59 +02:00
# plus an array of wrappers to set up.
package IkiWiki::Setup::Standard;
use warnings;
use strict;
2008-07-26 18:53:07 +02:00
use IkiWiki;
sub import {
IkiWiki::Setup::merge($_[1]);
}
sub dumpline ($$$$) {
2008-07-26 18:01:10 +02:00
my $key=shift;
my $value=shift;
my $type=shift;
2008-07-26 18:01:10 +02:00
my $prefix=shift;
2008-07-26 18:46:31 +02:00
eval q{use Data::Dumper};
error($@) if $@;
local $Data::Dumper::Terse=1;
local $Data::Dumper::Indent=1;
local $Data::Dumper::Pad="\t";
local $Data::Dumper::Sortkeys=1;
local $Data::Dumper::Quotekeys=0;
# only the perl version preserves utf-8 in output
local $Data::Dumper::Useperl=1;
2008-07-26 18:46:31 +02:00
my $dumpedvalue;
if (($type eq 'boolean' || $type eq 'integer') && $value=~/^[0-9]+$/) {
# avoid quotes
$dumpedvalue=$value;
}
2008-08-04 04:01:11 +02:00
elsif (ref $value eq 'ARRAY' && @$value && ! grep { /[^\S]/ } @$value) {
# dump simple array as qw{}
$dumpedvalue="[qw{".join(" ", @$value)."}]";
}
else {
$dumpedvalue=Dumper($value);
chomp $dumpedvalue;
2008-07-27 01:49:45 +02:00
if (length $prefix) {
# add to second and subsequent lines
my @lines=split(/\n/, $dumpedvalue);
$dumpedvalue="";
for (my $x=0; $x <= $#lines; $x++) {
$lines[$x] =~ s/^\t//;
$dumpedvalue.="\t".($x ? $prefix : "").$lines[$x]."\n";
}
}
$dumpedvalue=~s/^\t//;
2008-07-27 01:49:45 +02:00
chomp $dumpedvalue;
}
2008-07-26 18:01:10 +02:00
return "\t$prefix$key => $dumpedvalue,";
}
sub dumpvalues ($@) {
2008-07-26 18:01:10 +02:00
my $setup=shift;
my @ret;
while (@_) {
my $key=shift;
my %info=%{shift()};
next if $key eq "plugin" || $info{type} eq "internal";
2008-07-26 18:01:10 +02:00
push @ret, "\t# ".$info{description} if exists $info{description};
if (exists $setup->{$key} && defined $setup->{$key}) {
push @ret, dumpline($key, $setup->{$key}, $info{type}, "");
2008-07-26 18:01:10 +02:00
delete $setup->{$key};
}
elsif (exists $info{example}) {
push @ret, dumpline($key, $info{example}, $info{type}, "#");
2008-07-26 18:01:10 +02:00
}
else {
push @ret, dumpline($key, "", $info{type}, "#");
}
2008-07-26 18:01:10 +02:00
}
return @ret;
}
2008-07-26 18:01:10 +02:00
sub gendump ($) {
2008-07-26 21:43:25 +02:00
my $description=shift;
2008-07-26 18:46:31 +02:00
my %setup=(%config);
2008-07-26 18:01:10 +02:00
my @ret;
2008-07-27 04:38:43 +02:00
# disable logging to syslog while dumping
$config{syslog}=undef;
2008-07-27 04:38:43 +02:00
push @ret, dumpvalues(\%setup, IkiWiki::getsetup());
2008-08-02 19:38:45 +02:00
foreach my $pair (IkiWiki::Setup::getsetup()) {
my $plugin=$pair->[0];
my $setup=$pair->[1];
my @values=dumpvalues(\%setup, @{$setup});
if (@values) {
push @ret, "", "\t# $plugin plugin", @values;
}
2008-07-27 01:10:11 +02:00
}
2008-07-27 00:10:01 +02:00
2008-07-26 21:43:25 +02:00
unshift @ret,
"#!/usr/bin/perl",
"# $description",
"#",
"# Passing this to ikiwiki --setup will make ikiwiki generate",
"# wrappers and build the wiki.",
"#",
"# Remember to re-run ikiwiki --setup any time you edit this file.",
"use IkiWiki::Setup::Standard {";
push @ret, "}";
2008-07-26 18:46:31 +02:00
2008-07-26 21:39:41 +02:00
return @ret;
}
1