Class: ReVIEW::EPUBMaker::EPUBv2

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

Overview

EPUBv2 is EPUB version 2 producer.

Constant Summary collapse

DC_ITEMS =
%w[title language date type format source description relation coverage subject rights]
CREATOR_ATTRIBUTES =
%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]
CONTRIBUTER_ATTRIBUTES =
%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]

Instance Attribute Summary

Attributes inherited from EPUBCommon

#config, #contents

Instance Method Summary collapse

Methods included from CallHook

#call_hook

Methods inherited from EPUBCommon

#call_hook, #colophon, #colophon_history, #container, #cover, #coverimage, #coveritem, #date_to_s, #flat_ncx, #h, #hierarchy_ncx, #initialize, #isbn_hyphen, #join_with_separator, #legacy_cover_and_title_file, #mimetype, #mytoc, #opf_coverimage, #opf_path, #produce_write_common, #template_name, #titlepage

Constructor Details

This class inherits a constructor from ReVIEW::EPUBMaker::EPUBCommon

Instance Method Details

#ncx(indentarray) ⇒ Object

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



81
82
83
84
85
86
87
# File 'lib/review/epubmaker/epubv2.rb', line 81

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

  ReVIEW::Template.generate(path: './ncx/epubv2.ncx.erb', binding: binding)
end

#ncx_doctitleObject



94
95
96
97
98
99
100
101
102
103
# File 'lib/review/epubmaker/epubv2.rb', line 94

def ncx_doctitle
  <<-EOT
  <docTitle>
    <text>#{h(config['title'])}</text>
  </docTitle>
  <docAuthor>
    <text>#{config['aut'].nil? ? '' : h(join_with_separator(config['aut'], ReVIEW::I18n.t('names_splitter')))}</text>
  </docAuthor>
EOT
end

#ncx_isbnObject



89
90
91
92
# File 'lib/review/epubmaker/epubv2.rb', line 89

def ncx_isbn
  uid = config['isbn'] || config['urnid']
  %Q(    <meta name="dtb:uid" content="#{uid}"/>\n)
end

#ncx_navmap(indentarray) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/review/epubmaker/epubv2.rb', line 105

def ncx_navmap(indentarray)
  s = <<EOT
  <navMap>
    <navPoint id="top" playOrder="1">
<navLabel>
  <text>#{h(config['title'])}</text>
</navLabel>
<content src="#{config['cover']}"/>
    </navPoint>
EOT

  nav_count = 2

  unless config['mytoc'].nil?
    s << <<EOT
    <navPoint id="toc" playOrder="#{nav_count}">
<navLabel>
  <text>#{h(ReVIEW::I18n.t('toctitle'))}</text>
</navLabel>
<content src="#{config['bookname']}-toc.#{config['htmlext']}"/>
    </navPoint>
EOT
    nav_count += 1
  end

  contents.each do |item|
    next if item.title.nil?

    indent = indentarray.nil? ? [''] : indentarray
    level = item.level.nil? ? 0 : (item.level - 1)
    level = indent.size - 1 if level >= indent.size
    s << <<EOT
    <navPoint id="nav-#{nav_count}" playOrder="#{nav_count}">
<navLabel>
  <text>#{indent[level]}#{h(item.title)}</text>
</navLabel>
<content src="#{item.file}"/>
    </navPoint>
EOT
    nav_count += 1
  end

  s << <<EOT
  </navMap>
EOT
  s
end

#opfObject

Return opf file content.



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

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

  ReVIEW::Template.generate(path: './opf/epubv2.opf.erb', binding: binding)
end

#opf_manifestObject



63
64
65
66
67
# File 'lib/review/epubmaker/epubv2.rb', line 63

def opf_manifest
  @items = contents.find_all { |item| item.file !~ /#/ } # skip subgroup

  ReVIEW::Template.generate(path: './opf/opf_manifest_epubv2.opf.erb', binding: binding)
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
# File 'lib/review/epubmaker/epubv2.rb', line 35

def opf_metainfo
  @dc_items = DC_ITEMS.map do |item|
    if config[item]
      if config[item].is_a?(Array)
        config.names_of(item).map { |_v| { tag: "dc:#{item}", val: item_sub } }
      else
        { tag: "dc:#{item}", val: config.name_of(item).to_s }
      end
    end
  end.flatten.compact

  # creator (should be array)
  @creators = CREATOR_ATTRIBUTES.map do |role|
    if config[role]
      config.names_of(role).map { |v| { role: role, val: v } }
    end
  end.flatten.compact

  # contributor (should be array)
  @contributers = CONTRIBUTER_ATTRIBUTES.map do |role|
    if config[role]
      config.names_of(role).map { |v| { role: role, val: v } }
    end
  end.flatten.compact

  ReVIEW::Template.generate(path: './opf/opf_metainfo_epubv2.opf.erb', binding: binding)
end

#opf_tocxObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/review/epubmaker/epubv2.rb', line 69

def opf_tocx
  @cover_linear = if config['epubmaker']['cover_linear'] && config['epubmaker']['cover_linear'] != 'no'
                    'yes'
                  else
                    'no'
                  end
  @ncx_contents = contents.find_all { |content| content.media =~ /xhtml\+xml/ } # skip non XHTML

  ReVIEW::Template.generate(path: './opf/opf_tocx_epubv2.opf.erb', binding: binding)
end

#produce(epubfile, work_dir, tmpdir, base_dir:) ⇒ Object

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



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/review/epubmaker/epubv2.rb', line 156

def produce(epubfile, work_dir, tmpdir, base_dir:)
  @workdir = base_dir
  produce_write_common(work_dir, tmpdir)

  ncx_file = "#{tmpdir}/OEBPS/#{config['bookname']}.ncx"
  File.write(ncx_file, ncx(config['epubmaker']['ncxindent']))

  if config['mytoc']
    toc_file = "#{tmpdir}/OEBPS/#{config['bookname']}-toc.#{config['htmlext']}"
    File.write(toc_file, mytoc)
  end

  call_hook('hook_prepack', tmpdir, base_dir: base_dir)
  expoter = ReVIEW::EPUBMaker::ZipExporter.new(tmpdir, config)
  expoter.export_zip(epubfile)
end