#!/usr/bin/env ruby require "optparse" require "time" require "yaml" front_matter = { "title" => "Hello World", "slug" => "hello-world", "layout" => "post", "categories" => [] } options = { force: false } OptionParser.new do |opts| opts.on("-t", "--title TITLE", "Title for post") do |title| front_matter["title"] = title end opts.on("-s", "--slug SLUG", "Slug for post") do |slug| front_matter["slug"] = slug end opts.on("-l", "--layout LAYOUT", "Layout for post") do |layout| front_matter["layout"] = layout end opts.on("-c", "--category CATEGORY", "Add category for post (can be specified multiple times)") do |category| front_matter["categories"] << category end opts.on("-f", "--force", "Overwrite existing file") do |val| options[:force] = val end end.parse! now = DateTime.now date = DateTime.new(now.year, now.month, now.day, 0, 0, 0, now.zone) front_matter["date"] = date.to_time.to_s front_matter["categories"] = front_matter["categories"].join(" ") fname = date.strftime("_posts/%Y-%m-%d-#{front_matter["slug"]}.md") raise "Already exists: #{fname}" if File.exists?(fname) && !options[:force] File.open(fname, "w") do |f| f << front_matter.to_yaml f << "---" << "\n" << "\n" f << "Hello reader..." end puts "Generated new post in: #{fname}"