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
#!/usr/bin/perl
use strict;
use warnings;
use YAML::PP::Highlight;
use Encode;
use Getopt::Long;
GetOptions(
'help|h' => \my $help,
'expand-tabs|et!' => \my $expand_tabs,
) or usage(1);
$expand_tabs = 1 unless defined $expand_tabs;
usage(0) if $help;
my ($file) = @ARGV;
my $yaml;
unless ($file) {
$yaml = do { local $/; <STDIN> };
$yaml = decode_utf8($yaml);
}
my $error;
my $tokens;
if (defined $file) {
($error, $tokens) = YAML::PP::Parser->yaml_to_tokens( file => $file );
}
else {
($error, $tokens) = YAML::PP::Parser->yaml_to_tokens( string => $yaml );
}
my $highlighted = YAML::PP::Highlight->ansicolored($tokens, expand_tabs => $expand_tabs);
print encode_utf8 $highlighted;
if ($error) {
die $error;
}
sub usage {
my ($rc) = @_;
print <<"EOM";
Usage:
$0 [options] < file
$0 [options] file
Options:
--expand-tabs --et Expand tabs to 8 spaces (default true)
--no-expand-tabs --no-et Don't expand tabs
EOM
exit $rc;
}