Created
November 23, 2014 22:59
-
-
Save hadleynet/c02909697dcad7819f5d to your computer and use it in GitHub Desktop.
Ruby script to convert Greymatter blog entries to Jekyll post format
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 | |
source_dir = ARGV[0] | |
dest_dir = ARGV[1] | |
def get_timestamp(components) | |
month = components[0].to_i | |
day = components[1].to_i | |
year = components[2].to_i | |
hour = components[3].to_i | |
minute = components[4].to_i | |
second = components[5].to_i | |
ampm = components[6] | |
if ampm == 'AM' and hour == 12 | |
hour = 0 | |
elsif ampm == 'PM' and hour < 12 | |
hour = hour + 12 | |
end | |
Time.local(year, month, day, hour, minute, second) | |
end | |
Dir.glob(File.join(source_dir, '*.cgi')).each do |file| | |
content = File.readlines(file) | |
# grab the post metadata | |
metadata = content[0].split('|') | |
title = metadata[2] | |
timestamp = get_timestamp(metadata[4..10]) | |
puts "#{file} - #{timestamp.strftime('%Y-%m-%d %H:%M:%S')}: #{title}" | |
# parse the rest of the file | |
in_comments = false | |
post_lines = [] | |
comment_lines = [] | |
content[2..-1].each do |line| | |
if line=="\n" | |
in_comments = true | |
elsif in_comments | |
comment_lines << line | |
else | |
post_lines << line | |
end | |
end | |
jekyll_file = "#{timestamp.strftime('%Y-%m-%d-')}#{title.gsub(/\W/, '-')}".gsub(/-+/,'-').chomp('-') | |
jekyll_file = jekyll_file + '.markdown' | |
jekyll_path = File.join(dest_dir, jekyll_file) | |
File.open(jekyll_path, 'w') do |file| | |
file.write("---\n") | |
file.write("layout: post\n") | |
file.write("title: \"#{title}\"\n") | |
file.write("date: #{timestamp.strftime('%Y-%m-%d %H:%M:%S')}\n") | |
file.write("categories: \n") | |
file.write("---\n") | |
post_lines.each do |line| | |
line.split('|*|').each do |subline| | |
file.write(subline.gsub("<p>","").gsub("</p>","")) | |
file.write("\n") | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment