aboutsummaryrefslogtreecommitdiff
path: root/IkiWiki/Setup/Standard.pm
blob: c8506930475a0c28ead9e613e7cde64d982b6db9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/perl

package IkiWiki::Setup::Standard;

use warnings;
use strict;
use IkiWiki;

# Parameters to import should be all the standard ikiwiki config, in a hash.
sub import {
	IkiWiki::Setup::merge($_[1]);
}

sub gendump ($@) {
	my $class=shift;

	"#!/usr/bin/perl",
	"#",
	(map { "# $_" } @_),
	"use IkiWiki::Setup::Standard {",
	IkiWiki::Setup::commented_dump(\&dumpline, "\t"),
	"}"
}

sub dumpline ($$$$) {
	my $key=shift;
	my $value=shift;
	my $type=shift;
	my $prefix=shift;
	
	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;
	
	my $dumpedvalue;
	if (($type eq 'boolean' || $type eq 'integer') && $value=~/^[0-9]+$/) {
		# avoid quotes
		$dumpedvalue=$value;
	}
	elsif (ref $value eq 'ARRAY' && @$value && ! grep { /[^\S]/ } @$value) {
		# dump simple array as qw{}
		$dumpedvalue="[qw{".join(" ", @$value)."}]";
	}
	else {
		$dumpedvalue=Dumper($value);
		chomp $dumpedvalue;
		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//;
		chomp $dumpedvalue;
	}
	
	return "\t$prefix$key => $dumpedvalue,";
}

1