module Softcover::EpubUtils

Public Instance Methods

content_opf_template(escaped_title, copyright, author, uuid, cover_id, toc_chapters, manifest_chapters, images) click to toggle source

Returns a content.opf file based on a valid template.

# File lib/softcover/builders/epub.rb, line 55
    def content_opf_template(escaped_title, copyright, author, uuid, cover_id,
                             toc_chapters, manifest_chapters, images)
      if cover_id
        cover_meta = %(<meta name="cover" content="#{cover_id}"/>)
        cover_html = %(<item id="cover" href="#{cover_filename}" media-type="application/xhtml+xml"/>)
        cover_ref  = '<itemref idref="cover" linear="no" />'
      else
        cover_meta = cover_html = cover_ref = ''
      end
%(<?xml version="1.0" encoding="UTF-8"?>
<package unique-identifier="BookID" version="3.0" xmlns="http://www.idpf.org/2007/opf">
  <metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcterms="http://purl.org/dc/terms/"
      xmlns:opf="http://www.idpf.org/2007/opf">
      <dc:title>#{escaped_title}</dc:title>
      <dc:language>en</dc:language>
      <dc:rights>Copyright (c) #{copyright} #{escape(author)}</dc:rights>
      <dc:creator>#{escape(author)}</dc:creator>
      <dc:publisher>Softcover</dc:publisher>
      <dc:identifier id="BookID">urn:uuid:#{uuid}</dc:identifier>
      <meta property="dcterms:modified">#{Time.now.strftime('%Y-%m-%dT%H:%M:%S')}Z</meta>
      #{cover_meta}
  </metadata>
  <manifest>
      <item href="#{nav_filename}" id="nav" media-type="application/xhtml+xml" properties="nav"/>
      <item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml"/>
      <item id="page-template.xpgt" href="styles/page-template.xpgt" media-type="application/vnd.adobe-page-template+xml"/>
      <item id="pygments.css" href="styles/pygments.css" media-type="text/css"/>
      <item id="softcover.css" href="styles/softcover.css" media-type="text/css"/>
      <item id="epub.css" href="styles/epub.css" media-type="text/css"/>
      <item id="custom.css" href="styles/custom.css" media-type="text/css"/>
      <item id="custom_epub.css" href="styles/custom_epub.css" media-type="text/css"/>
      #{cover_html}
      #{manifest_chapters.join("\n")}
      #{images.join("\n")}
  </manifest>
  <spine toc="ncx">
    #{cover_ref}
    #{toc_chapters.join("\n")}
  </spine>
</package>
)
    end
cover?(options={}) click to toggle source

Returns true when producing a cover. We include a cover when not producing an Amazon-specific book as long as there's a cover image. (When uploading a book to Amazon KDP, the cover gets uploaded separately, so the MOBI file itself should have not have a cover.)

# File lib/softcover/builders/epub.rb, line 24
def cover?(options={})
  !options[:amazon] && cover_img
end
cover_filename() click to toggle source
# File lib/softcover/builders/epub.rb, line 28
def cover_filename
  xhtml("cover.#{html_extension}")
end
cover_img() click to toggle source

Returns the name of the cover file. We support (in order) JPG/JPEG, PNG, and TIFF.

# File lib/softcover/builders/epub.rb, line 6
def cover_img
  extensions = %w[jpg jpeg png tiff]
  extensions.each do |ext|
    origin = "images/cover.#{ext}"
    target = "#{images_dir}/cover.#{ext}"
    if File.exist?(origin)
      FileUtils.cp(origin, target)
      return File.basename(target)
    end
  end
  return false
end
cover_img_path() click to toggle source
# File lib/softcover/builders/epub.rb, line 37
def cover_img_path
  path("#{images_dir}/#{cover_img}")
end
escape(string) click to toggle source
# File lib/softcover/builders/epub.rb, line 50
def escape(string)
  CGI.escape_html(string)
end
images_dir() click to toggle source
# File lib/softcover/builders/epub.rb, line 41
def images_dir
  path('epub/OEBPS/images')
end
nav_filename() click to toggle source
nav_html_template(escaped_title, nav_list) click to toggle source

Returns the navigation HTML based on a valid template.

toc_ncx_template(escaped_title, uuid, chapter_nav) click to toggle source

Returns a toc.ncx file based on a valid template.

# File lib/softcover/builders/epub.rb, line 99
    def toc_ncx_template(escaped_title, uuid, chapter_nav)
%(<?xml version="1.0" encoding="UTF-8"?>
<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">
    <head>
        <meta name="dtb:uid" content="urn:uuid:#{uuid}"/>
        <meta name="dtb:depth" content="2"/>
        <meta name="dtb:totalPageCount" content="0"/>
        <meta name="dtb:maxPageNumber" content="0"/>
    </head>
    <docTitle>
        <text>#{escaped_title}</text>
    </docTitle>
    <navMap>
      #{chapter_nav.join("\n")}
    </navMap>
</ncx>
)
    end
xhtml(filename) click to toggle source

Transforms foo.html to foo.xhtml

# File lib/softcover/builders/epub.rb, line 33
def xhtml(filename)
  filename.sub('.html', '.xhtml')
end