Created
March 6, 2013 19:19
-
-
Save jnozsc/5102189 to your computer and use it in GitHub Desktop.
This program can fix Octopress issue #1088
In fact, it is not a bug in Octopress.
It is introducted by 3rd party migration tool.
However, I have forgotten which tool causes this issue.
So I write this program to help others who meet the same issue
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
# encoding: utf-8 | |
# description: This program can fix Octopress issue #1088 | |
# In fact, it is not a bug in Octopress. | |
# It is introducted by 3rd party migration tool. | |
# However, I have forgotten which tool causes this issue. | |
# So I write this program to help others who meet the same issue | |
# usage: in your Terminal.app, type the following line: | |
# ruby issue_1088_fix.rb [/your/octopress/path/] | |
# e.g: ruby issue_1088_fix.rb ~/octopress/source/_posts/ | |
# Please feel free to contact me, if you find some bugs in this program. | |
# | |
# author: jnozsc | |
# email: jnozsc@gmail.com | |
# | |
require 'yaml' | |
if ARGV.size() != 1 | |
abort "error\nplease provide the path\ne.g: ruby issue_1088_fix.rb ~/octopress/source/_posts/" | |
end | |
file_path = ARGV[0].to_s | |
date_array = Array.new | |
i = 0 | |
Dir[file_path + '/*.markdown'].each do |file| | |
rawfile = File.read(file) | |
data = YAML.load rawfile.match(/(^-{3}\n)(.+?)(\n-{3})/m)[2] | |
if data.has_key?('date') == false | |
i += 1 | |
date_from_title = /\d{4}-\d{2}-\d{2}/.match(file.to_s) | |
hour = 12 # can be changed to any hour you like | |
second = 0 | |
new_string = 'date: ' + date_from_title.to_s + ' ' + hour.to_s + ':' + second.to_s.rjust(2, '0') | |
while date_array.include?(new_string) | |
second += 1 | |
if second >= 60 | |
second -= 60 | |
hour += 1 | |
end | |
new_string = 'date: ' + date_from_title.to_s + ' ' + hour.to_s + ':' + second.to_s.rjust(2, '0') | |
end | |
date_array.push(new_string) | |
File.open(file) do |f| | |
old_file_name = File.basename(f, File.extname(f)) | |
File.rename(f, file_path + '/' + old_file_name + '.bak') | |
f.each_line do |line| | |
File.open(file_path + '/' + old_file_name + '.markdown' , 'a+') do |out| | |
out.write line | |
out.write new_string + "\n" if /#{'title: '}/.match(line) | |
end | |
end | |
end | |
end | |
end | |
puts 'successfully handle ' + i.to_s + ' file(s)' + "\n" + 'remember to delete .bak file(s)' if i > 0 | |
puts 'nothing need to do' if i == 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment