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   188360 use strict;
  52         96  
  52         1458  
3 52     52   162 use warnings;
  52         62  
  52         5354  
4             package YAML::PP::Render;
5              
6             our $VERSION = 'v0.41.1'; # TRIAL VERSION
7              
8 52 50   52   253 use constant TRACE => $ENV{YAML_PP_TRACE} ? 1 : 0;
  52         83  
  52         40430  
9              
10             sub render_quoted {
11 491     491 0 929 my ($self, $style, $lines) = @_;
12              
13 491         672 my $quoted = '';
14 491         597 my $addspace = 0;
15              
16 491         1428 for my $i (0 .. $#$lines) {
17 1334         1478 my $line = $lines->[ $i ];
18 1334         1608 my $value = $line->{value};
19 1334         1573 my $last = $i == $#$lines;
20 1334         1473 my $first = $i == 0;
21 1334 100       1976 if ($value eq '') {
22 301 100       603 if ($first) {
    100          
23 66         94 $addspace = 1;
24             }
25             elsif ($last) {
26 66 100       164 $quoted .= ' ' if $addspace;
27             }
28             else {
29 169         202 $addspace = 0;
30 169         261 $quoted .= "\n";
31             }
32 301         415 next;
33             }
34              
35 1033 100       1535 $quoted .= ' ' if $addspace;
36 1033         1108 $addspace = 1;
37 1033 100       1447 if ($style eq '"') {
38 925 100       1916 if ($line->{orig} =~ m/\\$/) {
39 82         221 $line->{value} =~ s/\\$//;
40 82         203 $value =~ s/\\$//;
41 82         109 $addspace = 0;
42             }
43             }
44 1033         1695 $quoted .= $value;
45             }
46 491         1038 return $quoted;
47             }
48              
49             sub render_block_scalar {
50 1712     1712 0 3567 my ($self, $block_type, $chomp, $lines) = @_;
51              
52 1712         2181 my ($folded, $keep, $trim);
53 1712 100       3406 if ($block_type eq '>') {
54 532         698 $folded = 1;
55             }
56 1712 100       3997 if ($chomp eq '+') {
    100          
57 189         232 $keep = 1;
58             }
59             elsif ($chomp eq '-') {
60 305         400 $trim = 1;
61             }
62              
63 1712         2448 my $string = '';
64 1712 100       4215 if (not $keep) {
65             # remove trailing empty lines
66 1523         2526 while (@$lines) {
67 1715 100       3417 last if $lines->[-1] ne '';
68 222         385 pop @$lines;
69             }
70             }
71 1712 100       2590 if ($folded) {
72              
73 532         654 my $prev = 'START';
74 532         614 my $trailing = '';
75 532 100       931 if ($keep) {
76 39   100     176 while (@$lines and $lines->[-1] eq '') {
77 39         48 pop @$lines;
78 39         90 $trailing .= "\n";
79             }
80             }
81 532         1533 for my $i (0 .. $#$lines) {
82 1183         1418 my $line = $lines->[ $i ];
83              
84 1183 100       2443 my $type = $line eq ''
    100          
85             ? 'EMPTY'
86             : $line =~ m/\A[ \t]/
87             ? 'MORE'
88             : 'CONTENT';
89              
90 1183 100 100     6394 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       522 if ($type ne 'CONTENT') {
    50          
95 142         172 $string .= "\n";
96             }
97             elsif ($type eq 'CONTENT') {
98 146         211 $string .= ' ';
99             }
100             }
101             elsif ($prev eq 'START' and $type eq 'EMPTY') {
102 54         75 $string .= "\n";
103 54         65 $type = 'START';
104             }
105             elsif ($prev eq 'EMPTY' and $type ne 'CONTENT') {
106 126         138 $string .= "\n";
107             }
108              
109 1183         1486 $string .= $line;
110              
111 1183 100 100     2278 if ($type eq 'MORE' and $i < $#$lines) {
112 98         113 $string .= "\n";
113             }
114              
115 1183         1587 $prev = $type;
116             }
117 532 100       1016 if ($keep) {
118 39         49 $string .= $trailing;
119             }
120 532 100 100     1610 $string .= "\n" if @$lines and not $trim;
121             }
122             else {
123 1180         3123 for my $i (0 .. $#$lines) {
124 2096         3048 $string .= $lines->[ $i ];
125 2096 100 100     6151 $string .= "\n" if ($i != $#$lines or not $trim);
126             }
127             }
128 1712         1976 TRACE and warn __PACKAGE__.':'.__LINE__.$".Data::Dumper->Dump([\$string], ['string']);
129 1712         4010 return $string;
130             }
131              
132             sub render_multi_val {
133 242     242 0 464 my ($self, $multi) = @_;
134 242         376 my $string = '';
135 242         358 my $start = 1;
136 242         552 for my $line (@$multi) {
137 564 100       868 if (not $start) {
138 297 100       549 if ($line eq '') {
139 25         49 $string .= "\n";
140 25         41 $start = 1;
141             }
142             else {
143 272         483 $string .= " $line";
144             }
145             }
146             else {
147 267         443 $string .= $line;
148 267         383 $start = 0;
149             }
150             }
151 242         535 return $string;
152             }
153              
154              
155             1;