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   146402 use strict;
  52         79  
  52         1438  
3 52     52   172 use warnings;
  52         64  
  52         5144  
4             package YAML::PP::Render;
5              
6             our $VERSION = 'v0.40.0'; # VERSION
7              
8 52 50   52   220 use constant TRACE => $ENV{YAML_PP_TRACE} ? 1 : 0;
  52         77  
  52         41379  
9              
10             sub render_quoted {
11 489     489 0 970 my ($self, $style, $lines) = @_;
12              
13 489         665 my $quoted = '';
14 489         713 my $addspace = 0;
15              
16 489         1539 for my $i (0 .. $#$lines) {
17 1330         1688 my $line = $lines->[ $i ];
18 1330         1755 my $value = $line->{value};
19 1330         1822 my $last = $i == $#$lines;
20 1330         1536 my $first = $i == 0;
21 1330 100       2018 if ($value eq '') {
22 301 100       638 if ($first) {
    100          
23 66         82 $addspace = 1;
24             }
25             elsif ($last) {
26 66 100       134 $quoted .= ' ' if $addspace;
27             }
28             else {
29 169         273 $addspace = 0;
30 169         260 $quoted .= "\n";
31             }
32 301         454 next;
33             }
34              
35 1029 100       1640 $quoted .= ' ' if $addspace;
36 1029         1105 $addspace = 1;
37 1029 100       1549 if ($style eq '"') {
38 921 100       2057 if ($line->{orig} =~ m/\\$/) {
39 82         282 $line->{value} =~ s/\\$//;
40 82         245 $value =~ s/\\$//;
41 82         104 $addspace = 0;
42             }
43             }
44 1029         1796 $quoted .= $value;
45             }
46 489         1049 return $quoted;
47             }
48              
49             sub render_block_scalar {
50 1710     1710 0 3961 my ($self, $block_type, $chomp, $lines) = @_;
51              
52 1710         2333 my ($folded, $keep, $trim);
53 1710 100       3832 if ($block_type eq '>') {
54 532         740 $folded = 1;
55             }
56 1710 100       5439 if ($chomp eq '+') {
    100          
57 189         232 $keep = 1;
58             }
59             elsif ($chomp eq '-') {
60 305         383 $trim = 1;
61             }
62              
63 1710         2318 my $string = '';
64 1710 100       3070 if (not $keep) {
65             # remove trailing empty lines
66 1521         2876 while (@$lines) {
67 1713 100       3534 last if $lines->[-1] ne '';
68 222         396 pop @$lines;
69             }
70             }
71 1710 100       3142 if ($folded) {
72              
73 532         697 my $prev = 'START';
74 532         669 my $trailing = '';
75 532 100       1029 if ($keep) {
76 39   100     137 while (@$lines and $lines->[-1] eq '') {
77 39         47 pop @$lines;
78 39         95 $trailing .= "\n";
79             }
80             }
81 532         1653 for my $i (0 .. $#$lines) {
82 1183         1503 my $line = $lines->[ $i ];
83              
84 1183 100       2668 my $type = $line eq ''
    100          
85             ? 'EMPTY'
86             : $line =~ m/\A[ \t]/
87             ? 'MORE'
88             : 'CONTENT';
89              
90 1183 100 100     5141 if ($prev eq 'MORE' and $type eq 'EMPTY') {
    100 100        
    100 100        
    100          
91 30         37 $type = 'MORE';
92             }
93             elsif ($prev eq 'CONTENT') {
94 288 100       576 if ($type ne 'CONTENT') {
    50          
95 142         168 $string .= "\n";
96             }
97             elsif ($type eq 'CONTENT') {
98 146         188 $string .= ' ';
99             }
100             }
101             elsif ($prev eq 'START' and $type eq 'EMPTY') {
102 54         79 $string .= "\n";
103 54         85 $type = 'START';
104             }
105             elsif ($prev eq 'EMPTY' and $type ne 'CONTENT') {
106 126         150 $string .= "\n";
107             }
108              
109 1183         1551 $string .= $line;
110              
111 1183 100 100     2187 if ($type eq 'MORE' and $i < $#$lines) {
112 98         104 $string .= "\n";
113             }
114              
115 1183         1705 $prev = $type;
116             }
117 532 100       940 if ($keep) {
118 39         50 $string .= $trailing;
119             }
120 532 100 100     1845 $string .= "\n" if @$lines and not $trim;
121             }
122             else {
123 1178         3363 for my $i (0 .. $#$lines) {
124 2092         3288 $string .= $lines->[ $i ];
125 2092 100 100     6474 $string .= "\n" if ($i != $#$lines or not $trim);
126             }
127             }
128 1710         2029 TRACE and warn __PACKAGE__.':'.__LINE__.$".Data::Dumper->Dump([\$string], ['string']);
129 1710         3920 return $string;
130             }
131              
132             sub render_multi_val {
133 239     239 0 507 my ($self, $multi) = @_;
134 239         335 my $string = '';
135 239         286 my $start = 1;
136 239         490 for my $line (@$multi) {
137 558 100       770 if (not $start) {
138 294 100       514 if ($line eq '') {
139 25         37 $string .= "\n";
140 25         36 $start = 1;
141             }
142             else {
143 269         488 $string .= " $line";
144             }
145             }
146             else {
147 264         445 $string .= $line;
148 264         350 $start = 0;
149             }
150             }
151 239         568 return $string;
152             }
153              
154              
155             1;