Skip to content

Instantly share code, notes, and snippets.

@pdxmph

pdxmph/hpost.rb Secret

Last active June 26, 2023 04:43
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 pdxmph/9271cbb22d90f7e73a4b88664e0eaadd to your computer and use it in GitHub Desktop.
Save pdxmph/9271cbb22d90f7e73a4b88664e0eaadd to your computer and use it in GitHub Desktop.
Ruby script for posting with Hugo
#!/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
options[:type] = 'daily'
options[:category] = ['Daily']
options[:tags] = ['journal']
options[:title] = "Daily notes for #{Date.today.strftime('%Y-%m-%d')}"
end
parser.on("-h", "--help", "Get help.") do
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
if options[:type] == 'daily'
filename = "#{date}-daily-notes.md"
else
filename = "#{date}-#{slug}.md"
end
file_path = File.expand_path(site_posts_dir + filename)
abort("*** Error: #{file_path} already exists. Exiting.") if File.exist?(file_path)
post = <<~POST
---
title: "#{options[:title]}"
date: #{long_date}
draft: true
tags: #{options[:tags]}
categories: #{options[:category]}
ShowToc: true
TocOpen: true
cover:
image:
images:
- /momo-logo.jpg
summary:
---
POST
File.write(file_path, post)
`open #{file_path}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment