Skip to content

Instantly share code, notes, and snippets.

@atheken
Created October 9, 2010 13:53
Show Gist options
  • Save atheken/618192 to your computer and use it in GitHub Desktop.
Save atheken/618192 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'nokogiri'
require 'yaml'
class File
def writeline(value)
self.write(value + "\n")
end
end
inputFile = ARGV[0]
if inputFile then
File.new('_config.yml','w+')
unless(File.exists?('_posts')) then Dir.mkdir('_posts') end
unless(File.exists?('_layouts')) then Dir.mkdir('_layouts') end
unless(File.exists?('css')) then Dir.mkdir('css') end
unless(File.exists?('images')) then Dir.mkdir('images') end
doc = Nokogiri::XML(File.open(inputFile))
doc.xpath('//channel/item').each do |post|
post_date = Date.parse(post.xpath('wp:post_date/text()').to_s)
title = post.xpath("title").inner_text
curr = File.new("_posts/#{post_date.year}-#{post_date.month}-#{post_date.day}-#{title.gsub(/[^\s0-9a-zA-Z]/,'').gsub(/[\s]+/,'-')}.html", 'w+')
categories = (post.xpath('category[not(@domain)]').map do |cat| cat.inner_text end).to_a
hash = {"layout"=>"post","title"=> title, "tags"=>categories, "postdate"=>post.xpath('wp:post_date').inner_text}
status = post.xpath('wp:status').inner_text
if status != 'publish' then
hash["status"] = status
end
curr.write hash.to_yaml + "---\n"
curr.write(post.xpath("content:encoded").inner_html)
curr.close
end
else
p 'This script requires one argument, the path to your WordPress XML export file.'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment