/ hpost.rb · 6 months ago·

!/usr/bin/env ruby

- requires the slugify gem - gem install slugify

- opens the file in your default editor as set by macOS, not your shell

- does nothing to keep the slug length reasonable, so your film review of

"The Assassination of Jesse James by the Coward Robert Ford" is going to have a long


- hpost.rb --help for help on the options

require 'optparse' require 'slugify' require 'date'

Where's your post directory?

site_posts_dir = "~/src/simple/content/posts/"

options = {} do |parser| parser.on("-t", "--title TITLE", "The title of the post in quotes.") do |o| options[:title] = o end

parser.on("-T", "--tags TAGS", "Comma-delimited tags for the post, e.g. 'banana,apple,pear'") do |o| options[:tags] = o.split(',') end

parser.on("-c", "--category CATEGORY", "Category for the post.") do |o| options[:category] = o.split(',') end

parser.on("-d", "--daily", "Make this a daily post and preset tags and title.") do |o| options[:category] = "Daily posts" options[:tags] = ['daily','journal'] options[:title] = "Daily notes for #{"%Y-%m-%d")}" end

parser.on("-h", "--help", "Get help.") do |o| puts parser exit(0) end


date ="%Y-%m-%d") long_date ="%Y-%m-%dT%H:%M:%S%z") slug = options[:title].slugify filename = date + '-' + slug + '.md' file_path = File.expand_path(site_posts_dir + filename)

if File.exists?(file_path) abort("*** Error: #{file_path} already exists. Exiting.") end

post = <<-POST

title: "#{options[:title]}" date: #{long_date} draft: true tags: #{options[:tags]} category: #{options[:category]}
toc: false images:


File.write(file_path, post)

open #{file_path}