Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# This is derived version of
require 'rubygems'
require 'sequel'
require 'fileutils'
require 'yaml'
require 'ya2yaml'
module Jekyll
module WordPress
QUERY = "select post_title, post_name, post_date, post_content, post_excerpt, ID, guid from wp_posts where post_status = 'publish' and post_type = 'post'"
def self.process(dbname, user, pass, host = 'localhost')
db = Sequel.mysql(dbname, :user => user, :password => pass, :host => host, :encoding => 'utf8')
FileUtils.mkdir_p "_posts"
db[QUERY].each do |post|
# Get required fields and construct Jekyll compatible name
title = post[:post_title]
slug = post[:post_name]
date = post[:post_date]
content = post[:post_content]
name = "" % [date.year, date.month,, slug]
tag_query = "SELECT * FROM wp_term_relationships r, wp_term_taxonomy ta, wp_terms t where r.object_id=#{post[:ID]} and r.term_taxonomy_id=ta.term_taxonomy_id and ta.taxonomy='post_tag' and ta.term_id=t.term_id"
tags = db[tag_query].map { |tag| tag[:slug] }
data = {
'layout' => 'post',
'title' => title.to_s,
'date' => date,
'tags' => tags,
}.delete_if { |k,v| v.nil? || v == ''}.ya2yaml(:syck_compatible => true)
# Write out the data and content to file"_posts/#{name}", "w") do |f|
f.puts data
f.puts "---"
f.puts content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment