DocKnot/Spin/RSS.pm

filter: Statementstmt 94.9% 319 / 336 Branchbran 73.7% 115 / 156 Conditioncond 61.7% 29 / 47 MC/DCmcdc 41.9% 13 / 31 Subroutinesub 100% 28 / 28 Podpod 100% 2 / 2 totaltotal 84.3% 506 / 600 CC 101 SCAR 22.9
CC
101
Cov
86%
_rss_review
15.2
generate
14.7
_parse_changes
13.7
CRAP 9.9
« DocKnot/Spin/Pointer.pm ↑ Summary DocKnot/Spin/Sitemap.pm »
1 # Generate RSS and thread from a feed description file.
2 #
3 # This module generates RSS feeds and thread indexes of newly-published pages
4 # or change notes for a web site maintained with App::DocKnot::Spin.
5 #
6 # SPDX-License-Identifier: MIT
7  
8 ##############################################################################
9 # Modules and declarations
10 ##############################################################################
11  
12 package App::DocKnot::Spin::RSS v8.0.1;
13  
14 2 ▶use 5.024;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use parent qw(App::DocKnot);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use warnings FATAL => 'utf8';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 ▶use App::DocKnot::Spin::Thread;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use App::DocKnot::Util qw(print_checked print_fh);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Date::Language ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Date::Parse qw(str2time);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Path::Tiny qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 ##############################################################################
28 # Utility functions
29 ##############################################################################
30  
31 # Escapes &, <, and > characters for HTML or XML output.
32 #
33 # $string - Input string
34 #
35 # Returns: Escaped string
36 15 ▶sub _escape {
Subroutine _escape: called CC=1 SCAR=0.0
37 15 my ($string) = @_;
38 15 $string =~ s{ & }{&amp;}xmsg;
39 15 $string =~ s{ < }{&lt;}xmsg;
40 15 $string =~ s{ > }{&gt;}xmsg;
41 15 return $string;
42 }
43  
44 # List intersection.
45 #
46 # $one - First list
47 # $two - Second list
48 #
49 # Returns: Common elements of both lists as a list
50 4 ▶sub _intersect {
Subroutine _intersect: called CC=1 SCAR=0.0
51 4 my ($one, $two) = @_;
52 4 my %one = map { $_ => 1 } $one->@*;
53 4 return grep { $one{$_} } $two->@*;
54 }
55  
56 # Construct an absolute URL from a relative URL and a base URL. This plays
57 # fairly fast and loose with schemes and the like, since we don't need to be
58 # precise for our purposes.
59 #
60 # $url - Relative URL
61 # $base - Base URL to which it is relative
62 #
63 # Returns: Absolute URL
64 101 ▶sub _absolute_url {
Subroutine _absolute_url: called CC=4 SCAR=13.9
65 101 my ($url, $base) = @_;
66  
67 # If $url is already absolute, return it.
68 101 ▶ return $url if $url =~ m{ \A [[:lower:]]+ : }xms;
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ / \A [[:lower:]]+ : /msux 2 99 101
69  
70 # If $url starts with /, take only the scheme and host from the base URL.
71 99 ▶ if ($url =~ m{ \A / }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ m[ \A / ]msux) 3 96 99
72 3 $base =~ s{ \A ( [[:lower:]]+ :// [^/]+ ) .* }{$1}xms;
73 3 return $base . $url;
74 }
75  
76 # Otherwise, strip the last component off the base URL, and then strip
77 # more trailing components off the base URL for every ../ element in the
78 # relative URL. Then glue them together. This does not deal with the
79 # case where there are more ../ elements than there are elements in the
80 # base URL.
81 96 $base =~ s{ [^/]+ \z }{}xms;
82 96 ▶ while ($url =~ s{ \A [.][.]/+ }{}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ s[ \A [.][.]/+ ][]msux) 192 96 288
83 192 $base =~ s{ [^/]+ /+ \z }{}xms;
84 }
85 96 return $base . $url;
86 }
87  
88 # Construct a relative URL from an absolute URL and a base URL. If there is
89 # no base URL or if the URLs cannot be made relative to each other, return the
90 # relative URL unchanged.
91 #
92 # $url - Absolute URL
93 # $base - URL to which it should be relative
94 #
95 # Returns: Relative URL
96 1 ▶sub _relative_url {
Subroutine _relative_url: called CC=7 SCAR=21.3
97 1 my ($url, $base) = @_;
98 1 ▶ return $url if !$base;
Branch: 1 branch
BranchTrueFalseTotal
unless $base 0 1 1
99  
100 # Remove the protocol and host portion from the base URL and ensure that
101 # portion matches.
102 1 ▶ if ($base =~ s{ \A ( https?:// [^/]+ ) /* }{}xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base =~ s[ \A ( https?:// [^/]+ ) /* ][]msux) { } 1 0 1
103 1 my $host = $1;
104 1 ▶ if ($url !~ s{ \A \Q$host\E /* }{}xms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url =~ s[ \A \Q$host\E /* ][]msux) 0 1 1
105 0 return $url;
106 }
107 } else {
108 0 return $url;
109 }
110  
111 # Split the base URL into path segments. While the input URL starts with
112 # a matching segment, remove it. When we run out of matching segments,
113 # the relative URL is a number of ../ strings equal to the number of
114 # remaining base segments, plus the remaining input URL.
115 1 my @base = split(m{ /+ }xms, $base);
116 1 ▶ while ($url && @base) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url and @base) 2 1 3
Condition66%2/3
$url and @base
!ll&&!rl&&r
012
MC/DC50%1/2
$url and @base
$url @base
Truth table66%2/3
A and B
A= $urlB= @base
ABresult
0X0
100
111
117 2 my $segment = shift(@base);
118 2 ▶ if ($url !~ s{ \A \Q$segment\E (?: /+ | \z ) }{}xms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url =~ s[ \A \Q$segment\E (?: /+ | \z ) ][]msux) 0 2 2
119 0 return ('../' x (scalar(@base) + 1)) . $url;
120 }
121 }
122 1 return ('../' x scalar(@base)) . $url;
123 }
124  
125 # Spin a file into HTML.
126 #
127 # $file - Path::Tiny path to the file
128 #
129 # Returns: Rendered HTML as a list with one element per line
130 4 ▶sub _spin_file {
Subroutine _spin_file: called CC=1 SCAR=0.0
131 4 my ($self, $file) = @_;
132 4 my $source = $file->slurp_utf8();
133 4 my $page = $self->{spin}->spin_thread($source, $file);
134 4 return map { "$_\n" } split(m{ \n }xms, $page);
135 }
136  
137 # Report an action to standard output.
138 #
139 # $action - String description of the action
140 # $output - Output file generated
141 # $base - Base path for all output
142 6 ▶sub _report_action {
Subroutine _report_action: called CC=2 SCAR=7.4
143 6 my ($self, $action, $output) = @_;
144 6 ▶ my $shortout = $output->relative($self->{base} // path(q{.}));
Condition33%1/3
$self->{"base"} // path(".")
l!l&&r!l&&!r
600
MC/DC0%0/2
$self->{"base"} // path(".")
$self->{"base"} path(".")
Truth table33%1/3
A // B
A= $self->{"base"}B= path(".")
ABresult
1X1
011
000
145 6 print_checked("$action .../$shortout\n");
146 6 return;
147 }
148  
149 ##############################################################################
150 # Parsing
151 ##############################################################################
152  
153 # Read key/value blocks in an RFC-2822-style file.
154 #
155 # $file - File to read
156 #
157 # Returns: List of hashes corresponding to the blocks in the file.
158 10 ▶sub _read_rfc2822_file {
Subroutine _read_rfc2822_file: called CC=13 SCAR=26.0
159 10 my ($self, $file) = @_;
160 10 my $key;
161 10 my @blocks = ({});
162 10 my $current = $blocks[0];
163  
164 # Parse the file. $key holds the last key seen, used to append
165 # continuation values to the previous key. $current holds the current
166 # block being parsed and @blocks all blocks seen so far.
167 10 my $fh = $file->openr_utf8();
168 10 ▶ while (defined(my $line = <$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 2595 10 2605
169 2595 ▶ if ($line =~ m{ \A \s* \z }xms) {
Branch: 4 branches
BranchTrueFalseTotal
if ($line =~ / \A \s* \z /msux) { } 360 2235 2595
elsif ($line =~ / \A (\S+): [ \t]+ ([^\n]+) \Z /msux) { } 1325 910 2235
elsif ($line =~ / \A (\S+): \s* \z /msux) { } 200 710 910
elsif ($line =~ / \A \s /msux) { } 710 0 710
170 360 ▶ if ($key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key) 360 0 360
171 360 $current = {};
172 360 push(@blocks, $current);
173 360 undef $key;
174 }
175 } elsif ($line =~ m{ \A (\S+): [ \t]+ ([^\n]+) \Z }xms) {
176 1325 my ($new_key, $value) = ($1, $2);
177 1325 $value =~ s{ \s+ \z }{}xms;
178 1325 $key = lc($new_key);
179 1325 $current->{$key} = $value;
180 } elsif ($line =~ m{ \A (\S+): \s* \z }xms) {
181 200 my $new_key = $1;
182 200 $key = lc($new_key);
183 200 ▶ $current->{$key} //= q{};
Condition50%1/2
$current->{$key} //= ""
l!l
0200
MC/DC0%0/1
$current->{$key} //= ""
$current->{$key}
Truth table50%1/2
A
A= $current->{$key}
Aresult
11
00
184 } elsif ($line =~ m{ \A \s }xms) {
185 710 ▶ if (!$key) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key) 0 710 710
186 0 die "$file:$.: invalid continuation line\n";
187 }
188 710 my $value = $line;
189 710 $value =~ s{ \A \s }{}xms;
190 710 ▶ if ($value =~ m{ \A [.] \s* \Z }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ / \A [.] \s* \Z /msux) 55 655 710
191 55 $value = "\n";
192 }
193 710 ▶ if ($current->{$key} && $current->{$key} !~ m{ \n\z }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current->{$key} and not $current->{$key} =~ / \n\z /msux) 10 700 710
Condition100%3/3
$current->{$key} and not $current->{$key} =~ / \n\z /msux
!ll&&!rl&&r
20050010
MC/DC100%2/2
$current->{$key} and not $current->{$key} =~ / \n\z /msux
$current->{$key} not $current->{$key} =~ / \n\z /msux
Truth table100%3/3
A and not(B)
A= $current->{$key}B= not $current->{$key} =~ / \n\z /msux
ABresult
0X0
100
111
194 10 $current->{$key} .= "\n";
195 }
196 710 $current->{$key} .= $value;
197 } else {
198 0 die "$file:$.: cannot parse line\n";
199 }
200 }
201 10 close($fh);
202  
203 # If the file ends in a blank line, we'll have a stray empty block.
204 # Remove it.
205 10 ▶ if (!$key) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key) 0 10 10
206 0 pop(@blocks);
207 }
208  
209 # Return the parsed blocks.
210 10 return \@blocks;
211 }
212  
213 # Parse a change file. Save the metadata into the provided hash reference and
214 # the changes into the provided array reference. Each element of the array
215 # will be a hash with keys title, date, link, and description.
216 #
217 # $file - Path::Tiny path to file to read
218 #
219 # Returns: List of reference to metadata hash and reference to a list of
220 # hashes of changes
221 10 ▶sub _parse_changes {
Subroutine _parse_changes: called CC=13 SCAR=26.2
222 10 my ($self, $file) = @_;
223 10 my $blocks_ref = $self->_read_rfc2822_file($file);
224  
225 # The first block is our metadata. recent defaults to 15.
226 10 my $metadata_ref = shift($blocks_ref->@*);
227 10 ▶ if (!defined($metadata_ref->{recent})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $metadata_ref->{"recent"}) 10 0 10
228 10 $metadata_ref->{recent} = 15;
229 }
230  
231 # Canonicalize the data for the rest of the blocks, and check for
232 # duplicate GUIDs.
233 10 my %guids;
234 10 my $base = $metadata_ref->{base};
235 10 for my $block_ref ($blocks_ref->@*) {
236 $block_ref->{date} = str2time($block_ref->{date})
237 360 ▶ or die qq{cannot parse date "$block_ref->{date}"\n};
Branch: 1 branch
BranchTrueFalseTotal
$block_ref->{"date"} = str2time($block_ref->{"date"}) or die qq[cannot parse date "$block_ref->{"date"}"\n] 360 0 360
238  
239 # Relative links are relative to the base URL in the metadata.
240 360 ▶ if ($block_ref->{link} && $base) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_ref->{"link"} and $base) 360 0 360
Condition33%1/3
$block_ref->{"link"} and $base
!ll&&!rl&&r
00360
MC/DC0%0/2
$block_ref->{"link"} and $base
$block_ref->{"link"} $base
Truth table33%1/3
A and B
A= $block_ref->{"link"}B= $base
ABresult
0X0
100
111
241 360 ▶ if ($block_ref->{link} eq q{/}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_ref->{'link'} eq '/') { } 40 320 360
242 40 $block_ref->{link} = $base;
243 } else {
244 320 $block_ref->{link} = $base . $block_ref->{link};
245 }
246 }
247  
248 # If no GUID was given, take it from the link for journal and review
249 # entries, and otherwise from the date. Then ensure it's unique.
250 360 my $guid = $block_ref->{guid};
251 360 ▶ if (!$guid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($guid) 360 0 360
252 360 ▶ if ($block_ref->{journal} || $block_ref->{review}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_ref->{'journal'} or $block_ref->{'review'}) { } 10 350 360
Condition100%3/3
$block_ref->{"journal"} or $block_ref->{"review"}
l!l&&r!l&&!r
55350
MC/DC100%2/2
$block_ref->{"journal"} or $block_ref->{"review"}
$block_ref->{"journal"} $block_ref->{"review"}
Truth table100%3/3
A or B
A= $block_ref->{"journal"}B= $block_ref->{"review"}
ABresult
1X1
011
000
253 10 $guid = $block_ref->{link};
254 } else {
255 350 $guid = $block_ref->{date};
256 }
257 }
258 360 ▶ if ($guids{$guid}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($guids{$guid}) 0 360 360
259 0 die "duplicate GUID for entry $guid\n";
260 }
261 360 $block_ref->{guid} = $guid;
262  
263 # Determine the tags.
264 360 ▶ my @tags = $block_ref->{tags} ? split(q{ }, $block_ref->{tags}) : ();
Branch: 1 branch
BranchTrueFalseTotal
$block_ref->{'tags'} ? : 5 355 360
265 360 ▶ if ($block_ref->{review}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($block_ref->{"review"}) 5 355 360
266 5 push(@tags, 'review');
267 }
268 360 $block_ref->{tags} = \@tags;
269 }
270  
271 # Return the results.
272 10 return ($metadata_ref, $blocks_ref);
273 }
274  
275 ##############################################################################
276 # RSS output
277 ##############################################################################
278  
279 # Format a journal post into HTML for inclusion in an RSS feed. This depends
280 # heavily on my personal layout for journal posts.
281 #
282 # $file - Path::Tiny path to the journal post
283 #
284 # Returns: HTML suitable for including in an RSS feed
285 2 ▶sub _rss_journal {
Subroutine _rss_journal: called CC=9 SCAR=22.8
286 2 my ($self, $file) = @_;
287 2 my @page = $self->_spin_file($file);
288  
289 # Remove the parts that don't go into the RSS feed.
290 2 ▶ while (@page and $page[0] !~ m{ <h1> }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if (@page and not $page[0] =~ / <h1> /msux) 32 2 34
Condition66%2/3
@page and not $page[0] =~ / <h1> /msux
!ll&&!rl&&r
0232
MC/DC50%1/2
@page and not $page[0] =~ / <h1> /msux
@page not $page[0] =~ / <h1> /msux
Truth table66%2/3
A and not(B)
A= @pageB= not $page[0] =~ / <h1> /msux
ABresult
0X0
100
111
291 32 shift(@page);
292 }
293 2 shift(@page);
294 2 ▶ while (@page and $page[0] =~ m{ \A \s* \z }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if (@page and $page[0] =~ / \A \s* \z /msux) 2 2 4
Condition66%2/3
@page and $page[0] =~ / \A \s* \z /msux
!ll&&!rl&&r
022
MC/DC50%1/2
@page and $page[0] =~ / \A \s* \z /msux
@page $page[0] =~ / \A \s* \z /msux
Truth table66%2/3
A and B
A= @pageB= $page[0] =~ / \A \s* \z /msux
ABresult
0X0
100
111
295 2 shift(@page);
296 }
297 2 ▶ while (@page and $page[-1] !~ m{ <div [ ] class="date"><p> }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if (@page and not $page[-1] =~ / <div [ ] class="date"><p> /msux) 20 2 22
Condition66%2/3
@page and not $page[-1] =~ / <div [ ] class="date"><p> /msux
!ll&&!rl&&r
0220
MC/DC50%1/2
@page and not $page[-1] =~ / <div [ ] class="date"><p> /msux
@page not $page[-1] =~ / <div [ ] class="date"><p> /msux
Truth table66%2/3
A and not(B)
A= @pageB= not $page[-1] =~ / <div [ ] class="date"><p> /msux
ABresult
0X0
100
111
298 20 pop(@page);
299 }
300 2 pop(@page);
301 2 ▶ while (@page and $page[-1] =~ m{ \A \s* \z }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if (@page and $page[-1] =~ / \A \s* \z /msux) 0 2 2
Condition33%1/3
@page and $page[-1] =~ / \A \s* \z /msux
!ll&&!rl&&r
020
MC/DC0%0/2
@page and $page[-1] =~ / \A \s* \z /msux
@page $page[-1] =~ / \A \s* \z /msux
Truth table33%1/3
A and B
A= @pageB= $page[-1] =~ / \A \s* \z /msux
ABresult
0X0
100
111
302 0 pop(@page);
303 }
304  
305 # Return the rest.
306 2 return join(q{}, @page) . "\n";
307 }
308  
309 # Format a review into HTML for inclusion in an RSS feed. This depends even
310 # more heavily on my personal layout for review posts.
311 #
312 # $file - Path::Tiny path to the review
313 #
314 # Returns: HTML suitable for inclusion in an RSS feed
315 2 ▶sub _rss_review {
Subroutine _rss_review: called CC=14 SCAR=27.2
316 2 my ($self, $file, $metadata) = @_;
317 2 my @page = $self->_spin_file($file);
318  
319 # Find the title and author because we'll add them back in laater, and
320 # remove the preamble of the page not included in the RSS feed.
321 2 my ($title, $author);
322 2 ▶ while (@page && $page[0] !~ m{ <table [ ] class="info"> }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if (@page and not $page[0] =~ / <table [ ] class="info"> /msux) 44 2 46
Condition66%2/3
@page and not $page[0] =~ / <table [ ] class="info"> /msux
!ll&&!rl&&r
0244
MC/DC50%1/2
@page and not $page[0] =~ / <table [ ] class="info"> /msux
@page not $page[0] =~ / <table [ ] class="info"> /msux
Truth table66%2/3
A and not(B)
A= @pageB= not $page[0] =~ / <table [ ] class="info"> /msux
ABresult
0X0
100
111
323 44 ▶ if ($page[0] =~ m{ <h1> <cite> (.*) </cite> </h1> }xms) {
Branch: 2 branches
BranchTrueFalseTotal
if ($page[0] =~ m[ <h1> <cite> (.*) </cite> </h1> ]msux) { } 2 42 44
elsif ($page[0] =~ m[ <p [ ] class="(?:author|date)">(.*)</p>]msux) { } 2 40 42
324 2 $title = $1;
325 } elsif ($page[0] =~ m{ <p [ ] class="(?:author|date)">(.*)</p>}xms) {
326 2 $author = $1;
327 }
328 44 shift(@page);
329 }
330 2 ▶ if (!$title || !$author) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($title and $author) 0 2 2
Condition33%1/3
$title and $author
!ll&&!rl&&r
002
MC/DC0%0/2
$title and $author
$title $author
Truth table33%1/3
A and B
A= $titleB= $author
ABresult
0X0
100
111
331 0 die "cannot find title and author in $file\n";
332 }
333  
334 # Remove more stuff not included in the RSS feed. This is absurdly
335 # specific to exactly how I format reviews.
336 2 ▶ while (@page && $page[-1] !~ m{ <p [ ] class="rating"> }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if (@page and not $page[-1] =~ / <p [ ] class="rating"> /msux) 18 2 20
Condition66%2/3
@page and not $page[-1] =~ / <p [ ] class="rating"> /msux
!ll&&!rl&&r
0218
MC/DC50%1/2
@page and not $page[-1] =~ / <p [ ] class="rating"> /msux
@page not $page[-1] =~ / <p [ ] class="rating"> /msux
Truth table66%2/3
A and not(B)
A= @pageB= not $page[-1] =~ / <p [ ] class="rating"> /msux
ABresult
0X0
100
111
337 18 pop(@page);
338 }
339 2 my ($buy, $ebook);
340 2 for my $i (0 .. $#page) {
341 64 ▶ if ($page[$i] =~ m{ <p [ ] class="ebook"> }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page[$i] =~ / <p [ ] class="ebook"> /msux) 0 64 64
342 0 $ebook = $i;
343 }
344 64 ▶ if ($page[$i] =~ m{ <p [ ] class="buy"> }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page[$i] =~ / <p [ ] class="buy"> /msux) 2 62 64
345 2 $buy = $i;
346 2 last;
347 }
348 }
349 2 ▶ if ($buy) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buy) 2 0 2
350 2 splice(@page, $buy, 2);
351 }
352 2 ▶ if ($ebook) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ebook) 0 2 2
353 0 splice(@page, $ebook, 4);
354 }
355  
356 # Done with line-by-lne processing. Glue everything together into one
357 # page and do a bunch more random HTML cleanup.
358 2 my $page = join(q{}, @page);
359 2 $page =~ s{ ^ \s* <table[^>]+> }{<table>}xmsg;
360 2 $page =~ s{ ^ \s* <tr }{ <tr}xmsg;
361 2 $page =~ s{ ^ \s* <td[^>]+> }{ <td>}xmsg;
362 2 $page =~ s{ </tr> </table> </div> }{</tr></table>}xms;
363 2 $page =~ s{ <div [ ] class="review">}{}xms;
364 2 $page =~ s{ <p [ ] class="rating">}{<p>}xms;
365 2 $page =~ s{
366 <span [ ] class="story"><span [ ] id="\S+">(.*?)</span></span>
367 }{<strong>$1</strong>}xmsg;
368  
369 # Add the author and title to the top of the HTML because we stripped out
370 # the top-level heading where this would normally have been.
371 2 $page = "<p>Review: <cite>$title</cite>, $author</p>\n\n" . $page;
372  
373 # Return the cleaned-up page.
374 2 return $page . "\n";
375 }
376  
377 # Print out the RSS version of the changes information given. Lots of this is
378 # hard-coded. Use the date of the last change as <pubDate> and the current
379 # time as <lastBuildDate>; it's not completely clear to me that this is
380 # correct.
381 #
382 # $file - Path::Tiny path to the output file
383 # $base - Base Path::Tiny path for input files
384 # $metadata_ref - Hash of metadata for the RSS feed
385 # $entries_ref - Array of entries in the RSS feed
386 4 ▶sub _rss_output {
Subroutine _rss_output: called CC=8 SCAR=20.9
387 4 my ($self, $file, $base, $metadata_ref, $entries_ref) = @_;
388  
389 # Determine the current date and latest publication date of all of the
390 # entries, published in the obnoxious format used by RSS.
391 4 my $lang = Date::Language->new('English');
392 4 my $format = '%a, %d %b %Y %H:%M:%S %z';
393 4 my $now = $lang->strftime($format, [localtime()]);
394 4 my $latest = $now;
395 4 ▶ if ($entries_ref->@*) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$entries_ref) 4 0 4
396 4 $latest = strftime($format, localtime($entries_ref->[0]{date}));
397 }
398  
399 # Determine the URL of the RSS file we're generating, if possible.
400 4 my $url;
401 4 ▶ if ($metadata_ref->{'rss-base'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($metadata_ref->{"rss-base"}) 4 0 4
402 4 my $name = $file->basename();
403 4 $url = $metadata_ref->{'rss-base'} . $name;
404 }
405  
406 # Format the entries.
407 4 my @formatted_entries;
408 4 for my $entry_ref ($entries_ref->@*) {
409 19 my $date = $lang->strftime($format, [localtime($entry_ref->{date})]);
410 19 my $description;
411 19 ▶ if ($entry_ref->{description}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($entry_ref->{'description'}) { } 15 4 19
elsif ($entry_ref->{'journal'}) { } 2 2 4
elsif ($entry_ref->{'review'}) { } 2 0 2
412 15 $description = _escape($entry_ref->{description});
413 15 $description =~ s{ ^ }{ }xmsg;
414 15 $description =~ s{ \A (\s*) }{$1<p>}xms;
415 15 $description =~ s{ \n* \z }{</p>\n}xms;
416 } elsif ($entry_ref->{journal}) {
417 2 my $path = path($entry_ref->{journal})->absolute($base);
418 2 $description = $self->_rss_journal($path);
419 } elsif ($entry_ref->{review}) {
420 2 my $path = path($entry_ref->{review})->absolute($base);
421 2 $description = $self->_rss_review($path);
422 }
423  
424 # Make all relative URLs absolute.
425 19 $description =~ s{
426 ( < (?:a [ ] href | img [ ] src) = \" )
427 (?!http:)
428 ( [./\w] [^\"]+ ) \"
429 66 }{ $1 . _absolute_url($2, $entry_ref->{link}) . qq{\"} }xmsge;
430  
431 # Convert this into an object suitable for the output template.
432 my $formatted_ref = {
433 date => $date,
434 description => $description,
435 guid => $entry_ref->{guid},
436 link => $entry_ref->{link},
437 title => $entry_ref->{title},
438 19 };
439 19 push(@formatted_entries, $formatted_ref);
440 }
441  
442 # Generate the RSS output using the template.
443 my %vars = (
444 base => $metadata_ref->{base},
445 description => $metadata_ref->{description},
446 docknot_version => $App::DocKnot::VERSION,
447 entries => \@formatted_entries,
448 language => $metadata_ref->{language},
449 latest => $latest,
450 now => $now,
451 title => $metadata_ref->{title},
452 4 url => $url,
453 );
454 4 my $result;
455 $self->{template}->process($self->{templates}{rss}, \%vars, \$result)
456 4 ▶ or croak($self->{template}->error());
Branch: 1 branch
BranchTrueFalseTotal
$self->{"template"}->process($self->{"templates"}{"rss"}, \%vars, \$result) or croak($self->{"template"}->error) 4 0 4
457  
458 # Write the result to the output file.
459 4 $file->spew_utf8($result);
460 4 return;
461 }
462  
463 ##############################################################################
464 # Thread output
465 ##############################################################################
466  
467 # Print out the thread version of the recent changes list.
468 #
469 # $file - Path::Tiny output path
470 # $metadata_ref - RSS feed metadata
471 # $entries_ref - Entries
472 1 ▶sub _thread_output {
Subroutine _thread_output: called CC=5 SCAR=16.1
473 1 my ($self, $file, $metadata_ref, $entries_ref) = @_;
474  
475 # The entries are in a flat list, but we want a two-level list of entries
476 # by month so that the template can add appropriate month headings.
477 # Restructure the entry list accordingly.
478 1 my (@entries_by_month, $last_month);
479 1 for my $entry_ref ($entries_ref->@*) {
480 70 my $month = strftime('%B %Y', localtime($entry_ref->{date}));
481 70 my $date = strftime('%Y-%m-%d', localtime($entry_ref->{date}));
482  
483 # Copy the entry with a reformatted description.
484 70 my $description = $entry_ref->{description};
485 70 $description =~ s{ ^ }{ }xmsg;
486 70 $description =~ s{ \\ }{\\\\}xmsg;
487 my $formatted_ref = {
488 date => $date,
489 description => $description,
490 link => $entry_ref->{link},
491 title => $entry_ref->{title},
492 70 };
493  
494 # Add the entry to the appropriate month.
495 70 ▶ if (!$last_month || $month ne $last_month) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $last_month or $month ne $last_month) { } 8 62 70
Condition100%3/3
not $last_month or $month ne $last_month
l!l&&r!l&&!r
1762
MC/DC100%2/2
not $last_month or $month ne $last_month
not $last_month $month ne $last_month
Truth table100%3/3
not(A) or B
A= not $last_monthB= $month ne $last_month
ABresult
1X1
011
000
496 8 my $month_ref = { heading => $month, entries => [$formatted_ref] };
497 8 push(@entries_by_month, $month_ref);
498 8 $last_month = $month;
499 } else {
500 62 push($entries_by_month[-1]{entries}->@*, $formatted_ref);
501 }
502 }
503  
504 # Generate the RSS output using the template.
505 my %vars = (
506 1 prefix => $metadata_ref->{'thread-prefix'},
507 entries => \@entries_by_month,
508 );
509 1 my $result;
510 $self->{template}->process($self->{templates}{changes}, \%vars, \$result)
511 1 ▶ or croak($self->{template}->error());
Branch: 1 branch
BranchTrueFalseTotal
$self->{"template"}->process($self->{"templates"}{"changes"}, \%vars, \$result) or croak($self->{"template"}->error) 1 0 1
512  
513 # Write the result to the output file.
514 1 $file->spew_utf8($result);
515 1 return;
516 }
517  
518 ##############################################################################
519 # Index output
520 ##############################################################################
521  
522 # Translate the thread of a journal entry for inclusion in an index page.
523 #
524 # $file - Path::Tiny to the journal entry
525 #
526 # Returns: Thread to include in the index page
527 1 ▶sub _index_journal {
Subroutine _index_journal: called CC=5 SCAR=16.2
528 1 my ($self, $file, $url) = @_;
529 1 my $fh = $file->openr_utf8();
530  
531 # Skip to the first \h1 and exclude it.
532 1 ▶ while (defined(my $line = <$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 5 0 5
533 5 ▶ last if $line =~ m{ \\h1 }xms;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ / \\h1 /msux 1 4 5
534 }
535  
536 # Skip an initial blank line.
537 1 my $text = <$fh>;
538 1 $text =~ s{ \A \s* \z}{}xms;
539  
540 # Grab the rest of the entry until the \date command that ends it.
541 1 ▶ while (defined(my $line = <$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 458 0 458
542 458 ▶ last if $line =~ m{ \A \\date }xms;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ / \A \\date /msux 1 457 458
543 457 $text .= $line;
544 }
545  
546 # All done.
547 1 close($fh);
548 1 return $text;
549 }
550  
551 # Translate the thread of a book review for inclusion into an index page.
552 #
553 # $file - Path::Tiny to the book review
554 #
555 # Returns: Thread to include in the index page
556 1 ▶sub _index_review {
Subroutine _index_review: called CC=12 SCAR=25.9
557 1 my ($self, $file) = @_;
558 1 my $title;
559 my $author;
560  
561 # Regex to match a single "character" in a macro argument.
562 1 my $char = qr{ (?: [^\]\\] | \\entity \[ [^\]]+ \] ) }xms;
563  
564 # Scan for the author information and save it. Handle the case where the
565 # \header or \edited line is continued on the next line.
566 1 my $fh = $file->openr_utf8();
567 1 ▶ while (defined(my $line = <$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 3 0 3
568 3 ▶ if ($line =~ m{ \\ (?:header|edited) \s* \[ $char+ \] \s* \z }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ / \\ (?:header|edited) \s* \[ $char+ \] \s* \z /msux) 0 3 3
569 0 $line .= <$fh>;
570 }
571 3 ▶ if ($line =~ m{ \\(header|edited)\s*\[($char+)\]\s*\[($char+)\] }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ / \\(header|edited)\s*\[($char+)\]\s*\[($char+)\] /msux) 1 2 3
572 1 ($title, $author) = ($2, $3);
573 1 ▶ if ($1 eq 'edited') {
Branch: 1 branch
BranchTrueFalseTotal
if ($1 eq "edited") 0 1 1
574 0 $author .= ' (ed.)';
575 }
576 1 last;
577 }
578 }
579 1 ▶ if (!defined($author)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $author) 0 1 1
580 0 die "cannot find author in review $file\n";
581 }
582  
583 # Add the prefix saying what's being reviewed.
584 1 my $text;
585 1 ▶ if ($file =~ m{ /magazines/ }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file =~ m[ /magazines/ ]msux) { } 0 1 1
586 0 $text = "Review: \\cite[$title], $author\n\n";
587 } else {
588 1 $text = "Review: \\cite[$title], by $author\n\n";
589 }
590  
591 # Add the metadata table.
592 1 $text .= "\\table[][\n";
593 1 ▶ while (defined(my $line = <$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 13 0 13
594 13 ▶ last if $line =~ m{ \A \\div [(]review[)] \[ }xms;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ / \A \\div [(]review[)] \[ /msux 1 12 13
595 12 ▶ if ($line =~ m{ \A \s* \\data \[($char+)\] \s* \[($char+)\] }xms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ / \A \s* \\data \[($char+)\] \s* \[($char+)\] /msux) 6 6 12
596 6 $text .= " \\tablerow[$1][$2]\n";
597 }
598 }
599 1 $text .= "]\n\n";
600  
601 # Add the rest of the review.
602 1 ▶ while (defined(my $line = <$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 88 0 88
603 88 ▶ last if $line =~ m{ \A \\done }xms;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ / \A \\done /msux 1 87 88
604 87 $line =~ s{ \\story \[ \d+ \] }{\\strong}xmsg;
605 87 $line =~ s{ \\rating \s* \[($char+)\] }{Rating: $1 out of 10}xms;
606 87 $text .= $line;
607 }
608 1 close($fh);
609 1 return $text;
610 }
611  
612 # Print out the index version of the recent changes list.
613 #
614 # $file - Path::Tiny path to the output file
615 # $base - Base Path::Tiny path for input files
616 # $metadata_ref - RSS feed metadata
617 # $entries_ref - Entries
618 1 ▶sub _index_output {
Subroutine _index_output: called CC=5 SCAR=16.1
619 1 my ($self, $file, $base, $metadata_ref, $entries_ref) = @_;
620  
621 # Format each entry.
622 1 my @formatted_entries;
623 1 for my $entry_ref ($entries_ref->@*) {
624 2 my @time = localtime($entry_ref->{date});
625 2 my $date = strftime('%Y-%m-%d %H:%M', @time);
626 2 my $day = strftime('%Y-%m-%d', @time);
627  
628 # Get the text of the entry.
629 2 my $text;
630 2 ▶ if ($entry_ref->{journal}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($entry_ref->{'journal'}) { } 1 1 2
elsif ($entry_ref->{'review'}) { } 1 0 1
631 1 my $path = path($entry_ref->{journal})->absolute($base);
632 1 $text = $self->_index_journal($path);
633 } elsif ($entry_ref->{review}) {
634 1 my $path = path($entry_ref->{review})->absolute($base);
635 1 $text = $self->_index_review($path);
636 } else {
637 0 die "unknown entry type\n";
638 }
639  
640 # Make all the URLs absolute and then convert images back to relative
641 # based on the URL of the file we're creating. This handles
642 # correcting links from thread from elsewhere in the tree.
643 2 $text =~ s{
644 ( \\ (?: link | image ) \s* \[ ) ( [^\]]+ ) \]
645 35 }{ $1 . _absolute_url($2, $entry_ref->{link}) . ']' }xmsge;
646 2 $text =~ s{
647 ( \\ image \s* \[ ) ( [^\]]+ ) \]
648 1 }{$1 . _relative_url($2, $metadata_ref->{'index-base'}) . ']' }xmsge;
649  
650 # Add the entry to the list.
651 my $formatted_ref = {
652 date => $date,
653 day => $day,
654 link => $entry_ref->{link},
655 title => $entry_ref->{title},
656 2 text => $text,
657 };
658 2 push(@formatted_entries, $formatted_ref);
659 }
660  
661 # Generate the RSS output using the template.
662 my %vars = (
663 prefix => $metadata_ref->{'index-prefix'},
664 1 suffix => $metadata_ref->{'index-suffix'},
665 entries => \@formatted_entries,
666 );
667 1 my $result;
668 $self->{template}->process($self->{templates}{index}, \%vars, \$result)
669 1 ▶ or croak($self->{template}->error());
Branch: 1 branch
BranchTrueFalseTotal
$self->{"template"}->process($self->{"templates"}{"index"}, \%vars, \$result) or croak($self->{"template"}->error) 1 0 1
670  
671 # Write the result to the output file.
672 1 $file->spew_utf8($result);
673 1 return;
674 }
675  
676 ##############################################################################
677 # Public interface
678 ##############################################################################
679  
680 # Create a new RSS generator object.
681 #
682 # $args_ref - Anonymous hash of arguments with the following keys:
683 # base - Path::Tiny base path for output files
684 #
685 # Returns: Newly created object
686 5 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
687 5 my ($class, $args_ref) = @_;
688  
689 # Create and return the object.
690 5 ▶ my $base = defined($args_ref->{base}) ? path($args_ref->{base}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $args_ref->{'base'} ? : 5 0 5
691 5 ▶ my $tt = Template->new({ ABSOLUTE => 1, ENCODING => 'utf8' })
Branch: 1 branch
BranchTrueFalseTotal
my $tt = "Template"->new({"ABSOLUTE", 1, "ENCODING", "utf8"}) or croak("Template"->error) 5 0 5
692 or croak(Template->error());
693 5 my $self = {
694 base => $base,
695 spin => App::DocKnot::Spin::Thread->new(),
696 template => $tt,
697 };
698 5 bless($self, $class);
699 $self->{templates} = {
700 5 changes => $self->appdata_path('templates', 'changes.tmpl'),
701 index => $self->appdata_path('templates', 'index.tmpl'),
702 rss => $self->appdata_path('templates', 'rss.tmpl'),
703 };
704 5 return $self;
705 }
706  
707 # Generate specified output files from an .rss input file.
708 #
709 # $source - Path::Tiny path to the .rss file
710 # $base - Optional Path::Tiny base path for output
711 10 ▶sub generate {
Subroutine generate: called CC=14 SCAR=26.9
Pod documented
712 10 my ($self, $source, $base) = @_;
713 10 $source = path($source);
714 10 ▶ $base //= $self->{base};
Condition33%1/3
$base //= $self->{"base"}
l!l&&r!l&&!r
1000
MC/DC0%0/2
$base //= $self->{"base"}
$base $self->{"base"}
Truth table33%1/3
A //= B
A= $baseB= $self->{"base"}
ABresult
1X1
011
000
715 10 ▶ $base = defined($base) ? path($base) : path(q{.});
Branch: 1 branch
BranchTrueFalseTotal
defined $base ? : 10 0 10
716  
717 # Read in the changes.
718 10 my ($metadata_ref, $changes_ref) = $self->_parse_changes($source);
719  
720 # The output key tells us what files to write out.
721 10 my @output = ('*:rss:index.rss');
722 10 ▶ if ($metadata_ref->{output}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($metadata_ref->{"output"}) 10 0 10
723 10 @output = split(q{ }, $metadata_ref->{output});
724 }
725  
726 # Iterate through each specified output file.
727 10 for my $output (@output) {
728 30 my ($tags, $format, $file) = split(m{ : }xms, $output);
729 30 $file = path($file);
730 30 ▶ if ($file->is_relative()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->is_relative) 30 0 30
731 30 $file = $file->absolute($base);
732 }
733  
734 # If the output file is newer than the input file, do nothing.
735 30 ▶ next if ($file->exists() && -M "$file" <= -M "$source");
Branch: 1 branch
BranchTrueFalseTotal
if $file->exists and -M "$file" <= -M "$source" 24 6 30
Condition66%2/3
$file->exists and -M "$file" <= -M "$source"
!ll&&!rl&&r
6024
MC/DC50%1/2
$file->exists and -M "$file" <= -M "$source"
$file->exists -M "$file" <= -M "$source"
Truth table66%2/3
A and B
A= $file->existsB= -M "$file" <= -M "$source"
ABresult
0X0
100
111
736  
737 # Find all the changes of interest to this output file.
738 6 my @entries;
739 6 ▶ if ($tags eq q{*}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tags eq '*') { } 4 2 6
740 4 @entries = $changes_ref->@*;
741 } else {
742 2 my @tags = split(m{ , }xms, $tags);
743 @entries
744 2 = grep { _intersect($_->{tags}, \@tags) } $changes_ref->@*;
745 }
746  
747 # Write the output.
748 6 ▶ if ($format eq 'thread') {
Branch: 3 branches
BranchTrueFalseTotal
if ($format eq 'thread') { } 1 5 6
elsif ($format eq 'rss') { } 4 1 5
elsif ($format eq 'index') { } 1 0 1
749 1 $self->_report_action('Generating thread file', $file);
750 1 $self->_thread_output($file, $metadata_ref, \@entries);
751 } elsif ($format eq 'rss') {
752 4 ▶ if (scalar(@entries) > $metadata_ref->{recent}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @entries > $metadata_ref->{"recent"}) 1 3 4
753 1 splice(@entries, $metadata_ref->{recent});
754 }
755 4 $self->_report_action('Generating RSS file', $file);
756 4 $self->_rss_output($file, $base, $metadata_ref, \@entries);
757 } elsif ($format eq 'index') {
758 1 ▶ if (scalar(@entries) > $metadata_ref->{recent}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @entries > $metadata_ref->{"recent"}) 0 1 1
759 0 splice(@entries, $metadata_ref->{recent});
760 }
761 1 $self->_report_action('Generating index file', $file);
762 1 my $index_base = $source->parent();
763 1 $self->_index_output($file, $index_base, $metadata_ref, \@entries);
764 }
765 }
766 10 return;
767 }
768  
769 ##############################################################################
770 # Module return value and documentation
771 ##############################################################################
772  
773 2 1;
774 __END__
« DocKnot/Spin/Pointer.pm ↑ Summary DocKnot/Spin/Sitemap.pm »