class ReVIEW::EPUBMaker::EPUBv3

EPUBv3 is EPUB version 3 producer.

Constants

CONTRIBUTER_ATTRIBUTES
CREATOR_ATTRIBUTES
DC_ITEMS

Public Class Methods

new(producer) click to toggle source

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

Calls superclass method ReVIEW::EPUBMaker::EPUBCommon::new
# File lib/review/epubmaker/epubv3.rb, line 26
def initialize(producer)
  super
  @opf_prefix = {}
  if config['opf_prefix'].present?
    config['opf_prefix'].each { |k, v| @opf_prefix[k] = v }
  end
end

Public Instance Methods

ncx(indentarray) click to toggle source
# File lib/review/epubmaker/epubv3.rb, line 197
      def ncx(indentarray)
        ncx_main = if config['epubmaker']['flattoc'].nil?
                     hierarchy_ncx('ol')
                   else
                     flat_ncx('ol', config['epubmaker']['flattocindent'])
                   end

        @body = <<-EOT
  <nav xmlns:epub="http://www.idpf.org/2007/ops" epub:type="toc" id="toc">
  <h1 class="toc-title">#{h(ReVIEW::I18n.t('toctitle'))}</h1>
#{ncx_main}  </nav>
      EOT

        @title = h(ReVIEW::I18n.t('toctitle'))
        @language = config['language']
        @stylesheets = config['stylesheet']
        ReVIEW::Template.generate(path: './html/layout-html5.html.erb', binding: binding)
      end
opf() click to toggle source

Return opf file content.

# File lib/review/epubmaker/epubv3.rb, line 35
def opf
  @opf_metainfo = opf_metainfo
  @opf_coverimage = opf_coverimage
  @opf_manifest = opf_manifest
  @opf_toc = opf_tocx
  @package_attrs = ''

  if @opf_prefix && @opf_prefix.size > 0
    prefixes_str = @opf_prefix.map { |k, v| %Q(#{k}: #{v}) }.join(' ')
    @package_attrs << %Q( prefix="#{prefixes_str}")
  end

  ReVIEW::Template.generate(path: './opf/epubv3.opf.erb', binding: binding)
end
opf_contributers() click to toggle source
# File lib/review/epubmaker/epubv3.rb, line 121
def opf_contributers
  CONTRIBUTER_ATTRIBUTES.map do |role|
    next unless config[role]

    config[role].map.with_index do |v, i|
      case v
      when Hash
        refines = v.map { |name, val| { id: "#{role}-#{i}", property: name, scheme: nil, val: val } }.delete_if { |h| h[:property] == 'name' }
        contributer = {
          id: "#{role}-#{i}",
          val: v['name'],
          refines: [
            { id: "#{role}-#{i}", property: 'role', scheme: 'marc:relators', val: role }
          ].concat(refines)
        }
      else
        contributer = {
          id: "#{role}-#{i}",
          val: v,
          refines: [
            { id: "#{role}-#{i}", property: 'role', scheme: 'marc:relators', val: role }
          ]
        }
      end
      if %w[prt pbl].include?(role)
        contributer[:pub_id] = "pub-#{role}-#{i}"
        case v
        when Hash
          contributer[:pub_val] = v['name']
          pub_refines = v.map { |name, val| { id: "pub-#{role}-#{i}", property: name, scheme: nil, val: val } }.delete_if { |h| h[:property] == 'name' }
          contributer[:pub_refines] = [
            { id: "pub-#{role}-#{i}", property: 'role', scheme: 'marc:relators', val: role }
          ].concat(pub_refines)
        else
          contributer[:pub_val] = v
          contributer[:pub_refines] = [
            { id: "pub-#{role}-#{i}", property: 'role', scheme: 'marc:relators', val: 'prt' }
          ]
        end
      end

      contributer
    end
  end.flatten.compact
end
opf_creators() click to toggle source
# File lib/review/epubmaker/epubv3.rb, line 92
def opf_creators
  CREATOR_ATTRIBUTES.map do |role|
    next unless config[role]

    config[role].map.with_index do |v, i|
      case v
      when Hash
        refines = v.map { |name, val| { id: "#{role.sub('a-', '')}-#{i}", property: name.to_s, scheme: nil, val: val } }.delete_if { |h| h[:property] == 'name' }

        {
          id: "#{role}-#{i}",
          val: v['name'],
          refines: [
            { id: "#{role}-#{i}", property: 'role', scheme: 'marc:relators', val: role.sub('a-', '') }
          ].concat(refines)
        }
      else
        {
          id: "#{role}-#{i}",
          val: v,
          refines: [
            { id: "#{role}-#{i}", property: 'role', scheme: 'marc:relators', val: role.sub('a-', '') }
          ]
        }
      end
    end
  end.flatten.compact
end
opf_dc_items() click to toggle source
# File lib/review/epubmaker/epubv3.rb, line 62
def opf_dc_items
  DC_ITEMS.map do |item|
    next unless config[item]

    case config[item]
    when Array
      config[item].map.with_index do |v, i|
        if v.is_a?(Hash)
          { tag: "dc:#{item}",
            id: "#{item}-#{i}",
            val: v['name'],
            refines: v.map { |name, val| { name: name, val: val } }.delete_if { |h| h[:name] == 'name' } }
        else
          { tag: "dc:#{item}", id: "#{item}-#{i}", val: v.to_s, refines: [] }
        end
      end
    when Hash
      { tag: "dc:#{item}",
        id: item.to_s,
        val: config[item]['name'],
        refines: config[item].map { |name, val| { name: name, val: val } }.delete_if { |h| h[:name] == 'name' } }
    else
      { tag: "dc:#{item}",
        id: item.to_s,
        val: config[item].to_s,
        refines: [] }
    end
  end.flatten.compact
end
opf_manifest() click to toggle source
# File lib/review/epubmaker/epubv3.rb, line 167
def opf_manifest
  if config['coverimage']
    @coverimage = contents.find { |content| content.coverimage?(config['coverimage']) } # @coverimage can be nil
  end
  @items = if @coverimage
             contents.find_all { |content| content.file !~ /#/ && content.id != @coverimage.id } # skip subgroup, or @coverimage
           else
             contents.find_all { |content| content.file !~ /#/ }
           end

  ReVIEW::Template.generate(path: './opf/opf_manifest_epubv3.opf.erb', binding: binding)
end
opf_metainfo() click to toggle source
# File lib/review/epubmaker/epubv3.rb, line 50
def opf_metainfo
  @dc_items = opf_dc_items

  # creator (should be array)
  @creators = opf_creators

  # contributor (should be array)
  @contributers = opf_contributers

  ReVIEW::Template.generate(path: './opf/opf_metainfo_epubv3.opf.erb', binding: binding)
end
opf_tocx() click to toggle source
# File lib/review/epubmaker/epubv3.rb, line 180
def opf_tocx
  @cover_linear = if config['epubmaker']['cover_linear'] && config['epubmaker']['cover_linear'] != 'no'
                    'yes'
                  else
                    'no'
                  end
  @tocx_contents = []
  toc = nil
  contents.each do |item|
    next if item.media !~ /xhtml\+xml/ # skip non XHTML

    @tocx_contents << item
  end

  ReVIEW::Template.generate(path: './opf/opf_tocx_epubv3.opf.erb', binding: binding)
end
produce(epubfile, work_dir, tmpdir, base_dir:) click to toggle source

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

# File lib/review/epubmaker/epubv3.rb, line 219
def produce(epubfile, work_dir, tmpdir, base_dir:)
  produce_write_common(work_dir, tmpdir)

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

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