mph.paste.lol / hpost.rb · 1 year 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 
# slug. 
# - `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 = {}

OptionParser.new 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 #{Date.today.strftime("%Y-%m-%d")}"
  end

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

end.parse!

date = Date.today.strftime("%Y-%m-%d") 
long_date = Time.now.strftime("%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: 
  - /momo-logo.jpg

---

POST

File.write(file_path, post)

`open #{file_path}`