CSVUtils/csv2paras.pm

filter: Statementstmt 40.0% 20 / 50 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 55.5% 5 / 9 Podpod n/a totaltotal 29.4% 25 / 85 CC 13 SCAR 34.6
CC
13
Cov
28%
__ANON__
72.0
_escape_header
12.0
__ANON__
12.0
CRAP 31.7
« CSVUtils/csv_gen.pm ↑ Summary CSVUtils/paras2csv.pm »
1 package App::CSVUtils::csv2paras;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8 1 our $DATE = '2026-07-09'; # DATE
9 1 our $DIST = 'App-CSVUtils'; # DIST
10 1 our $VERSION = '1.038'; # VERSION
11  
12 1 ▶use App::CSVUtils qw(gen_csv_util);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use String::Pad qw(pad);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 0 ▶sub _escape_value {
Subroutine _escape_value: not called CC=2 SCAR=17.9
16 0 my $val = shift;
17 0 ▶ $val =~ s/(\\|\n)/$1 eq "\\" ? "\\\\" : "\\n\n "/eg;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '\\' ? : 0 0 0
18 0 $val;
19 }
20  
21 0 ▶sub _escape_header {
Subroutine _escape_header: not called CC=3 SCAR=24.8
22 0 my $val = shift;
23 0 ▶ $val =~ s/(\\|\n|:)/$1 eq "\\" ? "\\\\" : $1 eq ":" ? "\\:" : "\\n\n "/eg;
Branch: 2 branches
BranchTrueFalseTotal
$1 eq '\\' ? : 0 0 0
$1 eq ':' ? : 0 0 0
24 0 $val;
25 }
26  
27 gen_csv_util(
28 name => 'csv2paras',
29 summary => 'Convert CSV to paragraphs',
30 description => <<'MARKDOWN',
31  
32 This utility converts CSV format like this:
33  
34 name,email,phone,notes
35 bill,bill@example.com,555-1236,+
36 lisa,lisa@example.com,555-1235,from work
37 jimmy,jimmy@example.com,555-1237,
38  
39 into paragraphs format like this, which resembles (but not strictly follows)
40 email headers (RFC-822) or internet message headers (RFC-5322):
41  
42 name: bill
43 email: bill@example.com
44 phone: 555-1236
45 notes: +
46  
47 name: lisa
48 email: lisa@example.com
49 phone: 555-1235
50 notes: from work
51  
52 name: jimmy
53 email: jimmy@example.com
54 phone: 555-1237
55 notes:
56  
57 Why display in this format? It might be more visually readable or diff-able
58 especially if there are a lot of fields and/or there are long values.
59  
60 If a CSV value contains newline, it will escaped "\n", e.g.:
61  
62 # CSV
63 name,email,phone,notes
64 beth,beth@example.com,555-1231,"Has no last name
65 Might be adopted sometime by Jimmy"
66 matthew,matthew@example.com,555-1239,"Quit
67  
68 or fired?"
69  
70 # paragraph
71 name: beth
72 email: beth@example.com
73 phone: 555-1231
74 notes: Has no last name\nMight be adopted sometime by Jimmy
75  
76 name: matthew
77 email: matthew@example.com
78 phone: 555-1239
79 notes: Quit\n\n or fired?
80  
81 If a CSV value contains literal "\" (backslash) it will be escaped as "\\".
82  
83 Long lines are also by default folded at 78 columns (but you can customize with
84 the `--width` option); if a line is folded a literal backslash is added to the
85 end of each physical line and the next line will be indented by two spaces:
86  
87 notes: This is a long note. This is a long note. This is a long note. This is
88 a long note. This is a long note.
89  
90 A long word is also folded and the next line will be indented by one space:
91  
92 notes: Thisisalongwordthisisalongwordthisisalongwordthisisalongwordthisisalongw
93 ord
94  
95 Newline and backslash are also escaped in header; additionally a literal ":"
96 (colon) is escaped into "\:".
97  
98 There is option to skip displaying empty fields (`--hide-empty-values`) and to
99 align the ":" header separator.
100  
101 Keywords: paragraphs, cards, pages, headers
102  
103 MARKDOWN
104 add_args => {
105 width => {
106 summary => 'The width at which to fold long lines, -1 means to never fold',
107 schema => ['int*', 'clset|'=>[{is=>-1, "is.err_msg"=>"Must be >0 or -1"}, {min=>1}]],
108 default => 78,
109 },
110 hide_empty_values => {
111 summary => 'Whether to skip showing empty values',
112 schema => 'bool*',
113 },
114 align => {
115 summary => 'Whether to align header separator across lines',
116 schema => 'bool*',
117 description => <<'MARKDOWN',
118  
119 Note that if you want to convert the paragraphs back to CSV later using
120 <prog:paras2csv>, the padding spaces added by this option will become part of
121 header value, unless you use its `--trim-header` or `--rtrim-header` option.
122  
123 MARKDOWN
124 },
125 },
126 links => [
127 {url=>'prog:paras2csv'},
128 ],
129 tags => ['category:converting'],
130  
131 examples => [
132 {
133 summary => 'Convert to paragraphs format, show fields alphabetically, do not fold, hide empty values',
134 src => 'csv-sort-fields INPUT.csv | [[prog]] --width=-1 --hide-empty-values',
135 src_plang => 'bash',
136 test => 0,
137 'x.doc.show_result' => 0,
138 },
139 ],
140  
141 0 ▶ on_input_header_row => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
142 0 my $r = shift;
143  
144 # these are the keys we add to the stash
145 0 $r->{escaped_headers} = [];
146 0 $r->{longest_header_len} = 0;
147  
148 0 for my $field (@{ $r->{input_fields} }) {
149 0 push @{ $r->{escaped_headers} }, _escape_header($field);
150 0 my $l = length($r->{escaped_headers}[-1]);
151 0 ▶ $r->{longest_header_len} = $l if $r->{longest_header_len} < $l;
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"longest_header_len"} < $l 0 0 0
152 }
153 },
154  
155 0 ▶ on_input_data_row => sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
156 0 my $r = shift;
157  
158 0 ▶ print "\n" if $r->{input_data_rownum} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"input_data_rownum"} > 1 0 0 0
159  
160 0 for my $i (0 .. $#{ $r->{input_fields} }) {
161 0 my $val = $r->{input_row}[$i];
162 0 ▶ next if $r->{util_args}{hide_empty_values} && length $val == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"util_args"}{"hide_empty_values"} and length $val == 0 0 0 0
Condition0%0/3
$r->{"util_args"}{"hide_empty_values"} and length $val == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$r->{"util_args"}{"hide_empty_values"} and length $val == 0
$r->{"util_args"}{"hide_empty_values"} length $val == 0
Truth table0%0/3
A and B
A= $r->{"util_args"}{"hide_empty_values"}B= length $val == 0
ABresult
0X0
100
111
163 my $line =
164 0 ▶ ($r->{util_args}{align} ? pad($r->{escaped_headers}[$i], $r->{longest_header_len}, "r") : $r->{escaped_headers}[$i]).
Branch: 1 branch
BranchTrueFalseTotal
$r->{'util_args'}{'align'} ? : 0 0 0
165 ": ".
166 _escape_value($val);
167 0 ▶ if ($r->{util_args}{width} == -1 || length($line) <= $r->{util_args}{width}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{'util_args'}{'width'} == -1 or length $line <= $r->{'util_args'}{'width'}) { } 0 0 0
Condition0%0/3
$r->{"util_args"}{"width"} == -1 or length $line <= $r->{"util_args"}{"width"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$r->{"util_args"}{"width"} == -1 or length $line <= $r->{"util_args"}{"width"}
$r->{"util_args"}{"width"} == -1 length $line <= $r->{"util_args"}{"width"}
Truth table0%0/3
A or B
A= $r->{"util_args"}{"width"} == -1B= length $line <= $r->{"util_args"}{"width"}
ABresult
1X1
011
000
168 0 print $line, "\n";
169 } else {
170 0 require Text::Wrap::NoStrip;
171 0 local $Text::Wrap::NoStrip::columns = $r->{util_args}{width};
172 0 my $wrapped_line = Text::Wrap::NoStrip::wrap("", " ", $line);
173 0 print $wrapped_line, "\n";
174 }
175 }
176 },
177  
178 1 writes_csv => 0,
179 );
180  
181 1 1;
182 # ABSTRACT: Convert CSV to paragraphs
183  
184 __END__
« CSVUtils/csv_gen.pm ↑ Summary CSVUtils/paras2csv.pm »