Created
October 30, 2011 16:31
-
-
Save jaceju/1326086 to your computer and use it in GitHub Desktop.
WordPress To Octopress Markdown Converter
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 'fileutils' | |
require 'date' | |
require 'yaml' | |
require 'rexml/document' | |
require 'ya2yaml' | |
include REXML | |
doc = Document.new(File.new(ARGV[0])) | |
FileUtils.rmdir "_posts" | |
FileUtils.mkdir_p "_posts" | |
doc.elements.each("rss/channel/item[wp:status = 'publish' and wp:post_type = 'post']") do |e| | |
p e.elements['wp:post_name'].text | |
post = e.elements | |
wordpress_id = post['wp:post_id'].text | |
#slug = post['wp:post_name'].text | |
slug = wordpress_id | |
date = DateTime.parse(post['wp:post_date'].text) | |
name = "%02d-%02d-%02d-%s.markdown" % [date.year, date.month, date.day, slug] | |
date_string = "#{date.year}-#{date.month}-#{date.day}" | |
title_string = post['title'].text.gsub('\'', ''') | |
# convert all tags and categories into categories | |
categories = [] | |
post.each('category') do |cat| | |
categories << cat.text | |
end | |
content = post['content:encoded'].text.encode("UTF-8") | |
# convert html to markdown | |
content = content.gsub('\'', ''') | |
content = content.gsub(/<pre[^>]*>/, '{% codeblock %}' + "\n") | |
content = content.gsub(/<\/pre>/, "\n" + '{% endcodeblock %}' + "\n") | |
content = content.gsub(/<code[^>]*>/, '') | |
content = content.gsub(/<\/code>/, '') | |
content = content.gsub(/\n+/, "\n") | |
puts "Converting: #{name}" | |
data = { | |
'layout' => 'post', | |
'title' => title_string, | |
'date' => date_string, | |
'comments' => true, | |
'categories' => categories, | |
}.delete_if { |k,v| v.nil? || v == ''}.to_yaml | |
File.open("_posts/#{name}", "w") do |f| | |
f.puts "---" | |
f.puts "layout: post" | |
f.puts "title: '#{title_string}'" | |
f.puts "date: #{date_string}" | |
f.puts "wordpress_id: #{wordpress_id}" | |
f.puts "permalink: /blog/archives/#{wordpress_id}" | |
f.puts "comments: true" | |
#f.puts "categories: " + categories.to_s | |
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