#!/usr/bin/perl -T # Copyright (C) 2007 王亮 # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public Licence as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public Licence for more details. use strict; use warnings; use YAML::Syck; use HTML::Pager; use CGI; use File::HomeDir; use File::Spec::Functions; use HTML::Template; use Encode; my $feedman_home = catdir(File::HomeDir->my_home, ".feedman"); my $entriesfile = catfile($feedman_home, "last_update_entries.yaml"); my $tmplfile = catfile($feedman_home, "pager.tmpl"); my %entries; load_entries(); my @entry_list; get_entry_list(); my $num_of_entries = scalar @entry_list; my $tmpl = HTML::Template->new(filename=>$tmplfile); my $get_entries_callback = sub { my ($offset, $rows) = @_; my @current_entries; for my $e ($offset .. $rows + $offset - 1) { last if $e == $num_of_entries; push @current_entries, $entry_list[$e]; } return \@current_entries; }; my $query = CGI->new; my $pager = HTML::Pager->new(query=> $query, get_data_callback => $get_entries_callback, rows => $num_of_entries, page_size => 10, template => $tmpl); print $query->header(-charset=>'UTF-8'), $query->start_html(-title=>'Feedman', -head=>$query->meta({-http_equiv => 'Content-Type', -content => 'text/html; charset=UTF-8'})), $query->h1('Feedman'), encode_utf8($pager->output), $query->end_html; sub load_entries { local $YAML::Syck::ImplicitUnicode = 1; die "$entriesfile not found.\n" unless -e $entriesfile and -f _; my $data = LoadFile($entriesfile); %entries = %$data; } sub get_entry_list { for my $feed (keys %entries) { my $entries = $entries{$feed}; for my $entry (@$entries) { my $updated = (exists $entry->{modified}) ? $entry->{modified} : $entry->{issued}; push @entry_list, { title => $entry->{title}, link => $entry->{link}, updated => $updated, }; } } }