Class: BbEPUB::Transform::NavToc

Inherits:
Bookbinder::Transform
  • Object
show all
Defined in:
lib/bb-epub/transform/nav_toc.rb

Instance Method Summary collapse

Instance Method Details

#dependenciesObject



3
4
5
# File 'lib/bb-epub/transform/nav_toc.rb', line 3

def dependencies
  [BbEPUB::Transform::Nav, BbEPUB::Transform::NCX]
end

#from_map(package) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bb-epub/transform/nav_toc.rb', line 33

def from_map(package)
  package.if_file(:ncx) { |ncx_file|
    @max_depth = 0
    @chapter_count = 0
    toc = (package.map['nav'] || {})['toc'] || []
    build_ncx_map(ncx_file.document, toc)
    build_ncx_depth_meta(ncx_file.document, @max_depth)
  }
  package.if_file(:nav) { |nav_file|
    toc = (package.map['nav'] || {})['toc'] || []
    build_nav_tag(nav_file.document, toc)
  }
end

#to_map(package) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bb-epub/transform/nav_toc.rb', line 8

def to_map(package)
  toc = []
  package.if_file(:nav) { |nav_file|
    nav_doc = nav_file.document('r')
    base_path = content_rooted_path(package.content_root, nav_file.path)
    nav_doc.each('nav[epub|type="toc"] > ol > li') { |li|
      toc.push(extract_nav_chapter(nav_doc, li, base_path))
    }
  }
  unless toc.any?
    package.if_file(:ncx) { |ncx_file|
      ncx_doc = ncx_file.document
      base_path = content_rooted_path(package.content_root, ncx_file.path)
      ncx_doc.each('ncx|navMap > ncx|navPoint') { |point|
        toc.push(extract_ncx_chapter(ncx_doc, point, base_path))
      }
    }
  end
  if toc.any?
    package.map['nav'] ||= {}
    package.map['nav']['toc'] = toc
  end
end