YAML-Tidy
view release on metacpan or search on metacpan
lib/YAML/Tidy.pm view on Meta::CPAN
}
$event->{nextline} = -1;
if ($i < $#$events) {
my $next = $events->[ $i + 1 ];
my $nextline = $next->{start}->{line};
$event->{nextline} = $nextline;
}
_pp($event) if DEBUG;
}
$end->{id} = $id + 1;
_pp($end) if DEBUG;
$self->{tree} = $docs;
return $docs;
}
sub _parse($self, $yaml) {
my @events;
YAML::LibYAML::API::XS::parse_events($yaml, \@events);
return \@events;
}
sub _pp($event) {
my $name = $event->{name};
my $level = $event->{level};
$name =~ s/_event$//;
my $fmt = '%2d %-10s) <L %2d C %2d> <L %2d C %2d> %-14s';
my $indent = $level*2+2;
my $lstr = (' ' x $indent) . $level;
my @args = (
$event->{id}, $lstr,
$event->{start}->{line}, $event->{start}->{column},
$event->{end}->{line}, $event->{end}->{column},
$name,
);
if ($name =~ m/scalar|alias/) {
local $Data::Dumper::Useqq = 1;
my $str = Data::Dumper->Dump([$event->{value}], ['value']);
chomp $str;
$str =~ s/^\$value = //;
$fmt .= " %s";
push @args, $str;
}
elsif ($name =~ m/end/) {
}
else {
}
$fmt .= "\n";
printf $fmt, @args;
}
sub _debug_lines($self) {
say "====================================";
say for @{ $self->{lines} };
say "====================================";
}
sub highlight($self, $yaml, $type = 'ansi') {
my ($error, $tokens) = YAML::PP::Parser->yaml_to_tokens(string => $yaml);
if ($error) {
$tokens = [];
my @lines = split m/(?<=\n)/, $yaml;
for my $line (@lines) {
if ($line =~ s/( +\n)//) {
push @$tokens, { value => $line, name => 'PLAIN' };
push @$tokens, { value => $1, name => 'TRAILING_SPACE' };
next;
}
push @$tokens, { value => $line, name => 'PLAIN' };
}
}
if ($type eq 'html') {
return YAML::PP::Highlight->htmlcolored($tokens);
}
return YAML::PP::Highlight->ansicolored($tokens);
}
1;
__END__
=pod
=encoding utf-8
=head1 NAME
YAML::Tidy - Tidy YAML files
=head1 SYNOPSIS
% cat in.yaml
a: # a comment
b:
c: d
% yamltidy in.yaml
a: # a comment
b:
c: d
=head1 DESCRIPTION
yamltidy is a linter or rather a formatter for YAML files.
It can adjust formatting without removing comments or blank lines.
For examples see L<https://perlpunk.github.io/yamltidy>
The code can be found at L<https://github.com/perlpunk/yamltidy>.
=head1 FEATURES
=over
=item Trim trailing spaces
=item Adjust indentation
=item Add or remove headers and footers
=item Remove unnecessary quotes (currently according to the YAML 1.2 Core Schema)
( run in 0.749 second using v1.01-cache-2.11-cpan-71847e10f99 )