Adds support for tracking series and generating pages based on them.
authorChris Jaekl <cejaekl@yahoo.com>
Fri, 9 Jun 2017 14:48:09 +0000 (23:48 +0900)
committerChris Jaekl <cejaekl@yahoo.com>
Fri, 9 Jun 2017 14:48:09 +0000 (23:48 +0900)
book.rb
cover.rb
main.rb
page.rb
store.rb

diff --git a/book.rb b/book.rb
index f1481f0c7ef5cb6e96b72615a2b50f30ff044b0d..aa61930bc357fb89dcc861085eedd38921b1f654 100644 (file)
--- a/book.rb
+++ b/book.rb
@@ -78,8 +78,8 @@ class Book
     
     seriesInfo = []
     series = @store.load_series(@series_id)
-    if nil != series
-      seriesInfo.push(series.to_s)
+    if nil != series and nil != series.descr
+      seriesInfo.push(series.descr.to_s)
     end
     if nil != @volume
       seriesInfo.push(@volume.to_s)
index 5cc80a0f24930027dddd82f8b51ec50348d7f1c0..9b974847d087a800518bdccd983f02bc63e4f59b 100644 (file)
--- a/cover.rb
+++ b/cover.rb
@@ -1,11 +1,19 @@
 
 class Cover
   def initialize(inputStream, path, mimeType)
-    @data = inputStream.read
+    if nil != inputStream
+      @data = inputStream.read
+    else
+      @data = nil
+    end
     @path = path
     @mimeType = mimeType
   end
 
+  def path 
+    @path
+  end
+
   def inspect
     info = []
     if nil != @data
diff --git a/main.rb b/main.rb
index d193c22360fab18aa2850cbc5a2b6d132c2aa843..f3371b7d44daf55b9d65c3bbaf488b213d0f091b 100644 (file)
--- a/main.rb
+++ b/main.rb
@@ -35,33 +35,85 @@ end
 
 puts 'Creating output...'
 
-counts = {}
+atoz_counts = {}
 
 ('A'..'Z').each do |letter| 
   book_ids = @store.query_books_by_author(letter + '%')
   puts 'Authors starting with "' + letter + '":  ' + book_ids.length.to_s() + ' books.'
-  counts[letter] = book_ids.length
+  atoz_counts[letter] = book_ids.length
 
   page = Page.new(@store)
   if 'A' != letter
-    page.back = ['../output_' + (letter.ord - 1).chr + '/index.html', 'Prev']
+    page.back = ['../atoz/output_' + (letter.ord - 1).chr + '.html', 'Prev']
   end
   if 'Z' != letter
-    page.forward = ['../output_' + (letter.ord + 1).chr + '/index.html', 'Next']
+    page.forward = ['../atoz/output_' + (letter.ord + 1).chr + '.html', 'Next']
   end
-  page.output_dir = 'output_' + letter
+  page.output_dir = 'atoz'
+  page.index_file = 'output_' + letter + '.html'
   page.title = "Authors starting with '" + letter + "'"
-  page.up = ['../output/index.html', 'Index']
+  page.up = ['../atoz/index.html', 'Index']
 
   page.write_html(book_ids)
 end
 
+
+ages = ['beginner', 'junior', 'ya', 'adult']
+
+series_infos = []
+
+ages.each do |age|
+  puts 'Series for "' + age + '" readers...'
+
+  series_ids = @store.query_series_by_age(age)
+
+  series_ids.each do |id|
+    series = @store.load_series(id)
+    book_ids = @store.query_books_by_series_id(id)
+    if nil != book_ids and book_ids.length > 0
+      series_infos.push( [series, book_ids] )
+    end
+  end
+
+  for idx in 0 .. (series_infos.length - 1) do 
+    #puts series.descr + ': ' + book_ids.length.to_s + ' books.'
+
+    back = nil
+    fwd = nil
+
+    if idx > 0
+      back = series_infos[idx-1]
+    end
+    if idx < (series_infos.length - 1)
+      fwd = series_infos[idx+1]
+    end
+
+    cur = series_infos[idx]
+    series = cur[0]
+    book_ids = cur[1]
+
+    page = Page.new(@store)
+    if nil != back
+      page.back = [back[0].key + '.html', 'Back']
+    end
+    if nil != fwd
+      page.forward = [fwd[0].key + '.html', 'Forward']
+    end
+    page.output_dir = 'series'
+    page.index_file = series.key + '.html'
+    page.title = 'Series &ldquo;' + series.descr + '&rdquo; (' + book_ids.length.to_s + ' books)'
+    page.up = ['../atoz/index.html', 'Index']
+  
+    page.write_html(book_ids)
+  end
+end
+
 content = '<table><tr><th>Author</th><th>Books</th></tr>'
 ('A'..'Z').each do |letter|
-  content += '  <tr><td><a href="../output_' + letter + '/index.html">Starting with ' + letter + '</a></td><td>' + counts[letter].to_s + '</td></tr>'
+  content += '  <tr><td><a href="../atoz/output_' + letter + '.html">Starting with ' + letter + '</a></td><td>' + atoz_counts[letter].to_s + '</td></tr>'
 end
 page = Page.new(@store)
-page.output_dir = 'output'
+page.output_dir = 'atoz'
 page.special = content
 page.write_html( [] )
   
diff --git a/page.rb b/page.rb
index 2e5d73524c3fd9d0689b29576b93ab6560bd19b9..1d83add369433cc331c3f1e8ed8099d4d22738c3 100644 (file)
--- a/page.rb
+++ b/page.rb
@@ -5,6 +5,7 @@ class Page
   def initialize(store)
     @back = nil
     @forward = nil
+    @index_file = 'index.html'
     @output_dir = 'output'
     @special = nil
     @store = store
@@ -20,6 +21,10 @@ class Page
     @forward = value
   end
 
+  def index_file=(value)
+    @index_file = value
+  end
+
   def navig_link(data)
     if (nil == data)
       return ''
@@ -48,8 +53,10 @@ class Page
       book = @store.load_book(id)
       image = nil
       if nil != book.cover
-        @imageCount += 1
-        (path, mimeType) = book.cover.write_image(@output_dir, 'image' + @imageCount.to_s)
+        #@imageCount += 1
+        #(path, mimeType) = book.cover.write_image(@output_dir, 'image' + @imageCount.to_s)
+        #image = '<img class="cover-thumb" src="' + path + '"/>'
+        path = book.cover.path
         image = '<img class="cover-thumb" src="' + path + '"/>'
       else
         image = '(No cover image)'
@@ -87,7 +94,7 @@ class Page
       Dir.mkdir(@output_dir)
     end
 
-    open(@output_dir + '/index.html', 'w') do |fd|
+    open(@output_dir + '/' + @index_file, 'w') do |fd|
       fd.puts '<html>'
       fd.puts '  <head>'
       fd.puts '    <meta charset="utf-8"/>'
index 946c43c4f9056a53a1e753b2240babeb90c08e81..0ba3447e8807051c923b258cfcbf9f4270d398b3 100644 (file)
--- a/store.rb
+++ b/store.rb
@@ -3,6 +3,8 @@ require 'csv'
 require 'fileutils'
 require 'pg'
 
+require 'series'
+
 class Store
   def initialize
     @basepath = '/arc/quanlib' # TODO: FIXME: configure this in a sane way
@@ -244,11 +246,15 @@ EOS
 
     (efspath, efsname) = construct_efs_path(id)
 
-    File.open(@basepath + '/efs/' + efspath + '/' + efsname, 'rb') do |is|
-      return Cover.new(is, efsname, mime_type)
-    end
+    fullpath = @basepath + '/efs/' + efspath + '/' + efsname
 
-    return nil
+    return Cover.new(nil, fullpath, mime_type)
+
+    #File.open(fullpath, 'rb') do |is|
+    #  return Cover.new(is, fullpath, mime_type)
+    #end
+    #
+    #return nil
   end
 
   def store_cover(book)
@@ -314,11 +320,18 @@ EOS
   end
 
   def load_series(id)
-    sql = "SELECT descr FROM Series WHERE id=$1;"
+    sql = "SELECT age,genre,grouping,code,descr FROM Series WHERE id=$1;"
     args = [id]
     @conn.exec_params(sql, args) do |rs|
       if rs.ntuples > 0
-        return rs[0]['descr']
+        row = rs[0]
+        series = Series.new(id)
+        series.age = row['age']
+        series.genre = row['genre']
+        series.grouping = row['grouping']
+        series.code = row['code']
+        series.descr = row['descr']
+        return series
       end
     end
     return nil
@@ -360,5 +373,37 @@ EOS
     end
     return book_ids
   end
+
+  def query_books_by_series_id(id)
+    sql = 
+<<EOS
+      SELECT b.id FROM Books b
+      WHERE b.series = $1
+      ORDER BY b.volume,b.title
+EOS
+    book_ids = []
+    @conn.exec_params(sql, [id]) do |rs|
+      rs.each do |row|
+        book_ids.push(row['id'])
+      end
+    end
+    return book_ids
+  end
+
+  def query_series_by_age(pattern)
+    sql = 
+<<EOS
+      SELECT s.id FROM Series s
+      WHERE s.age LIKE $1
+      ORDER BY s.descr
+EOS
+    series_ids = []
+    @conn.exec_params(sql, [pattern]) do |rs|
+      rs.each do |row|
+        series_ids.push(row['id'])
+      end
+    end
+    return series_ids
+  end
 end