Module: Softcover::EpubUtils

Included in:
Builders::Epub, Builders::Mobi
Defined in:
lib/softcover/builders/epub.rb

Instance Method Summary collapse

Instance Method Details

#content_opf_template(title, copyright, author, uuid, cover_id, toc_chapters, manifest_chapters, images) ⇒ Object

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



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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/softcover/builders/epub.rb', line 37

def content_opf_template(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.html" 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>#{escape(title)}</dc:title>
  <dc:language>en</dc:language>
  <dc:rights>Copyright (c) #{copyright} #{escape(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>
  #{cover_meta}
  </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"/>
  <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?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/softcover/builders/epub.rb', line 20

def cover?
  cover_img
end

#cover_imgObject

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



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/softcover/builders/epub.rb', line 7

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_pathObject



24
25
26
# File 'lib/softcover/builders/epub.rb', line 24

def cover_img_path
  path("#{images_dir}/#{cover_img}")
end

#escape(string) ⇒ Object



32
33
34
# File 'lib/softcover/builders/epub.rb', line 32

def escape(string)
  CGI.escape_html(string)
end

#images_dirObject



28
29
30
# File 'lib/softcover/builders/epub.rb', line 28

def images_dir
  path('epub/OEBPS/images')
end

Returns the navigation HTML based on a valid template.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/softcover/builders/epub.rb', line 101

def nav_html_template(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>#{title}</title>
</head>
<body>
    <nav epub:type="toc">
        <h1>#{escape(title)}</h1>
        <ol>
          #{nav_list.join("\n")}
        </ol>
    </nav>
</body>
</html>
)
end

#toc_ncx_template(title, uuid, chapter_nav) ⇒ Object

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/softcover/builders/epub.rb', line 81

def toc_ncx_template(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="#{uuid}"/>
    <meta name="dtb:depth" content="2"/>
    <meta name="dtb:totalPageCount" content="0"/>
    <meta name="dtb:maxPageNumber" content="0"/>
</head>
<docTitle>
    <text>#{escape(title)}</text>
</docTitle>
<navMap>
  #{chapter_nav.join("\n")}
</navMap>
</ncx>
)
end