-
Notifications
You must be signed in to change notification settings - Fork 0
/
bible.rb
287 lines (229 loc) · 7.47 KB
/
bible.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
require 'launchy'
require 'yaml'
require 'time'
require './bible_organizer.rb' #gives us global variables $bible_collection, $abbreviations, $replacements
module Menuable
def make_numbered_list(array)
while (num ||= 1) <= array.count
puts "[#{num}] " + array[num-1].to_s
num += 1
end
end
def user_choice(list, prompt = "Make your selection")
chosen_numeral, first_try = nil, true
until (1..list.count).to_a.include?(chosen_numeral)
puts "** Invalid Selection ** \n\n" if first_try == false
make_numbered_list(list)
print "#{prompt} (1"
print " - #{list.count}" if list.count > 1
print "): "
chosen_numeral = gets.chomp.strip.to_i
first_try = false
system "clear" or system "cls"
end
return list[chosen_numeral - 1] # the item chosen
end
end
module Displayable
def separator
@separator = "-" * 80 + "\n"
end
def clear_screen
system "clear" or system "cls"
end
def wrap(s, width = 70, leading_numeral)
if leading_numeral.to_i < 10 and leading_numeral.to_i != 0
indent = " "
# Make a larger indentation for two-digit numbers
elsif leading_numeral.to_i >= 10
indent = " "
else
indent = ""
end
s.gsub(/(.{1,#{width}})(\s+|\Z)/, "\\1\n#{indent}")
end
def strip_html(html_string)
html_string.gsub(%r{</?[^>]+?>}, '')
end
def display(content, destination = 'Terminal', path_to_temp_file = './browser/index.html', path_to_template = './browser/template.html')
case destination
when 'Terminal'
puts strip_html(content)
run("", false)
when 'Browser'
content = content.gsub(separator, "\n")
htmlContent = File.read(path_to_template).gsub('{% content %}', content) # replace template token
File.write(path_to_temp_file, htmlContent)
Launchy.open(path_to_temp_file)
puts "Displaying results in browser."
run("", false)
end
end
end
module Historyable
def read_history
@history ||= []
if !File.exist?("history.yml")
File.open("history.yml", 'w') {|f| f.write("") }
end
@history = YAML.load_file("history.yml")
end
def save_to_history(passage)
@history.push({"#{Time.now}" => passage})
File.open('history.yml', 'w') do |file|
file.write(@history.to_yaml)
end
end
def clear_history
@history = []
File.open('history.yml', 'w') do |file|
file.write(@history.to_yaml)
end
run("History cleared")
end
end
class Bible
include Menuable
include Displayable
include Historyable
attr_accessor :bible, :books, :version, :top_menu, :where_to_display, :history
def initialize(version, scripture_hash, where_to_display)
read_history
@bible = scripture_hash
@books = @bible.keys
@version = version
@top_menu = ["Choose Book and Chapter", "Passage Lookup", "Proverb of the Day", "Psalms of the Day", "Proverb and Psalms of the Day", "History", "Quit"]
@where_to_display = where_to_display
end
def run(flash_message = "", clear_first = true)
clear_screen if clear_first == true
puts flash_message + "\n"
puts "Bible Version: #{version}"
puts "Make your selection below:"
puts separator
selection = user_choice(top_menu)
clear_screen
eval(selection.downcase.gsub(" ", "_"))
end
def quit
puts "Goodbye! \n\n"
exit
end
def choose_book_and_chapter
chosen_book = user_choice(books, "Select a book")
clear_screen
puts chosen_book.upcase + "\n"
available_chapters = bible[chosen_book].keys.map(&:to_i).sort
chosen_chapter = user_choice(available_chapters, "Select a chapter")
passage = "#{chosen_book} #{chosen_chapter.to_s}"
save_to_history(passage)
puts separator
display(make_passage(passage), where_to_display)
end
def passage_lookup
puts "Enter a passage (e.g. Romans 3:5-10 or 1 John 2): "
passage_string = gets.chomp.strip.downcase
save_to_history(passage_string)
display(make_passage(passage_string), where_to_display)
end
def history
puts "Choose a passage to look up again: "
if @history.any?
items = []
@history.each_with_index do |entry, index|
entry.each do |key, value|
t = Time.parse(key)
items.push("#{t.strftime("%e %b %Y %l:%M%p")}: #{value}")
end
end
items.push("Clear History", "Go Back")
choice = user_choice(items)
if choice == "Clear History"
clear_history
elsif choice == "Go Back"
run()
else
passage = choice.split(': ')[1]
display(make_passage(passage), where_to_display)
end
else
run("***History is empty***")
end
end
def proverb_of_the_day(display_it = true)
date_day = Time.now.day.to_s
passage = make_passage("Proverbs #{date_day}")
display(passage, where_to_display) if display_it == true
return passage
end
def psalms_of_the_day(display_it = true)
date_day = Time.now.day
assembled_psalms = ""
until (chapter_number ||= date_day) > 150
assembled_psalms += make_passage("Psalms #{chapter_number}")
chapter_number += 30
end
display(assembled_psalms, where_to_display) if display_it == true
return assembled_psalms
end
def proverb_and_psalms_of_the_day
assembled_passages = proverb_of_the_day(false) + psalms_of_the_day(false)
# Add bonus chapter from John (small hack for now)
assembled_passages += make_passage("John #{Time.now.day}") if Time.now.day < 22
display(assembled_passages, where_to_display)
end
def make_passage(input)
$abbreviations.each_with_index do |str, index| # arrays defined in correct_bibles.rb
input.gsub!(str, $replacements[index])
end
query = input.downcase.chomp.strip.split # splits at spaces
case query.count
when 3
book = "#{query[0]} #{query[1].capitalize}" # book name with ordinal, e.g. "1 Chronicles"
chapverse = query[2]
when 2
book = query[0].capitalize # book name without ordinal e.g. "Romans"
chapverse = query[1]
else
if query[0].downcase.include?('song') # Song of Solomon special case
book = "Song of Solomon"
chapverse = query[3]
else
run("Invalid Search")
end
end
verses = []
if chapverse.include?(':') # we want specific verse(s)
chapter = chapverse.split(':').first
verse_statement = chapverse.split(':').last
if verse_statement.include?('-') # it's a range of verses
verse_range = (verse_statement.split('-').first.to_i..verse_statement.split('-').last.to_i)
verse_range.to_a.each {|verse_number| verses << verse_number.to_s}
else # single verse
verses << verse_statement
end
else # There are no verses given; only a chapter is provided, e.g. "John 3"
chapter = chapverse
bible[book][chapter].keys.each {|verse_number| verses << verse_number } # store every verse in the chapter in verses
end
content = "\n<div class='passage'>\n" + separator + "<h2>#{input.upcase}\n</h2>" + separator
verses.sort_by(&:to_i).each do |verse|
verse_text = wrap(bible[book][chapter][verse], 70, verse)
content += "\n<p class='verse"
content += " single-digit" if verse.to_i < 10
content += "'>\n<span class='verse_number'>#{verse}.</span> " + verse_text + "</p>\n\n"
end
return content += "\n<div class='citation'>- #{version}</div>\n#{separator}\n</div><!--.passage-->\n"
end
end
include Menuable
include Displayable
bible_collection = File.open("bible_collection.marshal", "r"){|from_file| Marshal.load(from_file)}
clear_screen
puts "Choose a Bible Version:"
version_to_load = user_choice(bible_collection.keys)
display_options = ["Terminal", "Browser"]
puts "Choose where to display the Bible text:"
where_to_display = user_choice(display_options)
bible_to_run = Bible.new("#{version_to_load.upcase}", bible_collection[version_to_load], where_to_display)
bible_to_run.run("Welcome")