From 5f79502d918d8b55cf68d77fac1e10c2d3fe1123 Mon Sep 17 00:00:00 2001 From: Rob Watson Date: Tue, 30 Jan 2018 13:59:00 +0000 Subject: [PATCH] Add script to generate a new Jekyll post --- bin/new | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100755 bin/new diff --git a/bin/new b/bin/new new file mode 100755 index 0000000..9164dab --- /dev/null +++ b/bin/new @@ -0,0 +1,55 @@ +#!/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}"