App-WRT

 view release on metacpan or  search on metacpan

lib/App/WRT.pm  view on Meta::CPAN

=cut

sub feed_print_json_recent {
  my ($self, $count) = @_;

  $count //= $self->{feed_length};

  return $self->feed_print_json(
    $self->{entries}->recent_days($count)
  );
}

=item feed_print(@entries)

Return an Atom feed for the given list of entries.

Requires XML::Atom::SimpleFeed.

XML::Atom::SimpleFeed will give bogus results with input that's just a string
of octets (I think) if it contains characters outside of US-ASCII.  In order to
spit out clean UTF-8 output, we need to use Encode::decode() to flag entry
content as UTF-8 / represent it internally as a string of characters.  There's
a whole lot I don't really understand about how this is handled in Perl, and it
may be a locus of bugs elsewhere in wrt, but for now I'm just dealing with it
here.

Some references on that:

=over

=item * L<https://github.com/ap/XML-Atom-SimpleFeed/issues/2>

=item * L<https://rt.cpan.org/Public/Bug/Display.html?id=19722>

=item * L<https://cpanratings.perl.org/dist/XML-Atom-SimpleFeed>

=item * L<perlunitut>

=back

=cut

sub feed_print {
  my $self = shift;
  my (@entries) = @_;

  my $feed_url = $self->{url_root} . $self->{feed_alias};

  my ($first_entry_file, $first_entry_url) = $self->root_locations($entries[0]);

  my $feed = XML::Atom::SimpleFeed->new(
    -encoding => 'UTF-8',
    title     => $self->{title_prefix} . "::" . $self->{feed_alias},
    subtitle  => $self->{description},
    link      => $self->{url_root},
    link      => { rel => 'self', href => $feed_url, },
    icon      => $self->{favicon_url},
    author    => $self->{author},
    id        => $self->{url_root},
    generator => 'App::WRT.pm / XML::Atom::SimpleFeed',
    updated   => iso_date(get_mtime($first_entry_file)),
  );

  foreach my $entry (@entries) {
    my $content = $self->{html_cache}{$entry};
    if ( $self->{metadata_html_cache}{$entry} ) {
      $content .= '<div class=entry-metadata>'
                . $self->{metadata_html_cache}{$entry}
                . '</div>';
    }

    my ($entry_file, $entry_url) = $self->root_locations($entry);

    $feed->add_entry(
      title   => $self->get_title($entry),
      link    => $entry_url,
      id      => $entry_url,
      content => $content,
      updated => iso_date(get_mtime($entry_file)),
    );
  }

  # Note: This output should be served with
  # Content-type: application/atom+xml
  #
  # I'm not, to be frank, entirely clear on why the decode() call here is
  # necessary:
  return decode('UTF-8', $feed->as_string);
}

=item feed_print_json

Like feed_print(), but for JSON Feed.

L<https://jsonfeed.org/>

=cut

sub feed_print_json {
  my ($self, @entries) = @_;
  my ($first_entry_file, $first_entry_url) = $self->root_locations($entries[0]);

  my $json_feed_url = $self->{url_root} . $self->{feed_alias} . '.json';

  my $user_comment = "This feed allows you to read the posts from this site in"
    . " any feed reader that supports the JSON Feed format. To "
    . "add this feed to your reader, copy the following URL — "
    . "$json_feed_url — and add it your reader.";

  my $feed = JSON::Feed->new(
    user_comment  => $user_comment,
    title         => $self->{title_prefix} . "::" . $self->{feed_alias},
    home_page_url => $self->{url_root},
    feed_url      => $json_feed_url,
    description   => $self->{description},
    author        => +{ name => $self->{author}, },
  );

  if (defined $self->{favicon_url}) {
    $feed->set('favicon', $self->{favicon_url});
  }

  foreach my $entry (@entries) {
    my $content = $self->{html_cache}{$entry};
    if ($self->{metadata_html_cache}{$entry}) {
      $content .= '<div class=entry-metadata>'
                . $self->{metadata_html_cache}{$entry}
                . '</div>';
    }

    my ($entry_file, $entry_url) = $self->root_locations($entry);

    $feed->add_item(
      id             => $entry_url,
      title          => $self->get_title($entry),
      content_html   => $content,
      date_modified  => rfc_3339_date(get_mtime($entry_file)),
      date_published => rfc_3339_date(get_mtime($entry_file)),
    );



( run in 2.801 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )