Migrate Acrylamid to Jekyll http://www.g2w.me/2014/05/no-zuo-no-die/
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 'yaml' | |
require 'fileutils' | |
SOURCE_DIR = '/home/greatghoul/Workspace/greatghoul/blog/content/' | |
TARGET_DIR = '/home/greatghoul/Workspace/blog/_posts/' | |
def parse_file(filename) | |
puts "Parsing #{filename}" | |
content = File.read(filename) | |
if match = content.match(/^(?<metadata>---\s*\n.*?\n?)^(---\s*$\n?)/m) | |
meta = YAML.load(match[:metadata]) | |
body = match.post_match | |
return meta, body | |
end | |
end | |
def jekyll_filename(meta) | |
date = meta['date'].split(' ').first | |
slug = meta['slug'].strip | |
File.join(TARGET_DIR, "#{date}-#{slug}.md") | |
end | |
def jekyll_yaml_front_matter(meta) | |
header = ['---', 'layout: post', '---'] | |
meta.each do |k, v| | |
case k | |
when 'title', 'date' | |
header.insert(-2, "#{k}: #{v}") | |
when 'tags' | |
header.insert(-2, "#{k}: #{v.join(' ')}") | |
end | |
end | |
header.join("\n") | |
end | |
def jekyll_content(meta, body) | |
jekyll_yaml_front_matter(meta) + "\n\n" + body | |
end | |
Dir["#{SOURCE_DIR}**/*.md"].each do |filename| | |
puts "Processing #{filename}" | |
meta, body = parse_file(filename) | |
target_filename = jekyll_filename(meta) | |
puts "Writing #{target_filename} ..." | |
File.write(target_filename, jekyll_content(meta, body)) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment