Skip to content

Instantly share code, notes, and snippets.

@rmurphey
Created March 3, 2012 21:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save rmurphey/1968399 to your computer and use it in GitHub Desktop.
Save rmurphey/1968399 to your computer and use it in GitHub Desktop.
Generate Octopress posts from Posterous XML
#!/usr/bin/env ruby
require 'xmlsimple'
require 'active_support/inflector'
require 'yaml'
require 'json'
require 'fileutils'
require 'date'
class Post
def initialize(post)
@post = post
end
def write(dir)
filename = File.join(dir, "#{slug}.md")
puts "Writing #{filename}"
File.open(filename, 'w') do |f|
f.write frontmatter
f.write "\n\n"
f.write content
end
end
private
def content
@post['body'].first.strip
end
def title
@post['title'].first
end
def slug
date + '-' + title.downcase.parameterize
end
def date
d = Date.parse @post['date'].first
d.strftime '%Y-%m-%d'
end
def tags
return '' unless @post['tag']
@post['tag'].join(', ')
end
def frontmatter
[ {
'layout' => 'post',
'date' => date,
'title' => title,
'comments' => true,
'categories' => tags
}.to_yaml, '---' ].join("")
end
end
posts = XmlSimple.xml_in('posterous.xml')['post']
dir = File.join('source', '_posts')
FileUtils.mkdir_p dir unless File.exists? dir
posts.each do |post|
p = Post.new post
p.write(File.join('tmp', 'posts'))
end
@rmurphey
Copy link
Author

rmurphey commented Mar 3, 2012

I used http://www.backuperous.com/ to generate a backup, tweaked the XML it returned a tiny bit by giving it a single root node with post nodes as its children, and then ran the above.

@ajmalafif
Copy link

hey there, thank you so much for sharing this. Do I create this file (posterous-octopress.rb) and put this inside my plugin folder?

Can't see the file included from your repo here (https://github.com/rmurphey/blog-octopress/tree/master/plugins)

I am a novice around web dev but I believe in learning by doing. I hope I figure it out before you reply, but it'll be very helpful if you could point me out. Thanks again!

@rmurphey
Copy link
Author

rmurphey commented Mar 4, 2012

I just put it in the root of my octopress repo, along with the posterous.xml file, and then executed it with ./posterous.rb. Note that you'll also need to do chmod 755 posterous.rb in order to make the file executable. Note too that I had to fiddle with the XML that I got from backuperous.com in order to make it well-formed.

@ajmalafif
Copy link

Hi, thanks for the rescue! I was still on it and can't find how to work it.

I'll go through and update/report any unexpected findings. I believe it should work right out of the box.

Thanks again!

@ajmalafif
Copy link

Like you suggested, it required some adjustments on the XML file and I made some changes on the posterous.rb in order to make it worked (I had no clue what and how I did it though).

Again, thanks so much for this! Made my day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment