1
Fork 0
blog/ext/post

41 lines
874 B
Plaintext
Raw Permalink Normal View History

2011-09-02 22:20:01 +00:00
#!/usr/bin/env ruby
# encoding: utf-8
2023-06-18 13:59:30 +00:00
OUTPUT_DIR = File.expand_path(File.join(File.dirname(__FILE__), "..", "_posts"))
2012-09-15 23:20:35 +00:00
EDITOR = ENV['EDITOR'] || 'vim'
2013-06-02 21:02:56 +00:00
EXTENSION=".md"
2011-09-02 22:20:01 +00:00
HEADER = <<EOF
2018-12-24 14:29:16 +00:00
---
2018-01-10 11:24:54 +00:00
permalink: "/{{ year }}/{{ month }}/{{ day }}/%s"
2016-12-10 13:57:51 +00:00
title: "%s"
2018-01-10 11:24:54 +00:00
published_date: "%s"
layout: post.liquid
data:
route: blog
2011-09-02 22:20:01 +00:00
---
EOF
if ARGV.size == 0
$stderr.puts "usage: #{File.basename($0)} title"
exit 1
end
2017-07-09 12:18:22 +00:00
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
2011-09-02 22:20:01 +00:00
title = ARGV.join(' ')
file = File.join(OUTPUT_DIR, "#{filename}#{EXTENSION}")
2018-01-10 11:24:54 +00:00
date = now.strftime("%Y-%m-%d %H:%M:00 %z")
2011-09-02 22:20:01 +00:00
puts "new post: #{title}"
puts "file: #{filename}"
2018-01-10 11:24:54 +00:00
File.open(file, "w"){|f| f.write(HEADER % [slug, title, date]) }
2011-09-28 19:53:50 +00:00
exec EDITOR, '+', file