App-PFT

 view release on metacpan or  search on metacpan

bin/pft-gen-rss  view on Meta::CPAN

=head1 SEE ALSO

L<pft(1)>, L<pft-init(1)>

=cut

use strict;
use warnings;
use utf8;
use v5.16;

use Carp;
use Digest::MD5;
use HTML::Escape qw/escape_html/;
use PFT::Tree;
use POSIX qw/strftime/;
use Encode;
use Encode::Locale;

use File::Spec::Functions qw/catfile/;
use File::Path qw/make_path/;
use File::Basename qw/dirname/;

use Pod::Usage;
use Getopt::Long;
Getopt::Long::Configure qw/bundling/;

GetOptions(
    'help|h!'       => sub {
        pod2usage
            -exitval => 1,
            -verbose => 2,
            -input => App::PFT::help_of 'gen-rss',
    },
) or exit 1;

my $tree = eval{ PFT::Tree->new } || do {
    say STDERR $@ =~ s/ at.*$//rs;
    exit 2
};

my $conf = eval{ $tree->conf } || do {
    say STDERR 'Configuration error: ', $@ =~ s/ at.*$//rs;
    exit 3;
};

my $digest = Digest::MD5->new;

my $site_title  = $conf->{site}{title};
my $site_url    = $conf->{site}{url};
my $feed_path   = $conf->{site}{feed}{path} || "feed.rss";
my $feed_url    = "$site_url/$feed_path";
my $encoding    = $conf->{site}{encoding};
my $description = $conf->{site}{feed}{description} || "News from $site_title";
my $length      = $conf->{site}{feed}{length} || 10;

my $outfile;
do {
    my $path = catfile($tree->dir_build, $feed_path);

    make_path encode(locale_fs => dirname($path));
    open($outfile, ">:encoding($encoding)", encode(locale_fs => $path))
        or die "opening $path $!";
    select $outfile;
};

sub node_to_href {
    my $node = shift;
    confess unless $node;

    join '/', $site_url, do {
        my $hdr = $node->header;
        my $k = $node->content_type;

        if ($k =~ /::Blog$/) {(
            'blog',
            sprintf('%04d-%02d', $hdr->date->y, $hdr->date->m),
            sprintf('%02d-%s.html', $hdr->date->d, $hdr->slug),
        )} elsif ($k =~ /::Month$/) {(
            'blog',
            sprintf('%04d-%02d.html', $hdr->date->y, $hdr->date->m),
        )} elsif ($k =~ /::Page$/) {(
            'pages',
            $hdr->slug . '.html',
        )} elsif ($k =~ /::Tag$/) {(
            'tags',
            $hdr->slug . '.html',
        )} elsif ($k =~ /::Picture$/) {(
            'pics',
            $node->content->relpath
        )} elsif ($k =~ /::Attachment$/) {(
            'attachments',
            $node->content->relpath
        )} else { die $k }
    }
}

print <<"END";
<?xml version="1.0" encoding="$encoding"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
 <channel>
  <title>$site_title</title>
  <link>$site_url</link>
  <generator>App::PFT</generator>
  <description>$description</description>
  <atom:link href="$feed_url" rel="self" type="application/rss+xml"/>
END


my $first = 1;
foreach my $node ($tree->content_map->blog_recent($length)) {
    my $guid;
    my $pubDate;

    $digest->add($node->title);
    $guid = $digest->hexdigest;
    $digest->reset;

    $pubDate = do {
        my($y, $m, $d) = @{$node->date};
        strftime("%a, %d %b %Y %H:%M:%S %z", 0, 0, 0, $d, $m, $y - 1900)
    };



( run in 0.458 second using v1.01-cache-2.11-cpan-ceb78f64989 )