Skip to content

Instantly share code, notes, and snippets.

@scottymeyers
Last active November 28, 2016 17:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottymeyers/0c940334c0b7efe9205c4be704a68e85 to your computer and use it in GitHub Desktop.
Save scottymeyers/0c940334c0b7efe9205c4be704a68e85 to your computer and use it in GitHub Desktop.
Jekyll post creation script - provide a Title & directory of images.
#!/usr/bin/env ruby
require 'optparse'
# check for post title and image directory options
options = {}
OptionParser.new do |parser|
parser.banner = "Usage: jekyll_create_post.rb [options] [arguments...]"
parser.separator "This program creates a new Jekyll Post."
parser.on("-t", "--title POST TITLE", "The title of the jekyll post.") do |v|
options[:title] = v
end
parser.on("-d", "--img_dir IMAGE DIRECTORY", "The directory containing the images.") do |v|
options[:img_dir] = v
end
parser.on("-h", "--help", "Show this help message") do ||
puts parser
end
begin
# Parse and remove options from ARGV.
parser.parse!
rescue OptionParser::ParseError => error
# Without this rescue, Ruby would print the stack trace
# of the error. Instead, we want to show the error message,
# suggest -h or --help, and exit 1.
$stderr.puts error
$stderr.puts "(-h or --help will show valid options)"
exit 1
end
end
def createJekyllPost(title, photo_dir)
# create Jekyll friendly markdown filename
filename = Time.now.strftime("%Y-%m-%d") + "-" + title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
# create the file in _posts
output = File.open( "./_posts/#{filename}.markdown","w" )
# file output
output << "---\n"
output << "date-range:\n"
output << "layout: post\n"
output << "title: #{title}\n"
output << "images:\n"
# move to this posts photo dir and include each img
Dir.glob("assets/photos/#{photo_dir}/*.jpg") do |img|
output << " - url: /#{img}\n"
output << " alt: \n"
end
output << "---\n"
output << "\n"
# save file
output.close
end
# check for options
if [:img_dir, :title].all? {|s| options.key? s}
createJekyllPost(options[:title], options[:img_dir])
else
p 'Please include a title and image directory.'
p '> ruby jekyll_create_post.rb -h'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment