Skip to content

Instantly share code, notes, and snippets.

@practicingruby
Created August 6, 2008 05:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save practicingruby/4177 to your computer and use it in GitHub Desktop.
Save practicingruby/4177 to your computer and use it in GitHub Desktop.
["rubygems","builder","yaml", "fileutils", "redcloth", "erb","uri"].each { |l| require l }
class Blaag
TITLE = "Majestic Sea Creature"
DOMAIN = "blog.majesticseacreature.com"
AUTHOR = "Gregory Brown"
DESCRIPTION = "Technical Ramblings from #{AUTHOR}"
BASEDIR = File.dirname(File.expand_path(__FILE__)) + "/../"
def self.generate
entries = Dir["#{Blaag::BASEDIR}/entries/**/*"].
reject { |f| File.directory?(f) }
entries.map! { |e| Entry.load(e) }
@blaag = Blaag.new(entries)
File.open("#{Blaag::BASEDIR}/www/index.html","w") { |f|
f << layout { @blaag.to_html }
}
File.open("#{Blaag::BASEDIR}/www/rss.xml","w") { |f|
f << @blaag.to_rss
}
@blaag.entries.each do |e|
FileUtils.mkdir_p "#{Blaag::BASEDIR}/www/archives/#{e.archive}"
File.open("#{Blaag::BASEDIR}/www/archives/#{e.archive}/#{e.file}.html","w") { |f|
f << layout { e.to_html }
}
end
build_archive_indices(@blaag.entries)
end
def self.build_archive_indices(entries)
archives = Hash.new { |h,k| h[k] = [] }
entries.each do |e|
archives[e.archive] << e
end
archives.each do |arch, entries|
@top_entries = entries.sort_by { |e| e.published_date }.reverse
File.open("#{Blaag::BASEDIR}/www/archives/#{arch}/index.html","w") { |f|
f << layout { ProcessTemplate("blog") }
}
end
end
attr_reader :entries
def initialize(entries)
@entries = entries
@top_entries = entries.sort_by { |e| e.published_date }.reverse[0..14]
end
def to_html
ProcessTemplate("blog")
end
def to_rss
xml = Builder::XmlMarkup.new
xml.instruct!
xml.rss :version => "2.0" do
xml.channel do
xml.title TITLE
xml.link "http://#{DOMAIN}/"
xml.description DESCRIPTION
xml.language "en-us"
@entries.each do |entry|
xml.item do
xml.title entry.title
xml.description entry.description
xml.author AUTHOR
xml.pubDate entry.published_date
xml.link entry.url
xml.guid entry.url
end
end
end
end
end
class Entry
def self.registry
@registry ||= YAML.load_file("#{Blaag::BASEDIR}/data/registry.yml")
end
def self.update_registry(filename,mtime)
registry[filename] ||= mtime
File.open("#{Blaag::BASEDIR}/data/registry.yml","w") do |f|
f << @registry.to_yaml
end
end
def self.load(filename)
file = File.open(filename)
update_registry(filename,file.mtime)
options = parse(file.read)
options[:published_date] = registry[filename]
options[:archive] = File.dirname(filename) =~ /.*\/(.*)\/?.*/ && $1
options[:url] = "http://#{Blaag::DOMAIN}/archives/#{options[:archive]}/#{File.basename(filename)}.html"
options[:file] = File.basename(filename)
entry = Entry.new(options)
end
def self.parse(entry)
entry =~ /=title(.*)=description(.*)=entry(.*)/m
{ :title => $1.strip, :description => $2.strip, :entry => $3.strip }
end
def initialize(options={})
@title = options[:title]
@description = options[:description]
@entry = RedCloth.new(options[:entry]).to_html
@archive = options[:archive]
@published_date = options[:published_date]
@url = options[:url]
@file = options[:file]
end
attr_reader :title, :description, :entry, :archive, :published_date, :url,
:file
def to_html
ProcessTemplate("entry")
end
def related
link = URI.escape(url)
"<a href='http://blogsearch.google.com/blogsearch?hl=en&q=link%3A#{link}&btnG=Search+Blogs'>" +
"Responses</a>"
end
end
def self.layout(&block)
ProcessTemplate("layout",&block)
end
end
def ProcessTemplate(file)
ERB.new(File.read("#{Blaag::BASEDIR}/templates/#{file}.html.erb")).result(binding)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment