class BomDB::Cli::Application
Public Instance Methods
align(file, edition = '1829')
click to toggle source
# 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
create()
click to toggle source
# 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
editions()
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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
version()
click to toggle source
# File lib/bomdb/cli/application.rb, line 12 def version puts BomDB::VERSION end
Private Instance Methods
format_from_file(file)
click to toggle source
# File lib/bomdb/cli/application.rb, line 355 def format_from_file(file) case File.extname(file).downcase when ".txt" then "text" when ".json" then "json" else $stderr.puts "Unable to determine format from file: #{file}" exit -1 end end
read(file)
click to toggle source
# File lib/bomdb/cli/application.rb, line 337 def read(file) File.read(relative_or_data_file(file)) end
relative_or_data_file(file)
click to toggle source
# File lib/bomdb/cli/application.rb, line 341 def relative_or_data_file(file) if File.exist?(file) file else if File.basename(file) == file # Try our gem's data directory as a fallback for this file File.join(BomDB.config.data_dir, file) else $stderr.puts "File not found: #{file}" exit -1 end end end
show_result_and_maybe_exit(result)
click to toggle source
# File lib/bomdb/cli/application.rb, line 375 def show_result_and_maybe_exit(result) if result.success? $stderr.puts "Success" else $stderr.puts result.message # $stderr.puts "Try again with '--reset'? (NOTE: data may be deleted)" exit -1 end end
type_from_file(file)
click to toggle source
# File lib/bomdb/cli/application.rb, line 365 def type_from_file(file) type = File.basename(file).gsub(/\.(txt|json)$/, '').downcase end
verse_format_for_alignment()
click to toggle source
# File lib/bomdb/cli/application.rb, line 369 def verse_format_for_alignment verse_format = lambda do |book, chapter, verse| "[|#{book} #{chapter}:#{verse}|]" end end