Skip to content

Instantly share code, notes, and snippets.

@ajmalafif
Forked from rmurphey/posterous-octopress.rb
Created March 5, 2012 09:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajmalafif/1977574 to your computer and use it in GitHub Desktop.
Save ajmalafif/1977574 to your computer and use it in GitHub Desktop.
[ruby] - 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}.markdown")
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('source', '_posts'))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment