Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions lib/perron/site/builder/feeds/atom.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<generator uri="<%= @configuration.url %>" version="<%= Perron::VERSION %>">Perron</generator>
<id><%= current_feed_url %></id>
<title><%= config.title.presence || @configuration.site_name %></title>
<subtitle><%= config.description.presence || @configuration.site_description %></subtitle>
<link href="<%= current_feed_url %>" rel="self" type="application/atom+xml"/>
<link href="<%= @configuration.url %>" rel="alternate" type="text/html"/>
<updated><%= resources.first&.published_at&.iso8601 || Time.current.iso8601 %></updated>

<% feed_author = config.author || { name: @configuration.site_name, email: "noreply@#{URI.parse(@configuration.url).host}" } %>
<author>
<% if feed_author[:name] %><name><%= feed_author[:name] %></name><% end %>
<% if feed_author[:email] %><email><%= feed_author[:email] %></email><% end %>
</author>

<% resources.each do |resource| %>
<entry>
<id><%= url_for_resource(resource) || "#{@configuration.url}/posts/#{resource.id}" %></id>
<title><%= resource.metadata.title %></title>
<link href="<%= url_for_resource(resource) %>" rel="alternate" type="text/html"/>
<published><%= resource.published_at&.iso8601 %></published>
<updated><%= (resource.metadata.updated_at || resource.published_at)&.iso8601 %></updated>

<% entry_author = author(resource); if entry_author %>
<author>
<% if entry_author.name %><name><%= entry_author.name %></name><% end %>
<% if entry_author.email %><email><%= entry_author.email %></email><% end %>
</author>
<% end %>

<% base_url = url_for_resource(resource) %>
<% if base_url %>
<content type="html" xml:base="<%= base_url %>"><![CDATA[<%= Perron::Markdown.render(resource.content) %>]]></content>
<% else %>
<content type="html"><![CDATA[<%= Perron::Markdown.render(resource.content) %>]]></content>
<% end %>

<% resource.metadata.tags&.each do |tag| %>
<category term="<%= tag %>"/>
<% end %>
</entry>
<% end %>
</feed>
75 changes: 5 additions & 70 deletions lib/perron/site/builder/feeds/atom.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# frozen_string_literal: true

require "nokogiri"
require "perron/site/builder/feeds/author"
require "perron/site/builder/feeds/template"

module Perron
module Site
class Builder
class Feeds
class Atom
include Feeds::Author
include Feeds::Template

def initialize(collection:)
@collection = collection
Expand All @@ -18,58 +19,10 @@ def initialize(collection:)
def generate
return if resources.empty?

Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
xml.feed(xmlns: "http://www.w3.org/2005/Atom") do
xml.generator "Perron", uri: @configuration.url, version: Perron::VERSION
xml.id current_feed_url
xml.title feed_configuration.title.presence || @configuration.site_name
xml.subtitle feed_configuration.description.presence || @configuration.site_description
xml.link href: current_feed_url, rel: "self", type: "application/atom+xml"
xml.link href: @configuration.url, rel: "alternate", type: "text/html"
xml.updated resources.first&.published_at&.iso8601 || Time.current.iso8601
template = find_template("atom")
return unless template

feed_author = feed_configuration.author || {
name: @configuration.site_name,
email: "noreply@#{URI.parse(@configuration.url).host}"
}

xml.author do
xml.name feed_author[:name] if feed_author[:name]
xml.email feed_author[:email] if feed_author[:email]
end

resources.each do |resource|
xml.entry do
xml.title resource.metadata.title
xml.link href: url_for_resource(resource), rel: "alternate", type: "text/html"
xml.published resource.published_at&.iso8601
xml.updated (resource.metadata.updated_at || resource.published_at)&.iso8601
xml.id url_for_resource(resource) || "#{@configuration.url}/posts/#{resource.id}"

if (entry_author = author(resource))
xml.author do
xml.name entry_author.name if entry_author.name
xml.email entry_author.email if entry_author.email
end
end

if (base_url = url_for_resource(resource))
xml.content :type => "html", "xml:base" => base_url do
xml.cdata(Perron::Markdown.render(resource.content))
end
else
xml.content type: "html" do
xml.cdata(Perron::Markdown.render(resource.content))
end
end

resource.metadata.tags&.each do |tag|
xml.category term: tag
end
end
end
end
end.to_xml
render(template, feed_configuration)
end

private
Expand All @@ -81,24 +34,6 @@ def resources
.reverse
.take(feed_configuration.max_items)
end

def url_for_resource(resource)
routes
.polymorphic_url(resource, **@configuration.default_url_options.merge(ref: feed_configuration.ref))
.delete_suffix("?ref=")
rescue
nil
end

def current_feed_url
path = feed_configuration.path || "feed.atom"

URI.join(@configuration.url, path).to_s
end

def feed_configuration = @collection.configuration.feeds.atom

def routes = Rails.application.routes.url_helpers
end
end
end
Expand Down
19 changes: 19 additions & 0 deletions lib/perron/site/builder/feeds/json.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<%= {
generator: "Perron (#{Perron::VERSION})",
version: "https://jsonfeed.org/version/1.1",
home_page_url: @configuration.url,
title: config.title.presence || @configuration.site_name,
description: config.description.presence || @configuration.site_description,

items: resources.map { |resource|
item_author = author.call(resource)
{
id: resource.id,
url: url_for_resource.call(resource),
date_published: resource.published_at&.iso8601,
title: resource.metadata.title,
authors: (item_author && item_author.name ? [{ name: item_author.name, email: item_author.email, url: item_author.url, avatar: item_author.avatar }.compact] : nil),
content_html: Perron::Markdown.render(resource.content)
}.compact
}
}.to_json %>
50 changes: 7 additions & 43 deletions lib/perron/site/builder/feeds/json.rb
Original file line number Diff line number Diff line change
@@ -1,75 +1,39 @@
# frozen_string_literal: true

require "json"
require "perron/site/builder/feeds/author"
require "perron/site/builder/feeds/template"

module Perron
module Site
class Builder
class Feeds
class Json
include Feeds::Author
include Feeds::Template

def initialize(collection:)
@collection = collection
@configuration = Perron.configuration
end

def generate
return nil if resources.empty?
return if resources.empty?

hash = {
generator: "Perron (#{Perron::VERSION})",
version: "https://jsonfeed.org/version/1.1",
home_page_url: @configuration.url,
title: feed_configuration.title.presence || @configuration.site_name,
description: feed_configuration.description.presence || @configuration.site_description,
items: resources.filter_map { jsonify(it) }
}
template = find_template("json")
return unless template

JSON.pretty_generate hash
render(template, feed_configuration)
end

private

def resources
@resources ||= @collection.resources
@resource ||= @collection.resources
.reject { it.metadata.feed == false }
.sort_by { it.metadata.published_at || it.metadata.updated_at || Time.current }
.reverse
.take(feed_configuration.max_items)
end

def jsonify(resource)
{
id: resource.id,
url: url_for_resource(resource),
date_published: resource.published_at&.iso8601,
authors: authors(resource),
title: resource.metadata.title,
content_html: Perron::Markdown.render(resource.content)
}.compact
end

def url_for_resource(resource)
routes
.polymorphic_url(resource, **@configuration.default_url_options.merge(ref: feed_configuration.ref))
.delete_suffix("?ref=")
rescue
nil
end

def authors(resource)
author = author(resource)

return nil unless author&.name

[{name: author.name, email: author.email, url: author.url, avatar: author.avatar}.compact].presence
end

def feed_configuration = @collection.configuration.feeds.json

def routes = Rails.application.routes.url_helpers
end
end
end
Expand Down
28 changes: 28 additions & 0 deletions lib/perron/site/builder/feeds/rss.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<generator>Perron (<%= Perron::VERSION %>)</generator>
<link><%= @configuration.url %></link>
<title><%= config.title.presence || @configuration.site_name %></title>
<description><%= config.description.presence || @configuration.site_description %></description>

<% resources.each do |resource| %>
<item>
<guid isPermaLink="false"><%= resource.id %></guid>

<% resource_url = url_for_resource(resource) %>
<% if resource_url %>
<link><%= resource_url %></link>
<% end %>

<pubDate><%= resource.published_at&.rfc822 %></pubDate>
<% author = author(resource); if author && author.email %>
<author><%= author.name ? "#{author.email} (#{author.name})" : author.email %></author>
<% end %>
<title><%= resource.metadata.title %></title>

<description><![CDATA[<%= Perron::Markdown.render(resource.content) %>]]></description>
</item>
<% end %>
</channel>
</rss>
46 changes: 5 additions & 41 deletions lib/perron/site/builder/feeds/rss.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# frozen_string_literal: true

require "nokogiri"
require "perron/site/builder/feeds/author"
require "perron/site/builder/feeds/template"

module Perron
module Site
class Builder
class Feeds
class Rss
include Feeds::Author
include Feeds::Template

def initialize(collection:)
@collection = collection
Expand All @@ -18,35 +19,10 @@ def initialize(collection:)
def generate
return if resources.empty?

Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
xml.rss(:version => "2.0", "xmlns:atom" => "http://www.w3.org/2005/Atom") do
xml.channel do
xml.generator "Perron (#{Perron::VERSION})"
xml.title feed_configuration.title.presence || @configuration.site_name
xml.description feed_configuration.description.presence || @configuration.site_description
xml.link @configuration.url
template = find_template("rss")
return unless template

resources.each do |resource|
xml.item do
xml.guid resource.id, isPermaLink: false

if (resource_url = url_for_resource(resource))
xml.link resource_url
end

xml.pubDate(resource.published_at&.rfc822)

if (author = author(resource)) && author.email
xml.author author.name ? "#{author.email} (#{author.name})" : author.email
end

xml.title resource.metadata.title
xml.description { xml.cdata(Perron::Markdown.render(resource.content)) }
end
end
end
end
end.to_xml
render(template, feed_configuration)
end

private
Expand All @@ -58,18 +34,6 @@ def resources
.reverse
.take(feed_configuration.max_items)
end

def url_for_resource(resource)
routes
.polymorphic_url(resource, **@configuration.default_url_options.merge(ref: feed_configuration.ref))
.delete_suffix("?ref=")
rescue
nil
end

def feed_configuration = @collection.configuration.feeds.rss

def routes = Rails.application.routes.url_helpers
end
end
end
Expand Down
Loading
Loading