1
Fork 0
blog/ext/post

40 lines
851 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
permalink: "/{{ year }}/{{ month }}/{{ day }}/%s"
title: "%s"
published_date: "%s"
layout: post.liquid
data:
route: blog
---
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("%Y-%m-%d %H:%M:00 %z")
puts "new post: #{title}"
puts "file: #{filename}"
File.open(file, "w"){|f| f.write(HEADER % [slug, title, date]) }
exec EDITOR, '+', file