-
-
Save ajmalafif/1977574 to your computer and use it in GitHub Desktop.
[ruby] - 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}.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