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 51     51   145233 use strict;
  51         86  
  51         1460  
3 51     51   173 use warnings;
  51         59  
  51         3551  
4             package YAML::PP::Render;
5              
6             our $VERSION = 'v0.39.0'; # VERSION
7              
8 51 50   51   1852 use constant TRACE => $ENV{YAML_PP_TRACE} ? 1 : 0;
  51         143  
  51         40730  
9              
10             sub render_quoted {
11 489     489 0 985 my ($self, $style, $lines) = @_;
12              
13 489         645 my $quoted = '';
14 489         638 my $addspace = 0;
15              
16 489         1373 for my $i (0 .. $#$lines) {
17 1330         1629 my $line = $lines->[ $i ];
18 1330         1678 my $value = $line->{value};
19 1330         1717 my $last = $i == $#$lines;
20 1330         1593 my $first = $i == 0;
21 1330 100       1974 if ($value eq '') {
22 301 100       621 if ($first) {
    100          
23 66         85 $addspace = 1;
24             }
25             elsif ($last) {
26 66 100       139 $quoted .= ' ' if $addspace;
27             }
28             else {
29 169         204 $addspace = 0;
30 169         255 $quoted .= "\n";
31             }
32 301         460 next;
33             }
34              
35 1029 100       1632 $quoted .= ' ' if $addspace;
36 1029         1080 $addspace = 1;
37 1029 100       1636 if ($style eq '"') {
38 921 100       2214 if ($line->{orig} =~ m/\\$/) {
39 82         262 $line->{value} =~ s/\\$//;
40 82         227 $value =~ s/\\$//;
41 82         110 $addspace = 0;
42             }
43             }
44 1029         1823 $quoted .= $value;
45             }
46 489         1124 return $quoted;
47             }
48              
49             sub render_block_scalar {
50 1710     1710 0 3684 my ($self, $block_type, $chomp, $lines) = @_;
51              
52 1710         2348 my ($folded, $keep, $trim);
53 1710 100       3475 if ($block_type eq '>') {
54 532         648 $folded = 1;
55             }
56 1710 100       4134 if ($chomp eq '+') {
    100          
57 189         230 $keep = 1;
58             }
59             elsif ($chomp eq '-') {
60 305         505 $trim = 1;
61             }
62              
63 1710         2212 my $string = '';
64 1710 100       2888 if (not $keep) {
65             # remove trailing empty lines
66 1521         3034 while (@$lines) {
67 1713 100       3599 last if $lines->[-1] ne '';
68 222         427 pop @$lines;
69             }
70             }
71 1710 100       2868 if ($folded) {
72              
73 532         695 my $prev = 'START';
74 532         722 my $trailing = '';
75 532 100       1040 if ($keep) {
76 39   100     172 while (@$lines and $lines->[-1] eq '') {
77 39         43 pop @$lines;
78 39         96 $trailing .= "\n";
79             }
80             }
81 532         1663 for my $i (0 .. $#$lines) {
82 1183         1508 my $line = $lines->[ $i ];
83              
84 1183 100       2703 my $type = $line eq ''
    100          
85             ? 'EMPTY'
86             : $line =~ m/\A[ \t]/
87             ? 'MORE'
88             : 'CONTENT';
89              
90 1183 100 100     4912 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       613 if ($type ne 'CONTENT') {
    50          
95 142         170 $string .= "\n";
96             }
97             elsif ($type eq 'CONTENT') {
98 146         186 $string .= ' ';
99             }
100             }
101             elsif ($prev eq 'START' and $type eq 'EMPTY') {
102 54         75 $string .= "\n";
103 54         67 $type = 'START';
104             }
105             elsif ($prev eq 'EMPTY' and $type ne 'CONTENT') {
106 126         185 $string .= "\n";
107             }
108              
109 1183         1635 $string .= $line;
110              
111 1183 100 100     2065 if ($type eq 'MORE' and $i < $#$lines) {
112 98         113 $string .= "\n";
113             }
114              
115 1183         1746 $prev = $type;
116             }
117 532 100       1038 if ($keep) {
118 39         53 $string .= $trailing;
119             }
120 532 100 100     1913 $string .= "\n" if @$lines and not $trim;
121             }
122             else {
123 1178         3334 for my $i (0 .. $#$lines) {
124 2092         3195 $string .= $lines->[ $i ];
125 2092 100 100     6252 $string .= "\n" if ($i != $#$lines or not $trim);
126             }
127             }
128 1710         1974 TRACE and warn __PACKAGE__.':'.__LINE__.$".Data::Dumper->Dump([\$string], ['string']);
129 1710         3933 return $string;
130             }
131              
132             sub render_multi_val {
133 239     239 0 480 my ($self, $multi) = @_;
134 239         344 my $string = '';
135 239         302 my $start = 1;
136 239         538 for my $line (@$multi) {
137 558 100       831 if (not $start) {
138 294 100       516 if ($line eq '') {
139 25         40 $string .= "\n";
140 25         34 $start = 1;
141             }
142             else {
143 269         485 $string .= " $line";
144             }
145             }
146             else {
147 264         459 $string .= $line;
148 264         363 $start = 0;
149             }
150             }
151 239         593 return $string;
152             }
153              
154              
155             1;