root/trunk/plagger/lib/Plagger/Plugin/Subscription/XOXO.pm

Revision 687 (checked in by miyagawa, 14 years ago)

Use XPath rather than recursive tree manipulation to find XOXO, thanks to youpy
http://subtech.g.hatena.ne.jp/youpy/20060505/p1

Line 
1 package Plagger::Plugin::Subscription::XOXO;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use HTML::TreeBuilder::XPath;
6 use Plagger::Util;
7 use URI;
8
9 sub register {
10     my($self, $context) = @_;
11
12     $context->register_hook(
13         $self,
14         'subscription.load' => \&load,
15     );
16 }
17
18 sub load {
19     my($self, $context) = @_;
20     my $uri = URI->new($self->conf->{url})
21         or $context->error("config 'url' is missing");
22
23     $self->load_xoxo($context, $uri);
24 }
25
26 sub load_xoxo {
27     my($self, $context, $uri) = @_;
28
29     my $xhtml = Plagger::Util::load_uri($uri, $self);
30     my $tree = HTML::TreeBuilder::XPath->new;
31     $tree->parse($xhtml);
32     $tree->eof;
33
34     $self->find_xoxo($tree);
35 }
36
37 sub find_xoxo {
38     my($self, $tree) = @_;
39
40     for my $child ($tree->findnodes('//ul[@class="xoxo" or @class="subscriptionlist"]//a')) {
41         my $href  = $child->attr('href') or next;
42         my $title = $child->attr('title') || $child->as_text;
43
44         my $feed = Plagger::Feed->new;
45         $feed->url($href);
46         $feed->title($title);
47
48         Plagger->context->subscription->add($feed);
49     }
50 }
51
52 1;
53
54 __END__
55
56 =head1 NAME
57
58 Plagger::Plugin::Subscription::XOXO - Subscription list with XOXO microformats
59
60 =head1 SYNOPSIS
61
62   - module: Subscription::XOXO
63     config:
64       url: http://example.com/mySubscriptions.xhtml
65
66 =head1 DESCRIPTION
67
68 This plugin creates Subscription by fetching remote XOXO file by HTTP
69 or locally (with C<file://> URI). The parser is implemented in really
70 a dumb way and only supports extracting URL (I<href>) and title from A
71 links inside XOXO C<ul> or C<ol> tags.
72
73 =head1 AUTHOR
74
75 Tatsuhiko Miyagawa
76
77 =head1 SEE ALSO
78
79 L<Plagger>, L<http://microformats.org/wiki/xoxo>
80
81 =cut
Note: See TracBrowser for help on using the browser.