Refactor page generations, and add hard-coded series naming.
[quanlib.git] / page.rb
diff --git a/page.rb b/page.rb
new file mode 100644 (file)
index 0000000..2e5d735
--- /dev/null
+++ b/page.rb
@@ -0,0 +1,158 @@
+require 'store'
+
+
+class Page
+  def initialize(store)
+    @back = nil
+    @forward = nil
+    @output_dir = 'output'
+    @special = nil
+    @store = store
+    @title = 'Books'
+    @up = nil
+  end
+
+  def back=(value)
+    @back = value
+  end
+
+  def forward=(value)
+    @forward = value
+  end
+
+  def navig_link(data)
+    if (nil == data)
+      return ''
+    end
+    return '<a href="' + data[0] + '">' + data[1] + '</a>'
+  end
+
+  def output_dir=(value)
+    @output_dir = value
+  end
+
+  def special=(value)
+    @special = value
+  end
+
+  def title=(value)
+    @title = value
+  end
+
+  def up=(value)
+    @up = value
+  end
+
+  def write_books(fd, book_ids)
+    for id in book_ids
+      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)
+        image = '<img class="cover-thumb" src="' + path + '"/>'
+      else
+        image = '(No cover image)'
+      end
+
+      fd.puts '    <div><table>'
+      fd.puts '      <tr><td><a href="' + book.path + '">' + image + '</a></td>'
+
+      heading = book.heading()
+      description = book.description()
+      if nil != description
+        fd.puts '          <td><span class="popup">' + heading + '<span class="pop-inner"><p>' + heading + '</p><p>' + description + '</p></span></span></td></tr>'
+      else
+        fd.puts '          <td>' + heading + '</td></tr>'
+      end
+    
+      fd.puts '    </table></div>'
+    end
+  end
+
+  def write_footer(fd)
+    fd.puts '    <p class="navigator">' + navig_link(@back) + ' ' + navig_link(@up) + ' ' + navig_link(@forward) + '</p>'
+  end
+
+  def write_header(fd)
+    fd.puts '    <h1 class="header">' + @title + '</h1>'
+
+    fd.puts '    <p class="navigator">' + navig_link(@back) + ' ' + navig_link(@up) + ' ' + navig_link(@forward) + '</p>'
+  end
+
+  def write_html(book_ids)
+    @imageCount = 0
+
+    if ! Dir.exist?(@output_dir)
+      Dir.mkdir(@output_dir)
+    end
+
+    open(@output_dir + '/index.html', 'w') do |fd|
+      fd.puts '<html>'
+      fd.puts '  <head>'
+      fd.puts '    <meta charset="utf-8"/>'
+      fd.puts '    <title>' + @title + '</title>'
+
+      write_style_sheet(fd)
+
+      fd.puts '  </head>'
+      fd.puts '  <body>'
+      
+      write_header(fd)
+
+      write_special(fd)
+      write_books(fd, book_ids)
+  
+      write_footer(fd)
+
+      fd.puts "  </body>"
+      fd.puts "</html>"
+    end
+  end
+
+  def write_special(fd)
+    if (nil != @special)
+      fd.puts(@special)
+    end
+  end
+
+  def write_style_sheet(fd)
+      style = 
+<<EOS
+    <style>
+      div { 
+        display: inline-block;
+        width: 400px;
+        margin: 10px;
+        border 3px solid #73ad21;
+      }
+      h1.header { 
+        background: #4040a0;
+        color: #ffffff;
+        text-align: center;
+      }
+      img.cover-thumb { max-height: 200px; max-width: 200px; }
+      p.navigator { }
+      span.popup { }
+      span.popup:hover { text-decoration: none; background: #cfffff; z-index: 6; }
+      span.popup span.pop-inner { 
+        border-color:black; 
+        border-style:solid; 
+        border-width:1px;
+        display: none; 
+        margin: 4px 0 0 0px; 
+        padding: 3px 3px 3px 3px;
+        position: absolute; 
+      }
+      span.popup:hover span.pop-inner { 
+        background: #ffffaf; 
+        display: block; 
+        margin: 20px 0 0 0px; 
+        z-index:6;
+      }
+    </style>
+EOS
+      fd.puts style
+  end
+end
+