Class: EPUB::Searcher::Publication

Inherits:
Object
  • Object
show all
Defined in:
lib/epub/searcher/publication.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(word) ⇒ Publication

Returns a new instance of Publication.



12
13
14
# File 'lib/epub/searcher/publication.rb', line 12

def initialize(word)
  @word = word
end

Class Method Details

.search(package, word, **options) ⇒ Object



7
8
9
# File 'lib/epub/searcher/publication.rb', line 7

def search(package, word, **options)
  new(word).search(package, options)
end

Instance Method Details

#search(package, algorithm: :seamless) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/epub/searcher/publication.rb', line 16

def search(package, algorithm: :seamless)
  results = []

  spine = package.spine
  spine_step = Result::Step.new(:element, 2, {:name => 'spine', :id => spine.id})
  spine.each_itemref.with_index do |itemref, index|
    itemref_step = Result::Step.new(:itemref, index, {:id => itemref.id})
    XHTML::ALGORITHMS[algorithm].search(Nokogiri.XML(itemref.item.read), @word).each do |sub_result|
      results << Result.new([spine_step, itemref_step] + sub_result.parent_steps, sub_result.start_steps, sub_result.end_steps)
    end
  end

  results
end