/
file.rb
211 lines (170 loc) · 4.32 KB
/
file.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
120
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
module Epub
class File
include Logger
XML_NS = {
'xmlns' => 'http://www.idpf.org/2007/opf'
}
attr_accessor :file
def initialize(path)
@path = path
if type == :zip
@file = ZipFile.new(path)
else
@file = FileSystem.new(path)
end
@opf_xml = @file.read_xml(opf_path)
end
def self.extract(filepath, extract_path=nil)
if block_given?
Dir.mktmpdir do |outdir|
ZipFile.unzip(filepath, outdir)
yield Epub::File.new(outdir), outdir
ZipFile.zip(outdir, filepath)
end
elsif extract_path
ZipFile.unzip(filepath, extract_path)
else
raise "Incorrect arguments given"
end
end
# Flattens the directory structure, for example this:
#
# /
# |-- META-INF
# | `-- container.xml
# |-- mimetype
# `-- OEBPS
# |-- random_dir1
# |-- chA.css
# |-- ch01.html
# |-- ch02.html
# |-- image.jpg
# |-- random_dir2
# |-- chB.css
# |-- ch03.html
# |-- ch04.html
# |-- image.jpg
# |-- toc.ncx
# |-- content.opf
#
# Becomes:
#
# /
# |-- META-INF
# | `-- container.xml
# |-- mimetype
# `-- OEBPS
# |-- content.opf
# |-- content
# |-- 899ee1.css (was chA.css)
# |-- f54ff6.css (was chB.css)
# |-- c4b944.html (was ch01.html)
# |-- 4e895b.html (was ch02.html)
# |-- 89332e.html (was ch03.html)
# |-- c50b75.html (was ch04.html)
# |-- toc.ncx
# |-- assets
# |-- 5a17aa.jpg (was image.jpg)
# |-- b50b4b.jpg (was image.jpg)
#
# *NOTE:* the filenames above are a md5 hash of there original location
#
def normalize!
# Prep
log "preping"
@file.mkdir "META-INF"
@file.mkdir "OEBPS"
log "toc.normalize!"
toc.normalize!
log "guide.normalize!"
guide.normalize!
log "manifest.normalize!"
manifest.normalize!
log "finalize"
@file.clean_empty_dirs!
end
# Compresses/minifies the epub, no params will produce compress the entire epub
def compress!(*filter)
manifest.items(*filter).each do |item|
item.compress!
end
end
###
# Part of the OPF
###
def manifest
Manifest.new self
end
def metadata
Metadata.new self
end
def guide
Guide.new self
end
def spine
Spine.new self
end
def toc
spine.toc
end
# Validates the epub and produces an error report
# * Validates manifest contains everything referenced
# * Validate css
# * Validate html and all paths are relative
# * Validate images are of the correct formats
def valid?
# TODO: Later improvement
log "TODO"
end
# Save a partial opf
def save_opf!(doc_partial, xpath)
file.write(opf_path) do |f|
doc = opf_xml
# Find where we're inseting into
node = doc.xpath(xpath, 'xmlns' => XML_NS['xmlns']).first
# Because of <https://github.com/tenderlove/nokogiri/issues/391> we
# create the new doc before we insert, else we get a default namespace
# prefix
doc_partial = Nokogiri::XML(doc_partial.to_s)
node.replace(doc_partial.root)
data = doc.to_s
f.puts data
@opf_xml = doc
end
end
def opf_path=(v)
data = file.read("META-INF/container.xml")
# Parse XML
doc = Nokogiri::XML(data)
raise "Error" if !doc
# Edit the opf path
node = doc.xpath("//xmlns:rootfile").first
node["full-path"] = v
file.write("META-INF/container.xml") do |f|
f.puts doc.to_s
end
end
def opf_path
doc = @file.read_xml("META-INF/container.xml")
doc.xpath("//xmlns:rootfile").first.attributes["full-path"].to_s
end
def opf_xml
@opf_xml
end
def to_s
ret=""
file.each do |entry|
ret << entry.to_s
end
ret
end
private
def type
if ::File.file?(@path)
return :zip
else
return :filesystem
end
end
end
end