From a64c7e26410ba60f9a574e283d0b24883f6d2bb2 Mon Sep 17 00:00:00 2001
From: Sami Samhuri
Date: Sat, 18 Dec 2010 20:36:04 -0800
Subject: [PATCH] add rss feed for blog, overhaul blog generator
---
Makefile | 7 +-
blog.rb | 187 ++++++++++++------
blog.sh | 11 --
...emacs-to-develop-mojo-apps-for-webos.html} | 9 +-
...working-with-c-style-structs-in-ruby.html} | 11 +-
...-18_basics-of-the-mach-o-file-format.html} | 11 +-
..._a-preview-of-mach-o-file-generation.html} | 11 +-
...2010-11-04_37signals-chalk-dissected.html} | 9 +-
blog/index.html | 21 +-
blog/posts.json | 1 +
blog/rss | 1 +
templates/blog/index.html | 3 +-
templates/blog/post.html | 1 +
13 files changed, 179 insertions(+), 104 deletions(-)
delete mode 100755 blog.sh
rename blog/{using-emacs-to-develop-mojo-apps-for-webos.html => 2009-11-21_using-emacs-to-develop-mojo-apps-for-webos.html} (92%)
rename blog/{working-with-c-style-structs-in-ruby.html => 2010-01-17_working-with-c-style-structs-in-ruby.html} (90%)
rename blog/{basics-of-the-mach-o-file-format.html => 2010-01-18_basics-of-the-mach-o-file-format.html} (93%)
rename blog/{a-preview-of-mach-o-file-generation.html => 2010-01-20_a-preview-of-mach-o-file-generation.html} (84%)
rename blog/{37signals-chalk-dissected.html => 2010-11-04_37signals-chalk-dissected.html} (97%)
create mode 100644 blog/posts.json
create mode 100644 blog/rss
diff --git a/Makefile b/Makefile
index 00fd2c2..ea82688 100644
--- a/Makefile
+++ b/Makefile
@@ -6,14 +6,14 @@ MIN_JAVASCRIPTS=assets/blog.min.js assets/gitter.min.js assets/jquery-serializeO
assets/request.min.js assets/showdown.min.js assets/storage-polyfill.min.js assets/store.min.js \
assets/strftime.min.js assets/tmpl.min.js
-POSTS=$(shell echo _blog/*.html)
+POSTS=$(shell echo _blog/published/*.html)
all: proj blog combine
proj: projects.json templates/proj/index.html templates/proj/proj/index.html
./build.js
-blog: _blog/posts.json templates/blog/index.html templates/blog/post.html $(POSTS)
+blog: _blog/blog.json templates/blog/index.html templates/blog/post.html $(POSTS)
@echo
./blog.rb _blog blog
@@ -36,10 +36,11 @@ publish: publish_blog publish_proj index.html
publish assets
publish blog
publish proj
+ scp blog/posts.json bohodev.net:discussd/posts.json
clean:
rm -rf proj/*
rm -rf blog/*
rm assets/*.min.js
-.PHONY: blog
+.PHONY: proj
diff --git a/blog.rb b/blog.rb
index 3edc8cf..973e786 100755
--- a/blog.rb
+++ b/blog.rb
@@ -1,69 +1,144 @@
#!/usr/bin/env ruby
+require 'time'
require 'rubygems'
+require 'builder'
require 'json'
-require 'rdiscount'
require 'mustache'
+require 'rdiscount'
-srcdir = ARGV.shift.to_s
-destdir = ARGV.shift.to_s
-
-unless File.directory?(srcdir) && File.directory?(destdir)
- puts 'usage: blog.rb '
- exit 1
+def main
+ srcdir = ARGV.shift.to_s
+ destdir = ARGV.shift.to_s
+ unless File.directory?(srcdir) && File.directory?(destdir)
+ puts 'usage: blog.rb '
+ exit 1
+ end
+ Blag.go! srcdir, destdir
end
-template = File.read(File.join('templates', 'blog', 'post.html'))
+class Blag
+ def self.go! src, dest
+ self.new(src, dest).generate!
+ end
-# read posts
-posts_file = File.join(srcdir, 'posts.json')
-Posts = JSON.parse(File.read(posts_file))
-posts = Posts['published'].map do |filename|
- lines = File.readlines(File.join(srcdir, filename))
- post = { :filename => filename }
- loop do
- line = lines.shift.strip
- m = line.match(/(\w+):/)
- if m && param = m[1].downcase
- post[param.to_sym] = line.sub(Regexp.new('^' + param + ':\s*', 'i'), '').strip
- elsif line.match(/^----\s*$/)
- lines.shift while lines.first.strip.empty?
- break
- else
- puts "ignoring unknown header: #{line}"
+ def initialize src, dest
+ @src = src
+ @dest = dest
+ read_blog
+ end
+
+ def generate!
+ generate_posts
+ generate_index
+ generate_rss
+ generate_posts_json
+ end
+
+ def generate_index
+ template = File.read(File.join('templates', 'blog', 'index.html'))
+ # generate landing page
+ index_template = File.read(File.join('templates', 'blog', 'index.html'))
+ index_html = Mustache.render(index_template, { :posts => posts,
+ :post => posts.first,
+ :previous => posts[1],
+ :filename => posts.first[:filename],
+ :comments => posts.first[:comments]
+ })
+ File.open(File.join(@dest, 'index.html'), 'w') {|f| f.puts(index_html) }
+ end
+
+ def generate_posts
+ template = File.read(File.join('templates', 'blog', 'post.html'))
+ posts.each_with_index do |post, i|
+ post[:html] = Mustache.render(template, { :title => post[:title],
+ :post => post,
+ :previous => i < posts.length - 1 && posts[i + 1],
+ :next => i > 0 && posts[i - 1],
+ :filename => post[:filename],
+ :comments => post[:comments]
+ })
+ File.open(File.join(@dest, post[:filename]), 'w') {|f| f.puts(post[:html]) }
end
end
- post[:content] = lines.join
- post[:body] = RDiscount.new(post[:content]).to_html
- # comments on by default
- post[:comments] = true if post[:comments].nil?
- post
+
+ def generate_posts_json
+ json = JSON.generate({ :published => posts.map {|p| p[:filename]} })
+ File.open(File.join(@dest, 'posts.json'), 'w') { |f| f.puts(json) }
+ end
+
+ def generate_rss
+ File.open(File.join(@dest, 'rss'), 'w') { |f| f.puts(rss.to_s) }
+ end
+
+ def posts
+ prefix = File.join(@src, 'published') + '/'
+ @posts ||= Dir[File.join(prefix, '*')].sort.reverse.map do |filename|
+ lines = File.readlines(filename)
+ post = { :filename => filename.sub(prefix, '') }
+ loop do
+ line = lines.shift.strip
+ m = line.match(/(\w+):/)
+ if m && param = m[1].downcase
+ post[param.to_sym] = line.sub(Regexp.new('^' + param + ':\s*', 'i'), '').strip
+ elsif line.match(/^----\s*$/)
+ lines.shift while lines.first.strip.empty?
+ break
+ else
+ puts "ignoring unknown header: #{line}"
+ end
+ end
+ post[:content] = lines.join
+ post[:body] = RDiscount.new(post[:content]).to_html
+ post[:rfc822] = Time.parse(post[:date]).rfc822
+ post[:url] = @url + '/' + post[:filename]
+ # comments on by default
+ post[:comments] = true if post[:comments].nil?
+ post
+ end
+ end
+
+ def rss
+ xml = Builder::XmlMarkup.new
+ xml.instruct! :xml, :version => '1.0'
+ xml.rss :version => '2.0' do
+ xml.channel do
+ xml.title @title
+ xml.description @subtitle
+ xml.link @url
+ xml.pubDate @posts.first[:rfc822]
+
+ posts.each do |post|
+ xml.item do
+ xml.title post[:title]
+ xml.description post[:subtitle]
+ xml.pubDate post[:rfc822]
+ xml.link post[:url]
+ xml.guid post[:url]
+ end
+ end
+ end
+ end
+ xml
+ end
+
+ private
+
+ def blog_file
+ File.join(@src, 'blog.json')
+ end
+
+ def read_blog
+ blog = JSON.parse(File.read(blog_file))
+ @title = blog['title']
+ @subtitle = blog['subtitle']
+ @url = blog['url']
+ end
+
+ def rss_file
+ File.join(@dest, 'rss')
+ end
+
end
-# generate posts
-posts.each_with_index do |post, i|
- post[:html] = Mustache.render(template, { :title => post[:title],
- :post => post,
- :previous => i < posts.length - 1 && posts[i + 1],
- :next => i > 0 && posts[i - 1],
- :filename => post[:filename],
- :comments => post[:comments]
- })
-end
-
-# generate landing page
-index_template = File.read(File.join('templates', 'blog', 'index.html'))
-index_html = Mustache.render(index_template, { :posts => posts,
- :post => posts.first,
- :previous => posts[1],
- :filename => posts.first[:filename],
- :comments => posts.first[:comments]
- })
-
-# write landing page
-File.open(File.join(destdir, 'index.html'), 'w') {|f| f.puts(index_html) }
-
-# write posts
-posts.each do |post|
- File.open(File.join(destdir, post[:filename]), 'w') {|f| f.puts(post[:html]) }
-end
+main if $0 == __FILE__
diff --git a/blog.sh b/blog.sh
deleted file mode 100755
index 8874f01..0000000
--- a/blog.sh
+++ /dev/null
@@ -1,11 +0,0 @@
-#!/bin/sh
-
-if [[ ! -d _blog ]]; then
- git clone git://github.com/samsonjs/blog.git _blog
-else
- cd _blog
- git pull
- cd ..
-fi
-
-./blog.rb _blog
diff --git a/blog/using-emacs-to-develop-mojo-apps-for-webos.html b/blog/2009-11-21_using-emacs-to-develop-mojo-apps-for-webos.html
similarity index 92%
rename from blog/using-emacs-to-develop-mojo-apps-for-webos.html
rename to blog/2009-11-21_using-emacs-to-develop-mojo-apps-for-webos.html
index 719ded1..22d22ac 100644
--- a/blog/using-emacs-to-develop-mojo-apps-for-webos.html
+++ b/blog/2009-11-21_using-emacs-to-develop-mojo-apps-for-webos.html
@@ -3,6 +3,7 @@
Using Emacs to Develop Mojo Apps for WebOS :: samhuri.net
+