9 @@DC_NS_URL = 'http://purl.org/dc/elements/1.1/'
11 def initialize(fileName)
12 #puts 'InitBook(' + fileName + ')'
21 parseFileName!(fileName)
24 def self.canHandle?(fileName)
29 #puts "Filename: " + fileName.to_s
30 lowerName = fileName.downcase()
32 if lowerName.end_with?(".epub")
55 result.push('<b>' + @title + '</b>')
57 result.push('<i>(Unknown title)</i>')
60 result.push('<i>by ' + @author.to_s() + '</i>')
65 seriesInfo.push(@series.to_s)
68 seriesInfo.push(@volume.to_s)
70 if seriesInfo.length > 0
71 result.push(seriesInfo.join(' '))
74 return result.join('<br/>')
80 data.push('author="' + @author.inspect + '"')
83 data.push('series="' + @series + '"')
86 data.push('volume="' + @volume + '"')
89 data.push('title="' + @title + '"')
92 data.push(@cover.inspect())
95 data.push('path="' + @path + '"')
97 return '(Book:' + data.join(',') + ')'
122 return /[[:upper:]]/.match(c)
126 def massage_author(input)
132 input.each_char do |c|
133 if isUpper?(c) and (reading_order.length > 0)
142 # Returns (series, volumeNo, titleText)
144 def processTitle(input)
149 arr = input.split('_')
155 matchData = (arr[0]).match(/^([A-Z]+)([0-9]+)$/)
157 capt = matchData.captures
163 pos = arr[-1].rindex('.')
165 arr[-1] = arr[-1].slice(0, pos)
168 title = arr.join(' ')
170 return series, vol, title
174 def parseFileName!(fileName)
175 parts = fileName.split('/')
176 (@series, @volume, @title) = processTitle(parts[-1])
179 reading_order = massage_author(grouping)
181 @author = Author.new(grouping, reading_order, sort_order)
184 if fileName.downcase.end_with?(".epub")
190 def scanEpub!(fileName)
191 #puts 'Scanning "' + fileName.to_s + '"...'
193 Zip::File.open(fileName) do |zipfile|
194 entry = zipfile.find_entry('META-INF/container.xml')
196 puts 'No META-INF/container.xml, skipping book ' + fileName
199 contXml = zipfile.read('META-INF/container.xml')
200 contDoc = Nokogiri::XML(contXml)
201 opfPath = contDoc.css("container rootfiles rootfile")[0]['full-path']
203 scanOpf!(zipfile, opfPath)
205 rescue Zip::Error => exc
206 puts 'ERROR processing file "' + fileName + '":'
213 def scanOpf!(zipfile, opfPath)
216 opfXml = zipfile.read(opfPath)
217 opfDoc = Nokogiri::XML(opfXml)
222 grouping = @author.grouping
223 reading_order = @author.reading_order
224 sort_order = @author.sort_order
226 creators = opfDoc.css('dc|creator', 'dc' => @@DC_NS_URL)
227 if (creators.length > 0)
228 creator = creators[0]
230 role = creator['opf:role']
232 reading_order = creator.content
234 file_as = creator['opf:file-as']
240 @author = Author.new(grouping, reading_order, sort_order)
244 #---------------------------------------
247 titles = opfDoc.css('dc|title', 'dc' => @@DC_NS_URL)
251 @title = title.content
255 #---------------------------------------
258 descrNodes = opfDoc.css('dc|description', 'dc' => @@DC_NS_URL)
259 if (descrNodes.length > 0)
260 descrNode = descrNodes[0]
262 @description = descrNode.content
266 #---------------------------------------
267 # Other metadata: series, volume, cover
269 metas = opfDoc.css('package metadata meta')
272 content = m['content']
274 if 'calibre:series' == name
276 elsif 'calibre:series-index' == name
278 elsif 'cover' == name
280 #puts 'File ' + @path + ' coverId ' + coverId
287 @cover = loadCover(zipfile, opfPath, opfDoc, coverId)
291 def loadCover(zipfile, opfPath, opfDoc, coverId)
294 coverId = "cover-image"
297 items = opfDoc.css('package manifest item')
301 mimeType = i['media-type']
304 entry = zipfile.find_entry(href)
307 # Although the epub standard requires the path to be relative
308 # to the base of the epub (zip), some books encountered in the
309 # wild have been found to use a bath relative to the location
311 parts = opfPath.split('/')
312 opfBasePath = opfPath.split('/')[0..-2].join('/')
313 coverPath = opfBasePath + '/' + href
314 entry = zipfile.find_entry(coverPath)
318 puts 'WARNING! Cover image "' + href + '" not found in file "' + @path + '".'
321 entry.get_input_stream() do |is|
322 return Cover.new(is, href, mimeType)