Created
March 3, 2012 21:31
-
-
Save rmurphey/1968399 to your computer and use it in GitHub Desktop.
Generate Octopress posts from Posterous XML
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
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.