Merge branch 'master' of github.com:dpavlin/HTML5TV
[HTML5TV.git] / lib / HTML5TV / hCalendar.pm
1 package HTML5TV::hCalendar;
2
3 use warnings;
4 use strict;
5
6 use File::Slurp;
7 use HTML::TreeBuilder;
8 use Data::Dump qw/dump/;
9
10 sub new {
11         my $class = shift;
12         my $path = shift || die "need path to hCalendar";
13
14         my $tree = HTML::TreeBuilder->new;
15         $tree->parse_file($path);
16
17         my $self = {
18                 path => $path,
19                 tree => $tree,
20         };
21         bless $self, $class;
22         return $self;
23 }
24
25 # we don't want DESTROY to fallback into AUTOLOAD
26 sub DESTROY {}
27
28 our $AUTOLOAD;
29 sub AUTOLOAD {
30         my $self = shift;
31
32     my $class = $AUTOLOAD;
33     $class =~ s/.*://;
34
35 #warn "XX $class\n";
36
37         if ( my $vevent = $self->{tree}->look_down( class => 'vevent' ) ) {
38                 if ( my $text = $vevent->look_down( class => $class )->as_trimmed_text ) {
39                         return $text;
40                 } else {
41                         die "can't find vevent.$class in ", $self->{path};
42                 }
43         } else {
44                 die "can't find vevent in ", $self->{path};
45         }
46
47 }
48
49 sub as_HTML {
50         my $self = shift;
51         my $el = shift;
52         my $vevent = $self->{tree}->look_down( class => 'vevent' )
53                 ->unshift_content( $el )
54                 ->as_HTML('<>&')
55         ;
56 }
57
58 sub dtstart_iso {
59         my $self = shift;
60         $self->{tree}->look_down( class => 'vevent' )->look_down( class => 'dtstart' )->attr('title');
61 }
62
63 1;