aboutsummaryrefslogtreecommitdiff
path: root/IkiWiki/Setup/Yaml.pm
blob: 6bf20f480d523ad5144dc6d1b61ec03d1f0118b5 (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
#!/usr/bin/perl

package IkiWiki::Setup::Yaml;

use warnings;
use strict;
use IkiWiki;
use Encode;

sub loaddump ($$) {
	my $class=shift;
	my $content=shift;

	eval q{use YAML::XS};
	die $@ if $@;
	IkiWiki::Setup::merge(Load(encode_utf8($content)));
}

sub gendump ($@) {
	my $class=shift;
	
	"# IkiWiki::Setup::Yaml - YAML formatted setup file",
	"#",
	(map { "# $_" } @_),
	"#",
	IkiWiki::Setup::commented_dump(\&dumpline, "")
}


sub dumpline ($$$$) {
	my $key=shift;
	my $value=shift;
	my $type=shift;
	my $prefix=shift;
	
	eval q{use YAML::XS};
	die $@ if $@;
	$YAML::XS::QuoteNumericStrings=0;

	my $dump=decode_utf8(Dump({$key => $value}));
	$dump=~s/^---\n//; # yaml header, we don't want
	chomp $dump;
	if (length $prefix) {
		$dump=join("\n", map { $prefix.$_ } split(/\n/, $dump));
	}
	return $dump;
}

1