Last active
August 29, 2015 14:02
-
-
Save madelinecr/2861ab1bf901b50b7354 to your computer and use it in GitHub Desktop.
PostPublisher.rb
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 'psych' | |
module Jekyll | |
class PostPublisher < Generator | |
safe true | |
def replace(filepath, regex, *args, &block) | |
content = File.read(filepath).gsub(regex, *args, &block) | |
File.open(filepath, 'wb') { |file| file.write(content) } | |
end | |
def generate(site) | |
@files = Dir["_publish/*"] | |
@files.each do |f| | |
now = DateTime.now | |
replace(f, /---(.|\n)*---/) do |match| | |
header = Psych.load(match) | |
header["date"] = now.strftime("%Y-%m-%d %H:%M:%S") | |
"#{header.to_yaml}---" | |
end | |
today = now.strftime("%Y-%m-%d") | |
File.rename(f, "_posts/#{today}-#{File.basename(f)}") | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment