Add `arrived` attribute (file creation timestamp) to books table.
[quanlib.git] / navigator.rb
1 require_relative 'page'
2 require_relative '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 = '<p><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     content += '</table></p>'
21     page = Page.new(@store)
22     page.output_dir = 'atoz'
23     page.special = content
24     page.up = ['../output/index.html', 'Up']
25
26     page.write_html( [] )
27   end
28
29   def write_authors_starting_with(letter)
30     book_ids = @store.query_books_by_author(letter + '%')
31     puts 'Authors starting with "' + letter + '":  ' + book_ids.length.to_s() + ' books.'
32
33     page = Page.new(@store)
34     if 'A' != letter
35       page.back = ['../atoz/output_' + (letter.ord - 1).chr + '.html', 'Prev']
36     end
37     if 'Z' != letter
38       page.forward = ['../atoz/output_' + (letter.ord + 1).chr + '.html', 'Next']
39     end
40     page.output_dir = 'atoz'
41     page.index_file = 'output_' + letter + '.html'
42     page.title = "Authors starting with '" + letter + "'"
43     page.up = ['../atoz/index.html', 'Up']
44
45     page.write_html(book_ids)
46     return book_ids.length
47   end
48
49   def write_dewey
50     book_ids = @store.query_books_by_ddc()
51     puts 'Non-fiction books arranged by Dewey Decimal Classification:  ' + book_ids.length.to_s() + ' books.'
52
53     page = Page.new(@store)
54     page.output_dir = 'ddc'
55     page.index_file = 'index.html'
56     page.title = "Non-fiction books arranged by Dewey Decimal call number"
57     page.up = ['../output/index.html', 'Up']
58     
59     page.write_html(book_ids)
60     return book_ids.length
61   end
62
63   def write_series_for_age(age)
64     series_infos = []
65
66     series_ids = @store.query_series_by_age(age)
67
68     series_ids.each do |id|
69       series = @store.load_series(id)
70       book_ids = @store.query_books_by_series_id(id)
71       if nil != book_ids and book_ids.length > 0
72         series_infos.push( [series, book_ids] )
73       end
74     end
75
76     for idx in 0 .. (series_infos.length - 1) do 
77       #puts series.descr + ': ' + book_ids.length.to_s + ' books.'
78
79       back = nil
80       fwd = nil
81
82       if idx > 0
83         back = series_infos[idx-1]
84       end
85       if (idx + 1) < series_infos.length
86         fwd = series_infos[idx+1]
87       end
88
89       cur = series_infos[idx]
90       series = cur[0]
91       book_ids = cur[1]
92
93       page = Page.new(@store)
94       if nil != back
95         page.back = [back[0].key + '.html', 'Back']
96       end
97       if nil != fwd
98         page.forward = [fwd[0].key + '.html', 'Forward']
99       end
100       page.output_dir = 'series/series_' + age
101       page.index_file = series.key + '.html'
102       page.title = 'Series &ldquo;' + series.descr + '&rdquo; (' + book_ids.length.to_s + ' books)'
103       page.up = ['index.html', 'Up']
104   
105       page.write_html(book_ids)
106     end
107
108     content =  '<h1>&ldquo;' + age + '&rdquo; Series</h1>'
109     content += '<p><table><tr><th>Author</th><th>Series</th><th>Genre</th><th>Books</th></tr>'
110     series_infos.each do |cur|
111       series = cur[0]
112       book_ids = cur[1]
113
114       author = series.grouping
115       letter = author[0]
116
117       content += '  <tr>'
118       content += '<td><a href="../../atoz/output_' + letter + '.html">' + author + '</a></td>'
119       content += '<td><a href="' + series.key + '.html">' + series.descr + '</a></td>'
120       content += '<td>' + series.genre + '</td>'
121       content += '<td>' + book_ids.length.to_s + '</td>'
122       content += '</tr>'
123     end
124     content += '</table></p>'
125     page = Page.new(@store)
126     page.output_dir = 'series/series_' + age
127     page.special = content
128     page.up = ['../index.html', 'Up']
129     page.write_html( [] )
130
131     return series_infos.length
132   end
133
134   def write_series_listing
135     ages = ['beginner', 'junior', 'ya', 'adult']
136     series_counts = {}
137
138     ages.each do |age|
139       puts 'Series for "' + age + '" readers...'
140
141       series_counts[age] = write_series_for_age(age)
142     end
143
144     content = '<h1>Browse Books By Series</h1>'
145     content += '<p>'
146     content += '<table><tr><th>Age</th><th>Number of Series</th></tr>'
147     ages.each do |age|
148       content += '<tr><td><a href="series_' + age + '/index.html">' + age + '</a></td><td>' + series_counts[age].to_s + '</td></tr>'
149     end
150     content += '</table></p>'
151     page = Page.new(@store)
152     page.output_dir = 'series'
153     page.special = content
154     page.up = ['../output/index.html', 'Up']
155     page.write_html( [] )
156   end
157 end