lib/App/Dapper/Import.pm

filter: Statementstmt 20.5% 148 / 722 Branchbran 12.4% 61 / 490 Conditioncond 6.0% 14 / 230 MC/DCmcdc 5.8% 8 / 138 Subroutinesub 35.0% 21 / 60 Podpod 23.3% 7 / 30 totaltotal 15.5% 259 / 1670 CC 348 SCAR 61.0
CC
348
Cov
15%
hugo
1482.0
wordpress
1056.0
jekyll
1056.0
CRAP 444.4
« lib/App/Dapper/Check.pm ↑ Summary lib/App/Dapper/Agents.pm »
1 package App::Dapper::Import;
2  
3 =head1 NAME
4  
5 App::Dapper::Import - Bring a site in from WordPress, Jekyll, Hugo or Medium.
6  
7 =head1 DESCRIPTION
8  
9 C<dapper import> copies the writing from another site into the source
10 directory of a Dapper project, so you can move over without retyping
11 anything. Run it in a Dapper project (C<dapper init> first):
12  
13 $ dapper import wordpress export.xml
14 $ dapper import jekyll ../old-site
15 $ dapper import hugo ../old-site
16 $ dapper import medium medium-export.zip
17 $ dapper import jekyll ../old-site --dry-run # show what would happen
18 $ dapper import wordpress export.xml --to posts # posts go in _source/posts
19  
20 =over 4
21  
22 =item B<wordpress>
23  
24 Takes the export file from Tools, Export in WordPress. Posts go in
25 C<_source/blog> and pages in C<_source>, as HTML, with their title, date,
26 categories, tags and excerpt. Drafts, pending and private posts are imported
27 as drafts. Each page keeps its old address (with C<urlpattern>), so links to
28 it keep working. Videos from C<[embed]> and YouTube or Vimeo addresses on a
29 line of their own become embedded videos, and C<[caption]> becomes a figure.
30  
31 =item B<jekyll>
32  
33 Takes the site's directory. Posts in C<_posts> and drafts in C<_drafts> go
34 in C<_source/blog>, and other pages keep their place. Each page keeps its
35 address, worked out from C<permalink> as Jekyll does. Liquid that Dapper can
36 replace (C<highlight> blocks, C<site.baseurl>, C<relative_url>, C<post_url>)
37 is replaced. Folders such as C<assets> and C<images> are copied to the
38 project.
39  
40 =item B<hugo>
41  
42 Takes the site's directory. Pages in C<content> keep their place and their
43 address (from C<url>, C<slug>, or C<permalinks> in the Hugo configuration).
44 YAML, TOML and JSON front matter are all read. Files in page bundles are
45 copied with their page, C<static> is copied into the source directory,
46 C<aliases> become redirects, and the C<youtube>, C<vimeo> and C<figure>
47 shortcodes become components.
48  
49 =item B<medium>
50  
51 Takes the export from Medium's settings (the zip file, or the folder it
52 unzips to). Each story becomes an HTML page in C<_source/blog>, with Medium's
53 classes removed. Unpublished stories are imported as drafts.
54  
55 =back
56  
57 Nothing that is already in the source directory is overwritten. Redirects
58 are added to C<_data/redirects.yml>. Afterwards, C<dapper import> lists what
59 it couldn't convert (such as shortcodes and Liquid tags it doesn't know)
60 for you to look at, and C<dapper check> finds any links that need fixing.
61  
62 =cut
63  
64 191 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 191 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 191 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 191 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 191 ▶use File::Spec::Functions qw/ catfile /;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 191 ▶use File::Basename qw/ basename dirname /;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 191 ▶use File::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
72 191 ▶use File::Temp qw/ tempdir /;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 191 ▶use YAML::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
74 191 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
75 191 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
76  
77 191 ▶use App::Dapper::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 191 ▶use App::Dapper::Components;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 191 ▶use App::Dapper::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 191 ▶use App::Dapper::Publish;
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 191 my %IMPORTERS = (wordpress => \&wordpress, jekyll => \&jekyll, hugo => \&hugo, medium => \&medium);
83  
84 =head2 kinds
85  
86 Returns the kinds of site that can be imported.
87  
88 =cut
89  
90 0 ▶sub kinds { return sort keys %IMPORTERS }
Subroutine kinds: not called CC=1 SCAR=6.9
Pod documented
91  
92 =head2 run
93  
94 Takes a Dapper object, the kind of site (C<wordpress>, C<jekyll>, C<hugo> or
95 C<medium>), the file or directory to import, and options: C<to> (the
96 directory under the source directory for posts, default C<blog>) and
97 C<dry_run>. Imports the site and returns a summary as a hash reference.
98  
99 =cut
100  
101 0 ▶sub run {
Subroutine run: not called CC=7 SCAR=40.3
Pod documented
102 0 my ($dapper, $kind, $from, %options) = @_;
103  
104 0 ▶ die "error: say what to import from: dapper import <" . join('|', kinds()) . "> <file or directory>\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kind and defined $from 0 0 0
Condition0%0/3
defined $kind and defined $from
!ll&&!rl&&r
000
MC/DC0%0/2
defined $kind and defined $from
defined $kind defined $from
Truth table0%0/3
A and B
A= defined $kindB= defined $from
ABresult
0X0
100
111
105 unless defined $kind and defined $from;
106 0 $kind = lc $kind;
107 die "error: Dapper can't import from \"$kind\"; it can import from " . join(', ', kinds()) . "\n"
108 0 ▶ unless $IMPORTERS{$kind};
Branch: 1 branch
BranchTrueFalseTotal
unless $IMPORTERS{$kind} 0 0 0
109 0 ▶ die "error: \"$from\" doesn't exist\n" unless -e $from;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $from 0 0 0
110  
111 0 $dapper->read_project();
112  
113 0 ▶ my $to = defined $options{to} ? $options{to} : 'blog';
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'to'} ? : 0 0 0
114 0 $to =~ s{^/+|/+$}{}g;
115  
116 my $state = {
117 dapper => $dapper,
118 source => $dapper->{source},
119 to => $to,
120 dry_run => $options{dry_run},
121 0 ▶ layouts => { map { App::Dapper::Utils::filter_stem($_) => 1 } (-d $dapper->{layout} ? App::Dapper::Utils::read_dir($dapper->{layout}) : ()) },
Branch: 1 branch
BranchTrueFalseTotal
-d $dapper->{'layout'} ? : 0 0 0
122 pages => [],
123 skipped => [],
124 copied => 0,
125 redirects => {},
126 notes => {},
127 taken => {},
128 };
129  
130 0 $IMPORTERS{$kind}->($state, $from);
131 0 write_redirects($state);
132  
133 0 report($state);
134 0 return $state;
135 }
136  
137 # --- Writing ---------------------------------------------------------------
138  
139 # add_page(state, path, \@front_matter_pairs, body, kind) - Write a page to
140 # the source directory, unless something is already there.
141 0 ▶sub add_page {
Subroutine add_page: not called CC=14 SCAR=53.5
Pod undocumented
142 0 my ($state, $path, $front, $body, $kind) = @_;
143  
144 0 $path =~ s{^/+}{};
145 0 my $file = "$state->{source}/$path";
146  
147 # Two imported pages with the same name get -2, -3, ...
148 0 ▶ if ($state->{taken}->{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state->{"taken"}{$file}) 0 0 0
149 0 my $n = 2;
150 0 my ($stem, $ext) = $file =~ /^(.*?)(\.[^.\/]+)?$/;
151 0 ▶ $ext = '' unless defined $ext;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ext 0 0 0
152 0 ▶ $n++ while $state->{taken}->{"$stem-$n$ext"};
Branch: 1 branch
BranchTrueFalseTotal
$state->{"taken"}{"$stem-$n$ext"} and do { ++$n } 0 0 0
153 0 $file = "$stem-$n$ext";
154 }
155 0 $state->{taken}->{$file} = 1;
156  
157 0 ▶ if (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) 0 0 0
158 0 push @{$state->{skipped}}, $file;
159 0 return;
160 }
161  
162 0 fit_schema($state, $file, $front);
163  
164 # One spelling for each category and tag across the import ("Markup" on
165 # one post and "markup" on another would make two pages at one address)
166 0 ▶ for my $pair (grep { $_->[0] eq 'categories' or $_->[0] eq 'tags' } @$front) {
Condition0%0/3
$_->[0] eq 'categories' || $_->[0] eq 'tags'
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->[0] eq 'categories' || $_->[0] eq 'tags'
$_->[0] eq 'categories' $_->[0] eq 'tags'
Truth table0%0/3
A || B
A= $_->[0] eq 'categories'B= $_->[0] eq 'tags'
ABresult
1X1
011
000
167 0 ▶ next unless ref $pair->[1] eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $pair->[1] eq "ARRAY" 0 0 0
168 0 ▶ my $spelling = $state->{spelling}->{$pair->[0]} ||= {};
Condition0%0/2
$state->{"spelling"}{$pair->[0]} ||= {}
l!l
00
MC/DC0%0/1
$state->{"spelling"}{$pair->[0]} ||= {}
$state->{"spelling"}{$pair->[0]}
Truth table0%0/2
A
A= $state->{"spelling"}{$pair->[0]}
Aresult
11
00
169 0 my %seen;
170 0 $pair->[1] = [ grep { !$seen{lc $_}++ }
171 0 ▶ map { my $key = lc App::Dapper::Utils::slugify($_); $spelling->{$key} = $_ unless defined $spelling->{$key}; $spelling->{$key} }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spelling->{$key} 0 0 0
172 0 ▶ grep { defined and !ref } @{$pair->[1]} ];
Condition0%0/3
defined $_ && !ref($_)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && !ref($_)
defined $_ !ref($_)
Truth table0%0/3
A && not(B)
A= defined $_B= !ref($_)
ABresult
0X0
100
111
173 }
174  
175 0 my %front = map { $_->[0] => $_->[1] } @$front;
176 0 ▶ push @{$state->{pages}}, { file => $file, kind => $kind || 'page', draft => $front{draft} ? 1 : 0 };
Branch: 1 branch
BranchTrueFalseTotal
$front{'draft'} ? : 0 0 0
Condition0%0/2
$kind || 'page'
l!l
00
MC/DC0%0/1
$kind || 'page'
$kind
Truth table0%0/2
A
A= $kind
Aresult
11
00
177 0 ▶ return if $state->{dry_run};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"dry_run"} 0 0 0
178  
179 0 App::Dapper::Utils::write_file($file, "---\n" . front_matter($front) . "---\n" . $body);
180 }
181  
182 # fit_schema(state, file, \@pairs) - Make an imported page's front matter fit
183 # the _schema.yml of the folder it lands in, so the site still builds: a list
184 # keeps only the values the schema allows (categories it doesn't allow become
185 # tags where tags are free), a required list that ends up empty gets the
186 # first allowed value (so a post in a blog starter is in "blog"), a required
187 # title that is missing becomes "Untitled", and text that is too long is
188 # shortened. Each change is noted for the report.
189 0 ▶sub fit_schema {
Subroutine fit_schema: not called CC=30 SCAR=68.4
Pod undocumented
190 0 my ($state, $file, $pairs) = @_;
191  
192 0 ▶ my $schema = eval { App::Dapper::Schema::for_file($state->{source}, $file) } or return;
Branch: 1 branch
BranchTrueFalseTotal
my $schema = eval { do { App::Dapper::Schema::for_file($state->{"source"}, $file) } } or return 0 0 0
193 0 ▶ my $fields = $schema->{fields} || {};
Condition0%0/2
$schema->{'fields'} || {}
l!l
00
MC/DC0%0/1
$schema->{'fields'} || {}
$schema->{'fields'}
Truth table0%0/2
A
A= $schema->{'fields'}
Aresult
11
00
194 0 ▶ my %required = map { $_ => 1 } @{$schema->{required} || []};
Condition0%0/2
$schema->{'required'} || []
l!l
00
MC/DC0%0/1
$schema->{'required'} || []
$schema->{'required'}
Truth table0%0/2
A
A= $schema->{'required'}
Aresult
11
00
195 0 my %index = map { $pairs->[$_]->[0] => $_ } 0 .. $#$pairs;
196 0 ▶ my $get = sub { my ($key) = @_; defined $index{$key} ? $pairs->[$index{$key}]->[1] : undef };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
defined $index{$key} ? : 0 0 0
197 0 ▶ my $set = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
198 0 my ($key, $value) = @_;
199 0 ▶ if (defined $index{$key}) { $pairs->[$index{$key}]->[1] = $value }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $index{$key}) { } 0 0 0
200 0 else { push @$pairs, [ $key => $value ]; $index{$key} = $#$pairs }
201 0 };
202 0 my $schema_file = 'its folder\'s _schema.yml';
203  
204 # Free tags: the schema lists "tags" as a list without fixed values, or
205 # doesn't list it but allows other fields
206 0 my $tag_rules = $fields->{tags};
207 my $tags_free = $tag_rules ? ($tag_rules->{type} eq 'list' && !$tag_rules->{values})
208 0 ▶ : $schema->{additional};
Branch: 1 branch
BranchTrueFalseTotal
$tag_rules ? : 0 0 0
Condition0%0/3
$tag_rules->{"type"} eq "list" and not $tag_rules->{"values"}
!ll&&!rl&&r
000
MC/DC0%0/2
$tag_rules->{"type"} eq "list" and not $tag_rules->{"values"}
$tag_rules->{"type"} eq "list" not $tag_rules->{"values"}
Truth table0%0/3
A and not(B)
A= $tag_rules->{"type"} eq "list"B= not $tag_rules->{"values"}
ABresult
0X0
100
111
209  
210 0 for my $key (sort keys %$fields) {
211 0 my $rules = $fields->{$key};
212 0 ▶ next unless $rules->{type} eq 'list' and $rules->{values};
Branch: 1 branch
BranchTrueFalseTotal
unless $rules->{"type"} eq "list" and $rules->{"values"} 0 0 0
Condition0%0/3
$rules->{"type"} eq "list" and $rules->{"values"}
!ll&&!rl&&r
000
MC/DC0%0/2
$rules->{"type"} eq "list" and $rules->{"values"}
$rules->{"type"} eq "list" $rules->{"values"}
Truth table0%0/3
A and B
A= $rules->{"type"} eq "list"B= $rules->{"values"}
ABresult
0X0
100
111
213 0 my $value = $get->($key);
214 0 ▶ my @items = ref $value eq 'ARRAY' ? @$value : defined $value && length $value ? ($value) : ();
Branch: 2 branches
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 0 0 0
defined $value && length $value ? : 0 0 0
Condition0%0/3
defined $value and length $value
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and length $value
defined $value length $value
Truth table0%0/3
A and B
A= defined $valueB= length $value
ABresult
0X0
100
111
215 0 my %allowed = map { $_ => 1 } @{$rules->{values}};
216 0 my @kept = grep { $allowed{$_} } @items;
217 0 my @left = grep { !$allowed{$_} } @items;
218 0 ▶ if (@left) {
Branch: 1 branch
BranchTrueFalseTotal
if (@left) 0 0 0
219 0 ▶ if ($key eq 'categories' and $tags_free) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'categories' and $tags_free) { } 0 0 0
Condition0%0/3
$key eq "categories" and $tags_free
!ll&&!rl&&r
000
MC/DC0%0/2
$key eq "categories" and $tags_free
$key eq "categories" $tags_free
Truth table0%0/3
A and B
A= $key eq "categories"B= $tags_free
ABresult
0X0
100
111
220 # A category that is already a tag (in any case) isn't added twice
221 0 ▶ my %has = map { lc($_) => 1 } @{ $get->('tags') || [] };
Condition0%0/2
&$get('tags') || []
l!l
00
MC/DC0%0/1
&$get('tags') || []
&$get('tags')
Truth table0%0/2
A
A= &$get('tags')
Aresult
11
00
222 0 ▶ $set->(tags => [ @{ $get->('tags') || [] }, grep { !$has{lc $_}++ } @left ]);
Condition0%0/2
&$get('tags') || []
l!l
00
MC/DC0%0/1
&$get('tags') || []
&$get('tags')
Truth table0%0/2
A
A= &$get('tags')
Aresult
11
00
223 0 note($state, "categories $schema_file doesn't allow, kept as tags", $file);
224 }
225 else {
226 0 note($state, "values of \"$key\" $schema_file doesn't allow, left out", $file);
227 }
228 }
229 0 ▶ if (not @kept and $required{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @kept and $required{$key}) 0 0 0
Condition0%0/3
not @kept and $required{$key}
!ll&&!rl&&r
000
MC/DC0%0/2
not @kept and $required{$key}
not @kept $required{$key}
Truth table0%0/3
not(A) and B
A= not @keptB= $required{$key}
ABresult
0X0
100
111
230 0 @kept = ($rules->{values}->[0]);
231 0 ▶ note($state, "pages given \"$key: $kept[0]\", which $schema_file requires", $file) unless @items;
Branch: 1 branch
BranchTrueFalseTotal
unless @items 0 0 0
232 }
233 0 $set->($key => \@kept);
234 }
235  
236 0 ▶ if ($required{title}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($required{"title"}) 0 0 0
237 0 my $title = $get->('title');
238 0 ▶ if (not defined $title or ref $title or not length $title) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $title or ref $title or not length $title) 0 0 0
Condition0%0/6
not defined $title or ref $title or not length $title
l!l&&r!l&&!r
000
not defined $title or ref $title
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $title or ref $title or not length $title
not defined $title ref $title not length $title
Truth table0%0/4
not(A) or B or not(C)
A= not defined $titleB= ref $titleC= not length $title
ABCresult
1XX1
01X1
0011
0000
239 0 $set->(title => 'Untitled');
240 0 note($state, 'pages without a title, now called "Untitled"', $file);
241 }
242 }
243  
244 0 for my $key (sort keys %$fields) {
245 0 my $max = $fields->{$key}->{max_length};
246 0 my $value = $get->($key);
247 0 ▶ next unless defined $max and defined $value and !ref $value and length $value > $max;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max and defined $value and not ref $value and length $value > $max 0 0 0
Condition0%0/9
defined $max and defined $value and not ref $value and length $value > $max
!ll&&!rl&&r
000
defined $max and defined $value and not ref $value
!ll&&!rl&&r
000
defined $max and defined $value
!ll&&!rl&&r
000
MC/DC0%0/4
defined $max and defined $value and not ref $value and length $value > $max
defined $max defined $value not ref $value length $value > $max
Truth table0%0/5
A and B and not(C) and D
A= defined $maxB= defined $valueC= not ref $valueD= length $value > $max
ABCDresult
0XXX0
10XX0
110X0
11100
11111
248 0 my $short = substr($value, 0, $max - 1);
249 0 ▶ $short =~ s/\s+\S*\z// if $short =~ /\s/;
Branch: 1 branch
BranchTrueFalseTotal
if $short =~ /\s/ 0 0 0
250 0 $set->($key => "$short\x{2026}");
251 0 note($state, "\"$key\" shortened to the $max characters $schema_file allows", $file);
252 }
253 }
254  
255 # front_matter(\@pairs) - YAML for front matter, in the order given, leaving
256 # out empty values.
257 0 ▶sub front_matter {
Subroutine front_matter: not called CC=9 SCAR=45.0
Pod undocumented
258 0 my ($pairs) = @_;
259  
260 0 my $yaml = YAML::PP->new(header => 0);
261 0 my $out = '';
262 0 for my $pair (@$pairs) {
263 0 my ($key, $value) = @$pair;
264 0 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
265 0 ▶ next if ref $value eq 'ARRAY' and not @$value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" and not @$value 0 0 0
Condition0%0/3
ref $value eq "ARRAY" and not @$value
!ll&&!rl&&r
000
MC/DC0%0/2
ref $value eq "ARRAY" and not @$value
ref $value eq "ARRAY" not @$value
Truth table0%0/3
A and not(B)
A= ref $value eq "ARRAY"B= not @$value
ABresult
0X0
100
111
266 0 ▶ next if !ref $value and not length $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value or length $value 0 0 0
Condition0%0/3
ref $value or length $value
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $value or length $value
ref $value length $value
Truth table0%0/3
A or B
A= ref $valueB= length $value
ABresult
1X1
011
000
267 0 ▶ if ($key eq 'draft') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "draft") 0 0 0
268 0 ▶ $out .= "draft: true\n" if $value;
Branch: 1 branch
BranchTrueFalseTotal
if $value 0 0 0
269 0 next;
270 }
271 0 $out .= $yaml->dump_string({ $key => $value });
272 }
273 0 return $out;
274 }
275  
276 # copy_files(state, from, to) - Copy a file or directory into the project,
277 # skipping files that are already there.
278 0 ▶sub copy_files {
Subroutine copy_files: not called CC=8 SCAR=42.8
Pod undocumented
279 0 my ($state, $from, $to) = @_;
280  
281 0 ▶ if (-d $from) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $from) 0 0 0
282 0 for my $entry (App::Dapper::Utils::read_dir($from)) {
283 0 ▶ next if $entry =~ /^\./;
Branch: 1 branch
BranchTrueFalseTotal
if $entry =~ /^\./ 0 0 0
284 0 copy_files($state, "$from/$entry", "$to/$entry");
285 }
286 0 return;
287 }
288 0 ▶ return unless -f $from;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $from 0 0 0
289 0 ▶ if (-e $to) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $to) 0 0 0
290 0 push @{$state->{skipped}}, $to;
291 0 return;
292 }
293 0 $state->{copied}++;
294 0 ▶ return if $state->{dry_run};
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"dry_run"} 0 0 0
295 0 ▶ App::Dapper::Utils::create_dir(dirname($to)) unless -d dirname($to);
Branch: 1 branch
BranchTrueFalseTotal
unless -d dirname($to) 0 0 0
296 0 App::Dapper::Utils::copy_recursive($from, $to);
297 }
298  
299 # note(state, message) - Remember something to tell the user, once per page.
300 0 ▶sub note {
Subroutine note: not called CC=1 SCAR=6.9
Pod undocumented
301 0 my ($state, $message, $file) = @_;
302 0 push @{$state->{notes}->{$message}}, $file;
303 }
304  
305 0 ▶sub write_redirects {
Subroutine write_redirects: not called CC=8 SCAR=42.8
Pod undocumented
306 0 my ($state) = @_;
307  
308 0 my %new = %{$state->{redirects}};
309 0 ▶ return unless %new;
Branch: 1 branch
BranchTrueFalseTotal
unless %new 0 0 0
310  
311 0 my $file = catfile($state->{dapper}->{data_dir}, 'redirects.yml');
312 0 ▶ my $existing = -f $file ? YAML::PP::LoadFile($file) : {};
Branch: 1 branch
BranchTrueFalseTotal
-f $file ? : 0 0 0
313 0 ▶ $existing = {} unless ref $existing eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $existing eq "HASH" 0 0 0
314 0 my $added = 0;
315 0 for my $from (sort keys %new) {
316 0 ▶ next if exists $existing->{$from};
Branch: 1 branch
BranchTrueFalseTotal
if exists $existing->{$from} 0 0 0
317 0 $existing->{$from} = $new{$from};
318 0 $added++;
319 }
320 0 $state->{redirects_added} = $added;
321 0 ▶ return if $state->{dry_run} or not $added;
Branch: 1 branch
BranchTrueFalseTotal
if $state->{"dry_run"} or not $added 0 0 0
Condition0%0/3
$state->{"dry_run"} or not $added
l!l&&r!l&&!r
000
MC/DC0%0/2
$state->{"dry_run"} or not $added
$state->{"dry_run"} not $added
Truth table0%0/3
A or not(B)
A= $state->{"dry_run"}B= not $added
ABresult
1X1
011
000
322 0 App::Dapper::Utils::write_file($file, YAML::PP->new(header => 0)->dump_string($existing));
323 }
324  
325 0 ▶sub report {
Subroutine report: not called CC=22 SCAR=62.3
Pod undocumented
326 0 my ($state) = @_;
327  
328 0 my $dapper = $state->{dapper};
329 0 my @pages = @{$state->{pages}};
330 0 my $posts = grep { $_->{kind} eq 'post' } @pages;
331 0 my $drafts = grep { $_->{draft} } @pages;
332 0 ▶ my $verb = $state->{dry_run} ? 'Would import' : 'Imported';
Branch: 1 branch
BranchTrueFalseTotal
$state->{'dry_run'} ? : 0 0 0
333  
334 0 ▶ $dapper->_log("$verb $posts post" . ($posts == 1 ? '' : 's') . " and " . (@pages - $posts) . " page"
Branch: 3 branches
BranchTrueFalseTotal
$posts == 1 ? : 0 0 0
@pages - $posts == 1 ? : 0 0 0
$drafts ? : 0 0 0
335 . (@pages - $posts == 1 ? '' : 's') . " into $state->{source}" . ($drafts ? " ($drafts as drafts)" : '') . ".");
336 $dapper->_log(($state->{dry_run} ? 'Would copy' : 'Copied') . " $state->{copied} other file" . ($state->{copied} == 1 ? '' : 's') . ".")
337 0 ▶ if $state->{copied};
Branch: 3 branches
BranchTrueFalseTotal
if $state->{"copied"} 0 0 0
$state->{'dry_run'} ? : 0 0 0
$state->{'copied'} == 1 ? : 0 0 0
338 $dapper->_log(($state->{dry_run} ? 'Would add' : 'Added') . " $state->{redirects_added} redirect"
339 . ($state->{redirects_added} == 1 ? '' : 's') . " to " . catfile($dapper->{data_dir}, 'redirects.yml') . ".")
340 0 ▶ if $state->{redirects_added};
Branch: 3 branches
BranchTrueFalseTotal
if $state->{"redirects_added"} 0 0 0
$state->{'dry_run'} ? : 0 0 0
$state->{'redirects_added'} == 1 ? : 0 0 0
341  
342 0 ▶ if (my @skipped = @{$state->{skipped}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@skipped) = @{$state->{"skipped"};}) 0 0 0
343 0 ▶ $dapper->_log("Left alone " . scalar(@skipped) . " file" . (@skipped == 1 ? '' : 's') . " that already exist:");
Branch: 1 branch
BranchTrueFalseTotal
@skipped == 1 ? : 0 0 0
344 0 ▶ $dapper->_log(" $_") for @skipped[0 .. ($#skipped < 9 ? $#skipped : 9)];
Branch: 1 branch
BranchTrueFalseTotal
$#skipped < 9 ? : 0 0 0
345 0 ▶ $dapper->_log(" ...") if @skipped > 10;
Branch: 1 branch
BranchTrueFalseTotal
if @skipped > 10 0 0 0
346 }
347  
348 0 for my $message (sort keys %{$state->{notes}}) {
349 0 my %seen;
350 0 ▶ my @files = grep { defined and !$seen{$_}++ } @{$state->{notes}->{$message}};
Condition0%0/3
defined $_ && !$seen{$_}++
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && !$seen{$_}++
defined $_ !$seen{$_}++
Truth table0%0/3
A && not(B)
A= defined $_B= !$seen{$_}++
ABresult
0X0
100
111
351 0 ▶ $dapper->_log("Check: $message" . (@files ? " (" . scalar(@files) . " page" . (@files == 1 ? '' : 's') . ", such as $files[0])" : ''));
Branch: 2 branches
BranchTrueFalseTotal
@files ? : 0 0 0
@files == 1 ? : 0 0 0
352 }
353  
354 0 ▶ $dapper->_log("Run \"dapper check\" to find links that need fixing.") if @pages and not $state->{dry_run};
Branch: 1 branch
BranchTrueFalseTotal
if @pages and not $state->{"dry_run"} 0 0 0
Condition0%0/3
@pages and not $state->{"dry_run"}
!ll&&!rl&&r
000
MC/DC0%0/2
@pages and not $state->{"dry_run"}
@pages not $state->{"dry_run"}
Truth table0%0/3
A and not(B)
A= @pagesB= not $state->{"dry_run"}
ABresult
0X0
100
111
355 }
356  
357 # --- Helpers ---------------------------------------------------------------
358  
359 0 ▶sub decode_entities {
Subroutine decode_entities: not called CC=2 SCAR=17.9
Pod undocumented
360 0 my ($text) = @_;
361 0 ▶ return '' unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
362 0 $text =~ s/&lt;/</g;
363 0 $text =~ s/&gt;/>/g;
364 0 $text =~ s/&quot;/"/g;
365 0 $text =~ s/&#0*39;|&apos;/'/g;
366 0 $text =~ s/&#(\d+);/chr($1)/ge;
367 0 $text =~ s/&#x([0-9a-f]+);/chr(hex $1)/gie;
368 0 $text =~ s/&amp;/&/g;
369 0 return $text;
370 }
371  
372 0 ▶sub strip_tags {
Subroutine strip_tags: not called CC=2 SCAR=17.9
Pod undocumented
373 0 my ($html) = @_;
374 0 ▶ $html = '' unless defined $html;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $html 0 0 0
375 0 $html =~ s/<[^>]*>//g;
376 0 $html = decode_entities($html);
377 0 $html =~ s/\s+/ /g;
378 0 $html =~ s/^ | $//g;
379 0 return $html;
380 }
381  
382 # url_path(url) - The path of a URL: https://example.com/a/b/?x=1 is /a/b/.
383 # decode_path(text) - Percent-encoded letters outside ASCII (as WordPress
384 # writes "επίπεδο") back to the letters; encoded ASCII such as %2F is left as
385 # it is, since decoding it would change what the address means.
386 0 ▶sub decode_path {
Subroutine decode_path: not called CC=5 SCAR=34.0
Pod undocumented
387 0 my ($text) = @_;
388 0 ▶ return $text unless defined $text and $text =~ /%[89A-Fa-f][0-9A-Fa-f]/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and $text =~ /%[89A-Fa-f][0-9A-Fa-f]/ 0 0 0
Condition0%0/3
defined $text and $text =~ /%[89A-Fa-f][0-9A-Fa-f]/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $text and $text =~ /%[89A-Fa-f][0-9A-Fa-f]/
defined $text $text =~ /%[89A-Fa-f][0-9A-Fa-f]/
Truth table0%0/3
A and B
A= defined $textB= $text =~ /%[89A-Fa-f][0-9A-Fa-f]/
ABresult
0X0
100
111
389 0 ▶ my $bytes = $text =~ /[^\x00-\x7F]/ ? Encode::encode('UTF-8', $text) : $text;
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /[^\x00-\x7F]/ ? : 0 0 0
390 0 $bytes =~ s/%([89A-Fa-f][0-9A-Fa-f])/chr hex $1/ge;
391 0 my $decoded = eval { Encode::decode('UTF-8', $bytes, Encode::FB_CROAK()) };
392 0 ▶ return defined $decoded ? $decoded : $text;
Branch: 1 branch
BranchTrueFalseTotal
defined $decoded ? : 0 0 0
393 }
394  
395 0 ▶sub url_path {
Subroutine url_path: not called CC=4 SCAR=30.0
Pod undocumented
396 0 my ($url) = @_;
397 0 ▶ return unless defined $url and length $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url and length $url 0 0 0
Condition0%0/3
defined $url and length $url
!ll&&!rl&&r
000
MC/DC0%0/2
defined $url and length $url
defined $url length $url
Truth table0%0/3
A and B
A= defined $urlB= length $url
ABresult
0X0
100
111
398 0 $url =~ s{^[a-z]+://[^/]+}{}i;
399 0 $url =~ s/[?#].*$//;
400 0 $url =~ s{/{2,}}{/}g;
401 0 $url = decode_path($url);
402 0 ▶ $url = "/$url" unless $url =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ m[^/] 0 0 0
403 0 return $url;
404 }
405  
406 # date_string(text) - A date as YYYY-MM-DDTHH:MM:SS, or nothing.
407 0 ▶sub date_string {
Subroutine date_string: not called CC=7 SCAR=40.3
Pod undocumented
408 0 my ($date) = @_;
409 0 ▶ return unless defined $date;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $date 0 0 0
410 0 $date = "$date";
411 0 ▶ return if $date =~ /^0000-/;
Branch: 1 branch
BranchTrueFalseTotal
if $date =~ /^0000-/ 0 0 0
412 # Jekyll writes "2019-12-31 23:00:00 +0100"
413 0 $date =~ s/^(\d{4}-\d\d-\d\d)[ T](\d\d:\d\d(?::\d\d)?)\s*([+-]\d\d):?(\d\d)$/$1T$2$3:$4/;
414 0 ▶ my $parsed = eval { App::Dapper::Utils::parse_date($date) } or return;
Branch: 1 branch
BranchTrueFalseTotal
my $parsed = eval { do { App::Dapper::Utils::parse_date($date) } } or return 0 0 0
415 0 my $out = $parsed->strftime('%Y-%m-%dT%H:%M:%S');
416 0 ▶ if (not $parsed->time_zone->is_floating) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parsed->time_zone->is_floating) 0 0 0
417 0 my $offset = $parsed->strftime('%z');
418 0 $offset =~ s/(\d\d)$/:$1/;
419 0 ▶ $out .= $offset eq '+00:00' && $date =~ /Z$/ ? 'Z' : $offset;
Branch: 1 branch
BranchTrueFalseTotal
$offset eq '+00:00' && $date =~ /Z$/ ? : 0 0 0
Condition0%0/3
$offset eq "+00:00" and $date =~ /Z$/
!ll&&!rl&&r
000
MC/DC0%0/2
$offset eq "+00:00" and $date =~ /Z$/
$offset eq "+00:00" $date =~ /Z$/
Truth table0%0/3
A and B
A= $offset eq "+00:00"B= $date =~ /Z$/
ABresult
0X0
100
111
420 }
421 0 return $out;
422 }
423  
424 0 ▶sub list {
Subroutine list: not called CC=5 SCAR=34.0
Pod undocumented
425 0 my ($value) = @_;
426 0 ▶ return [] unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
427 0 ▶ return [ grep { defined and length } @$value ] if ref $value eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 0 0 0
Condition0%0/3
defined $_ && length $_
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table0%0/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
428 0 ▶ return [ grep { length } split /\s*,\s*|\s+/, $value ] if !ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 0 0 0
429 0 return [];
430 }
431  
432 0 ▶sub layout_for {
Subroutine layout_for: not called CC=3 SCAR=24.8
Pod undocumented
433 0 my ($state, $name) = @_;
434 0 ▶ return defined $name && $state->{layouts}->{$name} ? $name : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $name && $state->{'layouts'}{$name} ? : 0 0 0
Condition0%0/3
defined $name and $state->{"layouts"}{$name}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $name and $state->{"layouts"}{$name}
defined $name $state->{"layouts"}{$name}
Truth table0%0/3
A and B
A= defined $nameB= $state->{"layouts"}{$name}
ABresult
0X0
100
111
435 }
436  
437 # video(url) - The HTML for a YouTube or Vimeo address, or nothing.
438 0 ▶sub video {
Subroutine video: not called CC=5 SCAR=34.0
Pod undocumented
439 0 my ($url) = @_;
440 0 for my $site (qw/ youtube vimeo /) {
441 0 ▶ next unless $url =~ ($site eq 'youtube' ? qr{youtu\.?be} : qr{vimeo\.com});
Branch: 2 branches
BranchTrueFalseTotal
unless $url =~ ($site eq "youtube" ? qr/youtu\.?be/ : qr/vimeo\.com/) 0 0 0
$site eq 'youtube' ? : 0 0 0
442 0 ▶ my $id = App::Dapper::Components::video_id($site, $url) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $id = App::Dapper::Components::video_id($site, $url) or next 0 0 0
443 0 return App::Dapper::Components::built_in($site, [$id], '');
444 }
445 0 return;
446 }
447  
448 # --- WordPress -------------------------------------------------------------
449  
450 0 ▶sub wordpress {
Subroutine wordpress: not called CC=32 SCAR=69.6
Pod documented
451 0 my ($state, $from) = @_;
452  
453 0 ▶ die "error: give the WordPress export file (Tools, Export in WordPress), not a directory\n" if -d $from;
Branch: 1 branch
BranchTrueFalseTotal
if -d $from 0 0 0
454 0 my $xml = App::Dapper::Utils::read_file($from);
455 0 ▶ die "error: $from is not a WordPress export file\n" unless $xml =~ /<rss\b/ and $xml =~ /<wp:/;
Branch: 1 branch
BranchTrueFalseTotal
unless $xml =~ /<rss\b/ and $xml =~ /<wp:/ 0 0 0
Condition0%0/3
$xml =~ /<rss\b/ and $xml =~ /<wp:/
!ll&&!rl&&r
000
MC/DC0%0/2
$xml =~ /<rss\b/ and $xml =~ /<wp:/
$xml =~ /<rss\b/ $xml =~ /<wp:/
Truth table0%0/3
A and B
A= $xml =~ /<rss\b/B= $xml =~ /<wp:/
ABresult
0X0
100
111
456  
457 0 ▶ my $cdata = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
458 0 my ($text) = @_;
459 0 ▶ return undef unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
460 0 ▶ return $text =~ /^\s*<!\[CDATA\[(.*)\]\]>\s*$/s ? $1 : decode_entities($text);
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /^\s*<!\[CDATA\[(.*)\]\]>\s*$/s ? : 0 0 0
461 0 };
462  
463 0 my $layout = layout_for($state, 'post');
464  
465 0 ▶ while ($xml =~ m{<item>(.*?)</item>}gs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml =~ m[<item>(.*?)</item>]gs) 0 0 0
466 0 my $item = $1;
467 0 ▶ my $get = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
468 0 my ($tag) = @_;
469 0 ▶ return $item =~ m{<\Q$tag\E>(.*?)</\Q$tag\E>}s ? $cdata->($1) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$item =~ m[<\Q$tag\E>(.*?)</\Q$tag\E>]s ? : 0 0 0
470 0 };
471  
472 0 ▶ my $type = $get->('wp:post_type') || 'post';
Condition0%0/2
&$get('wp:post_type') || 'post'
l!l
00
MC/DC0%0/1
&$get('wp:post_type') || 'post'
&$get('wp:post_type')
Truth table0%0/2
A
A= &$get('wp:post_type')
Aresult
11
00
473 0 ▶ next unless $type eq 'post' or $type eq 'page';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "post" or $type eq "page" 0 0 0
Condition0%0/3
$type eq "post" or $type eq "page"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "post" or $type eq "page"
$type eq "post" $type eq "page"
Truth table0%0/3
A or B
A= $type eq "post"B= $type eq "page"
ABresult
1X1
011
000
474 0 ▶ my $status = $get->('wp:status') || 'publish';
Condition0%0/2
&$get('wp:status') || 'publish'
l!l
00
MC/DC0%0/1
&$get('wp:status') || 'publish'
&$get('wp:status')
Truth table0%0/2
A
A= &$get('wp:status')
Aresult
11
00
475 0 ▶ next if $status =~ /^(?:trash|auto-draft|inherit)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $status =~ /^(?:trash|auto-draft|inherit)$/ 0 0 0
476  
477 0 my $title = $get->('title');
478 0 ▶ $title = '' unless defined $title;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $title 0 0 0
479 0 my $slug = decode_path($get->('wp:post_name'));
480 0 ▶ $slug = App::Dapper::Utils::slugify($title) unless defined $slug and length $slug;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $slug and length $slug 0 0 0
Condition0%0/3
defined $slug and length $slug
!ll&&!rl&&r
000
MC/DC0%0/2
defined $slug and length $slug
defined $slug length $slug
Truth table0%0/3
A and B
A= defined $slugB= length $slug
ABresult
0X0
100
111
481 0 ▶ $slug = 'post-' . ($get->('wp:post_id') || scalar @{$state->{pages}}) unless defined $slug and length $slug;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $slug and length $slug 0 0 0
Condition0%0/5
defined $slug and length $slug
!ll&&!rl&&r
000
&$get('wp:post_id') || scalar @{$state->{'pages'};}
l!l
00
MC/DC0%0/2
defined $slug and length $slug
defined $slug length $slug
MC/DC0%0/1
&$get('wp:post_id') || scalar @{$state->{'pages'};}
&$get('wp:post_id')
Truth table0%0/3
A and B
A= defined $slugB= length $slug
ABresult
0X0
100
111
Truth table0%0/2
A
A= &$get('wp:post_id')
Aresult
11
00
482  
483 0 my (@categories, @tags);
484 0 ▶ while ($item =~ m{<category\s+domain="(category|post_tag)"[^>]*>(.*?)</category>}gs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item =~ m[<category\s+domain="(category|post_tag)"[^>]*>(.*?)</category>]gs) 0 0 0
485 # WordPress keeps names escaped, even in CDATA
486 0 my $name = decode_entities($cdata->($2));
487 0 ▶ next if $1 eq 'category' and $name eq 'Uncategorized';
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "category" and $name eq "Uncategorized" 0 0 0
Condition0%0/3
$1 eq "category" and $name eq "Uncategorized"
!ll&&!rl&&r
000
MC/DC0%0/2
$1 eq "category" and $name eq "Uncategorized"
$1 eq "category" $name eq "Uncategorized"
Truth table0%0/3
A and B
A= $1 eq "category"B= $name eq "Uncategorized"
ABresult
0X0
100
111
488 0 ▶ push @{ $1 eq 'category' ? \@categories : \@tags }, $name;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq 'category' ? : 0 0 0
489 }
490  
491 0 ▶ my $draft = $status =~ /^(?:draft|pending|private)$/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$status =~ /^(?:draft|pending|private)$/ ? : 0 0 0
492 # A link like /?p=5 only means "post 5"; the post has no address of its own
493 0 my $raw_link = $get->('link');
494 0 ▶ my $link = defined $raw_link && $raw_link !~ /[?&](?:p|page_id)=/ ? url_path($raw_link) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $raw_link && !($raw_link =~ /[?&](?:p|page_id)=/) ? : 0 0 0
Condition0%0/3
defined $raw_link and not $raw_link =~ /[?&](?:p|page_id)=/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $raw_link and not $raw_link =~ /[?&](?:p|page_id)=/
defined $raw_link not $raw_link =~ /[?&](?:p|page_id)=/
Truth table0%0/3
A and not(B)
A= defined $raw_linkB= not $raw_link =~ /[?&](?:p|page_id)=/
ABresult
0X0
100
111
495 0 ▶ my $urlpattern = !$draft && defined $link ? $link : undef;
Branch: 1 branch
BranchTrueFalseTotal
!$draft && defined $link ? : 0 0 0
Condition0%0/3
not $draft and defined $link
!ll&&!rl&&r
000
MC/DC0%0/2
not $draft and defined $link
not $draft defined $link
Truth table0%0/3
not(A) and B
A= not $draftB= defined $link
ABresult
0X0
100
111
496  
497 # WordPress's "posts page" only holds the list of posts; where the
498 # project makes a page per category, the blog's own list page is at
499 # that address already
500 0 ▶ if ($type eq 'page' and defined $urlpattern and $urlpattern eq "/$state->{to}/"
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "page" and defined $urlpattern and $urlpattern eq qq[/$state->{"to"}/] and $state->{"dapper"}{"site"}{"category_pages"} and App::Dapper::Publish::enabled($state->{"dapper"}{"site"}, "category_pages")) 0 0 0
Condition0%0/12
$type eq "page" and defined $urlpattern and $urlpattern eq qq[/$state->{"to"}/] and $state->{"dapper"}{"site"}{"category_pages"} and App::Dapper::Publish::enabled($state->{"dapper"}{"site"}, "category_pages")
!ll&&!rl&&r
000
$type eq "page" and defined $urlpattern and $urlpattern eq qq[/$state->{"to"}/] and $state->{"dapper"}{"site"}{"category_pages"}
!ll&&!rl&&r
000
$type eq "page" and defined $urlpattern and $urlpattern eq qq[/$state->{"to"}/]
!ll&&!rl&&r
000
$type eq "page" and defined $urlpattern
!ll&&!rl&&r
000
MC/DC0%0/5
$type eq "page" and defined $urlpattern and $urlpattern eq qq[/$state->{"to"}/] and $state->{"dapper"}{"site"}{"category_pages"} and App::Dapper::Publish::enabled($state->{"dapper"}{"site"}, "category_pages")
$type eq "page" defined $urlpattern $urlpattern eq qq[/$state->{"to"}/] $state->{"dapper"}{"site"}{"category_pages"} App::Dapper::Publish::enabled($state->{"dapper"}{"site"}, "category_pages")
Truth table0%0/6
A and B and C and D and E
A= $type eq "page"B= defined $urlpatternC= $urlpattern eq qq[/$state->{"to"}/]D= $state->{"dapper"}{"site"}{"category_pages"}E= App::Dapper::Publish::enabled($state->{"dapper"}{"site"}, "category_pages")
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
501 and $state->{dapper}->{site}->{category_pages}
502 and App::Dapper::Publish::enabled($state->{dapper}->{site}, 'category_pages')) {
503 0 note($state, "pages at the address of the list of posts (/$state->{to}/), left out since the project makes that list", "$slug.html");
504 0 next;
505 }
506  
507 0 ▶ my $path = $type eq 'post' ? "$state->{to}/$slug.html" : "$slug.html";
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'post' ? : 0 0 0
508 0 my $body = wordpress_html($state, $get->('content:encoded'), $path);
509 0 my $excerpt = strip_tags($get->('excerpt:encoded'));
510  
511 0 ▶ add_page($state, $path, [
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'post' ? : 0 0 0
512 [ title => $title ],
513 [ date => date_string($get->('wp:post_date')) ],
514 [ layout => $type eq 'post' ? $layout : undef ],
515 [ categories => \@categories ],
516 [ tags => \@tags ],
517 [ description => $excerpt ],
518 [ urlpattern => $urlpattern ],
519 [ draft => $draft ],
520 ], $body, $type);
521 }
522 }
523  
524 # wordpress_html(state, html, path) - WordPress content as plain HTML:
525 # paragraphs added where WordPress would add them, block comments removed,
526 # and [caption] and [embed] converted.
527 0 ▶sub wordpress_html {
Subroutine wordpress_html: not called CC=12 SCAR=50.5
Pod undocumented
528 0 my ($state, $html, $path) = @_;
529  
530 0 ▶ $html = '' unless defined $html;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $html 0 0 0
531 0 $html =~ s/<!--\s*\/?wp:.*?-->\n?//gs;
532  
533 # Without the block editor, WordPress adds paragraphs when it shows a post
534 0 my $classic = $html !~ m{<(?:p|div|h[1-6]|ul|ol|figure|blockquote|table|pre|section)\b}i;
535  
536 # [embed]URL[/embed], and a video's address on a line of its own
537 0 ▶ $html =~ s{\[embed[^\]]*\](.*?)\[/embed\]}{video($1) || $1}ge;
Condition0%0/3
video($1) || $1
l!l&&r!l&&!r
000
MC/DC0%0/2
video($1) || $1
video($1) $1
Truth table0%0/3
A || B
A= video($1)B= $1
ABresult
1X1
011
000
538 0 ▶ $html =~ s{^[ \t]*(https?://\S+)[ \t]*$}{video($1) || $1}gme;
Condition0%0/3
video($1) || $1
l!l&&r!l&&!r
000
MC/DC0%0/2
video($1) || $1
video($1) $1
Truth table0%0/3
A || B
A= video($1)B= $1
ABresult
1X1
011
000
539  
540 0 $html =~ s{\[caption[^\]]*\](.*?)\[/caption\]}{
541 0 my $inner = $1;
542 0 my ($image) = $inner =~ /(<a\b[^>]*>\s*<img\b[^>]*>\s*<\/a>|<img\b[^>]*>)/s;
543 0 my $caption = $inner;
544 0 ▶ $caption =~ s/\Q$image\E// if defined $image;
Branch: 1 branch
BranchTrueFalseTotal
if defined $image 0 0 0
545 0 $caption =~ s/^\s+|\s+$//g;
546 0 ▶ defined $image ? "<figure>$image<figcaption>$caption</figcaption></figure>" : $inner;
Branch: 1 branch
BranchTrueFalseTotal
defined $image ? : 0 0 0
547 }gse;
548  
549 0 ▶ if ($classic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($classic) 0 0 0
550 0 my @blocks = grep { /\S/ } split /\n\s*\n/, $html;
551 $html = join("\n\n", map {
552 0 my $block = $_;
553 0 $block =~ s/^\s+|\s+$//g;
554 0 ▶ $block =~ /^<(?:iframe|div|figure)\b/ ? $block : do { $block =~ s/\n/<br>\n/g; "<p>$block</p>" }
Branch: 1 branch
BranchTrueFalseTotal
$block =~ /^<(?:iframe|div|figure)\b/ ? : 0 0 0
555 } @blocks);
556 }
557  
558 0 my %shortcodes;
559 0 ▶ $shortcodes{$1}++ while $html =~ /\[([A-Za-z][\w-]*)(?:\s[^\]]*)?\]/g;
Branch: 1 branch
BranchTrueFalseTotal
$html =~ /\[([A-Za-z][\w-]*)(?:\s[^\]]*)?\]/g and do { ++$shortcodes{$1} } 0 0 0
560 0 ▶ note($state, "WordPress shortcodes that were left as text: " . join(', ', map { "[$_]" } sort keys %shortcodes), "$state->{source}/$path")
Branch: 1 branch
BranchTrueFalseTotal
if %shortcodes 0 0 0
561 if %shortcodes;
562 0 ▶ note($state, 'images that still load from the old WordPress site (wp-content/uploads); copy them into the site', "$state->{source}/$path")
Branch: 1 branch
BranchTrueFalseTotal
if $html =~ m[wp-content/uploads] 0 0 0
563 if $html =~ m{wp-content/uploads};
564  
565 0 ▶ $html .= "\n" unless $html =~ /\n$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $html =~ /\n$/ 0 0 0
566 0 return $html;
567 }
568  
569 # --- Jekyll ----------------------------------------------------------------
570  
571 191 my %JEKYLL_STYLES = (
572 date => '/:categories/:year/:month/:day/:title:output_ext',
573 pretty => '/:categories/:year/:month/:day/:title/',
574 ordinal => '/:categories/:year/:y_day/:title:output_ext',
575 none => '/:categories/:title:output_ext',
576 );
577  
578 0 ▶sub jekyll {
Subroutine jekyll: not called CC=32 SCAR=69.6
Pod documented
579 0 my ($state, $from) = @_;
580  
581 0 ▶ die "error: give the Jekyll site's directory\n" unless -d $from;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $from 0 0 0
582 0 ▶ my $config = -f "$from/_config.yml" ? (eval { YAML::PP::LoadFile("$from/_config.yml") } || {}) : {};
Branch: 1 branch
BranchTrueFalseTotal
-f "$from/_config.yml" ? : 0 0 0
Condition0%0/2
eval { do { YAML::PP::LoadFile("$from/_config.yml") } } or {}
l!l
00
MC/DC0%0/1
eval { do { YAML::PP::LoadFile("$from/_config.yml") } } or {}
eval { do { YAML::PP::LoadFile("$from/_config.yml") } }
Truth table0%0/2
A
A= eval { do { YAML::PP::LoadFile("$from/_config.yml") } }
Aresult
11
00
583 0 ▶ $config = {} unless ref $config eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $config eq "HASH" 0 0 0
584 0 ▶ my $permalink = defined $config->{permalink} ? $config->{permalink} : 'date';
Branch: 1 branch
BranchTrueFalseTotal
defined $config->{'permalink'} ? : 0 0 0
585 0 ▶ $permalink = $JEKYLL_STYLES{$permalink} if $JEKYLL_STYLES{$permalink};
Branch: 1 branch
BranchTrueFalseTotal
if $JEKYLL_STYLES{$permalink} 0 0 0
586  
587 0 my @items;
588  
589 # Posts and drafts
590 0 for my $dir (qw/ _posts _drafts /) {
591 0 ▶ next unless -d "$from/$dir";
Branch: 1 branch
BranchTrueFalseTotal
unless -d "$from/$dir" 0 0 0
592 0 ▶ File::Find::find({ no_chdir => 1, wanted => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
593 0 ▶ return unless -f $_ and /\.(?:md|markdown|mkd|html)$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $_ and /\.(?:md|markdown|mkd|html)$/i 0 0 0
Condition0%0/3
-f $_ and /\.(?:md|markdown|mkd|html)$/i
!ll&&!rl&&r
000
MC/DC0%0/2
-f $_ and /\.(?:md|markdown|mkd|html)$/i
-f $_ /\.(?:md|markdown|mkd|html)$/i
Truth table0%0/3
A and B
A= -f $_B= /\.(?:md|markdown|mkd|html)$/i
ABresult
0X0
100
111
594 0 my $name = basename($_);
595 0 my ($date, $slug, $ext) = $name =~ /^(\d{4}-\d{2}-\d{2})-(.+)\.(\w+)$/;
596 0 ▶ if ($dir eq '_drafts') { ($slug, $ext) = $name =~ /^(.+)\.(\w+)$/ unless defined $slug }
Branch: 2 branches
BranchTrueFalseTotal
if ($dir eq "_drafts") 0 0 0
unless defined $slug 0 0 0
597 0 ▶ return unless defined $slug;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $slug 0 0 0
598 0 push @items, { file => $_, kind => 'post', date => $date, slug => $slug, ext => $ext, draft => $dir eq '_drafts' };
599 0 } }, "$from/$dir");
600 }
601  
602 # Pages: files with front matter outside the folders Jekyll keeps to itself
603 0 ▶ File::Find::find({ no_chdir => 1, wanted => sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
604 0 ▶ my $rel = length($_) > length($from) ? substr($_, length($from) + 1) : '';
Branch: 1 branch
BranchTrueFalseTotal
length $_ > length $from ? : 0 0 0
605 0 ▶ if (-d $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $_) 0 0 0
606 0 ▶ $File::Find::prune = 1 if $rel =~ m{(?:^|/)[_.]} or $rel =~ m{^(?:node_modules|vendor|assets|images|img|css|js|scss|sass|files|uploads)$};
Branch: 1 branch
BranchTrueFalseTotal
if $rel =~ m[(?:^|/)[_.]] or $rel =~ /^(?:node_modules|vendor|assets|images|img|css|js|scss|sass|files|uploads)$/ 0 0 0
Condition0%0/3
$rel =~ m[(?:^|/)[_.]] or $rel =~ /^(?:node_modules|vendor|assets|images|img|css|js|scss|sass|files|uploads)$/
l!l&&r!l&&!r
000
MC/DC0%0/2
$rel =~ m[(?:^|/)[_.]] or $rel =~ /^(?:node_modules|vendor|assets|images|img|css|js|scss|sass|files|uploads)$/
$rel =~ m[(?:^|/)[_.]] $rel =~ /^(?:node_modules|vendor|assets|images|img|css|js|scss|sass|files|uploads)$/
Truth table0%0/3
A or B
A= $rel =~ m[(?:^|/)[_.]]B= $rel =~ /^(?:node_modules|vendor|assets|images|img|css|js|scss|sass|files|uploads)$/
ABresult
1X1
011
000
607 0 return;
608 }
609 0 ▶ return unless /\.(?:md|markdown|mkd|html)$/i and App::Dapper::Utils::has_frontmatter($_);
Branch: 1 branch
BranchTrueFalseTotal
unless /\.(?:md|markdown|mkd|html)$/i and App::Dapper::Utils::has_frontmatter($_) 0 0 0
Condition0%0/3
/\.(?:md|markdown|mkd|html)$/i and App::Dapper::Utils::has_frontmatter($_)
!ll&&!rl&&r
000
MC/DC0%0/2
/\.(?:md|markdown|mkd|html)$/i and App::Dapper::Utils::has_frontmatter($_)
/\.(?:md|markdown|mkd|html)$/i App::Dapper::Utils::has_frontmatter($_)
Truth table0%0/3
A and B
A= /\.(?:md|markdown|mkd|html)$/iB= App::Dapper::Utils::has_frontmatter($_)
ABresult
0X0
100
111
610 0 ▶ return if $rel =~ m{^(?:README|LICENSE|CHANGELOG)\b}i;
Branch: 1 branch
BranchTrueFalseTotal
if $rel =~ /^(?:README|LICENSE|CHANGELOG)\b/i 0 0 0
611 0 my ($stem, $ext) = $rel =~ /^(.+)\.(\w+)$/;
612 0 push @items, { file => $_, kind => 'page', rel => $stem, ext => $ext };
613 0 } }, $from);
614  
615 # Work out every post's address first, for {% post_url %}
616 0 my %post_url;
617 0 for my $item (@items) {
618 0 my $text = App::Dapper::Utils::read_file($item->{file});
619 0 my ($front, $body) = App::Dapper::Utils::split_frontmatter($text, $item->{file});
620 # A post without front matter is all body
621 0 ▶ $body = $text unless defined $front;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $front 0 0 0
622 0 ▶ $item->{front} = ref $front eq 'HASH' ? $front : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $front eq 'HASH' ? : 0 0 0
623 0 ▶ $item->{body} = defined $body ? $body : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $body ? : 0 0 0
624 0 my $f = $item->{front};
625 # YAML reads published: false as an empty string
626 0 ▶ $item->{draft} = 1 if defined $f->{published} and (!$f->{published} or $f->{published} =~ /^(?:false|no)$/i);
Branch: 1 branch
BranchTrueFalseTotal
if defined $f->{"published"} and !$f->{"published"} || $f->{"published"} =~ /^(?:false|no)$/i 0 0 0
Condition0%0/6
defined $f->{"published"} and !$f->{"published"} || $f->{"published"} =~ /^(?:false|no)$/i
!ll&&!rl&&r
000
not $f->{"published"} or $f->{"published"} =~ /^(?:false|no)$/i
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $f->{"published"} and !$f->{"published"} || $f->{"published"} =~ /^(?:false|no)$/i
defined $f->{"published"} not $f->{"published"} $f->{"published"} =~ /^(?:false|no)$/i
Truth table0%0/4
A and not(B) or C
A= defined $f->{"published"}B= not $f->{"published"}C= $f->{"published"} =~ /^(?:false|no)$/i
ABCresult
0XX0
1000
11X1
1011
627 0 $item->{date_string} = date_string($f->{date});
628 0 ▶ $item->{date_string} = date_string($item->{date}) unless defined $item->{date_string};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item->{"date_string"} 0 0 0
629  
630 0 ▶ if (defined $f->{permalink}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $f->{'permalink'}) { } 0 0 0
elsif ($item->{'kind'} eq 'post' and not $item->{'draft'}) { } 0 0 0
631 0 $item->{url} = jekyll_url($f->{permalink}, $item, $f);
632 }
633 ▶ elsif ($item->{kind} eq 'post' and not $item->{draft}) {
Condition0%0/3
$item->{"kind"} eq "post" and not $item->{"draft"}
!ll&&!rl&&r
000
MC/DC0%0/2
$item->{"kind"} eq "post" and not $item->{"draft"}
$item->{"kind"} eq "post" not $item->{"draft"}
Truth table0%0/3
A and not(B)
A= $item->{"kind"} eq "post"B= not $item->{"draft"}
ABresult
0X0
100
111
634 0 $item->{url} = jekyll_url($permalink, $item, $f);
635 }
636 0 ▶ if ($item->{kind} eq 'post' and defined $item->{date}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"kind"} eq "post" and defined $item->{"date"}) 0 0 0
Condition0%0/3
$item->{"kind"} eq "post" and defined $item->{"date"}
!ll&&!rl&&r
000
MC/DC0%0/2
$item->{"kind"} eq "post" and defined $item->{"date"}
$item->{"kind"} eq "post" defined $item->{"date"}
Truth table0%0/3
A and B
A= $item->{"kind"} eq "post"B= defined $item->{"date"}
ABresult
0X0
100
111
637 0 (my $key = basename($item->{file})) =~ s/\.\w+$//;
638 0 $post_url{$key} = $item->{url};
639 }
640 }
641  
642 0 my $post_layout = layout_for($state, 'post');
643 0 for my $item (@items) {
644 0 my $f = $item->{front};
645 0 my $markdown = $item->{ext} =~ /^(?:md|markdown|mkd)$/i;
646 0 ▶ my $ext = $markdown ? 'md' : 'html';
Branch: 1 branch
BranchTrueFalseTotal
$markdown ? : 0 0 0
647 0 ▶ my $path = $item->{kind} eq 'post' ? "$state->{to}/$item->{slug}.$ext" : "$item->{rel}.$ext";
Branch: 1 branch
BranchTrueFalseTotal
$item->{'kind'} eq 'post' ? : 0 0 0
648 0 my $body = jekyll_body($state, $item->{body}, $markdown, \%post_url, $config, "$state->{source}/$path");
649  
650 0 my @categories = @{ jekyll_categories($f) };
651 0 my %done = map { $_ => 1 } qw/ title date layout categories category tags permalink published description excerpt draft /;
652 0 my @rest = map { [ $_ => $f->{$_} ] } grep { !$done{$_} } sort keys %$f;
653  
654 add_page($state, $path, [
655 [ title => defined $f->{title} ? $f->{title} : undef ],
656 [ date => $item->{date_string} ],
657 [ layout => layout_for($state, $f->{layout}) || ($item->{kind} eq 'post' ? $post_layout : undef) ],
658 [ categories => \@categories ],
659 [ tags => list($f->{tags}) ],
660 [ description => defined $f->{description} ? $f->{description} : $f->{excerpt} ],
661 [ urlpattern => $item->{url} ],
662 @rest,
663 [ draft => $item->{draft} ],
664 0 ▶ ], $body, $item->{kind});
Branch: 3 branches
BranchTrueFalseTotal
defined $f->{'title'} ? : 0 0 0
$item->{'kind'} eq 'post' ? : 0 0 0
defined $f->{'description'} ? : 0 0 0
Condition0%0/3
layout_for($state, $f->{'layout'}) || ($item->{'kind'} eq 'post' ? $post_layout : undef)
l!l&&r!l&&!r
000
MC/DC0%0/2
layout_for($state, $f->{'layout'}) || ($item->{'kind'} eq 'post' ? $post_layout : undef)
layout_for($state, $f->{'layout'}) ($item->{'kind'} eq 'post' ? $post_layout : undef)
Truth table0%0/3
A || B
A= layout_for($state, $f->{'layout'})B= ($item->{'kind'} eq 'post' ? $post_layout : undef)
ABresult
1X1
011
000
665 }
666  
667 0 for my $dir (qw/ assets images img css js files uploads fonts /) {
668 0 ▶ copy_files($state, "$from/$dir", $dir) if -d "$from/$dir";
Branch: 1 branch
BranchTrueFalseTotal
if -d "$from/$dir" 0 0 0
669 }
670 0 ▶ copy_files($state, "$from/favicon.ico", "favicon.ico") if -f "$from/favicon.ico";
Branch: 1 branch
BranchTrueFalseTotal
if -f "$from/favicon.ico" 0 0 0
671 }
672  
673 # jekyll_categories(front matter) - A page's categories as Jekyll reads them:
674 # "categories" is a list (or words separated by spaces), and "category" is one.
675 0 ▶sub jekyll_categories {
Subroutine jekyll_categories: not called CC=5 SCAR=34.0
Pod undocumented
676 0 my ($front) = @_;
677 0 ▶ return list($front->{categories}) if defined $front->{categories};
Branch: 1 branch
BranchTrueFalseTotal
if defined $front->{"categories"} 0 0 0
678 return defined $front->{category} && !ref $front->{category} && length $front->{category}
679 0 ▶ ? [ $front->{category} ] : list($front->{category});
Branch: 1 branch
BranchTrueFalseTotal
defined $front->{'category'} && !ref($front->{'category'}) && length $front->{'category'} ? : 0 0 0
Condition0%0/6
defined $front->{"category"} and not ref $front->{"category"} and length $front->{"category"}
!ll&&!rl&&r
000
defined $front->{"category"} and not ref $front->{"category"}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $front->{"category"} and not ref $front->{"category"} and length $front->{"category"}
defined $front->{"category"} not ref $front->{"category"} length $front->{"category"}
Truth table0%0/4
A and not(B) and C
A= defined $front->{"category"}B= not ref $front->{"category"}C= length $front->{"category"}
ABCresult
0XX0
10X0
1100
1111
680 }
681  
682 # jekyll_url(pattern, item, front matter) - The address Jekyll gives a page.
683 0 ▶sub jekyll_url {
Subroutine jekyll_url: not called CC=10 SCAR=47.0
Pod undocumented
684 0 my ($pattern, $item, $front) = @_;
685  
686 0 ▶ my $date = eval { App::Dapper::Utils::parse_date($item->{date_string}) } if defined $item->{date_string};
Branch: 1 branch
BranchTrueFalseTotal
if defined $item->{"date_string"} 0 0 0
687 0 my @categories = map { App::Dapper::Utils::slugify($_) } @{ jekyll_categories($front) };
688 0 ▶ my $title = defined $item->{slug} ? $item->{slug} : basename($item->{rel} || '');
Branch: 1 branch
BranchTrueFalseTotal
defined $item->{'slug'} ? : 0 0 0
Condition0%0/2
$item->{'rel'} || ''
l!l
00
MC/DC0%0/1
$item->{'rel'} || ''
$item->{'rel'}
Truth table0%0/2
A
A= $item->{'rel'}
Aresult
11
00
689 my %token = (
690 categories => join('/', @categories),
691 title => defined $front->{slug} ? $front->{slug} : $title,
692 slug => defined $front->{slug} ? $front->{slug} : $title,
693 output_ext => '.html',
694 0 ▶ path => $item->{rel} || '',
Branch: 2 branches
BranchTrueFalseTotal
defined $front->{'slug'} ? : 0 0 0
defined $front->{'slug'} ? : 0 0 0
Condition0%0/2
$item->{'rel'} || ''
l!l
00
MC/DC0%0/1
$item->{'rel'} || ''
$item->{'rel'}
Truth table0%0/2
A
A= $item->{'rel'}
Aresult
11
00
695 );
696 0 ▶ if ($date) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date) 0 0 0
697 0 %token = (%token,
698 year => $date->strftime('%Y'), month => $date->strftime('%m'), day => $date->strftime('%d'),
699 i_month => $date->month, i_day => $date->day, short_year => $date->strftime('%y'),
700 y_day => $date->strftime('%j'), hour => $date->strftime('%H'), minute => $date->strftime('%M'),
701 second => $date->strftime('%S'));
702 }
703  
704 0 my $url = $pattern;
705 0 ▶ $url =~ s/:(categories|output_ext|short_year|i_month|i_day|y_day|year|month|day|hour|minute|second|title|slug|path)\b/defined $token{$1} ? $token{$1} : ''/ge;
Branch: 1 branch
BranchTrueFalseTotal
defined $token{$1} ? : 0 0 0
706 0 $url =~ s{/{2,}}{/}g;
707 0 ▶ $url = "/$url" unless $url =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ m[^/] 0 0 0
708 0 return $url;
709 }
710  
711 # jekyll_body(state, text, markdown?, \%post_url, config, file) - Replace the
712 # Liquid Dapper can do without.
713 0 ▶sub jekyll_body {
Subroutine jekyll_body: not called CC=4 SCAR=30.0
Pod undocumented
714 0 my ($state, $body, $markdown, $post_url, $config, $file) = @_;
715  
716 0 $body =~ s/\{%-?\s*raw\s*-?%\}|\{%-?\s*endraw\s*-?%\}//g;
717 0 ▶ if ($markdown) {
Branch: 1 branch
BranchTrueFalseTotal
if ($markdown) { } 0 0 0
718 0 $body =~ s{\{%-?\s*highlight\s+(\w+)[^%]*%\}\n?(.*?)\n?\{%-?\s*endhighlight\s*-?%\}}{```$1\n$2\n```}gs;
719 }
720 else {
721 0 $body =~ s{\{%-?\s*highlight\s+(\w+)[^%]*%\}\n?(.*?)\n?\{%-?\s*endhighlight\s*-?%\}}{
722 0 my ($lang, $code) = ($1, $2);
723 0 $code =~ s/&/&amp;/g; $code =~ s/</&lt;/g; $code =~ s/>/&gt;/g;
724 0 qq{<pre><code class="language-$lang">$code</code></pre>}
725 }gse;
726 }
727 0 $body =~ s/\{\{-?\s*site\.(?:baseurl|url)\s*-?\}\}//g;
728 0 $body =~ s/\{\{-?\s*(["'])(.*?)\1\s*\|\s*(?:relative_url|absolute_url)\s*-?\}\}/$2/g;
729 0 $body =~ s{\{%-?\s*post_url\s+(\S+)\s*-?%\}}{
730 0 my $given = $1;
731 0 (my $key = $given) =~ s{^.*/}{};
732 0 ▶ defined $post_url->{$key} ? $post_url->{$key} : do { note($state, "post_url links to posts that weren't found", $file); "{% post_url $given %}" }
Branch: 1 branch
BranchTrueFalseTotal
defined $post_url->{$key} ? : 0 0 0
733 }ge;
734  
735 0 ▶ note($state, 'Liquid tags Dapper does not run ({{ ... }} or {% ... %}); replace them, or set template: true and use Dapper template code', $file)
Branch: 1 branch
BranchTrueFalseTotal
if $body =~ /\{\{.*?\}\}|\{%.*?%\}/s 0 0 0
736 if $body =~ /\{\{.*?\}\}|\{%.*?%\}/s;
737 0 return $body;
738 }
739  
740 # --- Hugo ------------------------------------------------------------------
741  
742 0 ▶sub hugo {
Subroutine hugo: not called CC=38 SCAR=73.0
Pod documented
743 0 my ($state, $from) = @_;
744  
745 0 ▶ die "error: give the Hugo site's directory\n" unless -d $from;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $from 0 0 0
746 0 my $content = "$from/content";
747 0 ▶ die "error: $from has no content directory, so it doesn't look like a Hugo site\n" unless -d $content;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $content 0 0 0
748  
749 0 my $config = {};
750 0 for my $name (qw/ hugo.toml hugo.yaml hugo.yml hugo.json config.toml config.yaml config.yml config.json /) {
751 0 my $file = "$from/$name";
752 0 ▶ next unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
753 0 my $text = App::Dapper::Utils::read_file($file);
754 0 ▶ $config = $name =~ /\.toml$/ ? toml($text) : $name =~ /\.json$/ ? JSON->new->decode($text) : YAML::PP::Load($text);
Branch: 2 branches
BranchTrueFalseTotal
$name =~ /\.toml$/ ? : 0 0 0
$name =~ /\.json$/ ? : 0 0 0
755 0 last;
756 }
757 0 ▶ my $permalinks = ref $config->{permalinks} eq 'HASH' ? $config->{permalinks} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $config->{'permalinks'} eq 'HASH' ? : 0 0 0
758  
759 0 my $post_layout = layout_for($state, 'post');
760 0 my @files;
761 0 ▶ File::Find::find({ no_chdir => 1, wanted => sub { push @files, $_ if -f $_ and /\.(?:md|markdown|html)$/i } }, $content);
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if -f $_ and /\.(?:md|markdown|html)$/i 0 0 0
Condition0%0/3
-f $_ and /\.(?:md|markdown|html)$/i
!ll&&!rl&&r
000
MC/DC0%0/2
-f $_ and /\.(?:md|markdown|html)$/i
-f $_ /\.(?:md|markdown|html)$/i
Truth table0%0/3
A and B
A= -f $_B= /\.(?:md|markdown|html)$/i
ABresult
0X0
100
111
762  
763 0 for my $file (sort @files) {
764 0 my $rel = substr($file, length($content) + 1);
765 0 my $name = basename($rel);
766  
767 # In a page bundle (a folder with an index.md), other Markdown files
768 # are the page's resources, not pages
769 0 ▶ if ($name !~ /^_?index\./ and grep { -f catfile(dirname($file), "index.$_") } qw/ md markdown html /) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $name =~ /^_?index\./ and grep {-f catfile(dirname($file), "index.$_");} "md", "markdown", "html") 0 0 0
Condition0%0/3
not $name =~ /^_?index\./ and grep {-f catfile(dirname($file), "index.$_");} "md", "markdown", "html"
!ll&&!rl&&r
000
MC/DC0%0/2
not $name =~ /^_?index\./ and grep {-f catfile(dirname($file), "index.$_");} "md", "markdown", "html"
not $name =~ /^_?index\./ grep {-f catfile(dirname($file), "index.$_");} "md", "markdown", "html"
Truth table0%0/3
not(A) and B
A= not $name =~ /^_?index\./B= grep {-f catfile(dirname($file), "index.$_");} "md", "markdown", "html"
ABresult
0X0
100
111
770 0 note($state, 'Markdown files inside page bundles were left out (Hugo treats them as resources, not pages)', $file);
771 0 next;
772 }
773 0 ▶ if ($name =~ /^_index\./) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^_index\./) 0 0 0
774 0 note($state, 'Hugo section pages (_index.md) were not imported; Dapper lists pages with paginate', $file);
775 0 next;
776 }
777  
778 0 my ($front, $body) = hugo_front_matter(App::Dapper::Utils::read_file($file), $file);
779 0 my $bundle = $name =~ /^index\./;
780 0 (my $stem = $rel) =~ s/\.\w+$//;
781 0 ▶ $stem =~ s{/index$}{} if $bundle;
Branch: 1 branch
BranchTrueFalseTotal
if $bundle 0 0 0
782 0 my ($ext) = $name =~ /\.(\w+)$/;
783 0 ▶ $ext = lc $ext eq 'html' ? 'html' : 'md';
Branch: 1 branch
BranchTrueFalseTotal
lc $ext eq 'html' ? : 0 0 0
784  
785 0 my @dirs = split m{/}, $stem;
786 0 my $leaf = pop @dirs;
787 0 ▶ my $section = @dirs ? $dirs[0] : '';
Branch: 1 branch
BranchTrueFalseTotal
@dirs ? : 0 0 0
788 0 ▶ my $slug = defined $front->{slug} ? $front->{slug} : $leaf;
Branch: 1 branch
BranchTrueFalseTotal
defined $front->{'slug'} ? : 0 0 0
789 0 ▶ my $url = defined $front->{url} ? $front->{url} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $front->{'url'} ? : 0 0 0
790 0 ▶ if (not defined $url and defined $permalinks->{$section}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $url and defined $permalinks->{$section}) 0 0 0
Condition0%0/3
not defined $url and defined $permalinks->{$section}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $url and defined $permalinks->{$section}
not defined $url defined $permalinks->{$section}
Truth table0%0/3
not(A) and B
A= not defined $urlB= defined $permalinks->{$section}
ABresult
0X0
100
111
791 0 $url = hugo_url($permalinks->{$section}, $front, $section, $slug, $leaf);
792 }
793 0 ▶ $url = '/' . join('/', @dirs, $slug) . '/' unless defined $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url 0 0 0
794  
795 0 for my $alias (@{ list($front->{aliases}) }) {
796 0 ▶ $state->{redirects}->{ $alias =~ m{^/} ? $alias : "/$alias" } = $url;
Branch: 1 branch
BranchTrueFalseTotal
$alias =~ m[^/] ? : 0 0 0
797 }
798  
799 0 ▶ my $path = $bundle ? "$stem/index.$ext" : "$stem.$ext";
Branch: 1 branch
BranchTrueFalseTotal
$bundle ? : 0 0 0
800 0 my %done = map { $_ => 1 } qw/ title date publishDate publishdate lastmod layout type categories tags draft url slug aliases description summary weight /;
801 0 my @rest = map { [ $_ => $front->{$_} ] } grep { !$done{$_} } sort keys %$front;
802 0 ▶ my $date = defined $front->{date} ? $front->{date} : $front->{publishDate} || $front->{publishdate};
Branch: 1 branch
BranchTrueFalseTotal
defined $front->{'date'} ? : 0 0 0
Condition0%0/3
$front->{"publishDate"} or $front->{"publishdate"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$front->{"publishDate"} or $front->{"publishdate"}
$front->{"publishDate"} $front->{"publishdate"}
Truth table0%0/3
A or B
A= $front->{"publishDate"}B= $front->{"publishdate"}
ABresult
1X1
011
000
803  
804 add_page($state, $path, [
805 [ title => $front->{title} ],
806 [ date => date_string($date) ],
807 0 ▶ [ layout => layout_for($state, $front->{layout}) || (@{ list($front->{categories}) } || $section =~ /^(?:posts?|blog)$/ ? $post_layout : undef) ],
Branch: 3 branches
BranchTrueFalseTotal
defined $front->{'description'} ? : 0 0 0
$front->{'draft'} && !($front->{'draft'} =~ /^(?:false|0)$/i) ? : 0 0 0
$section =~ /^(?:posts?|blog)$/ ? : 0 0 0
Condition0%0/3
$front->{"draft"} and not $front->{"draft"} =~ /^(?:false|0)$/i
!ll&&!rl&&r
000
MC/DC0%0/2
$front->{"draft"} and not $front->{"draft"} =~ /^(?:false|0)$/i
$front->{"draft"} not $front->{"draft"} =~ /^(?:false|0)$/i
Truth table0%0/3
A and not(B)
A= $front->{"draft"}B= not $front->{"draft"} =~ /^(?:false|0)$/i
ABresult
0X0
100
111
808 [ categories => list($front->{categories}) ],
809 [ tags => list($front->{tags}) ],
810 [ description => defined $front->{description} ? $front->{description} : $front->{summary} ],
811 [ urlpattern => $url ],
812 @rest,
813 0 ▶ [ draft => $front->{draft} && $front->{draft} !~ /^(?:false|0)$/i ? 1 : 0 ],
Branch: 1 branch
BranchTrueFalseTotal
@{list($front->{'categories'});} || $section =~ /^(?:posts?|blog)$/ ? : 0 0 0
Condition0%0/6
layout_for($state, $front->{'layout'}) || (@{list($front->{'categories'});} || $section =~ /^(?:posts?|blog)$/ ? $post_layout : undef)
l!l&&r!l&&!r
000
@{list($front->{"categories"});} or $section =~ /^(?:posts?|blog)$/
l!l&&r!l&&!r
000
MC/DC0%0/3
layout_for($state, $front->{'layout'}) || (@{list($front->{'categories'});} || $section =~ /^(?:posts?|blog)$/ ? $post_layout : undef)
layout_for($state, $front->{'layout'}) @{list($front->{"categories"});} $section =~ /^(?:posts?|blog)$/
Truth table0%0/4
A || B or C
A= layout_for($state, $front->{'layout'})B= @{list($front->{"categories"});}C= $section =~ /^(?:posts?|blog)$/
ABCresult
1XX1
01X1
0011
0000
814 ], hugo_body($state, $body, "$state->{source}/$path"), $section =~ /^(?:posts?|blog)$/ ? 'post' : 'page');
815  
816 # The other files in a page bundle go with it
817 0 ▶ if ($bundle) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bundle) 0 0 0
818 0 my $dir = dirname($file);
819 0 for my $entry (App::Dapper::Utils::read_dir($dir)) {
820 0 ▶ next if $entry =~ /^\./ or $entry =~ /\.(?:md|markdown)$/i or -d "$dir/$entry";
Branch: 1 branch
BranchTrueFalseTotal
if $entry =~ /^\./ or $entry =~ /\.(?:md|markdown)$/i or -d "$dir/$entry" 0 0 0
Condition0%0/6
$entry =~ /^\./ or $entry =~ /\.(?:md|markdown)$/i or -d "$dir/$entry"
l!l&&r!l&&!r
000
$entry =~ /^\./ or $entry =~ /\.(?:md|markdown)$/i
l!l&&r!l&&!r
000
MC/DC0%0/3
$entry =~ /^\./ or $entry =~ /\.(?:md|markdown)$/i or -d "$dir/$entry"
$entry =~ /^\./ $entry =~ /\.(?:md|markdown)$/i -d "$dir/$entry"
Truth table0%0/4
A or B or C
A= $entry =~ /^\./B= $entry =~ /\.(?:md|markdown)$/iC= -d "$dir/$entry"
ABCresult
1XX1
01X1
0011
0000
821 0 copy_files($state, "$dir/$entry", "$state->{source}/$stem/$entry");
822 }
823 }
824 }
825  
826 0 ▶ copy_files($state, "$from/static", $state->{source}) if -d "$from/static";
Branch: 1 branch
BranchTrueFalseTotal
if -d "$from/static" 0 0 0
827 }
828  
829 0 ▶sub hugo_url {
Subroutine hugo_url: not called CC=7 SCAR=40.3
Pod undocumented
830 0 my ($pattern, $front, $section, $slug, $leaf) = @_;
831  
832 0 ▶ my $date = defined $front->{date} ? eval { App::Dapper::Utils::parse_date($front->{date}) } : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $front->{'date'} ? : 0 0 0
833 0 my $title = App::Dapper::Utils::slugify($front->{title});
834 0 ▶ my %token = (
Branch: 1 branch
BranchTrueFalseTotal
defined $title && length $title ? : 0 0 0
Condition0%0/3
defined $title and length $title
!ll&&!rl&&r
000
MC/DC0%0/2
defined $title and length $title
defined $title length $title
Truth table0%0/3
A and B
A= defined $titleB= length $title
ABresult
0X0
100
111
835 section => $section,
836 slug => $slug,
837 title => defined $title && length $title ? $title : $slug,
838 filename => $leaf,
839 contentbasename => $leaf,
840 );
841 0 ▶ %token = (%token, year => $date->strftime('%Y'), month => $date->strftime('%m'), day => $date->strftime('%d'),
Branch: 1 branch
BranchTrueFalseTotal
if $date 0 0 0
842 yearday => $date->strftime('%j'), monthname => lc $date->strftime('%B')) if $date;
843  
844 0 my $url = $pattern;
845 0 ▶ $url =~ s/:(contentbasename|monthname|yearday|section|filename|title|slug|year|month|day)\b/defined $token{$1} ? $token{$1} : ''/ge;
Branch: 1 branch
BranchTrueFalseTotal
defined $token{$1} ? : 0 0 0
846 0 $url =~ s{/{2,}}{/}g;
847 0 ▶ $url = "/$url" unless $url =~ m{^/};
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ m[^/] 0 0 0
848 0 return $url;
849 }
850  
851 # hugo_front_matter(text, file) - The front matter (YAML, TOML or JSON) and
852 # the body of a Hugo page.
853 0 ▶sub hugo_front_matter {
Subroutine hugo_front_matter: not called CC=8 SCAR=42.8
Pod undocumented
854 0 my ($text, $file) = @_;
855  
856 0 ▶ if ($text =~ /\A\+\+\+[ \t]*\r?\n(.*?)^\+\+\+[ \t]*\r?\n?(.*)\z/ms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\A\+\+\+[ \t]*\r?\n(.*?)^\+\+\+[ \t]*\r?\n?(.*)\z/ms) 0 0 0
857 0 return (toml($1), $2);
858 }
859 0 ▶ if ($text =~ /\A---[ \t]*\r?\n(.*?)^---[ \t]*\r?\n?(.*)\z/ms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\A---[ \t]*\r?\n(.*?)^---[ \t]*\r?\n?(.*)\z/ms) 0 0 0
860 0 my ($yaml, $body) = ($1, $2);
861 0 my $front = eval { YAML::PP::Load($yaml) };
862 0 ▶ die "error: could not read the front matter of $file: $@" unless ref $front eq 'HASH' or !length $yaml;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $front eq "HASH" or not length $yaml 0 0 0
Condition0%0/3
ref $front eq "HASH" or not length $yaml
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $front eq "HASH" or not length $yaml
ref $front eq "HASH" not length $yaml
Truth table0%0/3
A or not(B)
A= ref $front eq "HASH"B= not length $yaml
ABresult
1X1
011
000
863 0 ▶ return ($front || {}, $body);
Condition0%0/2
$front || {}
l!l
00
MC/DC0%0/1
$front || {}
$front
Truth table0%0/2
A
A= $front
Aresult
11
00
864 }
865 0 ▶ if ($text =~ /\A\s*\{/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\A\s*\{/) 0 0 0
866 0 my $json = JSON->new;
867 0 my ($front, $length) = eval { $json->decode_prefix($text) };
868 0 ▶ return ($front, substr($text, $length)) if ref $front eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $front eq "HASH" 0 0 0
869 }
870 0 return ({}, $text);
871 }
872  
873 # hugo_body(state, text, file) - Replace the shortcodes Dapper has components for.
874 0 ▶sub hugo_body {
Subroutine hugo_body: not called CC=11 SCAR=48.8
Pod undocumented
875 0 my ($state, $body, $file) = @_;
876  
877 0 ▶ my $args = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
878 0 my ($text) = @_;
879 0 my %named;
880 0 ▶ while ($text =~ /(\w+)=(?:"((?:[^"\\]|\\.)*)"|(\S+))/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /(\w+)=(?:"((?:[^"\\]|\\.)*)"|(\S+))/g) 0 0 0
881 0 ▶ my ($key, $value) = ($1, defined $2 ? $2 : $3);
Branch: 1 branch
BranchTrueFalseTotal
defined $2 ? : 0 0 0
882 0 $value =~ s/\\(["\\])/$1/g;
883 0 $named{$key} = $value;
884 }
885 0 (my $positional = $text) =~ s/\w+=(?:"(?:[^"\\]|\\.)*"|\S+)//g;
886 0 my @list = App::Dapper::Components::parse_args($positional);
887 0 return (\%named, \@list);
888 0 };
889 0 ▶ my $quote = sub { my $t = shift; $t =~ s/"/'/g; qq{"$t"} };
Subroutine __ANON__: not called CC=1 SCAR=6.9
890  
891 0 $body =~ s{\{\{[<%]\s*(youtube|vimeo)\s+(.*?)\s*[>%]\}\}}{
892 0 my ($whole, $kind, $text) = ($&, $1, $2);
893 0 my ($named, $list) = $args->($text);
894 0 ▶ my $id = $named->{id} || $list->[0];
Condition0%0/3
$named->{'id'} || $list->[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$named->{'id'} || $list->[0]
$named->{'id'} $list->[0]
Truth table0%0/3
A || B
A= $named->{'id'}B= $list->[0]
ABresult
1X1
011
000
895 0 ▶ defined $id ? "::: $kind $id" . (defined $named->{title} ? ' ' . $quote->($named->{title}) : '') : $whole
Branch: 2 branches
BranchTrueFalseTotal
defined $id ? : 0 0 0
defined $named->{'title'} ? : 0 0 0
896 }ge;
897 0 $body =~ s{\{\{[<%]\s*figure\s+(.*?)\s*/?[>%]\}\}}{
898 0 my ($whole, $text) = ($&, $1);
899 0 my ($named) = $args->($text);
900 0 my $src = $named->{src};
901 0 ▶ my $alt = defined $named->{alt} ? $named->{alt} : defined $named->{title} ? $named->{title} : '';
Branch: 2 branches
BranchTrueFalseTotal
defined $named->{'alt'} ? : 0 0 0
defined $named->{'title'} ? : 0 0 0
902 defined $src
903 0 ▶ ? "::: figure $src " . $quote->($alt) . (defined $named->{caption} ? ' ' . $quote->($named->{caption}) : '')
Branch: 2 branches
BranchTrueFalseTotal
defined $src ? : 0 0 0
defined $named->{'caption'} ? : 0 0 0
904 : $whole
905 }ge;
906 0 $body =~ s{\{\{[<%]\s*(?:rel)?ref\s+"([^"]+)"\s*[>%]\}\}}{
907 0 my $target = $1;
908 0 $target =~ s{\.md$}{};
909 0 $target =~ s{(?:^|/)index$}{};
910 0 $target =~ s{^/+}{};
911 0 ▶ length $target ? "/$target/" : '/'
Branch: 1 branch
BranchTrueFalseTotal
length $target ? : 0 0 0
912 }ge;
913  
914 0 my %shortcodes;
915 0 ▶ $shortcodes{$1}++ while $body =~ /\{\{[<%]\s*\/?(\w[\w-]*)/g;
Branch: 1 branch
BranchTrueFalseTotal
$body =~ m[\{\{[<%]\s*/?(\w[\w-]*)]g and do { ++$shortcodes{$1} } 0 0 0
916 0 ▶ note($state, "Hugo shortcodes that were left as they are: " . join(', ', sort keys %shortcodes), $file) if %shortcodes;
Branch: 1 branch
BranchTrueFalseTotal
if %shortcodes 0 0 0
917  
918 0 return $body;
919 }
920  
921 =head2 toml
922  
923 Takes TOML text and returns it as a hash reference. It reads what front
924 matter and Hugo's settings use: keys (including dotted keys) and values
925 (text with escapes, multi-line text, numbers, true and false, dates, and
926 lists, including lists of lists), tables (C<[params]>) and comments. Numbers
927 and dates are kept as they are written. Inline tables are kept as text, and
928 an array of tables (C<[[menu]]>) is read as one table.
929  
930 =cut
931  
932 60 ▶sub toml {
Subroutine toml: called CC=19 SCAR=29.5
Pod documented
933 60 my ($text) = @_;
934  
935 60 my %data;
936 60 my $table = \%data;
937 60 my @lines = split /\r?\n/, $text;
938  
939 60 ▶ while (defined(my $line = shift @lines)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = shift @lines)) 101 60 161
940 101 my $plain = _toml_strip($line);
941 101 ▶ next if $plain eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $plain eq "" 3 98 101
942  
943 98 ▶ if ($plain =~ /^\[\[?\s*([^\]]+?)\s*\]\]?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plain =~ /^\[\[?\s*([^\]]+?)\s*\]\]?$/) 14 84 98
944 14 $table = \%data;
945 14 for my $part (split /\./, $1) {
946 22 $part =~ s/^\s*["']|["']\s*$//g;
947 22 $part =~ s/^\s+|\s+$//g;
948 22 ▶ $table = $table->{$part} = ref $table->{$part} eq 'HASH' ? $table->{$part} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $table->{$part} eq 'HASH' ? : 5 17 22
949 }
950 14 next;
951 }
952  
953 84 ▶ my ($key, $value) = $plain =~ /^((?:"[^"]*"|'[^']*'|[\w-]+)(?:\s*\.\s*(?:"[^"]*"|'[^']*'|[\w-]+))*)\s*=\s*(.*)$/
Branch: 1 branch
BranchTrueFalseTotal
() = $plain =~ /^((?:"[^"]*"|'[^']*'|[\w-]+)(?:\s*\.\s*(?:"[^"]*"|'[^']*'|[\w-]+))*)\s*=\s*(.*)$/ or next 82 2 84
954 or next;
955  
956 # Text over several lines, between """ or '''
957 82 ▶ if ($value =~ /^("""|''')/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^("""|''')/) { } 1 81 82
958 1 my $quote = $1;
959 1 my ($raw) = $line =~ /=\s*\Q$quote\E(.*)$/;
960 1 ▶ $raw = '' unless defined $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw 0 1 1
961 1 ▶ until ($raw =~ /\Q$quote\E/ or not @lines) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($raw =~ /\Q$quote\E/ or not @lines) 1 1 2
Condition66%2/3
$raw =~ /\Q$quote\E/ or not @lines
l!l&&r!l&&!r
101
MC/DC50%1/2
$raw =~ /\Q$quote\E/ or not @lines
$raw =~ /\Q$quote\E/ not @lines
Truth table66%2/3
A or not(B)
A= $raw =~ /\Q$quote\E/B= not @lines
ABresult
1X1
011
000
962 1 $raw .= "\n" . shift @lines;
963 }
964 1 $raw =~ s/\Q$quote\E.*\z//s;
965 1 $raw =~ s/^\n//;
966 1 ▶ $value = $quote eq '"""' ? _toml_unescape($raw) : $raw;
Branch: 1 branch
BranchTrueFalseTotal
$quote eq '"""' ? : 1 0 1
967 }
968 else {
969 # A list can go over several lines
970 81 ▶ while ($value =~ /^\[/ and _brackets($value) > 0 and @lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^\[/ and _brackets($value) > 0 and @lines) 14 81 95
Condition100%6/6
$value =~ /^\[/ and _brackets($value) > 0 and @lines
!ll&&!rl&&r
80114
$value =~ /^\[/ and _brackets($value) > 0
!ll&&!rl&&r
631715
MC/DC100%3/3
$value =~ /^\[/ and _brackets($value) > 0 and @lines
$value =~ /^\[/ _brackets($value) > 0 @lines
Truth table100%4/4
A and B and C
A= $value =~ /^\[/B= _brackets($value) > 0C= @lines
ABCresult
0XX0
10X0
1100
1111
971 14 my $next = _toml_strip(shift @lines);
972 14 ▶ $value .= " $next" if length $next;
Branch: 1 branch
BranchTrueFalseTotal
if length $next 14 0 14
973 }
974 81 $value = _toml_value($value);
975 }
976  
977 # A dotted key (a.b = 1) puts the value in a table
978 82 my @parts = $key =~ /("[^"]*"|'[^']*'|[\w-]+)/g;
979 82 s/^["']|["']$//g for @parts;
980 82 my $last = pop @parts;
981 82 my $target = $table;
982 82 ▶ $target = $target->{$_} = ref $target->{$_} eq 'HASH' ? $target->{$_} : {} for @parts;
Branch: 1 branch
BranchTrueFalseTotal
ref $target->{$_} eq 'HASH' ? : 0 1 1
983 82 $target->{$last} = $value;
984 }
985  
986 60 return \%data;
987 }
988  
989 # _toml_strip(line) - The line without its comment and the spaces around it.
990 115 ▶sub _toml_strip {
Subroutine _toml_strip: called CC=9 SCAR=22.0
991 115 my ($line) = @_;
992  
993 115 my ($out, $quote) = ('', '');
994 115 ▶ for (my $i = 0; $i < length $line; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $line) 1157 105 1262
995 1157 my $c = substr($line, $i, 1);
996 1157 ▶ if ($quote) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quote) 260 897 1157
997 260 $out .= $c;
998 260 ▶ if ($quote eq '"' and $c eq '\\') { $out .= substr($line, ++$i, 1); next }
Branch: 1 branch
BranchTrueFalseTotal
if ($quote eq "\"" and $c eq "\\") 8 252 260
Condition100%3/3
$quote eq "\"" and $c eq "\\"
!ll&&!rl&&r
342188
MC/DC100%2/2
$quote eq "\"" and $c eq "\\"
$quote eq "\"" $c eq "\\"
Truth table100%3/3
A and B
A= $quote eq "\""B= $c eq "\\"
ABresult
0X0
100
111
999 252 ▶ $quote = '' if $c eq $quote;
Branch: 1 branch
BranchTrueFalseTotal
if $c eq $quote 48 204 252
1000 252 next;
1001 }
1002 897 ▶ last if $c eq '#';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "#" 10 887 897
1003 887 ▶ $quote = $c if $c eq '"' or $c eq "'";
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "\"" or $c eq "'" 50 837 887
Condition100%3/3
$c eq "\"" or $c eq "'"
l!l&&r!l&&!r
428837
MC/DC100%2/2
$c eq "\"" or $c eq "'"
$c eq "\"" $c eq "'"
Truth table100%3/3
A or B
A= $c eq "\""B= $c eq "'"
ABresult
1X1
011
000
1004 887 $out .= $c;
1005 }
1006 115 $out =~ s/^\s+|\s+$//g;
1007 115 return $out;
1008 }
1009  
1010 32 ▶sub _brackets {
Subroutine _brackets: called CC=1 SCAR=0.0
1011 32 my ($text) = @_;
1012 32 (my $plain = $text) =~ s/"(?:[^"\\]|\\.)*"|'[^']*'//g;
1013 32 return ($plain =~ tr/[//) - ($plain =~ tr/]//);
1014 }
1015  
1016 38 ▶sub _toml_unescape {
Subroutine _toml_unescape: called CC=4 SCAR=14.1
1017 38 my ($s) = @_;
1018 38 my %simple = (n => "\n", t => "\t", r => "\r", b => "\b", f => "\f", '"' => '"', '\\' => '\\');
1019 38 $s =~ s/\\(?:u([0-9A-Fa-f]{4})|U([0-9A-Fa-f]{8})|(.))/
1020 8 ▶ defined $1 ? chr(hex $1) : defined $2 ? chr(hex $2) : exists $simple{$3} ? $simple{$3} : "\\$3"/gse;
Branch: 3 branches
BranchTrueFalseTotal
defined $1 ? : 1 7 8
defined $2 ? : 0 7 7
exists $simple{$3} ? : 7 0 7
1021 38 return $s;
1022 }
1023  
1024 # _toml_value(text) - A value; text that can't be read is kept as it is.
1025 81 ▶sub _toml_value {
Subroutine _toml_value: called CC=3 SCAR=11.0
1026 81 my ($text) = @_;
1027  
1028 81 my $pos = 0;
1029 81 my $value = _toml_parse($text, \$pos);
1030 81 ▶ return $text unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 79 81
1031 79 ▶ return $text if substr($text, $pos) =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if substr($text, $pos) =~ /\S/ 1 78 79
1032 78 return $$value;
1033 }
1034  
1035 # _toml_parse(text, \pos) - Reads one value from the text at pos, and returns
1036 # a reference to it, or nothing if it can't.
1037 116 ▶sub _toml_parse {
Subroutine _toml_parse: called CC=12 SCAR=24.9
1038 116 my ($text, $pos) = @_;
1039  
1040 116 pos($text) = $$pos;
1041 116 $text =~ /\G\s*/gc;
1042  
1043 116 ▶ if ($text =~ /\G"((?:[^"\\]|\\.)*)"/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\G"((?:[^"\\]|\\.)*)"/cg) 37 79 116
1044 37 $$pos = pos($text);
1045 37 my $s = _toml_unescape($1);
1046 37 return \$s;
1047 }
1048 79 ▶ if ($text =~ /\G'([^']*)'/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\G'([^']*)'/cg) 6 73 79
1049 6 $$pos = pos($text);
1050 6 my $s = $1;
1051 6 return \$s;
1052 }
1053 73 ▶ if ($text =~ /\G\[/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\G\[/cg) 20 53 73
1054 20 my @items;
1055 20 my $p = pos($text);
1056 20 while (1) {
1057 40 pos($text) = $p;
1058 40 ▶ if ($text =~ /\G\s*\]/gc) { $$pos = pos($text); return \\@items }
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\G\s*\]/cg) 5 35 40
1059 35 ▶ my $item = _toml_parse($text, \$p) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $item = _toml_parse($text, \$p) or return 34 1 35
1060 34 push @items, $$item;
1061 34 pos($text) = $p;
1062 34 ▶ if ($text =~ /\G\s*,/gc) { $p = pos($text); next }
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\G\s*,/cg) 20 14 34
1063 14 ▶ if ($text =~ /\G\s*\]/gc) { $$pos = pos($text); return \\@items }
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\G\s*\]/cg) 14 0 14
1064 0 return;
1065 }
1066 }
1067 53 ▶ return if $text =~ /\G["']/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\G["']/cg 1 52 53
1068 52 ▶ if ($text =~ /\G([^,\]]+)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\G([^,\]]+)/cg) 51 1 52
1069 51 my $bare = $1;
1070 51 $bare =~ s/\s+$//;
1071 51 $$pos = pos($text);
1072 51 ▶ my $v = $bare eq 'true' ? 1 : $bare eq 'false' ? 0 : $bare;
Branch: 2 branches
BranchTrueFalseTotal
$bare eq 'true' ? : 2 49 51
$bare eq 'false' ? : 3 46 49
1073 51 return \$v;
1074 }
1075 1 return;
1076 }
1077  
1078 # --- Medium ----------------------------------------------------------------
1079  
1080 0 ▶sub medium {
Subroutine medium: not called CC=16 SCAR=56.1
Pod documented
1081 0 my ($state, $from) = @_;
1082  
1083 0 my $dir = $from;
1084 0 ▶ if (-f $from) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $from) 0 0 0
1085 0 ▶ die "error: give Medium's export (the .zip file, or the folder it unzips to)\n" unless $from =~ /\.zip$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $from =~ /\.zip$/i 0 0 0
1086 0 $dir = tempdir(CLEANUP => 1);
1087 0 ▶ system('unzip', '-q', '-o', $from, '-d', $dir) == 0
Branch: 1 branch
BranchTrueFalseTotal
system("unzip", "-q", "-o", $from, "-d", $dir) == 0 or die "error: could not unzip $from (is unzip installed?)\n" 0 0 0
1088 or die "error: could not unzip $from (is unzip installed?)\n";
1089 }
1090  
1091 0 my @files;
1092 0 ▶ File::Find::find({ no_chdir => 1, wanted => sub { push @files, $_ if -f $_ and m{(?:^|/)posts/[^/]+\.html$} } }, $dir);
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if -f $_ and m[(?:^|/)posts/[^/]+\.html$] 0 0 0
Condition0%0/3
-f $_ and m[(?:^|/)posts/[^/]+\.html$]
!ll&&!rl&&r
000
MC/DC0%0/2
-f $_ and m[(?:^|/)posts/[^/]+\.html$]
-f $_ m[(?:^|/)posts/[^/]+\.html$]
Truth table0%0/3
A and B
A= -f $_B= m[(?:^|/)posts/[^/]+\.html$]
ABresult
0X0
100
111
1093 0 ▶ die "error: there are no stories in $from (Medium's export has them in a posts folder)\n" unless @files;
Branch: 1 branch
BranchTrueFalseTotal
unless @files 0 0 0
1094  
1095 0 my $layout = layout_for($state, 'post');
1096 0 for my $file (sort @files) {
1097 0 my $html = App::Dapper::Utils::read_file($file);
1098  
1099 0 my ($title) = $html =~ m{<h1 class="p-name">(.*?)</h1>}s;
1100 0 ▶ ($title) = $html =~ m{<title>(.*?)</title>}s unless defined $title;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $title 0 0 0
1101 0 $title = strip_tags($title);
1102 0 my ($date) = $html =~ m{<time class="dt-published" datetime="([^"]+)"};
1103 0 my ($canonical) = $html =~ m{<a href="([^"]+)" class="p-canonical"};
1104 0 my ($subtitle) = $html =~ m{<section data-field="subtitle" class="p-summary">(.*?)</section>}s;
1105 0 my ($body) = $html =~ m{<section data-field="body" class="e-content">(.*?)</section>\s*<footer>}s;
1106 0 ▶ $body = '' unless defined $body;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 0 0 0
1107  
1108 0 my $slug;
1109 0 ▶ if (defined $canonical and $canonical =~ m{/([^/?#]+?)(?:-[0-9a-f]{8,12})?(?:[?#].*)?$}) { $slug = $1 }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $canonical and $canonical =~ m[/([^/?#]+?)(?:-[0-9a-f]{8,12})?(?:[?#].*)?$]) 0 0 0
Condition0%0/3
defined $canonical and $canonical =~ m[/([^/?#]+?)(?:-[0-9a-f]{8,12})?(?:[?#].*)?$]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $canonical and $canonical =~ m[/([^/?#]+?)(?:-[0-9a-f]{8,12})?(?:[?#].*)?$]
defined $canonical $canonical =~ m[/([^/?#]+?)(?:-[0-9a-f]{8,12})?(?:[?#].*)?$]
Truth table0%0/3
A and B
A= defined $canonicalB= $canonical =~ m[/([^/?#]+?)(?:-[0-9a-f]{8,12})?(?:[?#].*)?$]
ABresult
0X0
100
111
1110 0 ▶ $slug = App::Dapper::Utils::slugify($title) unless defined $slug and length $slug;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $slug and length $slug 0 0 0
Condition0%0/3
defined $slug and length $slug
!ll&&!rl&&r
000
MC/DC0%0/2
defined $slug and length $slug
defined $slug length $slug
Truth table0%0/3
A and B
A= defined $slugB= length $slug
ABresult
0X0
100
111
1111 0 ▶ $slug = App::Dapper::Utils::slugify(basename($file, '.html')) unless length $slug;
Branch: 1 branch
BranchTrueFalseTotal
unless length $slug 0 0 0
1112 0 $slug =~ s/^draft_//;
1113 0 ▶ $slug = 'untitled' unless length $slug;
Branch: 1 branch
BranchTrueFalseTotal
unless length $slug 0 0 0
1114  
1115 0 ▶ add_page($state, "$state->{to}/$slug.html", [
Branch: 1 branch
BranchTrueFalseTotal
basename($file) =~ /^draft_/ || !defined($date) ? : 0 0 0
Condition0%0/3
basename($file) =~ /^draft_/ or not defined $date
l!l&&r!l&&!r
000
MC/DC0%0/2
basename($file) =~ /^draft_/ or not defined $date
basename($file) =~ /^draft_/ not defined $date
Truth table0%0/3
A or not(B)
A= basename($file) =~ /^draft_/B= not defined $date
ABresult
1X1
011
000
1116 [ title => $title ],
1117 [ date => date_string($date) ],
1118 [ layout => $layout ],
1119 [ description => strip_tags($subtitle) ],
1120 [ draft => (basename($file) =~ /^draft_/ or not defined $date) ? 1 : 0 ],
1121 ], medium_html($body, $title), 'post');
1122 }
1123 }
1124  
1125 # medium_html(html, title) - A Medium story's body without Medium's markup.
1126 0 ▶sub medium_html {
Subroutine medium_html: not called CC=3 SCAR=24.8
Pod undocumented
1127 0 my ($html, $title) = @_;
1128  
1129 # The title is repeated as the first heading
1130 0 ▶ $html =~ s{<h3[^>]*>\s*\Q$title\E\s*</h3>}{}s if defined $title and length $title;
Branch: 1 branch
BranchTrueFalseTotal
if defined $title and length $title 0 0 0
Condition0%0/3
defined $title and length $title
!ll&&!rl&&r
000
MC/DC0%0/2
defined $title and length $title
defined $title length $title
Truth table0%0/3
A and B
A= defined $titleB= length $title
ABresult
0X0
100
111
1131 0 $html =~ s{<div class="section-divider"><hr class="section-divider"></div>}{<hr>}g;
1132 0 $html =~ s{</?(?:section|div)\b[^>]*>}{}g;
1133 0 $html =~ s{\s+(?:class|name|id|data-[\w-]+)="[^"]*"}{}g;
1134 0 $html =~ s{(</(?:p|h[1-6]|figure|blockquote|pre|ul|ol|li)>|<hr>)}{$1\n}g;
1135 0 $html =~ s/^\s+|\s+$//g;
1136 0 return "$html\n";
1137 }
1138  
1139 191 1;
« lib/App/Dapper/Check.pm ↑ Summary lib/App/Dapper/Agents.pm »