Created
September 24, 2011 19:00
-
-
Save stantont/1239706 to your computer and use it in GitHub Desktop.
Modified version of lib/jekyll/migrators/drupal.rb
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' | |
# 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 Drupal | |
# 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 node.nid, node.title, node_revisions.body, node.created, node.status, f.name as format, u.dst \ | |
from node \ | |
join node_revisions on node.vid = node_revisions.vid \ | |
join filter_formats f on node_revisions.format = f.format \ | |
left join url_alias u on concat('node/', node.nid) = u.src \ | |
where (node.type = 'blog' OR node.type = 'story' OR node.type = 'article')" | |
def self.process(dbname, user, pass, host = 'localhost') | |
puts QUERY | |
db = Sequel.mysql(dbname, :user => user, :password => pass, :host => '127.0.0.1', :encoding => 'utf8', :port => '3300') | |
# Create the refresh layout | |
# Change the refresh url if you customized your permalink config | |
File.open("source/_layouts/refresh.html", "w") do |f| | |
f.puts <<EOF | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> | |
<meta http-equiv="refresh" content="0;url={{ page.refresh_to_post_id }}.html" /> | |
</head> | |
</html> | |
EOF | |
end | |
db[QUERY].each do |post| | |
# Get required fields and construct Jekyll compatible name | |
node_id = post[:nid] | |
title = post[:title] | |
content = post[:body] | |
created = post[:created] | |
format = post[:format].strip.downcase == 'textile' ? 'textile' : 'markdown' | |
dst = post[:dst] || nil | |
time = Time.at(created) | |
is_published = post[:status] == 1 | |
published = is_published ? nil : false | |
dir = "source" | |
posts_dir = "#{dir}/_posts" | |
slug = title.strip.downcase.gsub(/(&|&)/, ' and ').gsub(/[\s\.\/\\]/, '-').gsub(/[^\w-]/, '').gsub(/[-_]{2,}/, '-').gsub(/^[-_]/, '').gsub(/[-_]$/, '') | |
name = time.strftime("%Y-%m-%d-") + slug + '.' + format | |
puts name | |
tag_query = "select distinct node.nid, type, td.name \ | |
from node \ | |
join term_node tn on node.nid = tn.nid \ | |
join term_data td on tn.tid = td.tid \ | |
where node.nid = #{node_id} order by node.nid" | |
tags = [] | |
db[tag_query].each do |tag| | |
tags.push tag[:name] | |
end | |
tag_list = tags.length == 0 ? nil : tags | |
# Get the relevant fields as a hash, delete empty fields and convert | |
# to YAML for the header | |
data = { | |
'layout' => 'post', | |
'title' => title.to_s, | |
'created' => time, | |
'published' => published, | |
'categories' => tag_list | |
}.delete_if { |k,v| v.nil? || v == ''}.to_yaml | |
# Write out the data and content to file | |
File.open("#{posts_dir}/#{name}", "w") do |f| | |
f.puts data | |
f.puts "---" | |
f.puts content | |
end | |
# Make a file to redirect from the old Drupal URL | |
if is_published | |
FileUtils.mkdir_p(dir + "/node/#{node_id}") | |
File.open(dir + "/node/#{node_id}/index.md", "w") do |f| | |
f.puts "---" | |
f.puts "layout: refresh" | |
f.puts "refresh_to_post_id: /blog/#{time.strftime("%Y/%m/%d/") + slug}/index" | |
f.puts "---" | |
end | |
if dst | |
FileUtils.mkdir_p("#{dir}/#{dst}") | |
File.open("#{dir}/#{dst}/index.md", "w") do |f| | |
f.puts "---" | |
f.puts "layout: refresh" | |
f.puts "refresh_to_post_id: /blog/#{time.strftime("%Y/%m/%d/") + slug}/index" | |
f.puts "---" | |
end | |
end | |
end | |
end | |
# TODO: Make dirs & files for nodes of type 'page' | |
# Make refresh pages for these as well | |
# TODO: Make refresh dirs & files according to entries in url_alias table | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is my modified version of the Drupal migrator. I made it handle the published status, tags, pretty URLs, and the post format (textile or markdown).