CSVUtils/paras2csv.pm

filter: Statementstmt 26.1% 17 / 65 Branchbran 0.0% 0 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 44.4% 4 / 9 Podpod n/a totaltotal 19.4% 21 / 108 CC 18 SCAR 34.0
CC
18
Cov
17%
__ANON__
56.0
_parse_para
42.0
_unescape_field
20.0
CRAP 29.8
« CSVUtils/csv2paras.pm ↑ Summary CSVUtils/csv_setop.pm »
1 package App::CSVUtils::paras2csv;
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  
14 0 ▶sub _unescape_field {
Subroutine _unescape_field: not called CC=4 SCAR=30.0
15 0 my $val = shift;
16 0 ▶ $val =~ s/(\\:|\\n|\\\\|[^\\:]+)/$1 eq "\\\\" ? "\\" : $1 eq "\\n" ? "\n" : $1 eq "\\:" ? ":" : $1/eg;
Branch: 3 branches
BranchTrueFalseTotal
$1 eq '\\\\' ? : 0 0 0
$1 eq '\\n' ? : 0 0 0
$1 eq '\\:' ? : 0 0 0
17 0 $val;
18 }
19  
20 0 ▶sub _unescape_value {
Subroutine _unescape_value: not called CC=3 SCAR=24.8
21 0 my $val = shift;
22 0 ▶ $val =~ s/(\\n|\\\\|[^\\]+)/$1 eq "\\\\" ? "\\" : $1 eq "\\n" ? "\n" : $1/eg;
Branch: 2 branches
BranchTrueFalseTotal
$1 eq '\\\\' ? : 0 0 0
$1 eq '\\n' ? : 0 0 0
23 0 $val;
24 }
25  
26 0 ▶sub _parse_line {
Subroutine _parse_line: not called CC=2 SCAR=17.9
27 0 my $line = shift;
28 0 $line =~ s/\R //g;
29 0 ▶ $line =~ /((?:[^\\:]+|\\n|\\\\|\\:)+): (.*)/ or return;
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /((?:[^\\:]+|\\n|\\\\|\\:)+): (.*)/ or return 0 0 0
30 0 my $field = _unescape_field($1);
31 0 my $value = _unescape_value($2);
32 0 ($field, $value);
33 }
34  
35 0 ▶sub _parse_para {
Subroutine _parse_para: not called CC=6 SCAR=37.4
36 0 my ($r, $para, $idx) = @_;
37  
38 0 my @h;
39 0 ▶ while ($para =~ s/\A(.+(?:\R .*)*)(?:\R|\z)//g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($para =~ s/\A(.+(?:\R .*)*)(?:\R|\z)//g) 0 0 0
40 #say "D:line=<$1>, para=<$para>";
41 0 my ($field, $val) = _parse_line($1);
42 0 ▶ defined $field or die [400, "Paragraph[$idx]: Can't parse line $1"];
Branch: 1 branch
BranchTrueFalseTotal
defined $field or die [400, "Paragraph[$idx]: Can't parse line $1"] 0 0 0
43 0 ▶ if ($r->{util_args}{trim_header}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($r->{'util_args'}{'trim_header'}) { } 0 0 0
elsif ($r->{'util_args'}{'ltrim_header'}) { } 0 0 0
elsif ($r->{'util_args'}{'rtrim_header'}) { } 0 0 0
44 0 $field =~ s/\A\s+//;
45 0 $field =~ s/\s+\z//;
46 } elsif ($r->{util_args}{ltrim_header}) {
47 0 $field =~ s/\A\s+//;
48 } elsif ($r->{util_args}{rtrim_header}) {
49 0 $field =~ s/\s+\z//;
50 }
51 0 push @h, $field, $val;
52 }
53 0 @h;
54 }
55  
56 gen_csv_util(
57 name => 'paras2csv',
58 summary => 'Convert paragraphs to CSV',
59 description => <<'MARKDOWN',
60  
61 This utility is the counterpart of the <prog:csv2paras> utility. See its
62 documentation for more details.
63  
64 Keywords: paragraphs, cards, pages, headers
65  
66 MARKDOWN
67 add_args => {
68 input_file => {
69 schema => 'filename*',
70 default => '-',
71 pos => 0,
72 },
73 trim_header => {
74 schema => 'bool*',
75 },
76 rtrim_header => {
77 schema => 'bool*',
78 },
79 ltrim_header => {
80 schema => 'bool*',
81 },
82 },
83 add_args_rels => {
84 'choose_one&' => [ [qw/trim_header rtrim_header ltrim_header/] ],
85 },
86 links => [
87 {url=>'prog:csv2paras'},
88 ],
89 tags => ['category:converting'],
90  
91 examples => [
92 {
93 summary => 'Convert paragraphs format to CSV',
94 src => '[[prog]] - OUTPUT.csv',
95 src_plang => 'bash',
96 test => 0,
97 'x.doc.show_result' => 0,
98 },
99 ],
100  
101 reads_csv => 0,
102  
103 0 ▶ after_read_input => sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
104 0 my $r = shift;
105  
106 0 my $fh;
107 0 ▶ if ($r->{util_args}{input_file} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{'util_args'}{'input_file'} eq '-') { } 0 0 0
108 0 $fh = \*STDIN;
109 } else {
110 open $fh, "<", $r->{util_args}{input_file}
111 0 ▶ or die [500, "Can't read file '$r->{util_args}{input_file}: $!"];
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $r->{"util_args"}{"input_file"} or die [500, qq[Can't read file '$r->{"util_args"}{"input_file"}: $!]] 0 0 0
112 }
113  
114 0 local $/ = "";
115 0 my $i = 0;
116 0 ▶ while (my $para = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $para = readline $fh)) 0 0 0
117 0 $para =~ s/\R{2}\z//;
118 #say "D:para=<$para>";
119 0 my @h = _parse_para($r, $para, $i);
120 0 $i++;
121 0 ▶ if ($i == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 1) 0 0 0
122 0 my @h2 = @h;
123 0 my $j = 0;
124 0 ▶ while (my ($field, $value) = splice @h2, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@h2, 0, 2)) 0 0 0
125 0 $r->{output_fields}[$j] = $field;
126 0 $r->{output_fields_idx}{$field} = $j;
127 0 $j++;
128 }
129 }
130 0 my @vals;
131 0 ▶ while (my ($field, $value) = splice @h, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@h, 0, 2)) 0 0 0
132 0 push @vals, $value;
133 }
134 0 $r->{code_print_row}->(\@vals);
135 }
136 },
137 1 );
138  
139 1 1;
140 # ABSTRACT: Convert paragraphs to CSV
141  
142 __END__
« CSVUtils/csv2paras.pm ↑ Summary CSVUtils/csv_setop.pm »