10 @@DC_NS_URL = 'http://purl.org/dc/elements/1.1/'
23 def loadFromFile(fileName)
25 parseFileName!(fileName)
28 def self.canHandle?(fileName)
33 #puts "Filename: " + fileName.to_s
34 lowerName = fileName.downcase()
36 if lowerName.end_with?(".epub")
63 def description=(value)
71 result.push('<b>' + @title + '</b>')
73 result.push('<i>(Unknown title)</i>')
76 result.push('<i>by ' + @author.reading_order + '</i>')
80 series = @store.load_series(@series_id)
81 if nil != series and nil != series.descr
82 seriesInfo.push(series.descr.to_s)
85 seriesInfo.push(@volume.to_s)
87 if seriesInfo.length > 0
88 result.push(seriesInfo.join(' '))
91 return result.join('<br/>')
97 data.push('author="' + @author.inspect + '"')
100 data.push('series_id="' + @series_id.to_s() + '"')
103 data.push('volume="' + @volume + '"')
106 data.push('title="' + @title + '"')
109 data.push(@cover.inspect())
112 data.push('path="' + @path + '"')
114 return '(Book:' + data.join(',') + ')'
129 def series_id=(value)
155 return /[[:upper:]]/.match(c)
159 def massage_author(input)
165 input.each_char do |c|
166 if isUpper?(c) and (reading_order.length > 0)
175 # Returns (series, volumeNo, titleText)
177 def processTitle(input)
182 arr = input.split('_')
188 matchData = (arr[0]).match(/^([A-Z]+)([0-9]+)$/)
190 capt = matchData.captures
196 pos = arr[-1].rindex('.')
198 arr[-1] = arr[-1].slice(0, pos)
201 title = arr.join(' ')
203 return series, vol, title
207 def parseFileName!(fileName)
208 parts = fileName.split('/')
209 (series_code, @volume, @title) = processTitle(parts[-1])
212 reading_order = massage_author(grouping)
214 @author = Author.new(grouping, reading_order, sort_order)
215 @series_id = @store.get_series(grouping, series_code)
218 if fileName.downcase.end_with?(".epub")
224 def scanEpub!(fileName)
225 #puts 'Scanning "' + fileName.to_s + '"...'
227 Zip::File.open(fileName) do |zipfile|
228 entry = zipfile.find_entry('META-INF/container.xml')
230 puts 'No META-INF/container.xml, skipping book ' + fileName
233 contXml = zipfile.read('META-INF/container.xml')
234 contDoc = Nokogiri::XML(contXml)
235 opfPath = contDoc.css("container rootfiles rootfile")[0]['full-path']
237 scanOpf!(zipfile, opfPath)
239 rescue Zip::Error => exc
240 puts 'ERROR processing file "' + fileName + '":'
247 def scanOpf!(zipfile, opfPath)
250 opfXml = zipfile.read(opfPath)
251 opfDoc = Nokogiri::XML(opfXml)
256 grouping = @author.grouping
257 reading_order = @author.reading_order
258 sort_order = @author.sort_order
260 creators = opfDoc.css('dc|creator', 'dc' => @@DC_NS_URL)
261 if (creators.length > 0)
262 creator = creators[0]
264 role = creator['opf:role']
266 reading_order = creator.content
268 file_as = creator['opf:file-as']
274 @author = Author.new(grouping, reading_order, sort_order)
278 #---------------------------------------
281 titles = opfDoc.css('dc|title', 'dc' => @@DC_NS_URL)
285 @title = title.content
289 #---------------------------------------
292 descrNodes = opfDoc.css('dc|description', 'dc' => @@DC_NS_URL)
293 if (descrNodes.length > 0)
294 descrNode = descrNodes[0]
296 @description = descrNode.content
300 #---------------------------------------
301 # Other metadata: series, volume, cover
303 metas = opfDoc.css('package metadata meta')
306 content = m['content']
308 if 'calibre:series' == name
309 # TODO: Dynamically create a new series?
310 # @series_id = content
311 elsif 'calibre:series-index' == name
313 elsif 'cover' == name
315 #puts 'File ' + @path + ' coverId ' + coverId
322 @cover = load_cover(zipfile, opfPath, opfDoc, coverId)
326 def load_cover(zipfile, opfPath, opfDoc, coverId)
329 coverId = "cover-image"
332 items = opfDoc.css('package manifest item')
336 mimeType = i['media-type']
339 entry = zipfile.find_entry(href)
342 # Although the epub standard requires the path to be relative
343 # to the base of the epub (zip), some books encountered in the
344 # wild have been found to use a bath relative to the location
346 parts = opfPath.split('/')
347 opfBasePath = opfPath.split('/')[0..-2].join('/')
348 coverPath = opfBasePath + '/' + href
349 entry = zipfile.find_entry(coverPath)
353 puts 'WARNING! Cover image "' + href + '" not found in file "' + @path + '".'
356 entry.get_input_stream() do |is|
357 return Cover.new(is, href, mimeType)