6 require 'classification'
11 @@DC_NS_URL = 'http://purl.org/dc/elements/1.1/'
15 @classification_id = nil
25 def load_from_file!(fileName)
27 parse_file_name!(fileName)
30 def self.can_handle?(fileName)
35 #puts "Filename: " + fileName.to_s
36 lowerName = fileName.downcase()
38 if lowerName.end_with?(".epub")
42 if lowerName.end_with?(".pdf")
61 def classification_id=(value)
62 @classification_id = value
77 def description=(value)
85 result.push('<b>' + @title + '</b>')
87 result.push('<i>(Unknown title)</i>')
90 result.push('<i>by ' + @author.reading_order + '</i>')
94 series = @store.load_series(@series_id)
95 if nil != series and nil != series.descr
96 seriesInfo.push(series.descr.to_s)
99 seriesInfo.push(@volume.to_s)
101 if seriesInfo.length > 0
102 result.push(seriesInfo.join(' '))
106 if nil != @classification_id
107 classification = @store.load_classification(@classification_id)
109 if nil != classification
110 if nil != classification.ddc
111 result.push('Dewey: ' + classification.ddc.to_s)
113 if nil != classification.lcc
114 result.push('LCC: ' + classification.lcc.to_s)
118 return result.join('<br/>')
124 data.push('author="' + @author.inspect + '"')
127 data.push('series_id="' + @series_id.to_s() + '"')
130 data.push('volume="' + @volume + '"')
133 data.push('title="' + @title + '"')
136 data.push(@cover.inspect())
139 data.push('path="' + @path + '"')
141 return '(Book:' + data.join(',') + ')'
156 def series_id=(value)
177 return File.basename(@path, '.*')
190 return /[[:upper:]]/.match(c)
194 def massage_author(input)
200 input.each_char do |c|
201 if isUpper?(c) and (reading_order.length > 0)
210 # Returns (series, volumeNo, titleText)
212 def processTitle(input)
217 arr = input.split('_')
223 matchData = (arr[0]).match(/^([A-Z]+)([0-9]+)$/)
225 capt = matchData.captures
231 pos = arr[-1].rindex('.')
233 arr[-1] = arr[-1].slice(0, pos)
236 title = arr.join(' ')
238 return series, vol, title
242 def parse_file_name!(file_name)
243 parts = file_name.split('/')
244 (series_code, @volume, @title) = processTitle(parts[-1])
247 reading_order = massage_author(grouping)
249 @author = Author.new(grouping, reading_order, sort_order)
250 @series_id = @store.get_series(grouping, series_code)
253 lc_file_name = file_name.downcase
254 if lc_file_name.end_with?(".epub")
256 elsif lc_file_name.end_with?(".pdf")
260 @classification_id = @store.find_classification(@author.grouping, File.basename(file_name, '.*'))
264 def scanEpub!(fileName)
265 #puts 'Scanning "' + fileName.to_s + '"...'
267 Zip::File.open(fileName) do |zipfile|
268 entry = zipfile.find_entry('META-INF/container.xml')
270 puts 'No META-INF/container.xml, skipping book ' + fileName
273 contXml = zipfile.read('META-INF/container.xml')
274 contDoc = Nokogiri::XML(contXml)
275 opfPath = contDoc.css("container rootfiles rootfile")[0]['full-path']
277 scanOpf!(zipfile, opfPath)
279 rescue Zip::Error => exc
280 puts 'ERROR processing file "' + fileName + '":'
287 def scan_pdf!(file_name)
288 #puts 'Scanning "' + file_name.to_s + '"...'
290 pdf_path = File.expand_path(file_name).to_s
291 if ! pdf_path.end_with?('.pdf')
292 puts 'Unexpected internal error: path "' + file_name.to_s + '" does not end with ".pdf".'
296 jpeg_path = pdf_path[0..-5] + '.jpeg'
297 if File.file?(jpeg_path)
298 File.open(jpeg_path, 'r') do |is|
299 @cover = Cover.new(is, jpeg_path, 'image/jpeg')
306 def scanOpf!(zipfile, opfPath)
309 opfXml = zipfile.read(opfPath)
310 opfDoc = Nokogiri::XML(opfXml)
315 grouping = @author.grouping
316 reading_order = @author.reading_order
317 sort_order = @author.sort_order
319 creators = opfDoc.css('dc|creator', 'dc' => @@DC_NS_URL)
320 if (creators.length > 0)
321 creator = creators[0]
323 role = creator['opf:role']
325 reading_order = creator.content
327 file_as = creator['opf:file-as']
333 @author = Author.new(grouping, reading_order, sort_order)
337 #---------------------------------------
340 titles = opfDoc.css('dc|title', 'dc' => @@DC_NS_URL)
344 @title = title.content
348 #---------------------------------------
351 descrNodes = opfDoc.css('dc|description', 'dc' => @@DC_NS_URL)
352 if (descrNodes.length > 0)
353 descrNode = descrNodes[0]
355 @description = descrNode.content
359 #---------------------------------------
360 # Other metadata: series, volume, cover
362 metas = opfDoc.css('package metadata meta')
365 content = m['content']
367 if 'calibre:series' == name
368 # TODO: Dynamically create a new series?
369 # @series_id = content
370 elsif 'calibre:series-index' == name
372 elsif 'cover' == name
374 #puts 'File ' + @path + ' coverId ' + coverId
381 @cover = load_cover(zipfile, opfPath, opfDoc, coverId)
385 def load_cover(zipfile, opfPath, opfDoc, coverId)
388 coverId = "cover-image"
391 items = opfDoc.css('package manifest item')
395 mimeType = i['media-type']
398 entry = zipfile.find_entry(href)
401 # Although the epub standard requires the path to be relative
402 # to the base of the epub (zip), some books encountered in the
403 # wild have been found to use a bath relative to the location
405 parts = opfPath.split('/')
406 opfBasePath = opfPath.split('/')[0..-2].join('/')
407 coverPath = opfBasePath + '/' + href
408 entry = zipfile.find_entry(coverPath)
412 puts 'WARNING! Cover image "' + href + '" not found in file "' + @path + '".'
415 entry.get_input_stream() do |is|
416 return Cover.new(is, href, mimeType)