Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add Atom feed. #2

Merged
merged 4 commits into from

2 participants

@jasoncodes

Once upon a time I realised I didn’t see any of Lindsey’s blog posts in my feed reader. Almost immediately I attempted to rectify the situation but was told in no uncertain terms that no feed could be found! “Surely this could not be. I must rectify this!”, I thought. So, here we are.

Your blog does not currently track when posts are updated (I have an updated entry in my YAML front matter) so any changes you make to existing posts may not show up in people’s feed readers.

Additionally, since only dates are stored, the timestamps are outputted at midnight in the server’s time zone. I can’t remember if Heroku uses US/East or UTC. IMO, this is not a big deal (my blog has the same issue). You could offset the timestamps in the feed if you have a more suitable time of day to report.

@LindseyB LindseyB merged commit 579437b into LindseyB:master
@LindseyB
Owner

You did this better than I did.

Merged. :heart:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 3, 2013
  1. @jasoncodes
  2. @jasoncodes
  3. @jasoncodes

    Lazily generate formatted post date and content.

    jasoncodes authored
    The archive doesn't need content at all and
    the feed only needs content for recent posts.
  4. @jasoncodes

    Add Atom feed.

    jasoncodes authored
This page is out of date. Refresh to see the latest.
View
1  Gemfile
@@ -8,3 +8,4 @@ gem 'thin'
gem 'rake'
gem 'haml'
gem 'redcarpet'
+gem 'builder'
View
2  Gemfile.lock
@@ -2,6 +2,7 @@ GEM
remote: http://rubygems.org/
specs:
backports (2.6.4)
+ builder (3.1.4)
daemons (1.1.6)
eventmachine (0.12.10)
haml (3.1.4)
@@ -35,6 +36,7 @@ PLATFORMS
ruby
DEPENDENCIES
+ builder
haml
rake
redcarpet
View
21 blog.rb
@@ -17,18 +17,20 @@ def title
end
def latest_posts
- ret = []
- Dir.glob("posts/*.md") do |post|
+ posts = Dir.glob("posts/*.md").map do |post|
post = post[/posts\/(.*?).md$/,1]
- p = Post.new(post)
- ret << {:id => post, :title => p.title, :date => p.formatted_date, :url => "/posts/#{post}"}
+ Post.new(post)
end
- ret.sort{|x,y| y[:id] <=> x[:id]}
+ posts.sort_by(&:name).reverse
end
def partial(page, options={})
haml "_#{page}".to_sym, options.merge!(:layout => false)
end
+
+ def url_base
+ "http://#{request.host_with_port}"
+ end
end
get '/index.php' do
@@ -41,7 +43,7 @@ def partial(page, options={})
end
get '/' do
- source = Post.new(latest_posts[0][:id])
+ source = latest_posts.first
@content = source.content
@title = source.title
@date = source.date
@@ -65,4 +67,11 @@ def partial(page, options={})
haml :archive
end
+
+ get '/feed' do
+ @posts = latest_posts.first(10)
+
+ content_type 'application/atom+xml'
+ builder :feed
+ end
end
View
26 post.rb
@@ -20,13 +20,13 @@ def image(link, title, alt_text)
end
class Post
- attr_reader :content
+ attr_reader :name
attr_reader :title
attr_reader :date
attr_reader :slug
- attr_reader :formatted_date
def initialize(name)
+ @name = name
begin
content = File.read("posts/#{name}.md")
rescue
@@ -37,7 +37,7 @@ def initialize(name)
unless match.nil?
meta_data = match[1]
- content = match[2]
+ @content_raw = match[2]
meta_data = YAML.load(meta_data)
@@ -45,13 +45,21 @@ def initialize(name)
@author = meta_data["author"]
end
- @date = name.match(/^\d{4}-\d{2}-\d{2}/)
- @slug = name[/#{@date}-(.*)$/,1]
+ date_str = name.match(/^\d{4}-\d{2}-\d{2}/).to_s
+ @date = Date.parse(date_str)
+ @slug = name[/#{date_str}-(.*)$/,1]
- @formatted_date = Date.parse(@date.to_s).strftime("%d %B %Y")
+ end
+
+ def content
+ @content ||= begin
+ renderer = Post::Renderer.new(@slug)
+ r = Redcarpet::Markdown.new(renderer, :fenced_code_blocks => true)
+ r.render(@content_raw)
+ end
+ end
- renderer = Post::Renderer.new(@slug)
- r = Redcarpet::Markdown.new(renderer, :fenced_code_blocks => true)
- @content = r.render(content)
+ def formatted_date
+ @formatted_date ||= @date.strftime("%d %B %Y")
end
end
View
8 views/archive.haml
@@ -16,11 +16,9 @@
- latest_posts.each do |post|
%li
%i{:class => "icon-circle-arrow-right"}
- %a{:href => post[:url],
- :title => post[:title],
- :alt=> post[:title]}
- #{post[:title]}
+ %a{:href => "/posts/#{post.name}", :title => post.title, :alt => post.title}
+ = post.title
%div.date
- #{post[:date]}
+ = post.formatted_date
View
27 views/feed.builder
@@ -0,0 +1,27 @@
+xml.instruct!
+xml.feed :xmlns => 'http://www.w3.org/2005/Atom' do
+ xml.title TITLE
+ xml.link :href => "#{url_base}/feed", :rel => :self, :type => 'application/atom+xml'
+ xml.link :href => "#{url_base}/archive", :rel => :alternate, :type => 'text/html'
+ xml.id "#{url_base}/"
+ xml.updated @posts.first.date.to_time.xmlschema
+
+ @posts.each do |post|
+ xml.entry do
+ url = "#{url_base}/posts/#{post.name}"
+ xml.title post.title, :type => :html
+ xml.link :href => url, :rel => :alternate, :type => 'text/html'
+ xml.published post.date.to_time.xmlschema
+ xml.updated post.date.to_time.xmlschema
+
+ xml.author do
+ xml.name TITLE
+ end
+
+ xml.id url
+ xml.content :type => :html, 'xml:base' => url do
+ xml.cdata! post.content
+ end
+ end
+ end
+end
View
1  views/layout.haml
@@ -16,6 +16,7 @@
%link{:href=>"/stylesheets/sunburst.css", :rel=>"stylesheet", :type=>"text/css"}
%link{:href=>"/stylesheets/font-awesome.css", :rel=>"stylesheet", :type=>"text/css"}
%link{:href=>"/stylesheets/style.css", :rel=>"stylesheet", :type=>"text/css"}
+ %link{:href=>"/feed", :rel=>"alternate", :type=>"application/atom+xml", :title=>title}
%title #{title}
%body
Something went wrong with that request. Please try again.