Created
March 13, 2012 01:15
-
-
Save eng/2025984 to your computer and use it in GitHub Desktop.
Make a local copy of Mephisto posts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> » #{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