Add `arrived` attribute (file creation timestamp) to books table.
[quanlib.git] / page.rb
1 require 'fileutils'
2
3 require_relative 'store'
4
5 class Page
6   def initialize(store)
7     @back = nil
8     @forward = nil
9     @index_file = 'index.html'
10     @output_dir = 'output'
11     @special = nil
12     @store = store
13     @title = 'Books'
14     @up = nil
15   end
16
17   def back=(value)
18     @back = value
19   end
20
21   def forward=(value)
22     @forward = value
23   end
24
25   def index_file=(value)
26     @index_file = value
27   end
28
29   def navig_link(data)
30     if (nil == data)
31       return ''
32     end
33     return '<a href="' + data[0] + '">' + data[1] + '</a>'
34   end
35
36   def output_dir=(value)
37     @output_dir = value
38   end
39
40   def special=(value)
41     @special = value
42   end
43
44   def title=(value)
45     @title = value
46   end
47
48   def up=(value)
49     @up = value
50   end
51
52   def write_books(fd, book_ids)
53     for id in book_ids
54       book = @store.load_book(id)
55       image = nil
56       if nil != book.cover
57         #@imageCount += 1
58         #(path, mimeType) = book.cover.write_image(@output_dir, 'image' + @imageCount.to_s)
59         #image = '<img class="cover-thumb" src="' + path + '"/>'
60         path = book.cover.path
61         image = '<img class="cover-thumb" src="' + path + '"/>'
62       else
63         image = '(No cover image)'
64       end
65
66       fd.puts '    <div><table>'
67       fd.puts '      <tr><td><a href="' + book.path + '">' + image + '</a></td>'
68
69       heading = book.heading()
70       description = book.description()
71       if nil != description
72         fd.puts '          <td><span class="popup">' + heading + '<span class="pop-inner"><p>' + heading + '</p><p>' + description + '</p></span></span></td></tr>'
73       else
74         fd.puts '          <td>' + heading + '</td></tr>'
75       end
76     
77       fd.puts '    </table></div>'
78     end
79   end
80
81   def write_footer(fd)
82     fd.puts '    <p class="navigator">' + navig_link(@back) + ' ' + navig_link(@up) + ' ' + navig_link(@forward) + '</p>'
83   end
84
85   def write_header(fd)
86     fd.puts '    <h1 class="header">' + @title + '</h1>'
87
88     fd.puts '    <p class="navigator">' + navig_link(@back) + ' ' + navig_link(@up) + ' ' + navig_link(@forward) + '</p>'
89   end
90
91   def write_html(book_ids)
92     @imageCount = 0
93
94     if ! Dir.exist?(@output_dir)
95       FileUtils.mkdir_p(@output_dir)
96     end
97
98     open(@output_dir + '/' + @index_file, 'w') do |fd|
99       fd.puts '<html>'
100       fd.puts '  <head>'
101       fd.puts '    <meta charset="utf-8"/>'
102       fd.puts '    <title>' + @title + '</title>'
103
104       write_style_sheet(fd)
105
106       fd.puts '  </head>'
107       fd.puts '  <body>'
108       
109       write_header(fd)
110
111       write_special(fd)
112       write_books(fd, book_ids)
113   
114       write_footer(fd)
115
116       fd.puts "  </body>"
117       fd.puts "</html>"
118     end
119   end
120
121   def write_special(fd)
122     if (nil != @special)
123       fd.puts(@special)
124     end
125   end
126
127   def write_style_sheet(fd)
128       style = 
129 <<EOS
130     <style>
131       div { 
132         display: inline-block;
133         width: 400px;
134         margin: 10px;
135         border 3px solid #73ad21;
136       }
137       h1.header { 
138         background: #4040a0;
139         color: #ffffff;
140         text-align: center;
141       }
142       img.cover-thumb { max-height: 200px; max-width: 200px; }
143       p.navigator { }
144       span.popup { }
145       span.popup:hover { text-decoration: none; background: #cfffff; z-index: 6; }
146       span.popup span.pop-inner { 
147         border-color:black; 
148         border-style:solid; 
149         border-width:1px;
150         display: none; 
151         margin: 4px 0 0 0px; 
152         padding: 3px 3px 3px 3px;
153         position: absolute; 
154       }
155       span.popup:hover span.pop-inner { 
156         background: #ffffaf; 
157         display: block; 
158         margin: 20px 0 0 0px; 
159         z-index:6;
160       }
161     </style>
162 EOS
163       fd.puts style
164   end
165 end
166