auto/share/dist/ODF-MailMerge/examples/Ex_Famnames.pl untested

filter: Statementstmt 0.0% 0 / 123 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 178 CC 1 SCAR 6.9
CC
1
Cov
0%
commify_number
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/bin/env perl
2 use strict; use warnings; use feature qw/say/;
3 STDOUT->autoflush; STDERR->autoflush;
4  
5 # define "console_in", "console_out", "locals_fs" encodings
6 use Encode::Locale qw/decode_argv/;
7 use Encode ();
8  
9 use FindBin qw($Bin);
10  
11 use File::Basename qw/basename/;
12 use DateTime ();
13 use DateTime::Format::Strptime ();
14 use Getopt::Long qw/GetOptions/;
15  
16 use Spreadsheet::Edit qw/read_spreadsheet alias apply sort_rows %crow/;
17 use Data::Dumper::Interp 7.013 qw/dvis qsh qshlist/;
18 Data::Dumper::Interp::addrvis_digits(5); # for Author's debugging :-)
19  
20 use ODF::lpOD;
21 use ODF::lpOD_Helper;
22 use ODF::MailMerge 1.000 qw/replace_tokens/;
23  
24 sub commify_number($) {
25 scalar reverse (reverse($_[0]) =~ s/(\d\d\d)(?=\d)(?!\d*\.)/$1,/gr)
26 }
27  
28 my ($outpath, $also_pdf, $also_txt, $skelpath, $dbpath, $quiet);
29 decode_argv( Encode::FB_CROAK );
30 GetOptions(
31 'o|outpath=s' => \$outpath,
32 '--pdf' => \$also_pdf,
33 '--txt' => \$also_txt,
34 's|skelpath=s' => \$skelpath,
35 'd|dbpath=s' => \$dbpath,
36 'q|quiet' => \$quiet,
37 ) or die "bad arguments";
38  
39 $skelpath //= "$Bin/Ex_Famnames_Skeleton.odt";
40  
41 $outpath //= "./".basename($skelpath) =~ s/_?Skel[a-z]*//r =~ s/\.odt/_output.odt/r;
42 die if $outpath eq $skelpath;
43  
44 $dbpath //= "$Bin/family_names.csv";
45  
46 #---------------------------------------------------------------
47  
48 ############################
49 # Load the 'skeleton' document
50 ############################
51 warn "> Loading $skelpath\n" unless $quiet;
52 my $doc = odf_get_document($skelpath, read_only => 1) // die "$skelpath : $!";
53 my $body = $doc->get_body;
54  
55 ############################
56 # Read the "data base" (just a .csv file)
57 ############################
58 warn "> Reading $dbpath\n" unless $quiet;
59 read_spreadsheet($dbpath);
60  
61 # Make alias identifiers for column titles matched by regular expressions.
62 # This is so we don't have to know the exact text of column titles here.
63 # The alias names will be keys in the row hashes. See Spreadsheet::Edit .
64 alias Name => qr/name/i;
65 alias Rank => qr/rank/i;
66 alias Origin => qr/origin/i;
67 alias Population => qr/pop/i;
68  
69 my $highest_Rank = 0;
70 # "apply" visits all spreadsheet data rows (i.e. rows after the title row),
71 # executing the specified code block. %crow is a tied hash which maps
72 # column titles *and aliases* to the corresponding data value in
73 # the "current row" being visited.
74 apply { $highest_Rank = $crow{Rank} if $crow{Rank} > $highest_Rank; };
75  
76 ########################################
77 # Generate the table showing data alphabetized by Name
78 ########################################
79 sort_rows { $a->{Name} cmp $b->{Name} };
80  
81 # Visit all the data rows and create an entry in the document for each
82 { my $engine = ODF::MailMerge::Engine->new(
83 context => $body, proto_tag => "{ByName_Proto}");
84 apply {
85 my $hash = { # massage the data before displaying
86 Name => $crow{Name},
87 Rank => $crow{Rank},
88 Origin => $crow{Origin},
89 Population => commify_number($crow{Population}),
90 };
91 $engine->add_record($hash);
92 };
93 $engine->finish();
94 }
95  
96 ########################################
97 # Generate the by-popularity table
98 ########################################
99 sort_rows { $a->{Rank} <=> $b->{Rank} };
100 { my $engine = ODF::MailMerge::Engine->new(
101 context => $body, proto_tag => "{ByPop_Proto}");
102 apply {
103 $engine->add_record(\%crow);
104 };
105 $engine->finish();
106 }
107  
108 ########################################
109 # Generate the by-origin table using a separate row for each name
110 # For brevity, origins including English are omitted.
111 ########################################
112 { my %Origin_to_Names;
113 apply {
114 push @{ $Origin_to_Names{$crow{Origin}} }, $crow{Name}
115 if $crow{Origin} !~ /English/;
116 };
117 { my $engine = ODF::MailMerge::Engine->new(
118 context => $body, proto_tag => "{ByOriginNonEng_Proto}");
119 foreach my $origin (sort keys %Origin_to_Names) {
120 my $namelist = $Origin_to_Names{$origin};
121 my $hash = {
122 Origin => $origin,
123 Names => $namelist,
124 };
125 $engine->add_record($hash);
126 }
127 $engine->finish();
128 }
129 }
130  
131 ########################################
132 # Generate the complete by-origin table, using a comma-separated
133 # list of names for each origin.
134 #
135 # There are two {Name} tags with different conditionals, each in its own
136 # frame; one conditional matches the the last item in the list, the other
137 # matches all items before the last and includes a ", " after the token
138 # in it's frame. The result is like "name1, name2, ..., nameLast" when
139 # the substituted values are strung together.
140 #
141 # When a multi-valued {token} is encapsulated in a frame, the frame is
142 # replicated instead of a whole table row; this is what allows multiple
143 # values to end up on the same line in the result (the frames are
144 # anchored "as Character" in the same paragraph).
145 #
146 # See the Ex_Famnames_Skeleton.odt document for how this is set up.
147 ########################################
148 { my %Origin_to_Names;
149 apply {
150 push @{ $Origin_to_Names{$crow{Origin}} }, $crow{Name};
151 };
152 { my $engine = ODF::MailMerge::Engine->new(
153 context => $body, proto_tag => "{ByOrigin2_Proto}");
154 foreach my $origin (sort keys %Origin_to_Names) {
155 my $namelist = $Origin_to_Names{$origin};
156 my $hash = {
157 Origin => $origin,
158 Names => $namelist,
159 };
160 $engine->add_record($hash, debug => 0);
161 }
162 $engine->finish();
163 }
164 }
165  
166 ########################################
167 # Replace "{Database Date}" with the modification time of the data file
168 ########################################
169 my $dbpath_mt_unixtime = (stat($dbpath))[9];
170 my $dbpath_mt_dt = DateTime->from_epoch(epoch => $dbpath_mt_unixtime);
171 my $dbpath_mt_string = $dbpath_mt_dt->strftime("%Y-%m-%d");
172 replace_tokens($body, { "Database Date" => $dbpath_mt_string })
173 == 1 or die "Did not find {Database Date}";
174  
175 ########################################
176 # Write out the result
177 ########################################
178 warn "> Writing $outpath\n" unless $quiet;
179 $doc->save(target => $outpath);
180  
181 if ($also_pdf) {
182 my @cmd = ("libreoffice", "--convert-to", "pdf", $outpath);
183 warn "> ", qshlist(@cmd),"\n" unless $quiet;
184 system @cmd;
185 }
186 if ($also_txt) {
187 # C.F. https://help.libreoffice.org/latest/en-US/text/shared/guide/convertfilters.html
188 my @cmd = ("libreoffice", "--convert-to", "txt:Text (encoded):UTF8", $outpath);
189 warn "> ", qshlist(@cmd),"\n" unless $quiet;
190 system @cmd;
191 }
192  
193 exit 0;