From: Chris Jaekl Date: Fri, 24 Feb 2017 15:20:41 +0000 (+0900) Subject: Create a basic html output, to validate that we're loading data correctly. X-Git-Url: https://jaekl.net/gitweb/?a=commitdiff_plain;h=061091d1fc2bb2351afc695a5fcbdbc19e48e03b;p=quanlib.git Create a basic html output, to validate that we're loading data correctly. --- diff --git a/book.rb b/book.rb index 72bb83a..370093d 100644 --- a/book.rb +++ b/book.rb @@ -2,8 +2,8 @@ require 'nokogiri' require 'zip' -require './author' -require './cover' +require 'author' +require 'cover' class Book def initialize(fileName) @@ -31,6 +31,36 @@ class Book return false end + def cover + return @cover + end + + def describe + result = [] + + if nil != @title + result.push('' + @title + '') + else + result.push('(Unknown title)') + end + if nil != @author + result.push(@author.to_s()) + end + + seriesInfo = [] + if nil != @series + seriesInfo.push(@series.to_s) + end + if nil != @volume + seriesInfo.push(@volume.to_s) + end + if seriesInfo.length > 0 + result.push(seriesInfo.join(' ')) + end + + return result.join('
') + end + def inspect data = [] if nil != @author @@ -126,6 +156,7 @@ class Book protected def scanEpub!(fileName) + puts 'Scanning "' + fileName.to_s + '"...' Zip::File.open(fileName) do |zipfile| contXml = zipfile.read('META-INF/container.xml') contDoc = Nokogiri::XML(contXml) diff --git a/cover.rb b/cover.rb new file mode 100644 index 0000000..b88f340 --- /dev/null +++ b/cover.rb @@ -0,0 +1,46 @@ + +class Cover + def initialize(inputStream, path, mimeType) + @data = inputStream.read + @path = path + @mimeType = mimeType + end + + def inspect + info = [] + if nil != @data + info.push('size=' + @data.length.to_s) + else + info.push('empty') + end + if nil != @path + info.push('path="' + @path + '"') + end + if nil != @mimeType + info.push('mimeType="' + @mimeType + '"') + end + return '(Cover:' + info.join(',') + ')' + end + + def to_s + return inspect + end + + def writeImage(outputDir, baseName) + filename = baseName + getExt() + open(outputDir + '/' + filename, 'wb') do |fd| + fd.write(@data) + end + return filename, @mimeType + end + + protected + def getExt + pos = @path.rindex('.') + if nil == pos + return '.img' + end + return @path.slice(pos, @path.length) + end +end + diff --git a/main.rb b/main.rb index 76f543e..4e73b07 100644 --- a/main.rb +++ b/main.rb @@ -1,10 +1,40 @@ -require './walkdir' +require 'walkdir' + +outputDir = 'output' books = [] +imageCount = 0 for arg in ARGV w = WalkDir.new(arg) - books.push(w.books) + books += (w.books) +end + +if ! Dir.exist?(outputDir) + Dir.mkdir(outputDir) +end + +open(outputDir + '/index.html', 'w') do |fd| + fd.puts "" + fd.puts " Books" + fd.puts " " + fd.puts " " + + for book in books + image = nil + if nil != book.cover + imageCount += 1 + (path, mimeType) = book.cover.writeImage(outputDir, 'image' + imageCount.to_s) + image = '' + else + image = '(No cover image)' + end + + fd.puts " " + end + + fd.puts "
" + image + "" + book.describe() + "
" + fd.puts " " + fd.puts "" end -puts books diff --git a/walkdir.rb b/walkdir.rb index c776dbf..df73e4e 100644 --- a/walkdir.rb +++ b/walkdir.rb @@ -18,7 +18,7 @@ # and Mrs. Pollifax volume 6, On the China Station, is # .../DorothyGilman/P06_On_the_China_Station.epub. -require './book' +require 'book' class WalkDir def initialize(root)