Skip to content

Instantly share code, notes, and snippets.

@gonzedge
Created February 3, 2012 20:07
Show Gist options
  • Save gonzedge/1732131 to your computer and use it in GitHub Desktop.
Save gonzedge/1732131 to your computer and use it in GitHub Desktop.
Migrating your blog posts to Markdown with Upmark and Nokogiri
<?xml version="1.0" encoding="UTF-8" ?>
<!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your site. -->
<!-- ... -->
<rss version="2.0"
xmlns:excerpt="http://wordpress.org/export/1.1/excerpt/"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:wp="http://wordpress.org/export/1.1/"
>
<channel>
<title>Rambling Labs</title>
<link>http://www.ramblinglabs.com</link>
<description></description>
<pubDate>Fri, 23 Dec 2011 18:49:41 +0000</pubDate>
<language>en</language>
<wp:wxr_version>1.1</wp:wxr_version>
<wp:base_site_url>http://www.ramblinglabs.com</wp:base_site_url>
<wp:base_blog_url>http://www.ramblinglabs.com</wp:base_blog_url>
<!-- ... -->
<!-- Several items in the following format -->
<item>
<title>The Name of the post</title>
<link>http://www.ramblinglabs.com/2012/12/the-name-of-the-post/</link>
<pubDate>Mon, 05 Dec 2011 19:30:17 +0000</pubDate>
<dc:creator>the_creator</dc:creator>
<guid isPermaLink="false">http://www.ramblinglabs.com/?p=8</guid>
<description></description>
<content:encoded><![CDATA[<!-- A lot of HTML -->]]></content:encoded>
<!-- ... -->
</item>
<!-- ... -->
</channel>
</rss>
File.open("export.xml") do |file|
items = Nokogiri::XML(file).xpath("//channel//item")
end
date_str = item.at_xpath("wp:post_date_gmt").text + " +0000"
name = item.at_xpath("wp:post_name").text.strip
date = Time.parse(date_str).utc
filename = date.strftime("%Y-%m-%d-%H%M%S-"+name+".markdown")
path = 'app/posts/'+filename
File.open(path, 'w') do |f|
f.puts content
end
# ...
items.each do |item|
content = Upmark.convert(item.at_xpath("content:encoded").text)
end
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment