Sha256: b840cb8774dc6f64bbc75c8343c01331fb6dce09e097324f4713b85df76cabe6

Contents?: true

Size: 1.43 KB

Versions: 6

Compression:

Stored size: 1.43 KB

Contents

require_relative 'table_of_contents/resource'
require_relative 'table_of_contents/navigation'

module EPUBInfo
  module Models
    class TableOfContents
      def initialize(parser)
        document = parser.metadata_document
        document_type = parser.metadata_type

        return if document.nil? || !document_type.eql?("application/oebps-package+xml")
        document.remove_namespaces!
        metadata = document.css('metadata')
        self.spine = metadata.xpath('//spine')
        self.manifest = metadata.xpath('//manifest')
        self.ncx   = Navigation.new(self)
        self.parser = parser
      end


      def type
        spine.first.attr('toc')
      end

      def resources
        @resources ||= Resource.new(self)
      end

      def document
        @toc_document ||= load_toc_file.remove_namespaces!
      end

      def path
        @toc_path ||= begin
          spine_path = nil
          if spine && !spine.empty?
            toc_id = spine[0]['toc']
            toc_ncx = manifest.xpath("item[@id = '#{toc_id}']").first.attr('href')
            spine_path = parser.zip_file.entries.map { |p| p.name }.select { |s| s.match(toc_ncx) }.first
          end
          spine_path
        end

      end

      attr_accessor :manifest
      attr_accessor :parser
      attr_accessor :spine
      attr_accessor :ncx

      private
      def load_toc_file
        Nokogiri::XML(parser.zip_file.read(path))
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
epubinfo_with_toc-0.5.7 lib/epubinfo/models/table_of_contents.rb
epubinfo_with_toc-0.5.6 lib/epubinfo/models/table_of_contents.rb
epubinfo_with_toc-0.5.5 lib/epubinfo/models/table_of_contents.rb
epubinfo_with_toc-0.5.4 lib/epubinfo/models/table_of_contents.rb
epubinfo_with_toc-0.5.2 lib/epubinfo/models/table_of_contents.rb
epubinfo_with_toc-0.5.0 lib/epubinfo/models/table_of_contents.rb