forked from arkadianriver/arkadianriver.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compose.rb
223 lines (192 loc) · 6.14 KB
/
compose.rb
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/ruby
#
# my first ruby script!
# (could surely be better, but it gets the job done well enough)
#
# Walks through creating a new post rather than reading everything
# in the "Guide to New Posts" topic and rather than typing the filename
# and all by hand, like jekyll-compose, but specific to this repo.
#
system Gem.win_platform? ? "cls" : "clear"
# for getting the name of your editor and extension
require 'yaml'
cfgfile = YAML.load_file('_config.yml')
# getting the default author (first in authors file if exist)
authfile = "./_data/authors.yml"
if (File.exist?(authfile))
author = YAML.load_file(authfile).keys[0]
end
# for getting user input to each question
def getinput(question, default = '')
askuser = question + " ('q' to quit)"
askuser << (!default.empty? ? " [#{default}]: " : ": ")
puts askuser
myui = gets.chomp
if myui.casecmp('q').zero? then
puts "Okay, g'bye!"
exit
end
return myui
end
# for print stmt buffers
STDOUT.sync = true
#
# interview
#
puts <<-eoh
This script helps you create a post. It adds it to the _drafts folder.
(Hit Ctrl-C at any time to cancel. Default values are shown in brackets;
to accept the default you can just hit return.)
This site has two types of posts:
- Topics: blog posts
- Works: portfolio entries
eoh
type = ''
while true
type = getinput("Is this a (t)opic or (w)ork?", "t")
unless (type == '' or /^[tw]$/.match(type)) then redo end
if type=='' then type = 't' end
break
end
categories = Array.new
if type=='w'
categories << 'works'
puts <<-eoh
Higher priority works are listed first and also get the same priority value
for the site map. 1.0 is the highest possible value, and you probably don't
want it to have a priority under 0.5. What priority should it have?
eoh
priority = ''
while true
priority = getinput("(0 - 1.0)", "0.7")
unless (priority == '' or (priority.to_f >= 0.0 and priority.to_f <= 1.0)) then redo end
if priority == '' then priority = '0.7' end
break
end
else
categories << 'topics'
puts <<-eoh
The first 10 featured posts are listed on the front page and get a higher
priority in the site map. Is this a featured post?
eoh
featured = ''
while true
featured = getinput("y or [n]")
unless (featured == '' or /^[yn]$/.match(featured)) then redo end
if (featured=='' or featured=='n') then featured = nil end
break
end
puts <<-eoh
If this is a part of a series of posts that you want to be displayed with
back/next links, you can specify a sub-category name. Note that only the
first topic in the sub-category is listed in the topics index. Also note
that categories should probably have alphanumeric, underscores, hyphens,
and space characters only. Tip: I title my first topic in the sub-category
with the same name as the sub-category so that the breadcrumb looks nice.
eoh
isgroup = getinput("So, is this part of a sub-category grouping? (y or [n])")
if isgroup == 'y'
trapped = true
subcat = ''
while trapped do
catch :trapped do
print "\nSub-category name: "
subcat = gets
subcat.strip!
if subcat == '' then throw :trapped end
subcat.each_char do |c|
unless (/[0-9A-Za-z_\- ]/.match(c))
puts "Use ASCII alphanumerics, underscores, hyphens, and spaces only."
throw :trapped
end
end
trapped = false
end
end
categories << subcat
isfirst = ''
while true
isfirst = getinput("\nIs this your first topic in the sub-category? (y or [n])")
unless (isfirst == '' or /^[yn]$/.match(isfirst)) then redo end
if isfirst == 'y'
perma = '/' + categories.join('/') + '/'
end
break
end
end
end
while true
title = getinput("\nPost title")
break unless title == ''
puts "Well you gotta call it something."
end
excerpt = getinput("\nPost excerpt")
if type == 't'
theauth = getinput("\nPost author", author)
if theauth == '' then theauth = author end
end
puts <<-eoc
If you want an image in the banner, put the image in the /images folder
and type the name here. If it's in another folder, prefix the image with
'/' and the folder name, such as /unique/images/mine.jpg. For no image,
just hit Enter.
eoc
back = getinput("background-image")
back = back == '' ? nil : back
puts <<-eoc
There are a few other options you could add, such as a mini-heading, a
full-width body, and a custom icon for featured posts on the front page.
For a full description of options, see the "Guide for New Posts" topic
when you run jekyll serve --future.
Otherwise, hit Enter to create this starter post.
eoc
letsdothis = gets
#
# a bit of formatting and date
#
tslug = title.dup
# replace illegal characters on Win/Linux filesystems as well as URLs with '-'
[' ','\\','/','<','>',':','"','|','?','*','#','%'].each do |x|
tslug.gsub!(x,'-')
end
tslug.downcase!
tstamp = Time.now.to_s
dstamp = tstamp.split.first
ext = cfgfile['compose']['extension'] ? cfgfile['compose']['extension'] : 'md'
fname = dstamp + "-" + tslug + '.' + ext
#
# Done, let's print the post
#
STDOUT.flush
if (File.exist?("./_drafts/" + fname) or File.exist?("./_posts/" + fname))
puts <<-eom
Sorry you went through all that trouble, but the file
#{fname} already exists.
eom
else
unless (Dir.exist?("./_drafts/")) then Dir.mkdir("./_drafts") end
File.open("./_drafts/" + fname, 'w') do |f|
f.puts "---"
f.puts "title: " + title
f.puts "excerpt: " + excerpt
unless (theauth.to_s == '') then f.puts "author: " + theauth end
if (featured) then f.puts "tags: featured" end
if (priority) then f.puts "priority: " + priority end
if (perma) then f.puts "permalink: " + perma end
f.puts "categories:"
categories.each do |cat|
f.puts " - " + cat
end
if (back) then f.puts "background-image: " + back end
f.puts "#date/lastmod are optional"
f.puts "#date: " + tstamp
f.puts "#lastmod: " + tstamp
f.puts "---"
patharg = Gem.win_platform? ? ".\\_drafts\\" + fname : "./_drafts/" + fname
puts "File created: " + patharg
# open it in an editor if specified
if (cfgfile['compose']['editor'])
system cfgfile['compose']['editor'] + ' ' + patharg
end
end
end