die "Unrecognised file type\n";
}
+my $format = $opt{format}; # override distinct from image fallback
if ($opt{output}) {{
# derive format from file extension
- if ($opt{output} =~ /\.yaml$/) {
- $opt{format} //= 'yaml';
- }
- elsif ($opt{output} !~ /\.txt$/) {
- $opt{format} //= 'pnm';
- last; # images are written directly to file
- }
-
- # redirect standard output to given file
- open my $output, '>', $opt{output}
- or die "Cannot output to '$opt{output}': $!";
- select $output;
-}}
+ $format //= $1 if $opt{output} =~ /\.([^.]+)$/;
-# output with user-preferred formatting
-given ($opt{format}) {
-when ('pnm') {
- require Games::Wormy::Render;
-
- my @request;
- if ($opt{levels}) {
- # find all numeric values in argument
- @request = $opt{levels} =~ /(\d+)/g;
+ if ($format ~~ [qw{ yaml json txt }]) {
+ # redirect standard output to given file
+ open my $output, '>', $opt{output}
+ or die "Cannot output to '$opt{output}': $!";
+ select $output;
}
else {
- # default to all singleplayer levels
- @request = 0 .. $data->{levelcount}->{single} - 1;
+ # images are written directly to file
}
- @request or die "no levels found or specified\n";
+}}
+else {
+ $format //= 'txt';
+}
- my $img = Games::Wormy::Render->composite(
- map { $data->{levels}->[$_] } @request
- ) or die "empty result for levels\n";
- $img->write(
- $opt{output} ? (file => $opt{output}) : (fh => \*STDOUT, type => 'pnm')
- ) or die $img->errstr;
+# output with user-preferred formatting
+given ($format) {
+when ('json') {
+ require JSON;
+ say JSON->new->encode($data);
}
when ('yaml') {
# full data in yaml (human-readable) formatting
print $yml;
}
-default {
+when ('txt') {
print $data->{name};
print " ($data->{description})" if defined $data->{description};
print "\n";
}
print "\n";
}
+default {
+ require Games::Wormy::Render;
+
+ my @request;
+ if ($opt{levels}) {
+ # find all numeric values in argument
+ @request = $opt{levels} =~ /(\d+)/g;
+ }
+ else {
+ # default to all levels
+ @request = 0 .. $data->{levelcount}->{total} - 1;
+ }
+ @request or die "no levels found or specified\n";
+
+ my $img = Games::Wormy::Render->composite(
+ map { $data->{levels}->[$_] } @request
+ ) or die "empty result for levels\n";
+ if ($format ~~ 'pbm') {
+ $img = $img->to_paletted({make_colors => 'mono'});
+ $opt{format} = 'pnm';
+ }
+ $img->write(
+ $opt{output} ? (file => $opt{output}) : (fh => \*STDOUT),
+ type => $opt{format},
+ ) or die $img->errstr;
+}
}
__END__
=head1 SYNOPSIS
- parse-wormedit [--format=<type>] [--levels=<number>] [--output=<file.ext>] <input.lvl>
+ parse-wormedit [--format=<type>] [--level=<number>] [--output=<file.ext>] <input.lvl>
=head1 DESCRIPTION
All parsed data in YAML syntax.
+=item json
+
+Parsed data in JSON syntax.
+
=item pnm, png, bmp, ...
Image drawing of rendered levels.