parse-wormedit: raw output in yaml
authorMischa Poslawsky <wormy@shiar.org>
Sun, 1 Mar 2009 15:11:09 +0000 (16:11 +0100)
committerMischa Poslawsky <wormy@shiar.org>
Mon, 2 Mar 2009 21:42:36 +0000 (22:42 +0100)
Format --raw data using YAML, which should be even more human-readable
than JSON, especially using inline hashes (requires some custom
replacements because no formatting module seems to support this yet).

parse-wormedit

index bc0d926c27c52cfd5247365e6fcadb1f7a10ecab..8c2f2c9b56db6fc2ffdd4f220749a448a704394e 100755 (executable)
@@ -413,9 +413,26 @@ else {
 
 # output with user-preferred formatting
 if ($opt{raw}) {
-       require JSON::XS;
-       my $output = JSON::XS->new->ascii->canonical->pretty->allow_nonref;
-       print $output->encode($data), "\n";
+       # full data in yaml (human-readable) formatting
+       require YAML;
+       local $YAML::CompressSeries;
+             $YAML::CompressSeries = 0;
+       my $yml = "# Wormy levelset\n" . YAML::Dump($data);
+
+       # inline format of short hashes
+       $yml =~ s{
+               ^(\ *) - \n                          # array indicator
+               ((?:\1\ \ [a-z0-9]{1,5}:\ *\d+\n)+)  # simple hash declaration
+               (?!\1\ )                             # no further children
+       }[
+               my ($indent, $value) = ($1, $2);
+               chop $value;
+               $value =~ s/^ +//gm;
+               $value =~ s/\n/, /g;
+               "$indent- {$value}\n";
+       ]egmx;
+
+       print $yml;
 }
 else {
        print $data->{name};