Skip to content

Instantly share code, notes, and snippets.

@julesfern
Created February 21, 2009 01:12
Show Gist options
  • Save julesfern/67820 to your computer and use it in GitHub Desktop.
Save julesfern/67820 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Script to import tumblr posts into local markdown posts ready to be consumed by Jekyll.
# Inspired by New Bamboo's post http://blog.new-bamboo.co.uk/2009/2/20/migrating-from-mephisto-to-jekyll
require 'rubygems'
require 'open-uri'
require 'nokogiri'
# Configuration
tumblr_domain = "YOUR DOMAIN"
write_directory = "PATH TO SAVE FILES TO (NO TRAILING SLASH)"
# Connect to Tumblr and read the API source
open(tumblr_domain+"/api/read") do |xml|
doc = Nokogiri::XML.parse(xml)
doc.css("post").each do |post_tag|
# Gather data about each post
date = Date.parse(post_tag.attributes["date"].content)
case
when post_tag.attributes["type"].content == "regular"
title = post_tag.css("regular-title").first.content
body = post_tag.css("regular-body").first.content
filename = "#{date.strftime("%Y-%m-%d")}-#{title.gsub(/(\s|[^a-zA-Z0-9])/,"-").gsub(/-+/,'-').gsub(/-$/,'')}.markdown"
jekyll_post = <<-EOPOST
---
title: #{title}
layout: post
---
#{body}
EOPOST
# Write files
puts filename
puts jekyll_post
puts ""
file = File.new("#{write_directory}/#{filename}", "w+")
file.write(jekyll_post)
file.close
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment