Module: Softcover::EpubUtils
- Included in:
- Builders::Epub, Builders::Mobi
- Defined in:
- lib/softcover/builders/epub.rb
Instance Method Summary collapse
-
#content_opf_template(escaped_title, copyright, author, uuid, cover_id, toc_chapters, manifest_chapters, images) ⇒ Object
Returns a content.opf file based on a valid template.
-
#cover?(options = {}) ⇒ Boolean
Returns true when producing a cover.
- #cover_filename ⇒ Object
-
#cover_img ⇒ Object
Returns the name of the cover file.
- #cover_img_path ⇒ Object
- #escape(string) ⇒ Object
- #images_dir ⇒ Object
- #nav_filename ⇒ Object
-
#nav_html_template(escaped_title, nav_list) ⇒ Object
Returns the navigation HTML based on a valid template.
-
#toc_ncx_template(escaped_title, uuid, chapter_nav) ⇒ Object
Returns a toc.ncx file based on a valid template.
-
#xhtml(filename) ⇒ Object
Transforms foo.html to foo.xhtml.
Instance Method Details
#content_opf_template(escaped_title, copyright, author, uuid, cover_id, toc_chapters, manifest_chapters, images) ⇒ Object
Returns a content.opf file based on a valid template.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/softcover/builders/epub.rb', line 55 def content_opf_template(escaped_title, copyright, , uuid, cover_id, toc_chapters, manifest_chapters, images) if cover_id = %(<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_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()}</dc:rights> <dc:creator>#{escape()}</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> #{} </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 = {}) ⇒ Boolean
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.)
24 25 26 |
# File 'lib/softcover/builders/epub.rb', line 24 def cover?(={}) ![:amazon] && cover_img end |
#cover_filename ⇒ Object
28 29 30 |
# File 'lib/softcover/builders/epub.rb', line 28 def cover_filename xhtml("cover.#{html_extension}") end |
#cover_img ⇒ Object
Returns the name of the cover file. We support (in order) JPG/JPEG, PNG, and TIFF.
6 7 8 9 10 11 12 13 14 15 16 17 |
# 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 ⇒ Object
37 38 39 |
# File 'lib/softcover/builders/epub.rb', line 37 def cover_img_path path("#{images_dir}/#{cover_img}") end |
#escape(string) ⇒ Object
50 51 52 |
# File 'lib/softcover/builders/epub.rb', line 50 def escape(string) CGI.escape_html(string) end |
#images_dir ⇒ Object
41 42 43 |
# File 'lib/softcover/builders/epub.rb', line 41 def images_dir path('epub/OEBPS/images') end |
#nav_filename ⇒ Object
45 46 47 |
# File 'lib/softcover/builders/epub.rb', line 45 def nav_filename xhtml("nav.#{html_extension}") end |
#nav_html_template(escaped_title, nav_list) ⇒ Object
Returns the navigation HTML based on a valid template.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/softcover/builders/epub.rb', line 119 def nav_html_template(escaped_title, nav_list) %(<?xml version="1.0" encoding="utf-8"?> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops"> <head> <meta charset="UTF-8" /> <title>#{escaped_title}</title> </head> <body> <nav epub:type="toc"> <h1>#{escaped_title}</h1> <ol> #{nav_list.join("\n")} </ol> </nav> </body> </html> ) end |
#toc_ncx_template(escaped_title, uuid, chapter_nav) ⇒ Object
Returns a toc.ncx file based on a valid template.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# 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) ⇒ Object
Transforms foo.html to foo.xhtml
33 34 35 |
# File 'lib/softcover/builders/epub.rb', line 33 def xhtml(filename) filename.sub('.html', '.xhtml') end |