2010-03-19 20:50:59 +01:00
|
|
|
#!/usr/bin/perl
|
|
|
|
|
|
|
|
package IkiWiki::Setup::Yaml;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
|
|
|
use IkiWiki;
|
2011-05-12 23:50:25 +02:00
|
|
|
use Encode;
|
2010-03-19 20:50:59 +01:00
|
|
|
|
|
|
|
sub loaddump ($$) {
|
|
|
|
my $class=shift;
|
|
|
|
my $content=shift;
|
|
|
|
|
2012-01-29 03:01:49 +01:00
|
|
|
eval q{use YAML::XS};
|
2010-03-19 21:09:07 +01:00
|
|
|
die $@ if $@;
|
2011-05-12 23:50:25 +02:00
|
|
|
IkiWiki::Setup::merge(Load(encode_utf8($content)));
|
2010-03-19 20:50:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub gendump ($@) {
|
|
|
|
my $class=shift;
|
2010-03-19 21:09:07 +01:00
|
|
|
|
2010-03-19 20:50:59 +01:00
|
|
|
"# 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;
|
|
|
|
|
2012-01-29 03:01:49 +01:00
|
|
|
eval q{use YAML::XS};
|
2010-03-24 21:12:35 +01:00
|
|
|
die $@ if $@;
|
2016-01-18 09:45:18 +01:00
|
|
|
no warnings 'once';
|
2012-01-29 03:01:49 +01:00
|
|
|
$YAML::XS::QuoteNumericStrings=0;
|
2016-01-18 09:45:18 +01:00
|
|
|
use warnings;
|
2010-03-24 21:12:35 +01:00
|
|
|
|
2012-01-29 03:01:49 +01:00
|
|
|
my $dump=decode_utf8(Dump({$key => $value}));
|
|
|
|
$dump=~s/^---\n//; # yaml header, we don't want
|
2010-03-19 20:50:59 +01:00
|
|
|
chomp $dump;
|
|
|
|
if (length $prefix) {
|
2010-03-19 22:54:58 +01:00
|
|
|
$dump=join("\n", map { $prefix.$_ } split(/\n/, $dump));
|
2010-03-19 20:50:59 +01:00
|
|
|
}
|
|
|
|
return $dump;
|
|
|
|
}
|
|
|
|
|
|
|
|
1
|