Start support for reading data back from the database.
[quanlib.git] / main.rb
1 require 'store'
2 require 'walkdir'
3
4 outputDir = 'output'
5
6 books = []
7 imageCount = 0
8
9 def handleArg(arg)
10   if "--purge" == arg
11     puts 'Purging database...'
12     @store.dropSchema()
13   elsif arg.start_with?("--")
14     abort('ERROR:  Unrecognized option "' + arg + '".')
15   end
16 end
17
18 @store = Store.new()
19 @store.connect()
20
21 for arg in ARGV
22   handleArg(arg)
23 end
24
25 @store.init_db()
26
27 for arg in ARGV
28   if ! arg.start_with?("--")
29     puts 'Scanning directory "' + arg + '"...'
30     w = WalkDir.new(@store, arg)
31     books += (w.books)
32   end
33 end
34
35 puts 'Creating output...'
36
37 if ! Dir.exist?(outputDir)
38   Dir.mkdir(outputDir)
39 end
40
41 open(outputDir + '/index.html', 'w') do |fd|
42   fd.puts '<html>'
43   fd.puts '  <head>'
44   fd.puts '    <meta charset="utf-8"/>'
45   fd.puts '    <title>Books</title>'
46   fd.puts '    <style>'
47   fd.puts 'div { '
48   fd.puts '  display: inline-block;'
49   fd.puts '  width: 400px;'
50   fd.puts '  margin: 10px;'
51   fd.puts '  border 3px solid #73ad21;'
52   fd.puts '}'
53   fd.puts 'span.popup {  }'
54   fd.puts 'span.popup:hover {text-decoration: none; background: #cfffff; z-index: 6; }'
55   fd.puts 'span.popup span {display: none; position: absolute; '
56   fd.puts '  margin: 4px 0 0 0px; padding: 3px 3px 3px 3px;'
57   fd.puts '  border-style:solid; border-color:black; border-width:1px;}'
58   fd.puts 'span.popup:hover span {display: block; margin: 20px 0 0 0px; background: #ffffaf; z-index:6;}'
59   fd.puts '    </style>'
60   fd.puts '  </head>'
61   fd.puts '  <body>'
62   
63   for book in books
64     image = nil
65     if nil != book.cover
66       imageCount += 1
67       (path, mimeType) = book.cover.write_image(outputDir, 'image' + imageCount.to_s)
68       image = '<img height="200px" src="' + path + '"/>'
69     else
70       image = '(No cover image)'
71     end
72
73     fd.puts '    <div><table>'
74     fd.puts '      <tr><td><a href="' + book.path + '">' + image + '</a></td>'
75
76     heading = book.heading()
77     description = book.description()
78     if nil != description
79       fd.puts '          <td><span class="popup">' + heading + '<span><p>' + heading + '</p><p>' + description + '</p></span></span></td></tr>'
80     else
81       fd.puts '          <td>' + heading + '</td></tr>'
82     end
83       
84     fd.puts '    </table></div>'
85   end
86   
87   fd.puts "    </table>"
88   fd.puts "  </body>"
89   fd.puts "</html>"
90 end
91
92 @store.disconnect()