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