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