Skip to content

Instantly share code, notes, and snippets.

@eng
Created March 13, 2012 01:15
Show Gist options
  • Save eng/2025984 to your computer and use it in GitHub Desktop.
Save eng/2025984 to your computer and use it in GitHub Desktop.
Make a local copy of Mephisto posts
require 'rubygems'
require 'sequel'
require 'fileutils'
require 'yaml'
require 'redcloth'
require 'rdiscount'
class Mephisto
QUERY = "SELECT id, permalink, body, published_at, title, filter \
FROM contents \
WHERE type = 'Article' \
AND published_at IS NOT NULL \
ORDER BY published_at DESC"
def self.process(dbname, user, pass, host = 'localhost')
db = Sequel.mysql(dbname, :user => user,
:password => pass,
:host => host,
:encoding => 'utf8')
FileUtils.mkdir_p "posts"
header = File.open("header.html", "rb").read
footer = File.open("footer.html", "rb").read
db[QUERY].each do |post|
title = post[:title]
slug = post[:permalink]
date = post[:published_at]
content = post[:body].to_s
filter = post[:filter]
name = [date.year, date.month, date.day, slug].join('-') + ".html"
data = case filter
when 'textile_filter'
RedCloth.new(content).to_html
else # markdown
RDiscount.new(content).to_html
end
File.open("posts/#{name}", "w") do |f|
f.puts header
f.puts "<h1><a href=\"../index.html\">Softies on Rails</a> &raquo; #{title}</h1>"
f.puts data
f.puts footer
end
end
end
end
Mephisto.process('softies', 'user', 'secret')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment