zeus.ugent.be/commands/post.rb

47 lines
1.2 KiB
Ruby
Raw Normal View History

2016-08-14 10:12:19 +02:00
require 'highline/import'
require 'yaml'
usage 'post [options]'
aliases :p
summary 'Add a new post'
description 'Adds a new post, filling in and validating metadata.'
def bold_say(str)
say "<%= color %(#{str}), :bold %>"
end
def bold_ask(str, *args)
2016-10-29 22:04:20 +02:00
res = ask "<%= color %(#{str}), :bold %>", *args
puts
res
2016-08-14 10:12:19 +02:00
end
run do |_opts, _args, _cmd|
result_hash = {}
2016-10-29 22:04:20 +02:00
post_directory = 'content/posts'
author_file = '.author-information'
2016-08-14 10:12:19 +02:00
bold_say "Let's make a new post, shall we?"
bold_say('-' * 20)
2016-10-29 22:04:20 +02:00
last_entry = "#{post_directory}/#{Dir.entries('content/posts').last}"
2016-08-14 10:12:19 +02:00
result_hash['title'] = bold_ask 'What will the title be?'
2016-10-29 22:04:20 +02:00
result_hash['description'] = bold_ask 'Give a description of the event'
2016-08-14 10:12:19 +02:00
2016-10-29 22:04:20 +02:00
result_hash['author'] = if File.exist? author_file
File.read(author_file).chomp
else
bold_ask 'What is your name?'
end
2016-08-14 10:12:19 +02:00
2016-10-29 22:04:20 +02:00
result_hash['created_at'] = Date.today
2016-08-14 10:12:19 +02:00
2016-10-29 22:04:20 +02:00
filename = result_hash['title'].downcase.tr(' ', '-').gsub(/[^0-9A-Za-z-]/, '')
2016-08-14 10:12:19 +02:00
2016-10-29 22:04:20 +02:00
File.open("#{last_entry}/#{filename}.md", 'w') do |file|
file.write(result_hash.to_yaml + '---')
end
2016-08-14 10:12:19 +02:00
end