9 @@DC_NS_URL = 'http://purl.org/dc/elements/1.1/'
21 def loadFromFile(fileName)
23 parseFileName!(fileName)
26 def self.canHandle?(fileName)
31 #puts "Filename: " + fileName.to_s
32 lowerName = fileName.downcase()
34 if lowerName.end_with?(".epub")
57 result.push('<b>' + @title + '</b>')
59 result.push('<i>(Unknown title)</i>')
62 result.push('<i>by ' + @author.reading_order + '</i>')
67 seriesInfo.push(@series.to_s)
70 seriesInfo.push(@volume.to_s)
72 if seriesInfo.length > 0
73 result.push(seriesInfo.join(' '))
76 return result.join('<br/>')
82 data.push('author="' + @author.inspect + '"')
85 data.push('series="' + @series + '"')
88 data.push('volume="' + @volume + '"')
91 data.push('title="' + @title + '"')
94 data.push(@cover.inspect())
97 data.push('path="' + @path + '"')
99 return '(Book:' + data.join(',') + ')'
124 return /[[:upper:]]/.match(c)
128 def massage_author(input)
134 input.each_char do |c|
135 if isUpper?(c) and (reading_order.length > 0)
144 # Returns (series, volumeNo, titleText)
146 def processTitle(input)
151 arr = input.split('_')
157 matchData = (arr[0]).match(/^([A-Z]+)([0-9]+)$/)
159 capt = matchData.captures
165 pos = arr[-1].rindex('.')
167 arr[-1] = arr[-1].slice(0, pos)
170 title = arr.join(' ')
172 return series, vol, title
176 def parseFileName!(fileName)
177 parts = fileName.split('/')
178 (@series, @volume, @title) = processTitle(parts[-1])
181 reading_order = massage_author(grouping)
183 @author = Author.new(grouping, reading_order, sort_order)
186 if fileName.downcase.end_with?(".epub")
192 def scanEpub!(fileName)
193 #puts 'Scanning "' + fileName.to_s + '"...'
195 Zip::File.open(fileName) do |zipfile|
196 entry = zipfile.find_entry('META-INF/container.xml')
198 puts 'No META-INF/container.xml, skipping book ' + fileName
201 contXml = zipfile.read('META-INF/container.xml')
202 contDoc = Nokogiri::XML(contXml)
203 opfPath = contDoc.css("container rootfiles rootfile")[0]['full-path']
205 scanOpf!(zipfile, opfPath)
207 rescue Zip::Error => exc
208 puts 'ERROR processing file "' + fileName + '":'
215 def scanOpf!(zipfile, opfPath)
218 opfXml = zipfile.read(opfPath)
219 opfDoc = Nokogiri::XML(opfXml)
224 grouping = @author.grouping
225 reading_order = @author.reading_order
226 sort_order = @author.sort_order
228 creators = opfDoc.css('dc|creator', 'dc' => @@DC_NS_URL)
229 if (creators.length > 0)
230 creator = creators[0]
232 role = creator['opf:role']
234 reading_order = creator.content
236 file_as = creator['opf:file-as']
242 @author = Author.new(grouping, reading_order, sort_order)
246 #---------------------------------------
249 titles = opfDoc.css('dc|title', 'dc' => @@DC_NS_URL)
253 @title = title.content
257 #---------------------------------------
260 descrNodes = opfDoc.css('dc|description', 'dc' => @@DC_NS_URL)
261 if (descrNodes.length > 0)
262 descrNode = descrNodes[0]
264 @description = descrNode.content
268 #---------------------------------------
269 # Other metadata: series, volume, cover
271 metas = opfDoc.css('package metadata meta')
274 content = m['content']
276 if 'calibre:series' == name
278 elsif 'calibre:series-index' == name
280 elsif 'cover' == name
282 #puts 'File ' + @path + ' coverId ' + coverId
289 @cover = loadCover(zipfile, opfPath, opfDoc, coverId)
293 def loadCover(zipfile, opfPath, opfDoc, coverId)
296 coverId = "cover-image"
299 items = opfDoc.css('package manifest item')
303 mimeType = i['media-type']
306 entry = zipfile.find_entry(href)
309 # Although the epub standard requires the path to be relative
310 # to the base of the epub (zip), some books encountered in the
311 # wild have been found to use a bath relative to the location
313 parts = opfPath.split('/')
314 opfBasePath = opfPath.split('/')[0..-2].join('/')
315 coverPath = opfBasePath + '/' + href
316 entry = zipfile.find_entry(coverPath)
320 puts 'WARNING! Cover image "' + href + '" not found in file "' + @path + '".'
323 entry.get_input_stream() do |is|
324 return Cover.new(is, href, mimeType)