Class: BomDB::Cli::Application

Inherits:
Thor
  • Object
show all
Defined in:
lib/bomdb/cli/application.rb

Defined Under Namespace

Classes: Chapter

Instance Method Summary collapse

Instance Method Details

#align(file, edition = '1829') ⇒ Object



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/bomdb/cli/application.rb', line 271

def align(file, edition = '1829')
  io = StringIO.new

  BomDB::Query.new(edition: edition).print(
    verse_format: verse_format_for_alignment,
    linesep: ' ',
    io: io
  )
  if options[:'edition-only']
    puts io.string
    exit
  end

  dwdiff = Diff::Dwdiff.new(options[:dwdiff])
  align_str = File.read(file).gsub(/\s\s+/, ' ').gsub(':', '~')
  diff = dwdiff.diff(io.string, align_str)

  if options[:'diff-only']
    puts diff
    exit
  end

  puts Diff::Aligner.parse(diff).gsub('~', ':')
end

#createObject



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
# File 'lib/bomdb/cli/application.rb', line 87

def create
  db_path = options[:file]

  if File.exist?(db_path) and !options[:delete]
    puts "Database file '#{db_path}' exists. Delete? (y/N) "
    if $stdin.gets.chomp.downcase != "y"
      puts "Exiting..."
      exit -1
    end
    verbing = 'Overwriting'
  else
    verbing = 'Creating'
  end

  puts "#{verbing} database '#{db_path}' ..."
  schema = BomDB::Schema.create(db_path)
  puts "Created the following tables:"
  schema.db.tables.each{ |t| puts "\t#{t}" }

  unless options[:bare]
    puts "Importing books..."
    import('books.json')

    puts "Importing verses..."
    import('verses.json')

    puts "Importing editions..."
    import('editions.json')

    puts "Importing contents..."
    import('contents.json')

    puts "Importing refs..."
    import('refs.json')
  end

  puts "Done."
end

#editionsObject



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
# File 'lib/bomdb/cli/application.rb', line 205

def editions
  BomDB.db[:editions].
    left_outer_join(:contents, :edition_id => :edition_id).
    select_group(Sequel.qualify("editions", "edition_id"), :edition_name).
    select_append{ Sequel.as(count(:verse_id), :count) }.
    order(:edition_name).
  each do |r|
    if r[:count] > 0 || options[:all]
      puts "#{r[:count]} verses\t#{r[:edition_name]}"
    end
    if options[:"show-missing-verses"] && r[:count] > 0
      BomDB.db[
        "SELECT b.book_name, v.verse_chapter, v.verse_number " +
        "FROM verses v " +
        "JOIN books b ON v.book_id = b.book_id " +
        "WHERE v.verse_heading IS NULL " +
        "AND v.verse_id NOT IN " +
        "(" +
        " SELECT verse_id FROM contents c " +
        " WHERE c.edition_id = ? " +
        " AND c.verse_id = v.verse_id" +
        ") " +
        "ORDER BY b.book_sort, v.verse_chapter, v.verse_number",
        r[:edition_id]
      ].
      each do |s|
        puts "  #{s[:book_name]} #{s[:verse_chapter]}:#{s[:verse_number]} missing"
      end
    end
  end
end

#export(type) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bomdb/cli/application.rb', line 55

def export(type)
  format = options[:format].downcase

  exporter =
  case type
  when 'books'    then BomDB::Export::Books.new(BomDB.db)
  when 'verses'   then BomDB::Export::Verses.new(BomDB.db)
  when 'editions' then BomDB::Export::Editions.new(BomDB.db)
  when 'contents' then BomDB::Export::Contents.new(BomDB.db, edition_prefixes: options[:editions])
  # when 'refs'     then BomDB::Export::Refs.new(BomDB.db)
  else
    puts "Unknown import type #{type}"
    exit -1
  end

  result = exporter.export(format: format)
  if result.success?
    puts result.to_s
  else
    show_result_and_maybe_exit(result)
  end
end

#import(file) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bomdb/cli/application.rb', line 23

def import(file)
  type   = (options[:type]   || type_from_file(file)).downcase
  format = (options[:format] || format_from_file(file)).downcase

  importer =
  case type
  when 'books'    then BomDB::Import::Books.new(BomDB.db)
  when 'verses'   then BomDB::Import::Verses.new(BomDB.db)
  when 'editions' then BomDB::Import::Editions.new(BomDB.db)
  when 'contents' then BomDB::Import::Contents.new(BomDB.db, edition_prefix: options[:edition])
  when 'refs'     then BomDB::Import::Refs.new(BomDB.db)
  else
    puts "Unknown import type #{type}"
    exit -1
  end

  begin
    result = importer.import(read(file), format: format)
  rescue JSON::ParserError
    puts "Couldn't parse as JSON. Use '--format=text'?"
    exit -1
  end
  show_result_and_maybe_exit(result)
end

#index(wordgroup) ⇒ Object



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/bomdb/cli/application.rb', line 305

def index(wordgroup)
  wordgroup = wordgroup.to_i

  chapters = BomDB::Query.new.chapters.map do |(book, chapter), content|
    Chapter.new(content.split(/\s+/).size, book, chapter)
  end

  total_words = chapters.inject(0){ |sum, ch| sum += ch.wordcount }

  idx = []
  words_so_far = 0
  chapter_index = 0
  j = 0

  (wordgroup..(total_words+wordgroup)).step(wordgroup) do |i|
    while words_so_far < i && chapter_index < chapters.size
      words_so_far += chapters[chapter_index].wordcount
      chapter_index += 1
    end
    c = chapters[chapter_index-1]
    idx << ["#{c.book} #{c.chapter}", j]
    # puts "[#{chapters[chapter_index-1].book} #{chapters[chapter_index-1].chapter},#{j}"
    j += 1
  end

  puts idx.to_json
end

#references(type = nil) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/bomdb/cli/application.rb', line 240

def references(type=nil)
  if type.nil?
    rts = BomDB.db[:refs].
      select_group(:ref_name).
      select_append{ Sequel.as(count(ref_id), :count) }.
      map { |r| "#{r[:ref_name]} (#{r[:count]} refs)" }
    puts rts.join("\n")
  else
    rts = BomDB.db[:refs].
      where(:ref_name => type).
      join(:verses, :verse_id => :verse_id).
      join(:books, :book_id => :book_id).
      map do |r|
        asterisk = r[:ref_is_quotation] ? '*' : ''
        "#{r[:ref_book]} #{r[:ref_chapter]}:#{r[:ref_verse]} => " +
        "#{r[:book_name]} #{r[:verse_chapter]}:#{r[:verse_number]}" +
        "#{asterisk}"
      end
    puts rts.join("\n")
  end
end

#show(range = nil) ⇒ Object



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
# File 'lib/bomdb/cli/application.rb', line 151

def show(range = nil)
  body_format = nil
  wordsep = options[:sep]
  linesep = options[:linesep].gsub("\\n", "\n")

  if options[:"for-alignment"]
    linesep = " "
    verse_format = verse_format_for_alignment(options[:color])
  elsif options[:clean]
    linesep = " "
    verse_format = lambda{ |b,c,v| '' }
    body_format = lambda do |body|
      TextClean.clean(body)
    end
  else
    if options[:verses]
      if options[:markdown]
        verse_format = lambda do |b,c,v|
          '**' + b + wordsep + c.to_s + ':' + v.to_s + '**'
        end
      elsif options[:color]
        verse_format = lambda do |b,c,v|
          b.colorize(:yellow) + wordsep + 
          c.to_s.colorize(:green) + ':' + 
          v.to_s.colorize(:light_green)
        end
      else
        verse_format = nil
      end
    else
      verse_format = lambda{ |b,c,v| '' }
    end
  end
  BomDB::Query.new(
    edition: options[:edition],
    range: range,
    search: options[:search],
    exclude: options[:exclude],
    exclude_only_quotations: options[:"exclude-only-quotations"]
  ).print(
    verse_format: verse_format,
    body_format: body_format,
    sep:     wordsep,
    linesep: linesep
  )
end

#versionObject



12
13
14
# File 'lib/bomdb/cli/application.rb', line 12

def version
  puts BomDB::VERSION
end