Skip to content

Instantly share code, notes, and snippets.

@blithe
Forked from burtlo/.zshrc
Last active October 5, 2015 16:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blithe/fc10f179a8d20dbefa21 to your computer and use it in GitHub Desktop.
Save blithe/fc10f179a8d20dbefa21 to your computer and use it in GitHub Desktop.
This is a shortcut to change into my journal directory, create a new article, and then immediately start editing it.
read () {
open "http://blitherocher.com"
}
article () {
# change in to the directory where I keep my journal project
cd /projects/personal_projects/personal_site
echo "Creating article '$@'"
# Run the command to generate the article with the tool I use
output=$(rake post title="$@")
# The output of that command:
#
# Creating new post: ./_posts/2015-09-29-new-post.html
#
# We want the last line, and then we want the filepath.
# I found it hard to retrieve the filepath because of some ANSI color that Thor
# uses in the output so I use awk to grap the fourth term.
#
# echo $(echo $output |& tail -1 | awk '{print $4;}')
new_article=$(echo $output |& tail -1 | awk '{print $4;}')
# echo "vim $new_article"
vim $new_article
}
# This Rakefile has been modified from https://github.com/plusjade/jekyll-bootstrap/blob/master/Rakefile
require 'rake'
require 'time'
SOURCE = "."
CONFIG = {
'posts' => File.join(SOURCE, "_posts"),
'post_ext' => "html",
}
# Usage: rake post title="A Title" [date="2012-02-09"] [tags=[tag1,tag2]] [category="category"]
desc "Begin a new post in #{CONFIG['posts']}"
task :post do
abort("rake aborted: '#{CONFIG['posts']}' directory not found.") unless FileTest.directory?(CONFIG['posts'])
title = ENV["title"] || "new-post"
slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
begin
date = (ENV['date'] ? Time.parse(ENV['date']) : Time.now).strftime('%Y-%m-%d')
rescue => e
puts "Error - date format must be YYYY-MM-DD, please check you typed it correctly!"
exit -1
end
filename = File.join(CONFIG['posts'], "#{date}-#{slug}.#{CONFIG['post_ext']}")
if File.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
puts "Creating new post: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: post"
post.puts "title: \"#{title.gsub(/-/,' ')}\""
post.puts "---"
end
end # task :post
def ask(message, valid_options)
if valid_options
answer = get_stdin("#{message} #{valid_options.to_s.gsub(/"/, '').gsub(/, /,'/')} ") while !valid_options.include?(answer)
else
answer = get_stdin(message)
end
answer
end
def get_stdin(message)
print message
STDIN.gets.chomp
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment