Add support for PDF (with .jpeg cover).
[quanlib.git] / navigator.rb
diff --git a/navigator.rb b/navigator.rb
new file mode 100644 (file)
index 0000000..16da652
--- /dev/null
@@ -0,0 +1,142 @@
+require 'page'
+require 'store'
+
+class Navigator
+  def initialize(store)
+    @store = store
+  end
+
+  def write_atoz_pages
+    atoz_counts = {}
+
+    ('A'..'Z').each do |letter| 
+      atoz_counts[letter] = write_authors_starting_with(letter)
+    end
+
+    content = '<table><tr><th>Author</th><th>Books</th></tr>'
+    ('A'..'Z').each do |letter|
+      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 = 'atoz'
+    page.special = content
+    page.up = ['../output/index.html', 'Up']
+
+    page.write_html( [] )
+  end
+
+  def write_authors_starting_with(letter)
+    book_ids = @store.query_books_by_author(letter + '%')
+    puts 'Authors starting with "' + letter + '":  ' + book_ids.length.to_s() + ' books.'
+
+    page = Page.new(@store)
+    if 'A' != letter
+      page.back = ['../atoz/output_' + (letter.ord - 1).chr + '.html', 'Prev']
+    end
+    if 'Z' != letter
+      page.forward = ['../atoz/output_' + (letter.ord + 1).chr + '.html', 'Next']
+    end
+    page.output_dir = 'atoz'
+    page.index_file = 'output_' + letter + '.html'
+    page.title = "Authors starting with '" + letter + "'"
+    page.up = ['../atoz/index.html', 'Up']
+
+    page.write_html(book_ids)
+    return book_ids.length
+  end
+
+  def write_series_for_age(age)
+    series_infos = []
+
+    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 + 1) < series_infos.length
+        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/series_' + age
+      page.index_file = series.key + '.html'
+      page.title = 'Series &ldquo;' + series.descr + '&rdquo; (' + book_ids.length.to_s + ' books)'
+      page.up = ['index.html', 'Up']
+  
+      page.write_html(book_ids)
+    end
+
+    content =  '<h1>&ldquo;' + age + '&rdquo; Series</h1>'
+    content += '<p><table><tr><th>Author</th><th>Series</th><th>Genre</th><th>Books</th></tr>'
+    series_infos.each do |cur|
+      series = cur[0]
+      book_ids = cur[1]
+
+      author = series.grouping
+      letter = author[0]
+
+      content += '  <tr>'
+      content += '<td><a href="../../atoz/output_' + letter + '.html">' + author + '</a></td>'
+      content += '<td><a href="' + series.key + '.html">' + series.descr + '</a></td>'
+      content += '<td>' + series.genre + '</td>'
+      content += '<td>' + book_ids.length.to_s + '</td>'
+      content += '</tr>'
+    end
+    content += '</table></p>'
+    page = Page.new(@store)
+    page.output_dir = 'series/series_' + age
+    page.special = content
+    page.up = ['../index.html', 'Up']
+    page.write_html( [] )
+
+    return series_infos.length
+  end
+
+  def write_series_listing
+    ages = ['beginner', 'junior', 'ya', 'adult']
+    series_counts = {}
+
+    ages.each do |age|
+      puts 'Series for "' + age + '" readers...'
+
+      series_counts[age] = write_series_for_age(age)
+    end
+
+    content = '<h1>Browse Books By Series</h1>'
+    content += '<p>'
+    content += '<table><tr><th>Age</th><th>Number of Series</th></tr>'
+    ages.each do |age|
+      content += '<tr><td><a href="series_' + age + '/index.html">' + age + '</a></td><td>' + series_counts[age].to_s + '</td></tr>'
+    end
+    content += '</table></p>'
+    page = Page.new(@store)
+    page.output_dir = 'series'
+    page.special = content
+    page.up = ['../output/index.html', 'Up']
+    page.write_html( [] )
+  end
+end