File Coverage

blib/lib/YAML/PP/Render.pm
Criterion Covered Total %
statement 87 87 100.0
branch 56 58 96.5
condition 21 21 100.0
subroutine 6 6 100.0
pod 0 3 0.0
total 170 175 97.1


line stmt bran cond sub pod time code
1             # ABSTRACT: YAML::PP Rendering functions
2 52     52   145371 use strict;
  52         84  
  52         1483  
3 52     52   206 use warnings;
  52         65  
  52         5792  
4             package YAML::PP::Render;
5              
6             our $VERSION = 'v0.41.0'; # VERSION
7              
8 52 50   52   233 use constant TRACE => $ENV{YAML_PP_TRACE} ? 1 : 0;
  52         80  
  52         40981  
9              
10             sub render_quoted {
11 491     491 0 953 my ($self, $style, $lines) = @_;
12              
13 491         680 my $quoted = '';
14 491         628 my $addspace = 0;
15              
16 491         1439 for my $i (0 .. $#$lines) {
17 1334         1521 my $line = $lines->[ $i ];
18 1334         1606 my $value = $line->{value};
19 1334         1545 my $last = $i == $#$lines;
20 1334         1499 my $first = $i == 0;
21 1334 100       1964 if ($value eq '') {
22 301 100       649 if ($first) {
    100          
23 66         80 $addspace = 1;
24             }
25             elsif ($last) {
26 66 100       139 $quoted .= ' ' if $addspace;
27             }
28             else {
29 169         197 $addspace = 0;
30 169         240 $quoted .= "\n";
31             }
32 301         444 next;
33             }
34              
35 1033 100       1516 $quoted .= ' ' if $addspace;
36 1033         1026 $addspace = 1;
37 1033 100       1493 if ($style eq '"') {
38 925 100       1959 if ($line->{orig} =~ m/\\$/) {
39 82         234 $line->{value} =~ s/\\$//;
40 82         198 $value =~ s/\\$//;
41 82         110 $addspace = 0;
42             }
43             }
44 1033         1801 $quoted .= $value;
45             }
46 491         1156 return $quoted;
47             }
48              
49             sub render_block_scalar {
50 1712     1712 0 3744 my ($self, $block_type, $chomp, $lines) = @_;
51              
52 1712         2184 my ($folded, $keep, $trim);
53 1712 100       3385 if ($block_type eq '>') {
54 532         727 $folded = 1;
55             }
56 1712 100       5327 if ($chomp eq '+') {
    100          
57 189         308 $keep = 1;
58             }
59             elsif ($chomp eq '-') {
60 305         417 $trim = 1;
61             }
62              
63 1712         2043 my $string = '';
64 1712 100       2866 if (not $keep) {
65             # remove trailing empty lines
66 1523         2754 while (@$lines) {
67 1715 100       3515 last if $lines->[-1] ne '';
68 222         393 pop @$lines;
69             }
70             }
71 1712 100       2843 if ($folded) {
72              
73 532         755 my $prev = 'START';
74 532         691 my $trailing = '';
75 532 100       1016 if ($keep) {
76 39   100     142 while (@$lines and $lines->[-1] eq '') {
77 39         60 pop @$lines;
78 39         102 $trailing .= "\n";
79             }
80             }
81 532         1544 for my $i (0 .. $#$lines) {
82 1183         1418 my $line = $lines->[ $i ];
83              
84 1183 100       2662 my $type = $line eq ''
    100          
85             ? 'EMPTY'
86             : $line =~ m/\A[ \t]/
87             ? 'MORE'
88             : 'CONTENT';
89              
90 1183 100 100     5186 if ($prev eq 'MORE' and $type eq 'EMPTY') {
    100 100        
    100 100        
    100          
91 30         39 $type = 'MORE';
92             }
93             elsif ($prev eq 'CONTENT') {
94 288 100       609 if ($type ne 'CONTENT') {
    50          
95 142         162 $string .= "\n";
96             }
97             elsif ($type eq 'CONTENT') {
98 146         207 $string .= ' ';
99             }
100             }
101             elsif ($prev eq 'START' and $type eq 'EMPTY') {
102 54         80 $string .= "\n";
103 54         76 $type = 'START';
104             }
105             elsif ($prev eq 'EMPTY' and $type ne 'CONTENT') {
106 126         167 $string .= "\n";
107             }
108              
109 1183         1563 $string .= $line;
110              
111 1183 100 100     2199 if ($type eq 'MORE' and $i < $#$lines) {
112 98         111 $string .= "\n";
113             }
114              
115 1183         1745 $prev = $type;
116             }
117 532 100       1005 if ($keep) {
118 39         50 $string .= $trailing;
119             }
120 532 100 100     1836 $string .= "\n" if @$lines and not $trim;
121             }
122             else {
123 1180         3242 for my $i (0 .. $#$lines) {
124 2096         3027 $string .= $lines->[ $i ];
125 2096 100 100     6286 $string .= "\n" if ($i != $#$lines or not $trim);
126             }
127             }
128 1712         1936 TRACE and warn __PACKAGE__.':'.__LINE__.$".Data::Dumper->Dump([\$string], ['string']);
129 1712         3973 return $string;
130             }
131              
132             sub render_multi_val {
133 242     242 0 443 my ($self, $multi) = @_;
134 242         374 my $string = '';
135 242         319 my $start = 1;
136 242         462 for my $line (@$multi) {
137 564 100       838 if (not $start) {
138 297 100       596 if ($line eq '') {
139 25         35 $string .= "\n";
140 25         30 $start = 1;
141             }
142             else {
143 272         502 $string .= " $line";
144             }
145             }
146             else {
147 267         463 $string .= $line;
148 267         356 $start = 0;
149             }
150             }
151 242         531 return $string;
152             }
153              
154              
155             1;