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, #export_zip, #export_zip_extcmd, #export_zip_rubyzip, #export_zip_rubyzip_addpath, #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 params and message resource hash res.



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

def initialize(producer)
  super
end

Instance Method Details

#ncx(indentarray) ⇒ Object

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



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

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)
  tmpl = ReVIEW::Template.load(tmplfile)
  return tmpl.result(binding)
end

#opfObject

Return opf file content.



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

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)
  return tmpl.result(binding)
end

#opf_manifestObject



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

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

  s << %Q[    <item id="toc" href="#{@producer.params["bookname"]}-toc.#{@producer.params["htmlext"]}" media-type="application/xhtml+xml"/>\n] if @producer.params["toc"] && @producer.params["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



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
74
# File 'lib/epubmaker/epubv2.rb', line 36

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

  # ID
  if @producer.params["isbn"].nil?
    s << %Q[    <dc:identifier id="BookId">#{@producer.params["urnid"]}</dc:identifier>\n]
  else
    s << %Q[    <dc:identifier id="BookId" opf:scheme="ISBN">#{@producer.params["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.params[role]
    @producer.params.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.params[role]
    @producer.params.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



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

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

  s = ""
  s << %Q[  <spine toc="ncx">\n]
  s << %Q[    <itemref idref="#{@producer.params["bookname"]}" linear="#{cover_linear}"/>\n]
  s << %Q[    <itemref idref="toc" />\n] unless @producer.params["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.



128
129
130
131
132
133
134
135
136
# File 'lib/epubmaker/epubv2.rb', line 128

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

  File.open("#{tmpdir}/OEBPS/#{@producer.params["bookname"]}.ncx", "w") {|f| @producer.ncx(f, @producer.params["epubmaker"]["ncxindent"]) }
  File.open("#{tmpdir}/OEBPS/#{@producer.params["bookname"]}-toc.#{@producer.params["htmlext"]}", "w") {|f| @producer.mytoc(f) } unless @producer.params["mytoc"].nil?

  @producer.call_hook(@producer.params["epubmaker"]["hook_prepack"], tmpdir)
  export_zip(tmpdir, epubfile)
end