From fffa1ed35ce07fdec65e1aa14a8f637fbdde9b10 Mon Sep 17 00:00:00 2001 From: Chris Jaekl Date: Fri, 9 Jun 2017 23:48:09 +0900 Subject: [PATCH] Adds support for tracking series and generating pages based on them. --- book.rb | 4 ++-- cover.rb | 10 ++++++++- main.rb | 68 +++++++++++++++++++++++++++++++++++++++++++++++++------- page.rb | 13 ++++++++--- store.rb | 57 ++++++++++++++++++++++++++++++++++++++++++----- 5 files changed, 132 insertions(+), 20 deletions(-) diff --git a/book.rb b/book.rb index f1481f0..aa61930 100644 --- 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) diff --git a/cover.rb b/cover.rb index 5cc80a0..9b97484 100644 --- 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 d193c22..f3371b7 100644 --- 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 “' + series.descr + '” (' + book_ids.length.to_s + ' books)' + page.up = ['../atoz/index.html', 'Index'] + + page.write_html(book_ids) + end +end + content = '' ('A'..'Z').each do |letter| - content += ' ' + content += ' ' 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 2e5d735..1d83add 100644 --- 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 = '' + path = book.cover.path image = '' 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 '' fd.puts ' ' fd.puts ' ' diff --git a/store.rb b/store.rb index 946c43c..0ba3447 100644 --- 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 = +<
AuthorBooks
Starting with ' + letter + '' + counts[letter].to_s + '
Starting with ' + letter + '' + atoz_counts[letter].to_s + '