1
Fork 0
blog/ext/post

38 lines
798 B
Ruby
Executable file

#!/usr/bin/env ruby
# encoding: utf-8
OUTPUT_DIR = File.expand_path("~/projects/fnordig.de/_posts")
EDITOR = ENV['EDITOR'] || 'vim'
EXTENSION=".md"
HEADER = <<EOF
extends: post.liquid
title: "%s"
date: %s
path: /:year/:month/:day/%s
---
EOF
if ARGV.size == 0
$stderr.puts "usage: #{File.basename($0)} title"
exit 1
end
def slugify(args)
args.map { |arg|
arg.downcase.gsub(/[^a-z0-9]/, '-')
}.join('-').gsub(/--+/, '-').gsub(/-$/, '')
end
now = Time.now
slug = slugify(ARGV)
filename = now.strftime("%Y-%m-%d-") + slug
title = ARGV.join(' ')
file = File.join(OUTPUT_DIR, "#{filename}#{EXTENSION}")
date = now.strftime("%d %b %Y %H:%M:%S %z")
puts "new post: #{title}"
puts "file: #{filename}"
File.open(file, "w"){|f| f.write(HEADER % [title, date, slug]) }
exec EDITOR, '+', file