Skip to content

Instantly share code, notes, and snippets.

@j-manu
Created November 18, 2011 11:02
Show Gist options
  • Save j-manu/1376174 to your computer and use it in GitHub Desktop.
Save j-manu/1376174 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'sequel'
require 'fileutils'
require 'yaml'
# NOTE: This converter requires Sequel and the MySQL gems.
# The MySQL gem can be difficult to install on OS X. Once you have MySQL
# installed, running the following commands should work:
# $ sudo gem install sequel
# $ sudo gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
module Jekyll
module WordPress
def self.process(dbname, user, pass, host = 'localhost', table_prefix = 'wp_')
db = Sequel.mysql(dbname, :user => user, :password => pass, :host => host, :encoding => 'utf8')
FileUtils.mkdir_p("_posts")
# Reads a MySQL database via Sequel and creates a post file for each
# post in wp_posts that has post_status = 'publish'. This restriction is
# made because 'draft' posts are not guaranteed to have valid dates.
query = "SELECT post_title, \
post_name, \
post_date, \
post_content, \
post_excerpt, \
ID, \
guid \
FROM #{table_prefix}posts \
WHERE post_status = 'publish' AND \
post_type = 'post'"
# Fetch all tags for a given POST ID
tags_query = "select tm.term_id,tm.name from wp_term_relationships tr
inner join wp_term_taxonomy tt on tr.term_taxonomy_id = tt.term_taxonomy_id
inner join wp_terms tm on tm.term_id=tt.term_id
where tr.object_id=%d and tt.taxonomy = 'post_tag'";
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 = "%02d-%02d-%02d-%s.markdown" % [date.year, date.month, date.day,
slug]
tags = []
db[tags_query % post[:ID]].each do |tag|
tags << tag[:name].to_s.gsub('+','').downcase
end
# Process content to rewrite some URLs
baseurl = "http://www.manu-j.com/blog/wp-content/uploads/"
content.gsub!(baseurl,"http://blog.manu-j.com/uploads/")
baseurl = "http://static.manu-j.com/blog/wp-content/uploads/"
content.gsub!(baseurl,"http://blog.manu-j.com/uploads/")
content.scan(/\[TABLE=(\d+)\]/i).each do |table_id|
table_id = table_id.first
table_query = "SELECT row_id, value FROM wp_golfresult
WHERE table_id = #{table_id} ORDER BY row_id"
table = "<table><thead><tr>\n"
previous_row_id = -1
total_column_count = 0
current_column = 0
aligns = {}
widths = {}
align_expand = {'L' => 'left', 'C' => 'center', 'R' => 'right'}
db[table_query].each do |row|
row_id = row[:row_id].to_i
if previous_row_id != row_id
current_column = 0
if previous_row_id == 1
table += "</tr></thead><tbody><tr>\n"
elsif previous_row_id > 1
table += "</tr><tr>\n"
end
end
current_column += 1
case row_id
when -1
aligns[current_column] = align_expand[row[:value]]
when 0
widths[current_column] = row[:value]
when 1
table += " <th align='#{aligns[current_column]}' width='#{widths[current_column]}px'>#{row[:value]}</th>\n"
else
table += " <td align='#{aligns[current_column]}' width='#{widths[current_column]}px'>#{row[:value]}</td>\n"
end
previous_row_id = row_id
end
table += '</tr></tbody></table>'
content.gsub!(/\[TABLE=#{table_id}\]/i,table)
end
# Get the relevant fields as a hash, delete empty fields and convert
# to YAML for the header.
data = {
'layout' => 'post',
'title' => title.to_s,
'excerpt' => post[:post_excerpt].to_s,
'wordpress_id' => post[:ID],
'permalink' => "/#{slug.downcase.gsub(/\s/,'-')}/#{post[:ID]}/",
'tags' => tags,
'date' => date
}.delete_if { |k,v| v.nil? || v == '' }.to_yaml
# Write out the data and content to file
File.open("_posts/#{name}", "w") do |f|
f.puts data
f.puts "---"
f.puts content
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment