Class: Thepub::Epub::NCX

Inherits:
Object
  • Object
show all
Includes:
ContainerItem
Defined in:
lib/thepub/epub/ncx.rb

Defined Under Namespace

Classes: DocTitle, Head, NavMap, NavPoint

Instance Attribute Summary collapse

Attributes included from ContainerItem

#file_path, #media_type

Instance Method Summary collapse

Methods included from ContainerItem

#document?

Constructor Details

#initialize(uid, file_path = 'toc.ncx') ⇒ NCX

Returns a new instance of NCX.



10
11
12
13
14
15
16
# File 'lib/thepub/epub/ncx.rb', line 10

def initialize(uid, file_path = 'toc.ncx')
  @file_path = file_path
  @media_type = 'application/x-dtbncx+xml'
  @head = Head.new(uid)
  @doc_title = DocTitle.new('Untitled')
  @nav_map = NavMap.new
end

Instance Attribute Details

Returns the value of attribute nav_map.



26
27
28
# File 'lib/thepub/epub/ncx.rb', line 26

def nav_map
  @nav_map
end

Instance Method Details

#saveObject



43
44
45
46
47
# File 'lib/thepub/epub/ncx.rb', line 43

def save
  File.open(@file_path, 'w') do |f|
    f << to_xml
  end
end

#titleObject



18
19
20
# File 'lib/thepub/epub/ncx.rb', line 18

def title
  @doc_title.text
end

#title=(text) ⇒ Object



22
23
24
# File 'lib/thepub/epub/ncx.rb', line 22

def title=(text)
  @doc_title = DocTitle.new(text)
end

#to_xmlObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/thepub/epub/ncx.rb', line 28

def to_xml
  out = ''
  builder = Builder::XmlMarkup.new(:target => out)
  builder.instruct!
  builder.declare! :DOCTYPE, :ncx, :PUBLIC, "-//NISO//DTD ncx 2005-1//EN", "http://www.daisy.org/z3986/2005/ncx-2005-1.dtd"
  builder.ncx :xmlns => "http://www.daisy.org/z3986/2005/ncx/", :version => "2005-1" do
    @nav_map.calc_depth_and_play_order
    @head.depth = @nav_map.depth
    @head.to_xml(builder)
    @doc_title.to_xml(builder)
    @nav_map.to_xml(builder)
  end
  out
end