Skip to content

Instantly share code, notes, and snippets.

@nikushi
Created February 14, 2012 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nikushi/1824307 to your computer and use it in GitHub Desktop.
Save nikushi/1824307 to your computer and use it in GitHub Desktop.
A tool to migrate wordpress xml file to jekyll. This is a copied version and I modified a bit for my wordpress data compatibirity. The original is here https://github.com/mojombo/jekyll .
# coding: utf-8
require 'rubygems'
require 'hpricot'
require 'fileutils'
require 'yaml'
require 'time'
# for multi byte string
require 'stringed'
require 'nkf'
module Jekyll
# This importer takes a wordpress.xml file, which can be exported from your
# wordpress.com blog (/wp-admin/export.php).
module WordpressDotCom
def self.process(filename = "wordpress.xml")
import_count = Hash.new(0)
doc = Hpricot::XML(File.read(filename))
(doc/:channel/:item).each do |item|
title = item.at(:title).inner_text.strip
## convert url encoded multibyte string to original by --url-input
## and to_url method converts multibyte string to a-z
permalink_title = NKF.nkf("-w --url-input", item.at('wp:post_name').inner_text).to_url
# Fallback to "prettified" title if post_name is empty (can happen)
if permalink_title == ""
permalink_title = title.downcase.split.join('-')
end
date = Time.parse(item.at('wp:post_date').inner_text)
status = item.at('wp:status').inner_text
if status == "publish"
published = true
else
published = false
end
type = item.at('wp:post_type').inner_text
tags = (item/:category).map{|c| c.inner_text}.reject{|c| c == 'Uncategorized'}.uniq
metas = Hash.new
item.search("wp:postmeta").each do |meta|
key = meta.at('wp:meta_key').inner_text
value = meta.at('wp:meta_value').inner_text
metas[key] = value;
end
name = "#{date.strftime('%Y-%m-%d')}-#{permalink_title}.html"
header = {
'layout' => type,
'title' => title,
'tags' => tags,
'status' => status,
'type' => type,
'published' => published,
'meta' => metas
}
FileUtils.mkdir_p "_#{type}s"
File.open("_#{type}s/#{name}", "w") do |f|
f.puts header.to_yaml
f.puts '---'
# convert CL+LF to LF
f.puts NKF.nkf("-Lu -w", item.at('content:encoded').inner_text)
end
import_count[type] += 1
end
import_count.each do |key, value|
puts "Imported #{value} #{key}s"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment