lib/App/Followme/FileData.pm

filter: Statementstmt 98.0% 102 / 104 Branchbran 90.0% 18 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 95.2% 20 / 21 Podpod 10.0% 1 / 10 totaltotal 90.9% 141 / 155 CC 11 SCAR 6.6
CC
11
Cov
97%
format_author
3.0
fetch_sections
3.0
fetch_content
3.0
CRAP 1.9
« lib/App/Followme.pm ↑ Summary lib/App/Followme/UploadLocal.pm »
1 package App::Followme::FileData;
2  
3 17 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 17 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 17 ▶use lib '../..';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 17 ▶use base qw(App::Followme::FolderData);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use App::Followme::FIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 17 ▶use App::Followme::NestedText;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 17 ▶use App::Followme::Web;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 17 our $VERSION = "2.03";
16  
17 #----------------------------------------------------------------------
18 # Read the default parameter values
19  
20 344 ▶sub parameters {
Subroutine parameters: called CC=1 SCAR=0.0
Pod documented
21 344 my ($self) = @_;
22  
23 return (
24 344 base_directory => '',
25 title_template => '',
26 );
27 }
28  
29 #----------------------------------------------------------------------
30 # Convert content represented in html format (stub)
31  
32 10 ▶sub fetch_as_html {
Subroutine fetch_as_html: called CC=1 SCAR=0.0
Pod undocumented
33 10 my ($self, $content_block) = @_;
34 10 return $content_block;
35 }
36  
37 #----------------------------------------------------------------------
38 # Parse content as html to get fallback values for data
39  
40 110 ▶sub fetch_content {
Subroutine fetch_content: called CC=3 SCAR=11.0
Pod undocumented
41 110 my ($self, $content_block) = @_;
42  
43 # Content within title template is title, rest of content is body
44  
45 110 my $metadata = [];
46 110 my $global = 0;
47 110 my $body;
48  
49  
50 110 ▶ if ($self->{title_template}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'title_template'}) { } 23 87 110
51  
52 22 ▶ my $title_parser = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
53 22 my ($metadata, @tokens) = @_;
54 22 my $text = web_only_text(@tokens);
55 22 push(@$metadata, 'title', $text);
56 22 return '';
57 23 };
58  
59 $body = web_substitute_tags($self->{title_template},
60 23 $content_block,
61 $title_parser,
62 $metadata,
63 $global
64 );
65 } else {
66 87 $body = $content_block;
67 }
68  
69 110 $body =~ s/^\s+//;
70 110 push(@$metadata, 'body', $body);
71 110 my %content = @$metadata;
72  
73 102 ▶ my $paragraph_parser = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
74 102 my ($paragraph, @tokens) = @_;
75 102 $$paragraph = web_only_text(@tokens);
76 102 return;
77 110 };
78  
79 110 my $paragraph;
80 110 ▶ if (web_match_tags('<p></p>',
Branch: 1 branch
BranchTrueFalseTotal
if (web_match_tags("<p></p>", $body, $paragraph_parser, \$paragraph, $global)) 102 8 110
81 $body,
82 $paragraph_parser,
83 \$paragraph,
84 $global)) {
85  
86 # Description is first sentence of first paragraph
87 102 $paragraph =~ /([^.!?\s][^.!?]*(?:[.!?](?!['"]?\s|$)[^.!?]*)*[.!?]?['"]?(?=\s|$))/;
88 102 $content{description} = $1;
89 102 $content{summary} = $paragraph;
90 }
91  
92 110 return %content;
93 }
94  
95 #----------------------------------------------------------------------
96 # Fetch data from all its possible sources
97  
98 558 ▶sub fetch_data {
Subroutine fetch_data: called CC=3 SCAR=11.0
Pod undocumented
99 558 my ($self, $name, $filename, $loop) = @_;
100  
101 # Check to see if you can get data without opening file
102 558 $self->check_filename($name, $filename);
103 558 my %data = $self->gather_data('get', $name, $filename, $loop);
104  
105 # Then open the file and try to read the data from it
106 %data = ($self->fetch_from_file($filename), %data)
107 558 ▶ unless exists $data{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data{$name} 137 421 558
108  
109 # If not found in the file, calculate from other fields
110 %data = (%data, $self->gather_data('calculate', $name, $filename, $loop))
111 558 ▶ unless exists $data{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $data{$name} 66 492 558
112  
113 558 return %data;
114 }
115  
116 #----------------------------------------------------------------------
117 # Look in the file for the data
118  
119 125 ▶sub fetch_from_file {
Subroutine fetch_from_file: called CC=2 SCAR=6.9
Pod undocumented
120 125 my ($self, $filename) = @_;
121  
122 125 my (%metadata, %content);
123 125 my $text = fio_read_page($filename);
124 125 ▶ return () unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 12 113 125
125  
126 113 my $section = $self->fetch_sections($text);
127  
128 # First look in the metadata and then the content
129 113 %metadata = $self->fetch_metadata($section->{metadata});
130 113 %content = $self->fetch_content($section->{body});
131  
132 113 return (%content, %metadata);
133 }
134  
135 #----------------------------------------------------------------------
136 # Fetch metadata directly from file
137  
138 59 ▶sub fetch_metadata {
Subroutine fetch_metadata: called CC=1 SCAR=0.0
Pod undocumented
139 59 my ($self, $metadata_block) = @_;
140  
141 59 my %metadata = nt_parse_almost_yaml_string($metadata_block);
142 59 return %metadata;
143 }
144  
145 #----------------------------------------------------------------------
146 # Split text into metadata and content sections
147  
148 60 ▶sub fetch_sections {
Subroutine fetch_sections: called CC=3 SCAR=11.0
Pod undocumented
149 60 my ($self, $text) = @_;
150  
151 60 my %section;
152 60 my $divider = qr(\.{3,}\s*\n);
153  
154 60 ▶ if ($text =~ /^$divider/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^$divider/m) { } 25 35 60
155 25 my @sections = split($divider, $text, 2);
156 25 $section{body} = $sections[1];
157 25 $section{metadata} = $sections[0];
158  
159 } else {
160 35 $section{body} = $text;
161 35 $section{metadata} = '';
162 }
163  
164 60 ▶ die "Could not fetch body of text\n" unless $section{body};
Branch: 1 branch
BranchTrueFalseTotal
unless $section{"body"} 0 60 60
165 60 $section{body} = $self->fetch_as_html($section{body});
166 60 return \%section;
167 }
168  
169 #----------------------------------------------------------------------
170 # Return author's name in sortable order
171  
172 29 ▶sub format_author {
Subroutine format_author: called CC=3 SCAR=11.0
Pod undocumented
173 29 my ($self, $sorted_order, $author) = @_;
174  
175 29 ▶ if ($sorted_order) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sorted_order) 1 28 29
176 1 $author = lc($author);
177  
178 # no need to sort if last name is first
179 1 ▶ if ($author !~ /^\w+,/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($author =~ /^\w+,/) 1 0 1
180 # put last name first
181 1 $author =~ s/[^\w\s]//g;
182 1 my @names = split(' ', $author);
183 1 my $name = pop(@names);
184 1 unshift(@names, $name);
185  
186 1 $author = join(' ', @names);
187 }
188 }
189  
190 29 return $author;
191 }
192  
193 #----------------------------------------------------------------------
194 # Return title in sortable order
195  
196 109 ▶sub format_title {
Subroutine format_title: called CC=2 SCAR=6.9
Pod undocumented
197 109 my ($self, $sorted_order, $title) = @_;
198  
199 109 ▶ if ($sorted_order) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sorted_order) 14 95 109
200 14 $title = lc($title);
201 14 $title =~ s/^a\s+//;
202 14 $title =~ s/^the\s+//;
203 }
204  
205 109 return $title;
206 }
207  
208 #----------------------------------------------------------------------
209 # Return the base directory defined in this object
210  
211 0 ▶sub get_base_directory {
Subroutine get_base_directory: not called CC=1 SCAR=6.9
Pod undocumented
212 0 my ($self) = @_;
213 0 return $self->{base_directory};
214 }
215  
216 17 1;
217  
218 =pod
219  
220 =encoding utf-8
221  
222 =head1 NAME
223  
224 App::Followme::FileData
225  
226 =head1 SYNOPSIS
227  
228 use App::Followme::FileData;
229 my $data = App::Followme::FileData->new();
230 my $html = App::Followme::Template->new('example.htm', $data);
231  
232 =head1 DESCRIPTION
233  
234 This module extracts data from a file. It assumes the file is a text
235 file with the metadata in a nested text block preceding a content block
236 that is in html format or convertible to html format. The two sections
237 are separated by a line of three or more dots. These asumptions can be
238 overriden by overriding the methods in the class. Like the other data
239 classes, this class is normally called by the Template class and not
240 directly by user code.
241  
242 =head1 METHODS
243  
244 All data classes are first instantiated by calling new and the object
245 created is passed to a template object. It calls the build method with an
246 argument name to retrieve that data item, though for the sake of
247 efficiency, all the data are read from the file at once.
248  
249 =head1 VARIABLES
250  
251 The file metadata class can evaluate the following variables. When passing
252 a name to the build method, the sigil should not be used.
253  
254 =over 4
255  
256 =item $body
257  
258 The main content of the file.
259  
260 =item $description
261  
262 A one sentence description of the file contents
263  
264 =item $date
265  
266 The creation date of a file. The display format is controlled by the
267 configuration variable, data_format
268  
269 =item $summary
270  
271 A summary of the file's contents, by default the first paragraph.
272  
273 =item $title
274  
275 The title of the file, either derived from the content or the file metadata.
276  
277 =back
278  
279 =head1 CONFIGURATION
280  
281 This class has the following configuration variable:
282  
283 =over 4
284  
285 =item base_directory
286  
287 The top directory containing the files to be processed
288  
289 =item title_template
290  
291 A set of html tags that bound the title in the file's contents. The default
292 value is "<h1></h1>".
293  
294 =back
295  
296 =head1 LICENSE
297  
298 Copyright (C) Bernie Simon.
299  
300 This library is free software; you can redistribute it and/or modify
301 it under the same terms as Perl itself.
302  
303 =head1 AUTHOR
304  
305 Bernie Simon E<lt>bernie.simon@gmail.comE<gt>
306  
307 =cut
« lib/App/Followme.pm ↑ Summary lib/App/Followme/UploadLocal.pm »