Last active
May 8, 2018 17:47
-
-
Save omarish/07a3091623fed8383e1511747945bf57 to your computer and use it in GitHub Desktop.
Script to make a new post in Jekyll.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'optparse' | |
options = { | |
:subdir => "_posts" | |
} | |
parsed = OptionParser.new do |opts| | |
opts.banner = 'Usage: newpost --title "post title" [options]' | |
opts.on("-t", "--title TITLE") do |t| | |
options[:title] = t | |
options[:slug] = t.strip.downcase.gsub(/ /, '-') | |
end | |
opts.on("-d", "--draft", "Create new post in _drafts") do | |
options[:subdir] = "_drafts" | |
end | |
end.parse! | |
if options[:title].nil? or options[:title].strip == "" | |
puts "Need post title. Specify using --title" | |
exit 2 | |
end | |
now = Time.now | |
date_prefix = now.strftime("%Y-%m-%d") | |
jekyll_dir = File.dirname(File.expand_path('.', __dir__)) | |
posts_dir = File.join(jekyll_dir, options[:subdir]) | |
post = File.join(posts_dir, "#{date_prefix}-#{options[:slug]}.md") | |
if File.exist?(post) | |
puts "File already exists." | |
exit 1 | |
end | |
header = <<-END | |
--- | |
date: #{now.strftime("%Y-%m-%d %H:%M:%S%z")} | |
layout: post | |
slug: "#{options[:slug]}" | |
title: #{options[:title]} | |
description: | |
tags: | |
--- | |
h1. {{ page.title }} | |
END | |
File.open(post, 'w') do |f| | |
f << header | |
end | |
editor = ENV['EDITOR'] | |
unless editor | |
puts "Created new post: \n#{post}" | |
else | |
system(editor, post) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment