Add support for PDF (with .jpeg cover).
[quanlib.git] / navigator.rb
1 require 'page'
2 require 'store'
3
4 class Navigator
5   def initialize(store)
6     @store = store
7   end
8
9   def write_atoz_pages
10     atoz_counts = {}
11
12     ('A'..'Z').each do |letter| 
13       atoz_counts[letter] = write_authors_starting_with(letter)
14     end
15
16     content = '<table><tr><th>Author</th><th>Books</th></tr>'
17     ('A'..'Z').each do |letter|
18       content += '  <tr><td><a href="../atoz/output_' + letter + '.html">Starting with ' + letter + '</a></td><td>' + atoz_counts[letter].to_s + '</td></tr>'
19     end
20     page = Page.new(@store)
21     page.output_dir = 'atoz'
22     page.special = content
23     page.up = ['../output/index.html', 'Up']
24
25     page.write_html( [] )
26   end
27
28   def write_authors_starting_with(letter)
29     book_ids = @store.query_books_by_author(letter + '%')
30     puts 'Authors starting with "' + letter + '":  ' + book_ids.length.to_s() + ' books.'
31
32     page = Page.new(@store)
33     if 'A' != letter
34       page.back = ['../atoz/output_' + (letter.ord - 1).chr + '.html', 'Prev']
35     end
36     if 'Z' != letter
37       page.forward = ['../atoz/output_' + (letter.ord + 1).chr + '.html', 'Next']
38     end
39     page.output_dir = 'atoz'
40     page.index_file = 'output_' + letter + '.html'
41     page.title = "Authors starting with '" + letter + "'"
42     page.up = ['../atoz/index.html', 'Up']
43
44     page.write_html(book_ids)
45     return book_ids.length
46   end
47
48   def write_series_for_age(age)
49     series_infos = []
50
51     series_ids = @store.query_series_by_age(age)
52
53     series_ids.each do |id|
54       series = @store.load_series(id)
55       book_ids = @store.query_books_by_series_id(id)
56       if nil != book_ids and book_ids.length > 0
57         series_infos.push( [series, book_ids] )
58       end
59     end
60
61     for idx in 0 .. (series_infos.length - 1) do 
62       #puts series.descr + ': ' + book_ids.length.to_s + ' books.'
63
64       back = nil
65       fwd = nil
66
67       if idx > 0
68         back = series_infos[idx-1]
69       end
70       if (idx + 1) < series_infos.length
71         fwd = series_infos[idx+1]
72       end
73
74       cur = series_infos[idx]
75       series = cur[0]
76       book_ids = cur[1]
77
78       page = Page.new(@store)
79       if nil != back
80         page.back = [back[0].key + '.html', 'Back']
81       end
82       if nil != fwd
83         page.forward = [fwd[0].key + '.html', 'Forward']
84       end
85       page.output_dir = 'series/series_' + age
86       page.index_file = series.key + '.html'
87       page.title = 'Series &ldquo;' + series.descr + '&rdquo; (' + book_ids.length.to_s + ' books)'
88       page.up = ['index.html', 'Up']
89   
90       page.write_html(book_ids)
91     end
92
93     content =  '<h1>&ldquo;' + age + '&rdquo; Series</h1>'
94     content += '<p><table><tr><th>Author</th><th>Series</th><th>Genre</th><th>Books</th></tr>'
95     series_infos.each do |cur|
96       series = cur[0]
97       book_ids = cur[1]
98
99       author = series.grouping
100       letter = author[0]
101
102       content += '  <tr>'
103       content += '<td><a href="../../atoz/output_' + letter + '.html">' + author + '</a></td>'
104       content += '<td><a href="' + series.key + '.html">' + series.descr + '</a></td>'
105       content += '<td>' + series.genre + '</td>'
106       content += '<td>' + book_ids.length.to_s + '</td>'
107       content += '</tr>'
108     end
109     content += '</table></p>'
110     page = Page.new(@store)
111     page.output_dir = 'series/series_' + age
112     page.special = content
113     page.up = ['../index.html', 'Up']
114     page.write_html( [] )
115
116     return series_infos.length
117   end
118
119   def write_series_listing
120     ages = ['beginner', 'junior', 'ya', 'adult']
121     series_counts = {}
122
123     ages.each do |age|
124       puts 'Series for "' + age + '" readers...'
125
126       series_counts[age] = write_series_for_age(age)
127     end
128
129     content = '<h1>Browse Books By Series</h1>'
130     content += '<p>'
131     content += '<table><tr><th>Age</th><th>Number of Series</th></tr>'
132     ages.each do |age|
133       content += '<tr><td><a href="series_' + age + '/index.html">' + age + '</a></td><td>' + series_counts[age].to_s + '</td></tr>'
134     end
135     content += '</table></p>'
136     page = Page.new(@store)
137     page.output_dir = 'series'
138     page.special = content
139     page.up = ['../output/index.html', 'Up']
140     page.write_html( [] )
141   end
142 end