Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 0786a56ba2
Fetching contributors…

Cannot retrieve contributors at this time

48 lines (42 sloc) 1.348 kb
class Blog < Sinatra::Base
get '/:year/:month/:day/:article' do
filename = params.values_at("year", "month", "day", "article").join('_') + '.txt'
@article = Article.find_by_filename( filename )
haml :show_article
end
get '/stylesheet.css' do
content_type 'text/css', charset: 'utf-8'
sass :stylesheet
end
get '/rss' do
@articles = Article.all
content_type 'application/rss+xml'
haml(:rss, :format => :xhtml, :escape_html => true, :layout => false)
end
get '/' do
@articles = Article.all
haml :index
end
end
class Article
def self.all
articles = []
Dir.glob('articles/*.txt') do |f|
articles << find_by_filename( f.split('/').last )
end
articles.sort_by(&:date).reverse
end
def self.find_by_filename( filename )
file_hash = filename.gsub('.txt', '').split('_')
article = OpenStruct.new
year = file_hash.shift
month = file_hash.shift
day = file_hash.shift
article.date = Date.civil(year.to_i, month.to_i, day.to_i)
article.permalink = "/" + [ year, month, day,
URI.escape(file_hash.join('_'))].join('/')
article.title = file_hash.join(' ').capitalize
article.content = File.open("articles/#{filename}").read
article
end
end
Jump to Line
Something went wrong with that request. Please try again.