9 def initialize(fileName)
17 parseFileName!(fileName)
20 def self.canHandle?(fileName)
25 lowerName = fileName.downcase()
27 if lowerName.end_with?(".epub")
37 data.push('author="' + @author.to_s + '"')
40 data.push('series="' + @series + '"')
43 data.push('volume="' + @volume + '"')
46 data.push('title="' + @title + '"')
49 data.push(@cover.inspect())
52 data.push('path="' + @path + '"')
54 return '(Book:' + data.join(',') + ')'
63 return /[[:upper:]]/.match(c)
67 def massageAuthor(input)
73 input.each_char do |c|
74 if isUpper?(c) and (result.length > 0)
83 # Returns (series, volumeNo, titleText)
85 def processTitle(input)
90 arr = input.split('_')
96 matchData = (arr[0]).match(/^([A-Z]+)([0-9]+)$/)
98 capt = matchData.captures
104 pos = arr[-1].rindex('.')
106 arr[-1] = arr[-1].slice(0, pos)
109 title = arr.join(' ')
111 return series, vol, title
115 def parseFileName!(fileName)
116 parts = fileName.split('/')
117 (@series, @volume, @title) = processTitle(parts[-1])
119 @author = massageAuthor(parts[-2])
122 if fileName.downcase.end_with?(".epub")
128 def scanEpub!(fileName)
129 Zip::File.open(fileName) do |zipfile|
130 contXml = zipfile.read('META-INF/container.xml')
131 contDoc = Nokogiri::XML(contXml)
132 opfPath = contDoc.css("container rootfiles rootfile")[0]['full-path']
134 scanOpf!(zipfile, opfPath)
139 def scanOpf!(zipfile, opfPath)
142 opfXml = zipfile.read(opfPath)
143 opfDoc = Nokogiri::XML(opfXml)
148 creator = opfDoc.css('dc|creator', 'dc' => 'http://purl.org/dc/elements/1.1/')
150 roleNode = creator.attr('role')
152 role = roleNode.value
154 name = creator.children[0].content
155 parts = name.split(' ')
158 givenNames = parts[0..-2].join(' ')
159 @author = Author.new(surname, givenNames)
161 @author = Author.new(name, '')
167 #---------------------------------------
168 # Other metadata: series, volume, cover
170 metas = opfDoc.css('package metadata meta')
173 content = m['content']
175 if 'calibre:series' == name
177 elsif 'calibre:series-index' == name
179 elsif 'cover' == name
189 items = opfDoc.css('package manifest item')
193 mimeType = i['media-type']
196 entry = zipfile.find_entry(href)
197 entry.get_input_stream() do |is|
198 @cover = Cover.new(is, href, mimeType)