Sha256: 14e893c3bf34fd3935b0296e36a45d1170c8593acd8d5507316759ec11f4a0fd

Contents?: true

Size: 1.16 KB

Versions: 17

Compression:

Stored size: 1.16 KB

Contents

require 'English'
require 'epub/parser'
require 'epub/cfi'
require 'nokogiri'

def usage
  <<EOS

USAGE:
 ruby #{$PROGRAM_NAME} ELEMENT EPUB

EOS
end

def main(argv)
  elem_name = argv.shift
  epub_path = argv.shift
  if elem_name.nil? or epub_path.nil?
    abort usage
  end

  spine_step = EPUB::CFI::Step.new(6)

  epub = EPUB::Parser.parse(epub_path)
  epub.package.spine.each_itemref.with_index do |itemref, i|
    assertion = itemref.id ? EPUB::CFI::IDAssertion.new(itemref.id) : nil
    itemref_step = EPUB::CFI::Step.new((i + 1) * 2, assertion)
    path_to_itemref = EPUB::CFI::Path.new([spine_step, itemref_step])
    itemref.item.content_document.nokogiri.search(elem_name).each do |elem|
      path = find_path(elem)
      location = EPUB::CFI::Location.new([path_to_itemref, path])
      puts
      puts location
      puts elem
    end
  end
end

def find_path(elem)
  steps = []
  until elem.parent.document?
    index = elem.parent.element_children.index(elem)
    assertion = elem["id"] ? EPUB::CFI::IDAssertion.new(elem["id"]) : nil
    steps.unshift EPUB::CFI::Step.new((index + 1) * 2, assertion)
    elem = elem.parent
  end
  EPUB::CFI::Path.new(steps)
end

main ARGV

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
epub-parser-0.4.8 examples/find-elements-and-cfis.rb
epub-parser-0.4.7 examples/find-elements-and-cfis.rb
epub-parser-0.4.6 examples/find-elements-and-cfis.rb
epub-parser-0.4.5 examples/find-elements-and-cfis.rb
epub-parser-0.4.4 examples/find-elements-and-cfis.rb
epub-parser-0.4.3 examples/find-elements-and-cfis.rb
epub-parser-0.4.2 examples/find-elements-and-cfis.rb
epub-parser-0.4.1 examples/find-elements-and-cfis.rb
epub-parser-0.4.0 examples/find-elements-and-cfis.rb
epub-parser-0.3.9 examples/find-elements-and-cfis.rb
epub-parser-0.3.8 examples/find-elements-and-cfis.rb
epub-parser-0.3.7 examples/find-elements-and-cfis.rb
epub-parser-0.3.6 examples/find-elements-and-cfis.rb
epub-parser-0.3.5 examples/find-elements-and-cfis.rb
epub-parser-0.3.4 examples/find-elements-and-cfis.rb
epub-parser-0.3.3 examples/find-elements-and-cfis.rb
epub-parser-0.3.2 examples/find-elements-and-cfis.rb