Created
October 26, 2013 14:59
-
-
Save shuhei/7170356 to your computer and use it in GitHub Desktop.
WordPress to Octopress
This file contains 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 'nokogiri' | |
xml = File.read('./wordpress.2013-07-10.xml') | |
doc = Nokogiri::XML.parse(xml) | |
items = doc.xpath('/rss/channel/item') | |
puts "#{items.size} items" | |
items.each do |item| | |
date = item.at('./wp:post_date').text | |
title = item.at('./title').text | |
post_name = item.at('./wp:post_name').text | |
status = item.at('./wp:status').text | |
content = item.at('./content:encoded').text | |
categories = item.xpath('./category[not(@domain)]') | |
.map { |cat| cat.text } | |
.reject { |cat| cat == '未分類' } | |
.reject { |cat| cat == 'Uncategorized' } | |
params = { | |
layout: 'post', | |
title: %{"#{title}"}, | |
published: status == 'publish', | |
date: date.split(':')[0..1].join(':'), | |
comments: true, | |
tags: '', | |
categories: "[#{categories.join(', ')}]" | |
} | |
filename = "#{date.split(' ')[0]}-#{post_name}.textile" | |
# puts status, filename | |
next unless params[:published] | |
File.open("./_posts/#{filename}", 'w') do |f| | |
f.puts '---' | |
f.puts params.map { |k, v| [k, v].join(': ') }.join("\n") | |
f.puts '---' | |
f.puts content | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment