Module: Pubgen::OPF::OPFImpl

Defined in:
lib/pubgen/opf.rb

Overview

Defines private class. Only Pubgen::OPF.generate use it.

Class Method Summary collapse

Class Method Details

.cgi_escape(text) ⇒ Object



89
90
91
# File 'lib/pubgen/opf.rb', line 89

def self.cgi_escape(text)
  CGI.escapeHTML(text || '')
end

.get_cover_id_and_manifest_xml(cover_path, manifest) ⇒ Object



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
# File 'lib/pubgen/opf.rb', line 57

def self.get_cover_id_and_manifest_xml(cover_path, manifest)
  no = 1;
  manifest_xml = "  <manifest>\n"
  cover_id = nil
  file2id = {}

  manifest.each do |path|
    if OPF.valid_manifest_element?(path) == false
      raise "A manifest file, #{path} is not in sub-directory of " + 
            "yaml file"
    end
    id = "i%03d" % no
    manifest_xml << "    <item id=\"#{id}\" href=\"#{path}\" " + 
                    "media-type=\"#{guess_media_type(path)}\"/>\n"
    if path == cover_path
      cover_id = id
    end
    file2id[path] = id

    no += 1
  end

  if cover_path && cover_id == nil
    raise "Failed to find cover-image from manifest" 
  end

  manifest_xml << "    <item id=\"ncx\" href=\"#{OPF.ncx_path}\" " + 
          "media-type=\"application/x-dtbncx+xml\"/>\n  </manifest>\n"

  return cover_id, manifest_xml, file2id
end

.get_guide_xml(guide, file2id) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/pubgen/opf.rb', line 121

def self.get_guide_xml(guide, file2id)
  guide_xml = "  <guide>\n"
  cover_page = guide['cover-page']
  if cover_page != nil
    if file2id[cover_page] == nil
      raise "Failed to find cover-page from manifest" 
    else
      guide_xml << "    <reference href=\"#{cover_page}\" " + 
        "type=\"cover\" title=\"Cover\"/>\n"
    end
  end
  toc_page =  guide['toc-page']
  if toc_page != nil
    if file2id[toc_page] == nil
      raise "Failed to find toc-page from manifest" 
    else
      guide_xml << "    <reference href=\"#{toc_page}\" type=\"toc\" " + 
        "title=\"Table of Contents\"/>\n"
    end
  end
  title_page =  guide['title-page']
  if title_page != nil
    if file2id[title_page] == nil
      raise "Failed to find title-page from manifest" 
    else
      guide_xml << "    <reference href=\"#{title_page}\" " + 
        "type=\"title-page\" title=\"Title Page\"/>\n"
    end
  end
  guide_xml << "  </guide>\n"
end

.get_metadata_xml(metadata, uuid, cover_id) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/pubgen/opf.rb', line 93

def self.(, uuid, cover_id)
  cover_id_xml = ''
  if cover_id != nil
    cover_id_xml = "\n    <meta name=\"cover\" content=\"#{cover_id}\"/>"
  end
  "  <metadata xmlns:dc=\"http://purl.org/dc/elements/1.1/\" \\\nxmlns:opf=\"http://www.idpf.org/2007/opf\">\n    <dc:title>\#{cgi_escape(metadata['title'])}</dc:title>\n    <dc:creator opf:role=\"aut\">\#{cgi_escape(metadata['creator'])}\\\n</dc:creator>\n    <dc:date>\#{cgi_escape((metadata['date'].is_a?(Fixnum) ? \\\nmetadata['date'].to_s : metadata['date']))}</dc:date>\n    <dc:language>\#{cgi_escape(metadata['language'])}</dc:language>\n    <dc:subject>\#{cgi_escape(metadata['subject'])}</dc:subject>\n    <dc:publisher>\#{cgi_escape(metadata['publisher'])}</dc:publisher>\n    <dc:description>\#{cgi_escape(metadata['description'])}</dc:description>\n    <dc:contributor opf:role=\"bkp\">\#{cgi_escape(metadata['contributor'])}\\\n</dc:contributor>\n    <dc:source>\#{cgi_escape(metadata['source'])}</dc:source>\n    <dc:rights>\#{cgi_escape(metadata['rights'])}</dc:rights>\n    <dc:relation>\#{cgi_escape(metadata['relation'])}</dc:relation>\n    <dc:identifier id=\"BookID\" opf:scheme=\"UUID\">\#{uuid}</dc:identifier>\\\n\#{cover_id_xml}\n  </metadata>\n"
end

.get_spine_xml(spine, file2id) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/pubgen/opf.rb', line 153

def self.get_spine_xml(spine, file2id)
  spine_xml = "  <spine toc=\"ncx\">\n"
  spine.each do |path|
    if file2id[path] != nil
      spine_xml << "    <itemref idref=\"#{file2id[path]}\"/>\n"
    else
      raise "Failed to find spine element `#{path}' from manifest"
    end
  end
  spine_xml << "  </spine>\n"
end

.guess_media_type(filename) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/pubgen/opf.rb', line 34

def self.guess_media_type(filename)
  case filename.downcase
  when /.*\.x?html?$/
    'application/xhtml+xml'
  when /.*\.css$/
    'text/css'
  when /.*\.(jpeg|jpg)$/
    'image/jpeg'
  when /.*\.png$/
    'image/png'
  when /.*\.gif$/
    'image/gif'
  when /.*\.svg$/
    'image/svg+xml'
  when /.*\.ncx$/
    'application/x-dtbncx+xml'
  when /.*\.opf$/
    'application/oebps-package+xml'
  else
    'application/octet-stream'
  end
end