Created
May 22, 2017 01:47
-
-
Save fracai/2ffc713fa76c313bbceef523f213ae4f to your computer and use it in GitHub Desktop.
A nanoc blogging helper for producing a JSON feed. See https://jsonfeed.org/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
module Nanoc::Helpers | |
# @see http://nanoc.ws/doc/reference/helpers/#blogging | |
module Blogging_JSON | |
include Nanoc::Helpers::Blogging | |
class JSONFeedBuilder < AtomFeedBuilder | |
def build | |
require 'json' | |
JSON[build_for_feed()] | |
end | |
def build_for_feed() | |
root_url = @config[:base_url] + '/' | |
json = {} | |
json['version'] = 'https://jsonfeed.org/version/1' | |
# Add primary attributes | |
json['home_page_url'] = root_url | |
json['title'] = title | |
## Add date | |
#xml.updated(updated.__nanoc_to_iso8601_time) | |
# Add links | |
json['feed_url'] = feed_url | |
# Add author information | |
json['author'] = {} | |
json['author']['name'] = author_name | |
json['author']['url'] = author_uri | |
# Add icon and logo | |
json['icon'] = icon if icon | |
json['favicon'] = logo if logo | |
# Add articles | |
json['items'] = [] | |
sorted_relevant_articles.each do |a| | |
json['items'] << build_for_article(a) | |
end | |
json | |
end | |
def build_for_article(a) | |
# Get URL | |
url = url_for(a) | |
return if url.nil? | |
json = {} | |
# Add primary attributes | |
json['id'] = atom_tag_for(a) | |
json['title'] = a[:title] | |
# Add dates | |
json['date_published'] = attribute_to_time(a[:created_at]).__nanoc_to_iso8601_time | |
json['date_modified'] = attribute_to_time(a[:updated_at] || a[:created_at]).__nanoc_to_iso8601_time | |
# Add specific author information | |
if a[:author_name] || a[:author_uri] | |
json['author']['name'] = a[:author_name] || author_name | |
json['url'] = a[:author_uri] || author_uri | |
end | |
# Add link | |
json['url'] = url | |
# Add content | |
summary = excerpt_proc.call(a) | |
json['content_html'] = content_proc.call(a) | |
json | |
end | |
end | |
# @option params [Number] :limit | |
# @option params [Array] :articles | |
# @option params [Boolean] :preserve_order | |
# @option params [Proc] :content_proc | |
# @option params [Proc] :excerpt_proc | |
# @option params [String] :title | |
# @option params [String] :author_name | |
# @option params [String] :author_uri | |
# @option params [String] :icon | |
# @option params [String] :logo | |
# | |
# @return [String] | |
def json_feed(params = {}) | |
require 'builder' | |
# Create builder | |
builder = JSONFeedBuilder.new(@config, @item) | |
# Fill builder | |
builder.limit = params[:limit] || 5 | |
builder.relevant_articles = params[:articles] || articles || [] | |
builder.preserve_order = params.fetch(:preserve_order, false) | |
builder.content_proc = params[:content_proc] || ->(a) { a.compiled_content(snapshot: :pre) } | |
builder.excerpt_proc = params[:excerpt_proc] || ->(a) { a[:excerpt] } | |
builder.title = params[:title] || @item[:title] || @config[:title] | |
builder.author_name = params[:author_name] || @item[:author_name] || @config[:author_name] | |
builder.author_uri = params[:author_uri] || @item[:author_uri] || @config[:author_uri] | |
builder.icon = params[:icon] | |
builder.logo = params[:logo] | |
# Run | |
builder.validate | |
builder.build | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment