Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Post publishing plugin for Jekyll
module Jekyll
class PostPublisher < Generator
safe false
def replace(filepath, regexp, *args, &block)
content = File.read(filepath).gsub(regexp, *args, &block)
File.open(filepath, 'wb') { |file| file.write(content) }
end
def generate(site)
@files = Dir["_publish/*"]
@files.each_with_index { |f,i|
now = DateTime.now.strftime("%Y-%m-%d %H:%M:%S")
replace(f, /^date: unpublished/mi) { |match| "date: \"" + now + "\"" }
now = Date.today.strftime("%Y-%m-%d")
File.rename(f, "_posts/#{now}-#{File.basename(f)}")
}
end
end
end
@pocketWashburn

This comment has been minimized.

Show comment
Hide comment
@pocketWashburn

pocketWashburn Jan 9, 2014

Great little plugin, saved me from trying to write the same thing for myself (it wouldn't have been as elegant). I did fork and make a slight change - instead of looking for _publish and _posts in the current directory, I prepended the site.source variable. So:

@files = Dir["#{site.source}/_publish/*"]

and

File.rename(f, "#{site.source}/_posts/#{now}-#{File.basename(f)}")

I almost never execute my build command from within my site's source folder, instead providing the source as a command line variable. I tested and it still works now either way you generate.

pocketWashburn commented Jan 9, 2014

Great little plugin, saved me from trying to write the same thing for myself (it wouldn't have been as elegant). I did fork and make a slight change - instead of looking for _publish and _posts in the current directory, I prepended the site.source variable. So:

@files = Dir["#{site.source}/_publish/*"]

and

File.rename(f, "#{site.source}/_posts/#{now}-#{File.basename(f)}")

I almost never execute my build command from within my site's source folder, instead providing the source as a command line variable. I tested and it still works now either way you generate.

@pacebl

This comment has been minimized.

Show comment
Hide comment
@pacebl

pacebl Jun 6, 2014

A great plugin, I've found it to be a life saver. I had a quick question though, is there any reason to use each_with_index over each in the generate method?

pacebl commented Jun 6, 2014

A great plugin, I've found it to be a life saver. I had a quick question though, is there any reason to use each_with_index over each in the generate method?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment