Class: EPUBMaker::EPUBv2

Inherits:
EPUBCommon show all
Defined in:
lib/epubmaker/epubv2.rb

Overview

EPUBv2 is EPUB version 2 producer.

Instance Method Summary collapse

Methods inherited from EPUBCommon

#colophon, #colophon_history, #container, #cover, #date_to_s, #flat_ncx, #hierarchy_ncx, #join_with_separator, #legacy_cover_and_title_file, #mimetype, #mytoc, #ncx_doctitle, #ncx_isbn, #ncx_navmap, #opf_coverimage, #opf_path, #produce_write_common, #titlepage

Constructor Details

#initialize(producer) ⇒ EPUBv2

Construct object with parameter hash config and message resource hash res.



19
20
21
# File 'lib/epubmaker/epubv2.rb', line 19

def initialize(producer)
  super
end

Instance Method Details

#ncx(indentarray) ⇒ Object

Return ncx content. indentarray has prefix marks for each level.



114
115
116
117
118
119
120
121
# File 'lib/epubmaker/epubv2.rb', line 114

def ncx(indentarray)
  @ncx_isbn = ncx_isbn
  @ncx_doctitle = ncx_doctitle
  @ncx_navmap = ncx_navmap(indentarray)

  tmplfile = File.expand_path('./ncx/epubv2.ncx.erb', ReVIEW::Template::TEMPLATE_DIR)
  ReVIEW::Template.load(tmplfile).result(binding)
end

#opfObject

Return opf file content.



24
25
26
27
28
29
30
31
32
33
# File 'lib/epubmaker/epubv2.rb', line 24

def opf
  @opf_metainfo = opf_metainfo
  @opf_coverimage = opf_coverimage
  @opf_manifest = opf_manifest
  @opf_toc = opf_tocx

  tmplfile = File.expand_path('./opf/epubv2.opf.erb', ReVIEW::Template::TEMPLATE_DIR)
  tmpl = ReVIEW::Template.load(tmplfile)
  tmpl.result(binding)
end

#opf_manifestObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/epubmaker/epubv2.rb', line 75

def opf_manifest
  s = ''
  s << <<EOT
  <manifest>
<item id="ncx" href="#{@producer.config['bookname']}.ncx" media-type="application/x-dtbncx+xml"/>
<item id="#{@producer.config['bookname']}" href="#{@producer.config['cover']}" media-type="application/xhtml+xml"/>
EOT

  s << %Q(    <item id="toc" href="#{@producer.config['bookname']}-toc.#{@producer.config['htmlext']}" media-type="application/xhtml+xml"/>\n) if @producer.config['toc'] && @producer.config['mytoc']

  @producer.contents.each do |item|
    next if item.file =~ /#/ # skip subgroup
    s << %Q(    <item id="#{item.id}" href="#{item.file}" media-type="#{item.media}"/>\n)
  end
  s << %Q(  </manifest>\n)
  s
end

#opf_metainfoObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/epubmaker/epubv2.rb', line 35

def opf_metainfo
  s = ''
  %w[title language date type format source description relation coverage subject rights].each do |item|
    next unless @producer.config[item]
    if @producer.config[item].is_a?(Array)
      s << @producer.config.names_of(item).map { |i| %Q(    <dc:#{item}>#{CGI.escapeHTML(i)}</dc:#{item}>\n) }.join
    else
      s << %Q(    <dc:#{item}>#{CGI.escapeHTML(@producer.config.name_of(item).to_s)}</dc:#{item}>\n)
    end
  end

  # ID
  if @producer.config['isbn'].nil?
    s << %Q(    <dc:identifier id="BookId">#{@producer.config['urnid']}</dc:identifier>\n)
  else
    s << %Q(    <dc:identifier id="BookId" opf:scheme="ISBN">#{@producer.config['isbn']}</dc:identifier>\n)
  end

  # creator (should be array)
  %w[aut a-adp a-ann a-arr a-art a-asn a-aqt a-aft a-aui a-ant a-bkp a-clb a-cmm a-dsr a-edt a-ill a-lyr a-mdc a-mus a-nrt a-oth a-pht a-prt a-red a-rev a-spn a-ths a-trc a-trl].each do |role|
    next unless @producer.config[role]
    @producer.config.names_of(role).each do |v|
      s << %Q(    <dc:creator opf:role="#{role.sub('a-', '')}">#{CGI.escapeHTML(v)}</dc:creator>\n)
    end
  end

  # contributor (should be array)
  %w[adp ann arr art asn aqt aft aui ant bkp clb cmm dsr edt ill lyr mdc mus nrt oth pht prt red rev spn ths trc trl].each do |role|
    next unless @producer.config[role]
    @producer.config.names_of(role).each do |v|
      s << %Q(    <dc:contributor opf:role="#{role}">#{CGI.escapeHTML(v)}</dc:contributor>\n)
      if role == 'prt'
        s << %Q(    <dc:publisher>#{v}</dc:publisher>\n)
      end
    end
  end

  s
end

#opf_tocxObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/epubmaker/epubv2.rb', line 93

def opf_tocx
  cover_linear = if @producer.config['epubmaker']['cover_linear'] && @producer.config['epubmaker']['cover_linear'] != 'no'
                   'yes'
                 else
                   'no'
                 end

  s = ''
  s << %Q(  <spine toc="ncx">\n)
  s << %Q(    <itemref idref="#{@producer.config['bookname']}" linear="#{cover_linear}"/>\n)
  s << %Q(    <itemref idref="toc" />\n) unless @producer.config['mytoc'].nil?

  @producer.contents.each do |item|
    next if item.media !~ /xhtml\+xml/ # skip non XHTML
    s << %Q(    <itemref idref="#{item.id}"/>\n)
  end
  s << %Q(  </spine>\n)
  s
end

#produce(epubfile, basedir, tmpdir) ⇒ Object

Produce EPUB file epubfile. basedir points the directory has contents. tmpdir defines temporary directory.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/epubmaker/epubv2.rb', line 126

def produce(epubfile, basedir, tmpdir)
  produce_write_common(basedir, tmpdir)

  File.open("#{tmpdir}/OEBPS/#{@producer.config['bookname']}.ncx", 'w') do |f|
    @producer.ncx(f, @producer.config['epubmaker']['ncxindent'])
  end
  if @producer.config['mytoc']
    File.open("#{tmpdir}/OEBPS/#{@producer.config['bookname']}-toc.#{@producer.config['htmlext']}", 'w') do |f|
      @producer.mytoc(f)
    end
  end

  @producer.call_hook(@producer.config['epubmaker']['hook_prepack'], tmpdir)
  expoter = EPUBMaker::ZipExporter.new(tmpdir, @producer.config)
  expoter.export_zip(epubfile)
end