DocKnot/Spin.pm

filter: Statementstmt 49.3% 144 / 292 Branchbran 35.7% 40 / 112 Conditioncond 20.0% 8 / 40 MC/DCmcdc 8.0% 2 / 25 Subroutinesub 66.6% 22 / 33 Podpod 100% 2 / 2 totaltotal 43.2% 218 / 504 CC 73 SCAR 40.9
CC
73
Cov
43%
_write_converter_output
210.0
_footer
182.0
_pod2html
42.0
CRAP 59.7
↑ Summary DocKnot/Spin/Text.pm »
1 # Static site builder supporting thread macro language.
2 #
3 # This module translates a tree of files, possibly written in thread (a custom
4 # macro language) into an HTML static site. It also handles formatting some
5 # other input types (text and POD, for example), copying other types of files
6 # to the output tree, and creating site navigation links.
7 #
8 # SPDX-License-Identifier: MIT
9  
10 ##############################################################################
11 # Modules and declarations
12 ##############################################################################
13  
14 package App::DocKnot::Spin v8.0.1;
15  
16 2 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use warnings FATAL => 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 ▶use App::DocKnot::Spin::Pointer;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use App::DocKnot::Spin::RSS;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use App::DocKnot::Spin::Sitemap;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use App::DocKnot::Spin::Thread;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use App::DocKnot::Spin::Versions;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use App::DocKnot::Util qw(is_newer print_checked print_fh);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use Git::Repository ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use IPC::System::Simple qw(capture);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use Path::Iterator::Rule ();
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use Path::Tiny qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use Pod::Thread 3.01 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 # The default list of files and/or directories to exclude from spinning. This
35 # can be added to (but not removed from) with the --exclude option. Each of
36 # these should be a regular expression.
37 2 my @EXCLUDES = (
38 qr{ ^ [.] (?!htaccess\z) }xms,
39 qr{ ^ (?:CVS|Makefile|RCS) \z }xms,
40 );
41  
42 # The URL to the software page for all of my web page generation software,
43 # used to embed a link to the software that generated the page.
44 2 my $URL = 'https://www.eyrie.org/~eagle/software/web/';
45  
46 ##############################################################################
47 # Output
48 ##############################################################################
49  
50 # Build te page footer, which consists of the navigation links, the regular
51 # signature, and the last modified date.
52 #
53 # $source - Path::Tiny path to the source file
54 # $out_path - Path::Tiny path to the output file
55 # $id - CVS Id of the source file or undef if not known
56 # @templates - Two templates to use. The first will be used if the
57 # modification and current dates are the same, and the second
58 # if they are different. %MOD% and %NOW% will be replaced with
59 # the appropriate dates and %URL% with the URL to the site
60 # generation software.
61 #
62 # Returns: HTML output
63 0 ▶sub _footer {
Subroutine _footer: not called CC=13 SCAR=52.0
64 0 my ($self, $source, $out_path, $id, @templates) = @_;
65 0 my $output = q{};
66 0 my $in_tree = 0;
67 0 ▶ if ($self->{source} && $self->{source}->subsumes($source)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"source"} and $self->{"source"}->subsumes($source)) 0 0 0
Condition0%0/3
$self->{"source"} and $self->{"source"}->subsumes($source)
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"source"} and $self->{"source"}->subsumes($source)
$self->{"source"} $self->{"source"}->subsumes($source)
Truth table0%0/3
A and B
A= $self->{"source"}B= $self->{"source"}->subsumes($source)
ABresult
0X0
100
111
68 0 $in_tree = 1;
69 }
70  
71 # Add the end-of-page navbar if we have sitemap information.
72 0 ▶ if ($self->{sitemap} && $self->{output}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sitemap"} and $self->{"output"}) 0 0 0
Condition0%0/3
$self->{"sitemap"} and $self->{"output"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"sitemap"} and $self->{"output"}
$self->{"sitemap"} $self->{"output"}
Truth table0%0/3
A and B
A= $self->{"sitemap"}B= $self->{"output"}
ABresult
0X0
100
111
73 0 my $page = $out_path->relative($self->{output});
74 0 $output .= join(q{}, $self->{sitemap}->navbar($page)) . "\n";
75 }
76  
77 # Figure out the modification dates. Use the RCS/CVS Id if available,
78 # otherwise use the Git repository if available.
79 0 my $modified;
80 0 ▶ if (defined($id)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $id) { } 0 0 0
elsif ($self->{'repository'} and $in_tree) { } 0 0 0
81 0 my (undef, undef, $date) = split(q{ }, $id);
82 0 ▶ if ($date && $date =~ m{ \A (\d+) [-/] (\d+) [-/] (\d+) }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($date and $date =~ m[ \A (\d+) [-/] (\d+) [-/] (\d+) ]msux) 0 0 0
Condition0%0/3
$date and $date =~ m[ \A (\d+) [-/] (\d+) [-/] (\d+) ]msux
!ll&&!rl&&r
000
MC/DC0%0/2
$date and $date =~ m[ \A (\d+) [-/] (\d+) [-/] (\d+) ]msux
$date $date =~ m[ \A (\d+) [-/] (\d+) [-/] (\d+) ]msux
Truth table0%0/3
A and B
A= $dateB= $date =~ m[ \A (\d+) [-/] (\d+) [-/] (\d+) ]msux
ABresult
0X0
100
111
83 0 $modified = sprintf('%d-%02d-%02d', $1, $2, $3);
84 }
85 ▶ } elsif ($self->{repository} && $in_tree) {
Condition0%0/3
$self->{"repository"} and $in_tree
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"repository"} and $in_tree
$self->{"repository"} $in_tree
Truth table0%0/3
A and B
A= $self->{"repository"}B= $in_tree
ABresult
0X0
100
111
86 $modified = $self->{repository}->run(
87 0 'log', '-1', '--format=%ct', "$source",
88 );
89 0 ▶ if ($modified) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modified) 0 0 0
90 0 $modified = strftime('%Y-%m-%d', gmtime($modified));
91 }
92 }
93 0 ▶ if (!$modified) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($modified) 0 0 0
94 0 $modified = strftime('%Y-%m-%d', gmtime($source->stat()->[9]));
95 }
96 0 my $now = strftime('%Y-%m-%d', gmtime());
97  
98 # Determine which template to use and substitute in the appropriate times.
99 0 $output .= "<address>\n" . q{ } x 4;
100 0 ▶ my $template = ($modified eq $now) ? $templates[0] : $templates[1];
Branch: 1 branch
BranchTrueFalseTotal
$modified eq $now ? : 0 0 0
101 0 $template =~ s{ %MOD% }{$modified}xmsg;
102 0 $template =~ s{ %NOW% }{$now}xmsg;
103 0 $template =~ s{ %URL% }{$URL}xmsg;
104 0 $output .= "$template\n";
105 0 $output .= "</address>\n";
106  
107 0 return $output;
108 }
109  
110 ##############################################################################
111 # External converters
112 ##############################################################################
113  
114 # Given the output from a converter, the file to save the output in, and an
115 # anonymous sub that takes three arguments, the first being the captured
116 # blurb, the second being the document ID if found, and the third being the
117 # base name of the output file, and prints out a last modified line, reformat
118 # the output of an external converter.
119 0 ▶sub _write_converter_output {
Subroutine _write_converter_output: not called CC=14 SCAR=53.5
120 0 my ($self, $page_ref, $output, $footer) = @_;
121 0 my $page = $output->relative($self->{output});
122 0 my $out_fh = $output->openw_utf8();
123  
124 # Grab the first few lines of input, looking for a blurb and Id string.
125 # Give up if we encounter <body> first. Also look for a </head> tag and
126 # add the navigation link tags before it, if applicable. Add the
127 # navigation bar right at the beginning of the body.
128 0 my ($blurb, $docid);
129 0 ▶ while (defined(my $line = shift($page_ref->@*))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = shift @$page_ref)) 0 0 0
130 0 ▶ if ($line =~ m{ <!-- \s* (\$Id.*?) \s* --> }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ / <!-- \s* (\$Id.*?) \s* --> /msux) 0 0 0
131 0 $docid = $1;
132 }
133 0 ▶ if ($line =~ m{ <!-- \s* ( (?:Generated|Converted) .*? )\s* --> }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ / <!-- \s* ( (?:Generated|Converted) .*? )\s* --> /msux) 0 0 0
134 0 $blurb = $1;
135  
136 # Only show the date of the output, not the time or time zone.
137 0 $blurb =~ s{ [ ] \d\d:\d\d:\d\d [ ] -0000 }{}xms;
138  
139 # Strip the date from the converter version output.
140 0 $blurb =~ s{ [ ] [(] \d{4}-\d\d-\d\d [)] }{}xms;
141 }
142 0 ▶ if ($self->{sitemap} && $line =~ m{ \A </head> }xmsi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sitemap"} and $line =~ m[ \A </head> ]imsux) 0 0 0
Condition0%0/3
$self->{"sitemap"} and $line =~ m[ \A </head> ]imsux
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"sitemap"} and $line =~ m[ \A </head> ]imsux
$self->{"sitemap"} $line =~ m[ \A </head> ]imsux
Truth table0%0/3
A and B
A= $self->{"sitemap"}B= $line =~ m[ \A </head> ]imsux
ABresult
0X0
100
111
143 0 my @links = $self->{sitemap}->links($page);
144 0 ▶ if (@links) {
Branch: 1 branch
BranchTrueFalseTotal
if (@links) 0 0 0
145 0 print_fh($out_fh, $output, @links);
146 }
147 }
148 0 print_fh($out_fh, $output, $line);
149 0 ▶ if ($line =~ m{ <body }xmsi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ / <body /imsux) 0 0 0
150 0 ▶ if ($self->{sitemap}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sitemap"}) 0 0 0
151 0 my @navbar = $self->{sitemap}->navbar($page);
152 0 ▶ if (@navbar) {
Branch: 1 branch
BranchTrueFalseTotal
if (@navbar) 0 0 0
153 0 print_fh($out_fh, $output, @navbar);
154 }
155 }
156 0 last;
157 }
158 }
159 0 ▶ warn "$0 spin: malformed HTML output for $output\n" unless $page_ref->@*;
Branch: 1 branch
BranchTrueFalseTotal
unless @$page_ref 0 0 0
160  
161 # Snarf input and write it to output until we see </body>, which is our
162 # signal to start adding things. We just got very confused if </body> was
163 # on the same line as <body>, so don't do that.
164 0 my $line;
165 0 ▶ while (defined($line = shift($page_ref->@*))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = shift @$page_ref)) 0 0 0
166 0 ▶ last if $line =~ m{ </body> }xmsi;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ m[ </body> ]imsux 0 0 0
167 0 print_fh($out_fh, $output, $line);
168 }
169  
170 # Add the footer and finish with the output.
171 0 print_fh($out_fh, $output, $footer->($blurb, $docid));
172 0 ▶ if (defined($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) 0 0 0
173 0 print_fh($out_fh, $output, $line, $page_ref->@*);
174 }
175 0 close($out_fh);
176 0 return;
177 }
178  
179 # These methods are all used, but are indirected through a table, so
180 # perlcritic gets confused.
181 #
182 ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
183  
184 # A wrapper around the cl2xhtml script, used to handle .changelog pointers in
185 # a tree being spun. Adds the navigation links and the signature to the
186 # cl2xhtml output.
187 0 ▶sub _cl2xhtml {
Subroutine _cl2xhtml: not called CC=2 SCAR=17.9
188 0 my ($self, $source, $output, $options, $style) = @_;
189 0 ▶ $style ||= $self->{style_url} . 'changelog.css';
Condition0%0/2
$style ||= $self->{"style_url"} . 'changelog.css'
l!l
00
MC/DC0%0/1
$style ||= $self->{"style_url"} . 'changelog.css'
$style
Truth table0%0/2
A
A= $style
Aresult
11
00
190 0 my @page = capture("cl2xhtml $options -s $style $source");
191 0 @page = map { decode('utf-8', $_) } @page;
192 0 ▶ my $footer = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
193 0 my ($blurb, $id) = @_;
194 0 ▶ if ($blurb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blurb) 0 0 0
195 0 $blurb =~ s{ cl2xhtml }{\n<a href="$URL">cl2xhtml</a>}xms;
196 }
197 0 $self->_footer($source, $output, $id, $blurb, $blurb);
198 0 };
199 0 $self->_write_converter_output(\@page, $output, $footer);
200 0 return;
201 }
202  
203 # A wrapper around the cvs2xhtml script, used to handle .log pointers in a
204 # tree being spun. Adds the navigation links and the signature to the
205 # cvs2xhtml output.
206 0 ▶sub _cvs2xhtml {
Subroutine _cvs2xhtml: not called CC=3 SCAR=24.8
207 0 my ($self, $source, $output, $options, $style) = @_;
208 0 ▶ $style ||= $self->{style_url} . 'cvs.css';
Condition0%0/2
$style ||= $self->{"style_url"} . 'cvs.css'
l!l
00
MC/DC0%0/1
$style ||= $self->{"style_url"} . 'cvs.css'
$style
Truth table0%0/2
A
A= $style
Aresult
11
00
209  
210 # Separate the source file into a directory and filename.
211 0 my $name = $source->basename();
212 0 my $dir = $source->parent();
213  
214 # Construct the options to cvs2xhtml.
215 0 ▶ if ($options !~ m{ -n [ ] }xms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options =~ / -n [ ] /msux) 0 0 0
216 0 $options .= " -n $name";
217 }
218 0 $options .= " -s $style";
219  
220 # Run the converter and write the output.
221 0 my @page = capture("(cd $dir && cvs log $name) | cvs2xhtml $options");
222 0 @page = map { decode('utf-8', $_) } @page;
223 0 ▶ my $footer = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
224 0 my ($blurb, $id, $file) = @_;
225 0 ▶ if ($blurb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blurb) 0 0 0
226 0 $blurb =~ s{ cvs2xhtml }{\n<a href="$URL">cvs2xhtml</a>}xms;
227 }
228 0 $self->_footer($source, $output, $id, $blurb, $blurb);
229 0 };
230 0 $self->_write_converter_output(\@page, $output, $footer);
231 0 return;
232 }
233  
234 # A wrapper around the faq2html script, used to handle .faq pointers in a tree
235 # being spun. Adds the navigation links and the signature to the faq2html
236 # output.
237 0 ▶sub _faq2html {
Subroutine _faq2html: not called CC=2 SCAR=17.9
238 0 my ($self, $source, $output, $options, $style) = @_;
239 0 ▶ $style ||= $self->{style_url} . 'faq.css';
Condition0%0/2
$style ||= $self->{"style_url"} . 'faq.css'
l!l
00
MC/DC0%0/1
$style ||= $self->{"style_url"} . 'faq.css'
$style
Truth table0%0/2
A
A= $style
Aresult
11
00
240 0 my @page = capture("faq2html $options -s $style $source");
241 0 @page = map { decode('utf-8', $_) } @page;
242 0 ▶ my $footer = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
243 0 my ($blurb, $id, $file) = @_;
244 0 ▶ if ($blurb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blurb) 0 0 0
245 0 $blurb =~ s{ faq2html }{\n<a href="$URL">faq2html</a>}xms;
246 }
247 0 $self->_footer($source, $output, $id, $blurb, $blurb);
248 0 };
249 0 $self->_write_converter_output(\@page, $output, $footer);
250 0 return;
251 }
252  
253 # A wrapper around Pod::Thread and a nested spin_fh invocation, used to handle
254 # .pod pointers in a tree being spun. Adds the navigation links and the
255 # signature to the output.
256 0 ▶sub _pod2html {
Subroutine _pod2html: not called CC=6 SCAR=37.4
257 0 my ($self, $source, $output, $options, $style) = @_;
258 0 ▶ $style //= 'pod';
Condition0%0/2
$style //= "pod"
l!l
00
MC/DC0%0/1
$style //= "pod"
$style
Truth table0%0/2
A
A= $style
Aresult
11
00
259  
260 # Construct the Pod::Thread formatter object.
261 0 my %options = (style => $style);
262 0 ▶ if ($options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options) { } 0 0 0
263 0 ▶ if ($options =~ m{ -c ( \s | \z ) }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options =~ / -c ( \s | \z ) /msux) 0 0 0
264 0 $options{contents} = 1;
265 }
266 0 ▶ if ($options =~ m{ -t \s+ (?: '(.*)' | ( [^\'] \S+ ) ) }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options =~ / -t \s+ (?: '(.*)' | ( [^\'] \S+ ) ) /msux) 0 0 0
267 0 ▶ $options{title} = $1 || $2;
Condition0%0/3
$1 || $2
l!l&&r!l&&!r
000
MC/DC0%0/2
$1 || $2
$1 $2
Truth table0%0/3
A || B
A= $1B= $2
ABresult
1X1
011
000
268 }
269 } else {
270 0 $options{navbar} = 1;
271 }
272 0 my $podthread = Pod::Thread->new(%options);
273  
274 # Grab the thread output.
275 0 my $data;
276 0 $podthread->output_string(\$data);
277 0 $podthread->parse_file("$source");
278 0 $data = decode('utf-8', $data);
279  
280 # Spin that thread into HTML.
281 0 my $page = $self->{thread}->spin_thread($data);
282  
283 # Push the result through _write_converter_output.
284 0 ▶ my $footer = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
285 0 my ($blurb) = @_;
286 0 my $link = '<a href="%URL%">spun</a>';
287 0 $self->_footer(
288 $source, $output, undef,
289 "Last modified and\n $link %MOD%",
290 "Last $link\n %NOW% from POD modified %MOD%",
291 );
292 0 };
293 0 my @page = map { "$_\n" } split(qr{\n}xms, $page);
294 0 $self->_write_converter_output(\@page, $output, $footer);
295 0 return;
296 }
297  
298 ## use critic
299  
300 ##############################################################################
301 # Per-file operations
302 ##############################################################################
303  
304 # Given a pointer file, read the master file name and any options, returning
305 # them as a list with the newlines chomped off.
306 #
307 # $file - Path::Tiny for the file to read
308 #
309 # Returns: List of the master file, any command-line options, and the style
310 # sheet to use, as strings
311 # Throws: Text exception if no master file is present in the pointer
312 # autodie exception if the pointer file could not be read
313 0 ▶sub _read_pointer {
Subroutine _read_pointer: not called CC=4 SCAR=30.0
314 0 my ($self, $file) = @_;
315  
316 # Read the pointer file.
317 0 my ($master, $options, $style) = $file->lines_utf8();
318  
319 # Clean up the contents.
320 0 ▶ if (!$master) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($master) 0 0 0
321 0 die "no master file specified in $file\n";
322 }
323 0 chomp($master);
324 0 ▶ if (defined($options)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options) { } 0 0 0
325 0 chomp($options);
326 } else {
327 0 $options = q{};
328 }
329 0 ▶ if (defined($style)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $style) 0 0 0
330 0 chomp($style);
331 }
332  
333 # Return the details.
334 0 return (path($master), $options, $style);
335 }
336  
337 # Convert an input path to an output path.
338 #
339 # $input - Path::Tiny input path
340 # $extension - If given, remove this extension and add .html in its place
341 155 ▶sub _output_for_file {
Subroutine _output_for_file: called CC=2 SCAR=6.9
342 155 my ($self, $input, $extension) = @_;
343 155 my $output = $input->relative($self->{source})->absolute($self->{output});
344 155 ▶ if ($extension) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extension) 60 95 155
345 60 my $output_file = $input->basename($extension) . '.html';
346 60 $output = $output->sibling($output_file);
347 }
348 155 return $output;
349 }
350  
351 # Report an action to standard output.
352 #
353 # $action - String description of the action
354 # $output - Output file generated
355 32 ▶sub _report_action {
Subroutine _report_action: called CC=1 SCAR=0.0
356 32 my ($self, $action, $output) = @_;
357 32 my $shortout = $output->relative($self->{output});
358 32 print_checked("$action .../$shortout\n");
359 32 return;
360 }
361  
362 # This routine is called for every file in the source tree. It decides what
363 # to do with each file, whether spinning it or copying it.
364 #
365 # $input - Path::Tiny path to the input file
366 #
367 # Throws: Text exception on any processing error
368 # autodie exception if files could not be accessed or written
369 155 ▶sub _process_file {
Subroutine _process_file: called CC=17 SCAR=30.5
370 155 my ($self, $input) = @_;
371  
372 # Conversion rules for pointers. The key is the extension, the first
373 # value is the name of the command for the purposes of output, and the
374 # second is the name of the method to run.
375 #<<<
376 155 my %rules = (
377 changelog => ['cl2xhtml', '_cl2xhtml'],
378 faq => ['faq2html', '_faq2html'],
379 log => ['cvs2xhtml', '_cvs2xhtml'],
380 rpod => ['pod2thread', '_pod2html'],
381 );
382 #>>>
383  
384 # Figure out what to do with the input.
385 155 ▶ if ($input->is_dir()) {
Branch: 3 branches
BranchTrueFalseTotal
if ($input->is_dir) { } 50 105 155
elsif ($input->basename =~ / [.] spin \z /msux) { } 15 90 105
elsif ($input->basename =~ / [.] th \z /msux) { } 45 45 90
386 50 my $output = $self->_output_for_file($input);
387 50 $self->{generated}{"$output"} = 1;
388 50 ▶ if ($output->exists() && !$output->is_dir()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($output->exists and not $output->is_dir) { } 0 50 50
elsif (not $output->is_dir) { } 9 41 50
Condition66%2/3
$output->exists and not $output->is_dir
!ll&&!rl&&r
9410
MC/DC0%0/2
$output->exists and not $output->is_dir
$output->exists not $output->is_dir
Truth table66%2/3
A and not(B)
A= $output->existsB= not $output->is_dir
ABresult
0X0
100
111
389 0 die "cannot replace $output with a directory\n";
390 } elsif (!$output->is_dir()) {
391 9 $self->_report_action('Creating', $output);
392 9 $output->mkpath();
393 }
394 } elsif ($input->basename() =~ m{ [.] spin \z }xms) {
395 15 my $output = $self->_output_for_file($input, '.spin');
396 15 $self->{generated}{"$output"} = 1;
397 15 ▶ if ($self->{pointer}->is_out_of_date($input, $output)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pointer"}->is_out_of_date($input, $output)) 4 11 15
398 4 $self->_report_action('Converting', $output);
399 4 $self->{pointer}->spin_pointer($input, $output);
400 }
401 } elsif ($input->basename() =~ m{ [.] th \z }xms) {
402 45 my $output = $self->_output_for_file($input, '.th');
403 45 $self->{generated}{"$output"} = 1;
404  
405 # See if we're forced to regenerate the file because it is affected by
406 # a software release.
407 45 ▶ if ($output->exists() && $self->{versions}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($output->exists and $self->{'versions'}) { } 36 9 45
Condition66%2/3
$output->exists and $self->{"versions"}
!ll&&!rl&&r
9036
MC/DC50%1/2
$output->exists and $self->{"versions"}
$output->exists $self->{"versions"}
Truth table66%2/3
A and B
A= $output->existsB= $self->{"versions"}
ABresult
0X0
100
111
408 36 my $relative = $input->relative($self->{source});
409 36 my $time = $self->{versions}->latest_release($relative);
410 return
411 36 ▶ if is_newer($output, $input) && $output->stat()->[9] >= $time;
Branch: 1 branch
BranchTrueFalseTotal
if is_newer($output, $input) and $output->stat->[9] >= $time 35 1 36
Condition66%2/3
is_newer($output, $input) and $output->stat->[9] >= $time
!ll&&!rl&&r
0135
MC/DC50%1/2
is_newer($output, $input) and $output->stat->[9] >= $time
is_newer($output, $input) $output->stat->[9] >= $time
Truth table66%2/3
A and B
A= is_newer($output, $input)B= $output->stat->[9] >= $time
ABresult
0X0
100
111
412 } else {
413 9 ▶ return if is_newer($output, $input);
Branch: 1 branch
BranchTrueFalseTotal
if is_newer($output, $input) 0 9 9
414 }
415  
416 # The output file is not newer. Respin it.
417 10 $self->_report_action('Spinning', $output);
418 10 $self->{thread}->spin_thread_file($input, $output);
419 } else {
420 45 my ($extension) = ($input->basename =~ m{ [.] ([^.]+) \z }xms);
421 45 ▶ if (defined($extension) && $rules{$extension}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $extension and $rules{$extension}) { } 0 45 45
Condition33%1/3
defined $extension and $rules{$extension}
!ll&&!rl&&r
0450
MC/DC0%0/2
defined $extension and $rules{$extension}
defined $extension $rules{$extension}
Truth table33%1/3
A and B
A= defined $extensionB= $rules{$extension}
ABresult
0X0
100
111
422 0 my ($name, $sub) = $rules{$extension}->@*;
423 0 my $output = $self->_output_for_file($input, q{.} . $extension);
424 0 $self->{generated}{"$output"} = 1;
425 0 my ($source, $options, $style) = $self->_read_pointer($input);
426 0 ▶ return if is_newer($output, $input, $source);
Branch: 1 branch
BranchTrueFalseTotal
if is_newer($output, $input, $source) 0 0 0
427 0 $self->_report_action("Running $name for", $output);
428 0 $self->$sub($source, $output, $options, $style);
429 } else {
430 45 my $output = $self->_output_for_file($input);
431 45 $self->{generated}{"$output"} = 1;
432 45 ▶ return if is_newer($output, $input);
Branch: 1 branch
BranchTrueFalseTotal
if is_newer($output, $input) 36 9 45
433 9 $self->_report_action('Updating', $output);
434 9 $input->copy($output);
435 }
436 }
437 84 return;
438 }
439  
440 # This routine is called for every file in the destination tree in depth-first
441 # order, if the user requested file deletion of files not generated from the
442 # source tree. It checks each file to see if it is in the $self->{generated}
443 # hash that was generated during spin processing, and if not, removes it.
444 #
445 # $file - Path::Tiny path to the file
446 #
447 # Throws: autodie exception on failure of rmdir or unlink
448 95 ▶sub _delete_files {
Subroutine _delete_files: called CC=3 SCAR=11.0
449 95 my ($self, $file) = @_;
450 95 ▶ return if $self->{generated}{"$file"};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"generated"}{"$file"} 93 2 95
451 2 my $shortfile = $file->relative($self->{output});
452 2 print_checked("Deleting .../$shortfile\n");
453 2 ▶ if ($file->is_dir()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->is_dir) { } 1 1 2
454 1 rmdir($file);
455 } else {
456 1 $file->remove();
457 }
458 2 return;
459 }
460  
461 ##############################################################################
462 # Public interface
463 ##############################################################################
464  
465 # Create a new App::DocKnot::Spin object, which will be used for subsequent
466 # calls.
467 #
468 # $args - Anonymous hash of arguments with the following keys:
469 # delete - Whether to delete files missing from the source tree
470 # exclude - List of regular expressions matching file names to exclude
471 # style-url - Partial URL to style sheets
472 #
473 # Returns: Newly created object
474 2 ▶sub new {
Subroutine new: called CC=4 SCAR=14.8
Pod documented
475 2 my ($class, $args_ref) = @_;
476  
477 # Treat all exclude arguments as regular expressions and add them to the
478 # global exclusion list.
479 2 my @excludes = @EXCLUDES;
480 2 ▶ if ($args_ref->{exclude}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args_ref->{"exclude"}) 0 2 2
481 0 push(@excludes, map { qr{$_}xms } $args_ref->{exclude}->@*);
482 }
483  
484 # Add a trailing slash to the partial URL for style sheets.
485 2 ▶ my $style_url = $args_ref->{'style-url'} // q{};
Condition50%1/2
$args_ref->{"style-url"} // ""
l!l
20
MC/DC0%0/1
$args_ref->{"style-url"} // ""
$args_ref->{"style-url"}
Truth table50%1/2
A
A= $args_ref->{"style-url"}
Aresult
11
00
486 2 ▶ if ($style_url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($style_url) 2 0 2
487 2 $style_url =~ s{ /* \z }{/}xms;
488 }
489  
490 # Create and return the object.
491 my $self = {
492 delete => $args_ref->{delete},
493 2 excludes => [@excludes],
494 style_url => $style_url,
495 };
496 2 bless($self, $class);
497 2 return $self;
498 }
499  
500 # Spin a directory of files into a web site.
501 #
502 # $input - The input directory
503 # $output - The output directory (which may not exist)
504 #
505 # Raises: Text exception on processing error
506 5 ▶sub spin {
Subroutine spin: called CC=11 SCAR=24.4
Pod documented
507 5 my ($self, $input, $output) = @_;
508  
509 # Reset data from a previous run.
510 5 delete $self->{repository};
511 5 delete $self->{sitemap};
512 5 delete $self->{versions};
513  
514 # Canonicalize and check input.
515 5 $input = path($input)->realpath();
516 5 ▶ if (!$input->is_dir()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($input->is_dir) 0 5 5
517 0 die "input tree $input must be a directory\n";
518 }
519 5 $self->{source} = $input;
520  
521 # Canonicalize and check output.
522 5 $output = path($output);
523 5 ▶ if (!$output->is_dir()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($output->is_dir) 0 5 5
524 0 for my $created ($output->mkpath()) {
525 0 print_checked("Creating $created\n");
526 }
527 }
528 5 $output = $output->realpath();
529 5 $self->{output} = $output;
530  
531 # Read metadata from the top of the input directory.
532 5 my $sitemap_path = $input->child('.sitemap');
533 5 ▶ if ($sitemap_path->exists()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sitemap_path->exists) 5 0 5
534 5 $self->{sitemap} = App::DocKnot::Spin::Sitemap->new($sitemap_path);
535 }
536 5 my $versions_path = $input->child('.versions');
537 5 ▶ if ($versions_path->exists()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($versions_path->exists) 5 0 5
538 5 $self->{versions} = App::DocKnot::Spin::Versions->new($versions_path);
539 }
540 5 ▶ if ($input->child('.git')->is_dir()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input->child(".git")->is_dir) 0 5 5
541 0 $self->{repository} = Git::Repository->new(work_tree => $input);
542 }
543  
544 # Process all .rss files in the input tree first. This is done as a
545 # separate pass because Path::Iterator::Rule appears to not always re-read
546 # the directory when it's modified during the iteration.
547 5 my $rss = App::DocKnot::Spin::RSS->new({ base => $input });
548 5 my $rule = Path::Iterator::Rule->new()->name('.rss');
549 5 my $iter = $rule->iter("$input", { follow_symlinks => 0 });
550 5 ▶ while (defined(my $file = $iter->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file = &$iter())) 10 5 15
551 10 $rss->generate(path($file), path($file)->parent);
552 }
553  
554 # Create a new thread converter object.
555 $self->{thread} = App::DocKnot::Spin::Thread->new(
556 {
557 output => $output,
558 sitemap => $self->{sitemap},
559 source => $input,
560 'style-url' => $self->{style_url},
561 versions => $self->{versions},
562 },
563 5 );
564  
565 # Create the processor for pointers.
566 $self->{pointer} = App::DocKnot::Spin::Pointer->new(
567 {
568 output => "$output",
569 sitemap => $self->{sitemap},
570 'style-url' => $self->{style_url},
571 thread => $self->{thread},
572 },
573 5 );
574  
575 # Process the input tree.
576 5 my %options = (follow_symlinks => 0, report_symlinks => 1);
577 5 $rule = Path::Iterator::Rule->new();
578 5 $rule = $rule->skip($rule->new()->name($self->{excludes}->@*));
579 5 $iter = $rule->iter("$input", \%options);
580 5 ▶ while (defined(my $file = $iter->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file = &$iter())) 155 5 160
581 155 $self->_process_file(path($file));
582 }
583  
584 # Remove stray files from the output tree.
585 5 ▶ if ($self->{delete}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"delete"}) 3 2 5
586 3 $options{depthfirst} = 1;
587 3 $iter = $rule->iter("$output", \%options);
588 3 ▶ while (defined(my $file = $iter->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $file = &$iter())) 95 3 98
589 95 $self->_delete_files(path($file));
590 }
591 }
592 5 return;
593 }
594  
595 ##############################################################################
596 # Module return value and documentation
597 ##############################################################################
598  
599 2 1;
600 __END__
↑ Summary DocKnot/Spin/Text.pm »