Skip to content

Instantly share code, notes, and snippets.

@tk3
Created September 1, 2018 12:21
Show Gist options
  • Save tk3/e5eb0ba401790891c40e9506162ae712 to your computer and use it in GitHub Desktop.
Save tk3/e5eb0ba401790891c40e9506162ae712 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'fileutils'
require "time"
require "openssl"
if ARGV.length < 2
$stderr.puts "Error"
exit 1
end
in_path = ARGV[0]
out_path = ARGV[1]
FileUtils.mkdir_p out_path
Dir.glob("#{in_path}/**") do |file|
in_data = false
out_data = ""
File.open(file) do |r_io|
while line = r_io.gets
case line
when /^---$/
in_data = !in_data
out_data << line
when /^title:/
out_data << line
when /^categories:/
out_data << line unless in_data
when /^layout:/
out_data << line unless in_data
when /^date:/
if in_data
tm = Time.parse($_.split(":", 2)[1].strip)
slug = OpenSSL::Digest::SHA1.hexdigest(tm.to_i.to_s + Time.now.to_s)
filename_prefix = tm.strftime("%Y-%m-%d-")
if File.basename(file).match(/#{filename_prefix}/).nil?
$stderr.puts "Not match date: #{file} : #{tm.iso8601}"
end
out_data << "date: #{tm.iso8601}\n"
out_data << "slug: #{slug}\n"
else
out_data << line
end
else
out_data << line
end
end
end
base_name = File.basename(file, ".*")
File.open("#{out_path}/#{base_name}.md", "w") do |w_io|
w_io.puts out_data
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment