lib/softcover/builders/epub.rb in softcover-0.9.12 vs lib/softcover/builders/epub.rb in softcover-0.9.13
- old
+ new
@@ -68,16 +68,15 @@
# Writes the HTML for the EPUB.
# Included is a math detector that processes the page with MathJax
# (via page.js) so that math can be included in EPUB (and thence MOBI).
def write_html(options={})
- images_dir = File.join('epub', 'OEBPS', 'images')
texmath_dir = File.join(images_dir, 'texmath')
mkdir images_dir
mkdir texmath_dir
- File.write(path('epub/OEBPS/cover.html'), cover_page)
+ File.write(path('epub/OEBPS/cover.html'), cover_page) if cover?
pngs = []
chapters.each_with_index do |chapter, i|
target_filename = path("epub/OEBPS/#{chapter.fragment_name}")
File.open(target_filename, 'w') do |f|
@@ -111,10 +110,14 @@
FileUtils.rm(f)
end
end
end
+ def images_dir
+ File.join('epub', 'OEBPS', 'images')
+ end
+
# Returns HTML for HTML source that includes math.
# As a side-effect, html_with_math creates PNGs corresponding to any
# math in the given source. The technique involves using PhantomJS to
# hit the HTML source for each page containing math to create SVGs
# for every math element. Since ereader support for SVGs is spotty,
@@ -314,10 +317,11 @@
%(<itemref idref="#{chapter.slug}"/>)
end
image_files = Dir['epub/OEBPS/images/**/*'].select { |f| File.file?(f) }
images = image_files.map do |image|
ext = File.extname(image).sub('.', '') # e.g., 'png'
+ ext = 'jpeg' if ext == 'jpg'
# Strip off the leading 'epub/OEBPS'.
sep = File::SEPARATOR
href = image.split(sep)[2..-1].join(sep)
# Define an id based on the filename.
# Prefix with 'img-' in case the filname starts with an
@@ -335,11 +339,11 @@
<dc:rights>Copyright (c) #{copyright} #{author}</dc:rights>
<dc:creator>#{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>
- <meta name="cover" content="img-cover-png"/>
+ <meta name="cover" content="cover-image"/>
</metadata>
<manifest>
<item href="nav.html" 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"/>
@@ -367,14 +371,29 @@
<head>
<title>Cover</title>
</head>
<body>
<div id="cover">
- <img width="573" height="800" src="images/cover.png" alt="cover image" />
+ <img width="573" height="800" src="images/#{cover_img}" alt="cover" />
</div>
</body>
</html>
)
+ end
+
+ # Returns the name of the cover file.
+ # We support (in order) JPG/JPEG, PNG, and TIF.
+ def cover_img
+ extensions = %w[jpg jpeg ng tif]
+ extensions.each do |ext|
+ file = Dir[path("#{images_dir}/cover.#{ext}")].first
+ return File.basename(file) if file
+ end
+ return false
+ end
+
+ def cover?
+ cover_img
end
# Returns the Table of Contents for the spine.
def toc_ncx
title = manifest.title
\ No newline at end of file