Class: EPUB::Search::Formatter::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/epub/search/formatter/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(data, word, highlight = false) ⇒ CLI

Returns a new instance of CLI.



5
6
7
# File 'lib/epub/search/formatter/cli.rb', line 5

def initialize(data, word, highlight=false)
  @data, @word, @highlight = data, word, highlight
end

Instance Method Details

#formatObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/epub/search/formatter/cli.rb', line 9

def format
  word_re = /(?<word>#{Regexp.escape(@word)})/io
  highlighter = HighLine.Style(:red, :bold) if highlight?
  gray = HighLine.Style(:gray) if highlight?
  @data.each_pair do |location, records|
    records.each do |record|
      record.content.each_line do |line|
        next unless line =~ word_re
        result = line.chomp
        result.gsub! word_re, highlighter.color($~[:word]) if highlight?
        book_info = "[#{record.page_title}(#{record.book_title}): #{location} - #{record.iri}]"
        book_info = gray.color(book_info)
        result << book_info
        puts result
      end
    end
  end
end

#highlight?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/epub/search/formatter/cli.rb', line 28

def highlight?
  @highlight
end