diff --git a/bin/compile.sh b/bin/compile.sh index f76cce6..f7e9899 100755 --- a/bin/compile.sh +++ b/bin/compile.sh @@ -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() { diff --git a/bin/rss.rb b/bin/feeds.rb similarity index 59% rename from bin/rss.rb rename to bin/feeds.rb index 2afd936..f4ebe3e 100755 --- a/bin/rss.rb +++ b/bin/feeds.rb @@ -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 ' + puts 'usage: feeds.rb ' 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__ diff --git a/public/_layout.ejs b/public/_layout.ejs index 9df8205..4ad9b56 100644 --- a/public/_layout.ejs +++ b/public/_layout.ejs @@ -48,7 +48,8 @@ <% } -%> - + + diff --git a/public/images/apple-touch-icon-300.png b/public/images/apple-touch-icon-300.png new file mode 100644 index 0000000..e943b8e Binary files /dev/null and b/public/images/apple-touch-icon-300.png differ diff --git a/public/images/apple-touch-icon-80.png b/public/images/apple-touch-icon-80.png new file mode 100644 index 0000000..cd1d46d Binary files /dev/null and b/public/images/apple-touch-icon-80.png differ diff --git a/public/images/me.jpg b/public/images/me.jpg new file mode 100644 index 0000000..ddd4c02 Binary files /dev/null and b/public/images/me.jpg differ diff --git a/templates/link.rss.html b/templates/link.feed.html similarity index 100% rename from templates/link.rss.html rename to templates/link.feed.html diff --git a/templates/post.rss.html b/templates/post.feed.html similarity index 100% rename from templates/post.rss.html rename to templates/post.feed.html