]> jaekl.net Git - quanlib.git/commitdiff
Create a basic html output, to validate that we're loading data correctly.
authorChris Jaekl <cejaekl@yahoo.com>
Fri, 24 Feb 2017 15:20:41 +0000 (00:20 +0900)
committerChris Jaekl <cejaekl@yahoo.com>
Fri, 24 Feb 2017 15:20:41 +0000 (00:20 +0900)
book.rb
cover.rb [new file with mode: 0644]
main.rb
walkdir.rb

diff --git a/book.rb b/book.rb
index 72bb83ad1170f77b29e58279eac85cb32c505146..370093da92964413102b286792ab27a4e8ed6415 100644 (file)
--- 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('<b>' + @title + '</b>')
+    else
+      result.push('<i>(Unknown title)</i>')
+    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('<br/>')
+  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 (file)
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 76f543e61cffd9019e2a1eee49b4860e7e6ebbf4..4e73b07c0b490eca6eef199f504055406bfb907d 100644 (file)
--- 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 "<html>"
+  fd.puts "  <head><title>Books</title></head>"
+  fd.puts "  <body>"
+  fd.puts "    <table>"
+  
+  for book in books
+    image = nil
+    if nil != book.cover
+      imageCount += 1
+      (path, mimeType) = book.cover.writeImage(outputDir, 'image' + imageCount.to_s)
+      image = '<img src="' + path + '"/>'
+    else
+      image = '(No cover image)'
+    end
+
+    fd.puts "      <tr><td>" + image + "</td><td>" + book.describe() + "</td></tr>"
+  end
+  
+  fd.puts "    </table>"
+  fd.puts "  </body>"
+  fd.puts "</html>"
 end
 
-puts books
index c776dbfd7e67822042dd8bd3c825bc35ac338625..df73e4e9f393b0f9b9cf956bb50b0e87601fbb11 100644 (file)
@@ -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)