-
Notifications
You must be signed in to change notification settings - Fork 71
/
converter.rb
64 lines (53 loc) · 1.53 KB
/
converter.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
#!/usr/bin/env ruby
# format is like this
# #Q The author of the novel A Portrait of the Artist as a Young Man is this writer.
# ^ James Joyce
# A T. S. Eliot
# B Samuel Beckett
# C William Faulkner
# D James Joyce
# use converter like this:
# ruby converter.rb input_file
# this will create an input_file.json
# you can also pass multiple files like so
# ruby converter.rb input_file1 input_file2
# or even like so
# ruby converter.rb folder/*
require 'json'
require 'pathname'
def stripAndEncode(str)
return str.strip!.force_encoding('ISO-8859-1').encode('UTF-8')
end
ARGV.each do |file|
if File.directory?(file)
puts "#{file} is a directory. Skipping."
next
end
questions = []
question = {}
File.open(file, "r") do |fh|
while(line = fh.gets) != nil
line = stripAndEncode(line)
if line.start_with?('#Q ')
question[:question] = line[3..-1]
question[:category] = Pathname.new(file).basename
until (line = fh.gets) == nil || line.start_with?('^ ') do
line = stripAndEncode(line)
question[:question] << "\n"
question[:question] << line
end
line = stripAndEncode(line)
end
if line.start_with?('^ ')
question[:answer] = line[2..-1]
elsif ('A '..'Z ').include?(line[0..1])
question[:choices] ||= []
question[:choices] << line[2..-1]
elsif line.empty?
questions << question unless question.empty?
question = {}
end
end
end
File.write("#{file}.json", questions.to_json)
end