/
Rakefile
227 lines (191 loc) · 5.89 KB
/
Rakefile
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
224
225
226
227
=begin
Rakefile created by Jae Hee Lee @ 2017
The rake files dependent on this main rakefile can be found in tasks/*.rake
=end
require "rubygems"
require "rake"
require 'securerandom'
require "time"
require "fileutils"
require "json"
require "yaml"
SOURCE = '.'
CONFIG = {
'version' => "1.0.0",
'layouts' => File.join(SOURCE, "_layouts"),
'posts' => File.join(SOURCE, "_posts"),
'categories' => File.join(SOURCE, "category"),
'data' => File.join(SOURCE, "_data"),
'post_ext' => "md",
'cat_file' => '_data/categories.json',
'trans_file' => '_data/localization.json',
'config_file' => '_config.yml'
}
=begin
Common Functions
=end
# asking user for further options
def ask(message, valid_options)
if valid_options
answer = get_stdin("#{message} #{valid_options.to_s.gsub(/"/, '').gsub(/, /,'/')} ") while !valid_options.include?(answer)
else
answer = get_stdin(message)
end
answer
end
def get_stdin(message)
print message
STDIN.gets.chomp
end
# creates category folder and index.md associated with that category
def create_cat(title, id, subcat_of = nil)
if(subcat_of == nil)
FileUtils::mkdir_p File.join(CONFIG['categories'], "#{id}")
filename = File.join(CONFIG['categories'], "#{id}/index.#{CONFIG['post_ext']}")
else
FileUtils::mkdir_p File.join(CONFIG['categories'], "#{subcat_of}/#{id}")
filename = File.join(CONFIG['categories'], "#{subcat_of}/#{id}/index.#{CONFIG['post_ext']}")
end
if(File.exist?(filename))
abort("rake aborted") if ask("#{filename} already exists. Overwrite?", ['y', 'n']) == 'n'
end
if(subcat_of != nil)
puts "Creating new subcategory: #{filename}."
open(filename, 'w') do |category|
category.puts "---"
category.puts "layout: post"
category.puts "title: #{title}"
category.puts "category: #{id}"
category.puts "---"
category.puts ""
category.puts "{% include category.html param = page.layout %}"
end
else
puts "Creating new category: #{filename}."
open(filename, 'w') do |category|
category.puts "---"
category.puts "layout: category"
category.puts "title: #{title}"
category.puts "category: #{id}"
category.puts "---"
category.puts ""
category.puts "{% include category.html param = page.layout %}"
end
end
end
#modifies category index.md file
def modify_cat(id, title = nil)
filename = File.join(CONFIG['categories'], "#{id}/index.#{CONFIG['post_ext']}")
puts "Modifying category " + id + " ..."
modify_cat_cont(id, false, title)
end
def modify_subcat(parent_id, child_id, title = nil)
filename = File.join(CONFIG['categories'], "#{parent_id}/#{child_id}/index.#{CONFIG['post_ext']}")
puts "Modifying subcategory " + parent_id + " belonging to category " + child_id + " ..."
modify_cat_cont(child_id, true, title)
end
def modify_cat_cont(id, isSubcat, title = nil)
extracted_title = ""
if(title == nil)
cat_file = File.read(CONFIG['cat_file'])
cat_file_hash = JSON.parse(cat_file)
cat_file_hash.each do |elem|
if !isSubcat
if( elem['id'] == id)
extracted_title = elem['title']
end
else
if (elem['subcategories'] != nil)
for subelem in elem['subcategories']
if( subelem['id'] == id)
extracted_title = subelem['title']
end
end
end
end
end
end
open(filename, 'w') do |category|
category.puts "---"
if !isSubcat
category.puts "layout: category"
else
category.puts "layout: post"
end
if(title == nil)
category.puts "title: #{extracted_title}"
else
category.puts "title: #{title}"
end
category.puts "category: #{id}"
category.puts "---"
category.puts ""
category.puts "{% include category.html param = page.layout %}"
end
end
# renames the specified directory
def rename_directory(prev, curr)
File.rename prev, curr
end
def delete_directory(parent, child)
FileUtils::rm_rf(parent + '/' + child)
puts parent + '/' + child + " directory has been deleted."
end
# takes an id and executes localization tasks
def localize(id)
#loading _config.yml to fetch languages option.
config_yml = YAML.load_file(CONFIG['config_file'])
available_langs = config_yml['languages']
translated = Hash.new
puts "Creating new localization data ..."
#localization step
for lang in available_langs
puts "Please enter " + lang + " translation for the category '" + id + "':"
ARGV.clear
response = gets.chomp()
translated[lang] = response
end
tempJSON = Hash.new
tempJSON[id] = translated
permJSON = tempJSON.to_json
permJSON[0] = "" # will have to remove the first { to concatenate to existing string.
#parsing
permJSON.insert(permJSON.index('{'), '[')
indexes = Array.new
for i in (0..permJSON.length)
if(permJSON[i] == ',')
indexes.push(i)
end
end
counter = 0
for idx in indexes
permJSON.insert(idx + counter, '}')
permJSON.insert(idx + counter + 2, '{')
counter += 2
end
permJSON.insert(permJSON.rindex('}'), ']')
#adding the localization for the category into localization.json file
File.truncate(CONFIG['trans_file'], File.size(CONFIG['trans_file']) - 1)
File.open(CONFIG['trans_file'], 'a') do |trans|
trans.write(',' + permJSON)
end
end
def validate(env_var)
if(ENV[env_var] == nil)
abort("rake aborted! Please provide " + env_var + " option.")
end
end
def directory_check(config_var)
config_var = CONFIG[config_var]
abort("rake aborted: '#{config_var}' directory not found.") unless FileTest.directory?(config_var)
end
def hash_write_to_json_file(hash_file, file_name)
# convert hash back to json
json_file = JSON.generate(hash_file)
# clears the actual file and write the result to it
File.open(CONFIG[file_name], 'w') do |cat|
cat.write(json_file)
end
end
# Loads all separate rake files
Dir['tasks/*.rake'].sort.each { |f| load f }