Permalink
Browse files

Merge branch 'feed' of github.com:jasoncodes/lindseybieda-blog

Conflicts:
	Gemfile.lock
	blog.rb
  • Loading branch information...
LindseyB committed Feb 15, 2013
2 parents 05c1cb5 + 579437b commit 1045a6b3f1ed90aadac869c55748abe339ef395d
Showing with 63 additions and 49 deletions.
  1. +1 −1 Gemfile.lock
  2. +14 −34 blog.rb
  3. +17 −9 post.rb
  4. +3 −5 views/archive.haml
  5. +27 −0 views/feed.builder
  6. +1 −0 views/layout.haml
View
@@ -2,7 +2,7 @@ GEM
remote: http://rubygems.org/
specs:
backports (2.6.4)
- builder (3.0.4)
+ builder (3.1.4)
daemons (1.1.6)
eventmachine (0.12.10)
haml (3.1.4)
View
48 blog.rb
@@ -16,23 +16,21 @@ def title
"#{TITLE} » #{@title}"
end
- def latest_posts(content = false)
- ret = []
- Dir.glob("posts/*.md") do |post|
+ def latest_posts
+ posts = Dir.glob("posts/*.md").map do |post|
post = post[/posts\/(.*?).md$/,1]
- p = Post.new(post)
- if content
- ret << {:id => post, :title => p.title, :date => p.formatted_date, :url => "/posts/#{post}", :content => p.content}
- else
- ret << {:id => post, :title => p.title, :date => p.formatted_date, :url => "/posts/#{post}"}
- end
+ 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
@@ -45,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
@@ -70,28 +68,10 @@ def partial(page, options={})
haml :archive
end
- get '/rss.xml' do
- @posts = latest_posts(true)
-
- builder do |xml|
- xml.instruct! :xml, :version => '1.0'
- xml.rss :version => "2.0" do
- xml.channel do
- xml.title "Lindsey Bieda"
- xml.description "Lindsey Bieda's blog."
- xml.link "http://rarlindseysmash.com"
-
- @posts.each do |post|
- xml.item do
- xml.title post[:title]
- xml.link "http://rarlindseysmash#{post[:url]}"
- xml.description "<![CDATA[ #{post[:content]} ]]>"
- xml.pubDate Time.parse(post[:date].to_s).rfc822()
- xml.guid "http://rarlindseysmash#{post[:url]}"
- end
- end
- end
- end
- 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,21 +37,29 @@ def initialize(name)
unless match.nil?
meta_data = match[1]
- content = match[2]
+ @content_raw = match[2]
meta_data = YAML.load(meta_data)
@title = meta_data["title"]
@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
@@ -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
@@ -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
@@ -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

0 comments on commit 1045a6b

Please sign in to comment.