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   146262 use strict;
  52         103  
  52         1471  
3 52     52   169 use warnings;
  52         55  
  52         5054  
4             package YAML::PP::Render;
5              
6             our $VERSION = 'v0.40.1'; # TRIAL VERSION
7              
8 52 50   52   225 use constant TRACE => $ENV{YAML_PP_TRACE} ? 1 : 0;
  52         91  
  52         39897  
9              
10             sub render_quoted {
11 491     491 0 948 my ($self, $style, $lines) = @_;
12              
13 491         669 my $quoted = '';
14 491         643 my $addspace = 0;
15              
16 491         1436 for my $i (0 .. $#$lines) {
17 1334         1561 my $line = $lines->[ $i ];
18 1334         1588 my $value = $line->{value};
19 1334         1654 my $last = $i == $#$lines;
20 1334         1457 my $first = $i == 0;
21 1334 100       1970 if ($value eq '') {
22 301 100       609 if ($first) {
    100          
23 66         87 $addspace = 1;
24             }
25             elsif ($last) {
26 66 100       182 $quoted .= ' ' if $addspace;
27             }
28             else {
29 169         199 $addspace = 0;
30 169         242 $quoted .= "\n";
31             }
32 301         446 next;
33             }
34              
35 1033 100       1626 $quoted .= ' ' if $addspace;
36 1033         1435 $addspace = 1;
37 1033 100       1666 if ($style eq '"') {
38 925 100       1937 if ($line->{orig} =~ m/\\$/) {
39 82         255 $line->{value} =~ s/\\$//;
40 82         237 $value =~ s/\\$//;
41 82         141 $addspace = 0;
42             }
43             }
44 1033         1830 $quoted .= $value;
45             }
46 491         1108 return $quoted;
47             }
48              
49             sub render_block_scalar {
50 1712     1712 0 3594 my ($self, $block_type, $chomp, $lines) = @_;
51              
52 1712         3571 my ($folded, $keep, $trim);
53 1712 100       3450 if ($block_type eq '>') {
54 532         693 $folded = 1;
55             }
56 1712 100       4114 if ($chomp eq '+') {
    100          
57 189         280 $keep = 1;
58             }
59             elsif ($chomp eq '-') {
60 305         408 $trim = 1;
61             }
62              
63 1712         2185 my $string = '';
64 1712 100       3095 if (not $keep) {
65             # remove trailing empty lines
66 1523         2872 while (@$lines) {
67 1715 100       3402 last if $lines->[-1] ne '';
68 222         413 pop @$lines;
69             }
70             }
71 1712 100       2802 if ($folded) {
72              
73 532         661 my $prev = 'START';
74 532         647 my $trailing = '';
75 532 100       949 if ($keep) {
76 39   100     292 while (@$lines and $lines->[-1] eq '') {
77 39         48 pop @$lines;
78 39         95 $trailing .= "\n";
79             }
80             }
81 532         1515 for my $i (0 .. $#$lines) {
82 1183         1438 my $line = $lines->[ $i ];
83              
84 1183 100       2656 my $type = $line eq ''
    100          
85             ? 'EMPTY'
86             : $line =~ m/\A[ \t]/
87             ? 'MORE'
88             : 'CONTENT';
89              
90 1183 100 100     4886 if ($prev eq 'MORE' and $type eq 'EMPTY') {
    100 100        
    100 100        
    100          
91 30         35 $type = 'MORE';
92             }
93             elsif ($prev eq 'CONTENT') {
94 288 100       589 if ($type ne 'CONTENT') {
    50          
95 142         156 $string .= "\n";
96             }
97             elsif ($type eq 'CONTENT') {
98 146         204 $string .= ' ';
99             }
100             }
101             elsif ($prev eq 'START' and $type eq 'EMPTY') {
102 54         83 $string .= "\n";
103 54         62 $type = 'START';
104             }
105             elsif ($prev eq 'EMPTY' and $type ne 'CONTENT') {
106 126         141 $string .= "\n";
107             }
108              
109 1183         1608 $string .= $line;
110              
111 1183 100 100     2160 if ($type eq 'MORE' and $i < $#$lines) {
112 98         118 $string .= "\n";
113             }
114              
115 1183         1964 $prev = $type;
116             }
117 532 100       971 if ($keep) {
118 39         55 $string .= $trailing;
119             }
120 532 100 100     1656 $string .= "\n" if @$lines and not $trim;
121             }
122             else {
123 1180         3242 for my $i (0 .. $#$lines) {
124 2096         3117 $string .= $lines->[ $i ];
125 2096 100 100     6137 $string .= "\n" if ($i != $#$lines or not $trim);
126             }
127             }
128 1712         1999 TRACE and warn __PACKAGE__.':'.__LINE__.$".Data::Dumper->Dump([\$string], ['string']);
129 1712         3742 return $string;
130             }
131              
132             sub render_multi_val {
133 242     242 0 473 my ($self, $multi) = @_;
134 242         312 my $string = '';
135 242         320 my $start = 1;
136 242         476 for my $line (@$multi) {
137 564 100       772 if (not $start) {
138 297 100       555 if ($line eq '') {
139 25         42 $string .= "\n";
140 25         36 $start = 1;
141             }
142             else {
143 272         445 $string .= " $line";
144             }
145             }
146             else {
147 267         470 $string .= $line;
148 267         364 $start = 0;
149             }
150             }
151 242         577 return $string;
152             }
153              
154              
155             1;