File Coverage

lib/App/Followme/WebData.pm
Criterion Covered Total %
statement 53 53 100.0
branch n/a
condition 3 5 60.0
subroutine 14 14 100.0
pod 2 4 50.0
total 72 76 94.7


line stmt bran cond sub pod time code
1             package App::Followme::WebData;
2              
3 12     12   743 use 5.008005;
  12         56  
4 12     12   75 use strict;
  12         33  
  12         344  
5 12     12   64 use warnings;
  12         23  
  12         415  
6 12     12   73 use integer;
  12         23  
  12         90  
7 12     12   427 use lib '../..';
  12         28  
  12         79  
8              
9 12     12   1768 use base qw(App::Followme::FileData);
  12         25  
  12         5139  
10 12     12   90 use App::Followme::FIO;
  12         31  
  12         1061  
11 12     12   75 use App::Followme::Web;
  12         25  
  12         4595  
12              
13             our $VERSION = "2.02";
14              
15             #----------------------------------------------------------------------
16             # Read the default parameter values
17              
18             sub parameters {
19 288     288 1 473 my ($self) = @_;
20              
21             return (
22 288         1011 body_tag => 'primary',
23             metadata_tag => 'meta',
24             extension => '',
25             );
26             }
27              
28             #----------------------------------------------------------------------
29             # Get the html metadata from the page header
30              
31             sub fetch_metadata {
32 55     55 0 113 my ($self, $metadata_block) = @_;
33 55         100 my $metadata = [];
34              
35 55         79 my $global = 0;
36             my $title_parser = sub {
37 55     55   143 my ($metadata, @tokens) = @_;
38 55         144 my $text = web_only_text(@tokens);
39 55         119 push(@$metadata, 'title', $text);
40 55         121 return;
41 55         251 };
42              
43 55         180 web_match_tags('<title></title>', $metadata_block,
44             $title_parser, $metadata, $global);
45              
46 55         112 $global = 1;
47             my $metadata_parser = sub {
48 171     171   348 my ($metadata, @tokens) = @_;
49 171         360 foreach my $tag (web_only_tags(@tokens)) {
50 171         478 push(@$metadata, $tag->{name}, $tag->{content});
51             }
52 171         461 return;
53 55         257 };
54              
55 55         168 web_match_tags('<meta name=* content=*>', $metadata_block,
56             $metadata_parser, $metadata, $global);
57              
58 55         239 my %metadata = @$metadata;
59 55         592 return %metadata;
60             }
61              
62             #----------------------------------------------------------------------
63             # Split text into metadata and content sections
64              
65             sub fetch_sections {
66 55     55 0 143 my ($self, $text) = @_;
67              
68 55         186 my $section = web_parse_sections($text);
69              
70 55         94 my %section;
71 55         101 foreach my $section_name (qw(metadata body)) {
72 110         249 my $tag = $self->{$section_name . '_tag'};
73 110   100     334 $section{$section_name} = $section->{$tag} || '';
74             }
75              
76 55         215 return \%section;
77             }
78              
79             #----------------------------------------------------------------------
80             # Initialize the extension if unset
81              
82             sub setup {
83 72     72 1 142 my ($self) = @_;
84              
85 72   33     188 $self->{extension} ||= $self->{web_extension};
86 72         133 return;
87             }
88              
89             1;
90              
91             =pod
92              
93             =encoding utf-8
94              
95             =head1 NAME
96              
97             App::Followme::WebData - Read metadata from a web file
98              
99             =head1 SYNOPSIS
100              
101             use App::Followme::WebData;
102             my $data = App::Followme::WebData->new();
103             my $html = App::Followme::Template->new('example.htm', $data);
104              
105             =head1 DESCRIPTION
106              
107             This module extracts data from a web page and uses it to build variables from a
108             template.
109              
110             =head1 METHODS
111              
112             All data classes are first instantiated by calling new and the object
113             created is passed to a template object. It calls the build method with an
114             argument name to retrieve that data item, though for the sake of
115             efficiency, all the data are read from the file at once.
116              
117             =head1 VARIABLES
118              
119             This class whatever values are returned in metadata section in the header
120             as well as the title and body extracted from the body section.
121              
122             =head1 CONFIGURATION
123              
124             This class has the following configuration variable:
125              
126             =over 4
127              
128             =item body_tag
129              
130             The name of the section containing the body text. The default value is
131             'primary'.
132              
133             =item metadata_tag
134              
135             The name of the section containing the metadata tags. The default value is
136             'meta'.
137              
138             =item extension
139              
140             The file extension of web pages. The default value is 'html'.
141              
142             =back
143              
144             =head1 LICENSE
145              
146             Copyright (C) Bernie Simon.
147              
148             This library is free software; you can redistribute it and/or modify
149             it under the same terms as Perl itself.
150              
151             =head1 AUTHOR
152              
153             Bernie Simon E<lt>bernie.simon@gmail.comE<gt>
154              
155             =cut