Skip to content

Instantly share code, notes, and snippets.

@chagel
Created January 17, 2014 14:25
Show Gist options
  • Save chagel/8474157 to your computer and use it in GitHub Desktop.
Save chagel/8474157 to your computer and use it in GitHub Desktop.
Ruby script to export Jekyll & Octopress posts for Ghost
#encoding: utf-8
require 'json'
require 'yaml'
require 'active_support/core_ext'
I18n.enforce_available_locales = false
export_data = {
meta: {
version: "000"
},
data: {
posts: [],
tags: [],
posts_tags: []
}
}
class Tag
class << self
def ids
@ids ||= 2000.step(1_000_000).to_enum
end
def next_id
ids.next
end
def [](tag_name)
repository[tag_name] ||= Tag.new(tag_name)
end
def repository
@repository ||= {}
end
end
attr_reader :name, :id
def initialize(name)
@name = name || '-'
@id = Tag.next_id
end
def slug
name.parameterize
end
def to_json(*args)
{
id: id,
name: name,
slug: slug
}
end
end
class Post
class << self
def next_id
ids.next
end
def ids
@ids ||= 1000.step(1_000_000).to_enum
end
end
attr_reader :id, :tags
def initialize(raw, slug)
@metadata = YAML.load raw
@content = raw.split('---')[2, 10].join.strip
@id = Post.next_id
@tags = [@metadata["tags"]].flatten.collect { |c_name| Tag[c_name] }
@slug = slug
end
def title
@metadata["title"]
end
def slug
@slug
end
def content
remove_more
# fix_images
strip_newlines
fix_blockquotes
@content
end
def timestamp
@metadata["date"].to_time.to_i * 1000
end
def as_json(*args)
{
id: id,
title: title,
slug: slug,
markdown: content,
image: nil,
featured: 0,
page: 0,
status: "published",
language: "zh_CN",
author_id: 1,
created_at: timestamp,
created_by: 1,
updated_at: timestamp,
updated_by: 1,
published_at: timestamp,
published_by: 1
}
end
private
def remove_more
@content.gsub! '<!-- more -->', ''
end
def fix_images
@content.gsub!(/\{% img.*?(\/.*?) \d+ \d+\s?(.*?) %}/, '![\2](\1)')
@content.gsub!('images', 'content/images')
end
def strip_newlines
@content = @content.strip.gsub(/(.)\n(.)/, '\1 \2')
end
def fix_blockquotes
@content.gsub!(/{% blockquote %}(.*?){% endblockquote %}/, '> \1')
end
end
tag_ids = 3000.step(1_000_000).to_enum
posts = Dir["*.markdown"]
posts.each do |post_file|
name = post_file.split('.').first
slug = name[11..name.length]
post = Post.new File.read(post_file), slug
export_data[:data][:posts] << post.as_json
post.tags.each do |tag|
export_data[:data][:posts_tags] << {
id: tag_ids.next,
post_id: post.id,
tag_id: tag.id
}
end
end
export_data[:data][:tags] = Tag.repository.values
puts export_data.to_json
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment