-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_post
executable file
·53 lines (42 loc) · 1.18 KB
/
_post
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env ruby
=begin
Script to kickstart a new jekyll post.
Usage: $ ./new_post.rb
=end
require 'date'
require 'rubygems'
require 'json'
require 'open-uri'
def get_input(message)
print message
gets.chomp.strip
end
def translate(src, to = 'en')
base_uri = 'http://ajax.googleapis.com/ajax/services/language'
begin
detect = JSON.parse(open("#{base_uri}/detect?v=1.0&q=#{URI.encode(src)}").read)
from = detect['responseData']['language']
langpair = "langpair=#{from}%7C#{to}"
result = JSON.parse(open("#{base_uri}/translate?v=1.0&q=#{URI.encode(src)}&#{langpair}").read)
result['responseData']['translatedText']
rescue Exception => e
"Google: Translation Faild."
end
end
title = translate((get_input 'Post Title: ')).downcase.gsub(' ', '-')
speaker = get_input 'Speaker Name: '
extension = 'markdown'
today = Date.today
path = "_posts/#{today}-#{title}.#{extension}"
header = <<HEADER
---
layout: post
title: #{title}
speaker: #{speaker}
---
{{ page.title }}
================================================================================
HEADER
File.open(path, 'w') { |f| f.write(header) }
system("$EDITOR #{path}")