Created
March 15, 2012 15:01
-
-
Save tonkla/2044668 to your computer and use it in GitHub Desktop.
Blog migration script. Migrate from MySQL to Markdown text file (Jekyll).
This file contains hidden or 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
require 'rubygems' | |
require 'erb' | |
require 'mysql2' | |
require 'thor' | |
class Blogelf < Thor | |
desc 'migrate', 'Migrate from MySQL to Markdown text file (Jekyll)' | |
def migrate | |
Dir.mkdir('_posts') unless Dir.exist?('_posts') | |
db = Mysql2::Client.new(host: 'localhost', username: 'root', database: 'blogelf') | |
result = db.query("SELECT id, title, body, description, created_at FROM posts") | |
result.each do |r| | |
@title = r['title'] | |
@body = r['body'] | |
@created_at = r['created_at'] | |
@description = r['description'] | |
year = @created_at.strftime('%Y') | |
Dir.mkdir("_posts/#{year}") unless Dir.exist?("_posts/#{year}") | |
File.open("_posts/#{year}/#{@created_at.strftime('%Y-%m-%d')}-#{r['id']}.md", 'w') do |f| | |
f.write(ERB.new(File.read('post_template.erb')).result(binding)) | |
end | |
end | |
db.close | |
end | |
end |
This file contains hidden or 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
--- | |
layout: post | |
title: "<%= @title.gsub(/"/, '\"') %>" | |
description: "<%= @description.gsub(/"/, '\"') if @description %>" | |
date: <%= @created_at.strftime('%Y-%m-%d %H:%M:%S') %> | |
categories: | |
--- | |
<%= @body %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment