File Coverage

blib/lib/Data/Microformat/hFeed/hEntry.pm
Criterion Covered Total %
statement 59 103 57.2
branch 27 52 51.9
condition 3 8 37.5
subroutine 12 13 92.3
pod 4 4 100.0
total 105 180 58.3


line stmt bran cond sub pod time code
1             package Data::Microformat::hFeed::hEntry;
2              
3 5     5   2516 use strict;
  5         10  
  5         186  
4 5     5   28 use base qw(Data::Microformat);
  5         6  
  5         831  
5 5     5   3647 use Data::Microformat::hCard;
  5         14  
  5         162  
6 5     5   4222 use DateTime::Format::W3CDTF;
  5         967904  
  5         16235  
7              
8 4     4 1 13 sub class_name { "hentry" }
9 4     4 1 37 sub singular_fields { qw(id base geo title content summary modified issued link author) }
10 4     4 1 16 sub plural_fields { qw(tags) }
11              
12             sub from_tree {
13 4     4 1 94 my $class = shift;
14 4         9 my $tree = shift;
15 4         8 my $url = shift;
16 4         8 my @entries;
17 4         49 foreach my $entry_tree ($tree->look_down('class', qr/hentry/)) {
18 4         773 push @entries, $class->_convert($entry_tree, $url);
19             }
20 4 100       65 return wantarray ? @entries : $entries[0];
21             }
22              
23             sub _convert {
24 4     4   10 my $class = shift;
25 4         10 my $tree = shift;
26 4         8 my $url = shift;
27 4         38 my $entry = $class->new;
28 4         33 $entry->{_no_dupe_keys} = 1;
29 4 50       19 $entry->base($url) if $url;
30 4         8 my $title;
31             my $id;
32             $tree->look_down(sub {
33 66     66   845 my $bit = shift;
34 66   50     180 my $entry_class = $bit->attr('class') || $bit->attr('rel') || $bit->tag || return 0;
35             # TODO
36             # Summary and Content as html
37              
38              
39 66 50       1299 if (!$entry_class) {
    100          
    100          
    50          
    100          
    100          
    100          
    100          
    100          
    50          
    100          
    100          
40 0         0 return 0;
41             } elsif (_match($entry_class, 'hentry')) {
42 4         19 $entry->id($bit->attr('id'));
43             } elsif (_match($entry_class, 'entry-title')) {
44 4         22 $entry->title($bit->as_text);
45 4         22 $entry->id($bit->attr('id'));
46             } elsif ($entry_class =~ m!^h\d!) {
47 0 0       0 $title = $bit->as_text unless defined $title;
48 0 0       0 $id = $bit->attr('id') unless defined $id;
49             } elsif (_match($entry_class, 'entry-summary')) {
50 4         38 $entry->summary($class->_get_child_html_from_element($bit));
51             } elsif (_match($entry_class, 'entry-content')) {
52 4         21 $entry->content($class->_get_child_html_from_element($bit));
53             } elsif (_match($entry_class, 'published')) {
54 4         16 $entry->issued(_do_date($bit));
55             } elsif (_match($entry_class, 'modified')) {
56 4         15 $entry->modified(_do_date($bit));
57             } elsif (_match($entry_class,'vcard')) {
58 2         21 my $card = Data::Microformat::hCard->from_tree($bit);
59 2         18 $entry->author($card);
60             } elsif (_match($entry_class, 'geo')) {
61 0         0 $bit->detach;
62 0         0 my $geo = Data::Microformat::geo->from_tree($bit, $url);
63 0         0 $entry->geo($geo);
64             } elsif (_match($entry_class, 'bookmark')) {
65 12         82 $entry->link($class->_url_decode($bit->attr('href')));
66             } elsif (_match($entry_class, 'tag')) {
67 8         34 $entry->tags($bit->as_text);
68             } else {
69             # print "Unknown class $entry_class\n";
70             }
71 66         559 return 0;
72 4         63 });
73             # Use first h tag we found if title or id isn't set
74 4 50       111 $entry->title($title) if defined $title;
75 4 50       18 $entry->title($id) if defined $id;
76 4         9 $entry->{_no_dupe_keys} = 0;
77 4         24 return $entry;
78             }
79              
80             sub _do_date {
81 8     8   13 my $element = shift;
82 8   50     29 my $title = $element->attr('title') || return;
83 8         113 $title =~ s!(([+-])(\d+))$!$2.join(":", grep {length} split /(\d\d)/, $3)!e; # egregious hack to work with malformed dates
  0         0  
  0         0  
84 8         15 my $dt = eval { DateTime::Format::W3CDTF->parse_datetime($title) };
  8         64  
85 8 50       3108 print "Aargh: $@\n".caller()."\n" if $@;
86 8         83 return $dt;
87             }
88              
89             sub _match {
90 486   50 486   988 my $field = shift || return 0;
91 486         505 my $target = shift;
92 486         10443 return $field =~ m!(^|\s*)$target(\s*|$)!i;
93             }
94              
95             sub _to_hcard_elements {
96 0     0     my $entry = shift;
97              
98 0           my $root = HTML::Element->new('div', class => 'hentry entry');
99              
100             # id
101 0 0         $root->attr('id', $entry->id) if $entry->id;
102              
103              
104             # title
105 0 0         if ($entry->title) {
106 0           my $title = HTML::Element->new('h3', class => 'entry-title');
107 0 0         if ($entry->link) {
108 0           my $a = HTML::Element->new('a', href => $entry->link, rel => 'bookmark', title => $entry->title );
109 0           $a->push_content($entry->title);
110 0           $title->push_content($a);
111             } else {
112 0           $title->push_content($entry->title);
113             }
114 0           $root->push_content($title);
115             }
116              
117             # summary
118             # content
119 0           foreach my $attr (qw(summary content)) {
120 0 0         next unless $entry->$attr;
121 0           my $div = HTML::Element->new('div', class => "entry-$attr");
122 0           $div->push_content($entry->$attr);
123 0           $root->push_content($div);
124             }
125              
126             # post info
127 0           my $post_info = HTML::Element->new('ul');
128 0           my $saw_something = 0;
129             # published
130             # modified
131 0           foreach my $date (qw(issued modified)) {
132 0   0       my $val = $entry->$date || next;
133 0           $saw_something++;
134 0 0         my $name = ($date eq 'issued') ? 'published' : $date;
135              
136 0           my $li = HTML::Element->new('li');
137 0           $li->push_content(ucfirst($name).": ");
138 0           my $abbr = HTML::Element->new('abbr', class => $name, title => DateTime::Format::W3CDTF->format_datetime($entry->$date));
139 0           $abbr->push_content($entry->$date->strftime("%B %d, %Y"));
140 0           $li->push_content($abbr);
141 0           $post_info->push_content($li);
142             }
143              
144             # author
145             # geo
146 0           foreach my $attr (qw(author geo)) {
147 0 0         next unless $entry->$attr;
148 0           $saw_something++;
149 0           my $li = HTML::Element->new('li');
150 0           $li->push_content($entry->$attr->_to_hcard_elements);
151 0           $post_info->push_content($li);
152             }
153              
154 0 0         $root->push_content($post_info) if $saw_something;
155 0           return $root;
156             }
157              
158             1;
159              
160             __END__