mirror of
https://github.com/samsonjs/samhuri.net.git
synced 2026-03-25 09:05:47 +00:00
add a JSON feed
This commit is contained in:
parent
5955792247
commit
1ad8a6759f
8 changed files with 81 additions and 22 deletions
|
|
@ -27,14 +27,15 @@ lock
|
|||
|
||||
function main() {
|
||||
echo "* compile rss feed"
|
||||
compile_rss
|
||||
compile_feeds
|
||||
|
||||
echo "* harp compile $BLOG_DIR $TARGET"
|
||||
rm -rf "$TARGET/*" "$TARGET/.*"
|
||||
"$HARP" compile "$BLOG_DIR" "$TARGET"
|
||||
|
||||
# clean up temporary feed
|
||||
# clean up temporary feeds
|
||||
rm $BLOG_DIR/public/feed.xml
|
||||
rm $BLOG_DIR/public/feed.json
|
||||
|
||||
echo "* munge html files to make them available without an extension"
|
||||
munge_html
|
||||
|
|
@ -48,8 +49,8 @@ function main() {
|
|||
delete_lock_file
|
||||
}
|
||||
|
||||
function compile_rss() {
|
||||
ruby -w $DIR/rss.rb $BLOG_DIR/public
|
||||
function compile_feeds() {
|
||||
ruby -w $DIR/feeds.rb $BLOG_DIR/public
|
||||
}
|
||||
|
||||
function munge_html() {
|
||||
|
|
|
|||
|
|
@ -9,10 +9,20 @@ require 'json'
|
|||
require 'rdiscount'
|
||||
require 'mustache'
|
||||
|
||||
class Hash
|
||||
def compact
|
||||
h = {}
|
||||
each_pair do |k, v|
|
||||
h[k] = v unless v.nil?
|
||||
end
|
||||
h
|
||||
end
|
||||
end
|
||||
|
||||
def main
|
||||
dir = ARGV.shift.to_s
|
||||
unless File.directory? dir
|
||||
puts 'usage: rss.rb <dir>'
|
||||
puts 'usage: feeds.rb <dir>'
|
||||
exit 1
|
||||
end
|
||||
b = Blag.new dir
|
||||
|
|
@ -25,18 +35,20 @@ class Blag
|
|||
self.new(dir).generate!
|
||||
end
|
||||
|
||||
def initialize dir, num_posts = 10
|
||||
def initialize dir, index_posts = 10, feed_posts = 30
|
||||
@dir = dir
|
||||
@num_posts = num_posts
|
||||
@index_posts = index_posts
|
||||
@feed_posts = feed_posts
|
||||
end
|
||||
|
||||
def generate!
|
||||
generate_latest_posts_json
|
||||
generate_rss
|
||||
generate_json_feed
|
||||
end
|
||||
|
||||
def generate_latest_posts_json
|
||||
data['posts'] = latest_posts.map do |post|
|
||||
data['posts'] = latest_posts(@index_posts).map do |post|
|
||||
{
|
||||
title: post['title'],
|
||||
date: post['date'],
|
||||
|
|
@ -56,7 +68,11 @@ class Blag
|
|||
File.write(rss_file, feed_xml.target!)
|
||||
end
|
||||
|
||||
def latest_posts(n = @num_posts)
|
||||
def generate_json_feed
|
||||
File.write(json_feed_file, feed_json)
|
||||
end
|
||||
|
||||
def latest_posts(n)
|
||||
posts.first(n)
|
||||
end
|
||||
|
||||
|
|
@ -81,12 +97,13 @@ class Blag
|
|||
post['title'] = "→ #{post['title']}" if post['type'] == :link
|
||||
post['relative_url'] = relative_url
|
||||
post['url'] = root_url + post['relative_url']
|
||||
post['external_url']
|
||||
post['content'] = content
|
||||
post['body'] = RDiscount.new(post['content'], :smart).to_html
|
||||
post['rfc822'] = Time.at(post['timestamp']).rfc822
|
||||
post['body'] = RDiscount.new(content, :smart).to_html
|
||||
post['time'] = Time.at(post['timestamp'])
|
||||
post
|
||||
end
|
||||
end.flatten.compact.sort_by { |p| -p['timestamp'] }.first(@num_posts)
|
||||
end.flatten.compact.sort_by { |p| -p['timestamp'] }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -113,11 +130,11 @@ private
|
|||
@data ||= JSON.parse File.read(data_file)
|
||||
end
|
||||
|
||||
def rss_template(type)
|
||||
def feed_template(type)
|
||||
if type == :post
|
||||
@post_rss_template ||= File.read(File.join('templates', 'post.rss.html'))
|
||||
@post_rss_template ||= File.read(File.join('templates', 'post.feed.html'))
|
||||
elsif type == :link
|
||||
@link_rss_template ||= File.read(File.join('templates', 'link.rss.html'))
|
||||
@link_rss_template ||= File.read(File.join('templates', 'link.feed.html'))
|
||||
else
|
||||
raise 'unknown post type: ' + type
|
||||
end
|
||||
|
|
@ -127,8 +144,10 @@ private
|
|||
File.join @dir, 'feed.xml'
|
||||
end
|
||||
|
||||
def rss_html post
|
||||
Mustache.render rss_template(post['type']), post: post
|
||||
def feed_html post
|
||||
html = Mustache.render feed_template(post['type']), post: post
|
||||
# this is pretty disgusting
|
||||
html.gsub 'src="/', "src=\"#{root_url}/"
|
||||
end
|
||||
|
||||
def feed_xml
|
||||
|
|
@ -142,13 +161,13 @@ private
|
|||
xml.title title
|
||||
xml.description subtitle
|
||||
xml.link root_url
|
||||
xml.pubDate posts.first['rfc822']
|
||||
xml.pubDate posts.first['time'].rfc822
|
||||
|
||||
posts.each do |post|
|
||||
latest_posts(@feed_posts).each do |post|
|
||||
xml.item do
|
||||
xml.title post['title']
|
||||
xml.description rss_html(post)
|
||||
xml.pubDate post['rfc822']
|
||||
xml.description feed_html(post)
|
||||
xml.pubDate post['time'].rfc822
|
||||
xml.author post['author']
|
||||
xml.link post['link'] || post['url']
|
||||
xml.guid post['url']
|
||||
|
|
@ -159,6 +178,44 @@ private
|
|||
xml
|
||||
end
|
||||
|
||||
def json_feed_file
|
||||
File.join @dir, 'feed.json'
|
||||
end
|
||||
|
||||
def feed_json
|
||||
JSON.pretty_generate(build_json_feed)
|
||||
end
|
||||
|
||||
def build_json_feed
|
||||
{
|
||||
version: "https://jsonfeed.org/version/1",
|
||||
title: title,
|
||||
home_page_url: root_url,
|
||||
feed_url: "#{root_url}/feed.json",
|
||||
author: {
|
||||
url: "https://samhuri.net",
|
||||
name: "Sami J. Samhuri",
|
||||
avatar: "#{root_url}/images/me.jpg"
|
||||
},
|
||||
icon: "#{root_url}/images/apple-touch-icon-300.png",
|
||||
favicon: "#{root_url}/images/apple-touch-icon-80.png",
|
||||
items: latest_posts(@feed_posts).map do |post|
|
||||
{
|
||||
title: post['title'],
|
||||
date_published: post['time'].to_datetime.rfc3339,
|
||||
id: post['url'],
|
||||
url: post['url'],
|
||||
external_url: post['link'],
|
||||
author: {
|
||||
name: post['author']
|
||||
},
|
||||
content_html: feed_html(post),
|
||||
tags: post['tags']
|
||||
}.compact
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
main if $0 == __FILE__
|
||||
|
|
@ -48,7 +48,8 @@
|
|||
<% } -%>
|
||||
|
||||
<link rel="author" type="text/plain" href="/humans.txt">
|
||||
<link rel="alternate" type="application/rss+xml" href="/feed.xml" title="<%= site %>">
|
||||
<link rel="alternate" type="application/rss+xml" href="<%= url %>/feed.xml" title="<%= site %>">
|
||||
<link rel="alternate" type="application/json" href="<%= url %>/feed.json" title="<%= site %>">
|
||||
|
||||
<link rel="dns-prefetch" href="https://gist.github.com">
|
||||
|
||||
|
|
|
|||
BIN
public/images/apple-touch-icon-300.png
Normal file
BIN
public/images/apple-touch-icon-300.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 7.8 KiB |
BIN
public/images/apple-touch-icon-80.png
Normal file
BIN
public/images/apple-touch-icon-80.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 4.5 KiB |
BIN
public/images/me.jpg
Normal file
BIN
public/images/me.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 22 KiB |
Loading…
Reference in a new issue