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

Revision 681 (checked in by miyagawa, 14 years ago)
  • New plugin: Subscription::XOXO
  • Moved load_uri method to Util ... should be refactored to methods of some class
Line 
1 package Plagger::Plugin::Subscription::XOXO;
2 use strict;
3 use base qw( Plagger::Plugin );
4
5 use HTML::TreeBuilder;
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
31     my $tree = HTML::TreeBuilder->new;
32     $tree->parse($xhtml);
33     $tree->eof;
34
35     $self->find_xoxo($tree);
36 }
37
38 sub find_xoxo {
39     my($self, $tree) = @_;
40
41     for my $child ($tree->content_list) {
42         next unless ref $child;
43         if ($child->tag eq 'ul' || $child->tag eq 'ol') {
44             my $class = $child->attr('class') || '';
45             if ($class eq 'xoxo' || $class eq 'subscriptionlist') {
46                 $self->find_list($child);
47             }
48         } else {
49             $self->find_xoxo($child);
50         }
51     }
52 }
53
54 sub find_list {
55     my($self, $tree) = @_;
56
57     for my $child ($tree->content_list) {
58         next unless ref $child;
59         if ($child->tag eq 'a') {
60             my $href  = $child->attr('href') or next;
61             my $title = $child->attr('title') || $child->as_text;
62
63             my $feed = Plagger::Feed->new;
64             $feed->url($href);
65             $feed->title($title);
66
67             Plagger->context->subscription->add($feed);
68         } else {
69             $self->find_list($child);
70         }
71     }
72 }
73
74 1;
75
76 __END__
77
78 =head1 NAME
79
80 Plagger::Plugin::Subscription::XOXO - Subscription list with XOXO microformats
81
82 =head1 SYNOPSIS
83
84   - module: Subscription::XOXO
85     config:
86       url: http://example.com/mySubscriptions.xhtml
87
88 =head1 DESCRIPTION
89
90 This plugin creates Subscription by fetching remote XOXO file by HTTP
91 or locally (with C<file://> URI). The parser is implemented in really
92 a dumb way and only supports extracting URL (I<href>) and title from A
93 links inside XOXO C<ul> or C<ol> tags.
94
95 =head1 AUTHOR
96
97 Tatsuhiko Miyagawa
98
99 =head1 SEE ALSO
100
101 L<Plagger>, L<http://microformats.org/wiki/xoxo>
102
103 =cut
Note: See TracBrowser for help on using the browser.