Adds support for tracking series and generating pages based on them.
[quanlib.git] / main.rb
diff --git a/main.rb b/main.rb
index bc175fbf51ae33205a37735d1390872a3ea630e1..f3371b7d44daf55b9d65c3bbaf488b213d0f091b 100644 (file)
--- a/main.rb
+++ b/main.rb
@@ -1,9 +1,10 @@
+require 'page'
 require 'store'
 require 'walkdir'
 
 outputDir = 'output'
 
-books = []
+book_ids = []
 imageCount = 0
 
 def handleArg(arg)
@@ -28,65 +29,93 @@ for arg in ARGV
   if ! arg.start_with?("--")
     puts 'Scanning directory "' + arg + '"...'
     w = WalkDir.new(@store, arg)
-    books += (w.books)
+    book_ids += (w.books)
   end
 end
 
 puts 'Creating output...'
 
-if ! Dir.exist?(outputDir)
-  Dir.mkdir(outputDir)
+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.'
+  atoz_counts[letter] = book_ids.length
+
+  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', 'Index']
+
+  page.write_html(book_ids)
 end
 
-open(outputDir + '/index.html', 'w') do |fd|
-  fd.puts '<html>'
-  fd.puts '  <head>'
-  fd.puts '    <meta charset="utf-8"/>'
-  fd.puts '    <title>Books</title>'
-  fd.puts '    <style>'
-  fd.puts 'div { '
-  fd.puts '  display: inline-block;'
-  fd.puts '  width: 400px;'
-  fd.puts '  margin: 10px;'
-  fd.puts '  border 3px solid #73ad21;'
-  fd.puts '}'
-  fd.puts 'span.popup {  }'
-  fd.puts 'span.popup:hover {text-decoration: none; background: #cfffff; z-index: 6; }'
-  fd.puts 'span.popup span {display: none; position: absolute; '
-  fd.puts '  margin: 4px 0 0 0px; padding: 3px 3px 3px 3px;'
-  fd.puts '  border-style:solid; border-color:black; border-width:1px;}'
-  fd.puts 'span.popup:hover span {display: block; margin: 20px 0 0 0px; background: #ffffaf; z-index:6;}'
-  fd.puts '    </style>'
-  fd.puts '  </head>'
-  fd.puts '  <body>'
-  
-  for book in books
-    image = nil
-    if nil != book.cover
-      imageCount += 1
-      (path, mimeType) = book.cover.writeImage(outputDir, 'image' + imageCount.to_s)
-      image = '<img height="200px" src="' + path + '"/>'
-    else
-      image = '(No cover image)'
-    end
 
-    fd.puts '    <div><table>'
-    fd.puts '      <tr><td><a href="' + book.path + '">' + image + '</a></td>'
+ages = ['beginner', 'junior', 'ya', 'adult']
+
+series_infos = []
+
+ages.each do |age|
+  puts 'Series for "' + age + '" readers...'
 
-    heading = book.heading()
-    description = book.description()
-    if nil != description
-      fd.puts '          <td><span class="popup">' + heading + '<span><p>' + heading + '</p><p>' + description + '</p></span></span></td></tr>'
-    else
-      fd.puts '          <td>' + heading + '</td></tr>'
+  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
-      
-    fd.puts '    </table></div>'
   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']
   
-  fd.puts "    </table>"
-  fd.puts "  </body>"
-  fd.puts "</html>"
+    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="../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.write_html( [] )
+  
 @store.disconnect()
+