Skip to content

Instantly share code, notes, and snippets.

@alexfalkowski
Last active August 29, 2015 14:16
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 alexfalkowski/b020412ac7f09f6f2ea9 to your computer and use it in GitHub Desktop.
Save alexfalkowski/b020412ac7f09f6f2ea9 to your computer and use it in GitHub Desktop.
Nanoc Refactor
require 'stringex'
usage 'create-post [options] title'
aliases :create_post, :cp
summary 'create a new blog post'
description 'Creates new blog post with standard template.'
flag :h, :help, 'show help for this command' do |value, cmd|
puts cmd.help
exit 0
end
run do |opts, args, cmd|
unless title = args.first
puts cmd.help
exit 0
end
options = {
title: title,
date: Time.now
}
post = Post.new(options)
if post.create(Template.new(options))
puts "Created post: #{post.path}"
puts "URL: #{post.published_url}"
exit 0
else
puts "Post already created: #{post.path}"
exit 1
end
end
class Template
def initialize(options = {})
@title = options.fetch(:title)
@date = options.fetch(:date)
end
def create
<<TEMPLATE
---
title: "#{title}"
created_at: #{date}
kind: article
publish: false
author: anonymous
tags: [ 'foo', 'bar', 'baz' ]
---
TEMPLATE
end
private
attr_reader :title, :date
end
class Post
def initialize(options = {})
@title = options.fetch(:title)
@date = options.fetch(:date)
end
def path
"./content/posts/#{date.strftime('%Y-%m-%d')}-#{title.to_url}.md"
end
def published_url
"http://blog.arkency.com/#{date.year}/#{date.month}/#{title.to_url}"
end
def exists?
File.exist?(path)
end
def create(template)
if exists?
false
else
File.open(path, 'w') { |f| f.write(template.create) }
true
end
end
private
attr_reader :title, :date
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment