{file:docs/Home.markdown} > **{file:docs/Searcher.markdown}** = Searcher *Searcher is experimental now. Note that all interfaces are not stable at all.* == Example ---- epub = EPUB::Parser.parse('childrens-literature.epub') search_word = 'INTRODUCTORY' results = EPUB::Searcher.search_text(epub, search_word) # => [#], # @parent_steps= # [#"spine", :id=>nil}, @type=:element>, # ##nil}, @type=:itemref>, # ##"body", :id=>nil}, @type=:element>, # ##"nav", :id=>"toc"}, @type=:element>, # ##"ol", :id=>"tocList"}, @type=:element>, # ##"li", :id=>"np-313"}, @type=:element>, # ##"ol", :id=>nil}, @type=:element>, # ##"li", :id=>"np-317"}, @type=:element>, # ##"a", :id=>nil}, @type=:element>, # ##], # @start_steps=[#]>, # #], # @parent_steps= # [#"spine", :id=>nil}, @type=:element>, # ##nil}, @type=:itemref>, # ##"body", :id=>nil}, @type=:element>, # ##"section", :id=>"pgepubid00492"}, @type=:element>, # ##"section", :id=>"pgepubid00498"}, @type=:element>, # ##"h3", :id=>nil}, @type=:element>, # ##], # @start_steps=[#]>] puts results.collect(&:to_cfi).collect(&:to_fragment) # epubcfi(/6/4!/4/2[toc]/4[tocList]/2[np-313]/4/4[np-317]/2/1,:0,:12) # epubcfi(/6/6!/4/2[pgepubid00492]/8[pgepubid00498]/4/1,:0,:12) # => nil ---- == Search result Search result is an array of {EPUB::Searcher::Result} and it may be converted to an EPUBCFI string by {EPUB::Searcher::Result#to_cfi_s}. == Seamless XHTML Searcher Now default searcher for XHTML is *seamless* searcher, which ignores tags when searching. You can search words 'search word' from XHTML document below: ---- Sample document

search word

---- == Restricted XHTML Searcher You can also use *restricted* searcher, which means that it can search from only single elements. For instance, it can find 'search word' from XHTML document below: ---- Sample document

search word

---- But cannot from document below: ---- Sample document

search word

---- because the words 'search' and 'word' are not in the same element. To use restricted searcher, specify `algorithm` option for `search` method: results = EPUB::Searcher.search_text(epub, search_word, algorithm: :restricted) == Element Searcher You can search XHTML elements by CSS selector or XPath. ---- EPUB::Searcher::Publication.search_element(@package, css: 'ol > li').collect {|result| result[:location]}.map(&:to_fragment) # => ["epubcfi(/4/4!/4/2[toc]/4[tocList]/2[np-313])", # "epubcfi(/4/4!/4/2[toc]/4[tocList]/2[np-313]/4/2[np-315])", # "epubcfi(/4/4!/4/2[toc]/4[tocList]/2[np-313]/4/4[np-317])", # "epubcfi(/4/4!/4/2[toc]/4[tocList]/2[np-313]/4/6)", # "epubcfi(/4/4!/4/2[toc]/4[tocList]/2[np-313]/4/6/4/2[np-319])", # "epubcfi(/4/4!/4/2[toc]/4[tocList]/2[np-313]/4/6/4/2[np-319]/4/2)", # : # : ---- == Search by EPUB CFI You can fetch XML node from EPUB document by EPUB CFI. ---- require "epub/parser" require "epub/searcher" epub = EPUB::Parser.parse("childrens-literature.epub") cfi = EPUB::CFI("/6/4!/4/2[toc]/4[tocList]/2[np-313]/4/4[np-317]") itemref, node = EPUB::Searcher.search_by_cfi(epub, cfi) puts itemref.item.full_path puts node # EPUB/nav.xhtml #
  • # INTRODUCTORY #
  • ----