Adds support for tracking series and generating pages based on them.
[quanlib.git] / main.rb
1 require 'page'
2 require 'store'
3 require 'walkdir'
4
5 outputDir = 'output'
6
7 book_ids = []
8 imageCount = 0
9
10 def handleArg(arg)
11   if "--purge" == arg
12     puts 'Purging database...'
13     @store.dropSchema()
14   elsif arg.start_with?("--")
15     abort('ERROR:  Unrecognized option "' + arg + '".')
16   end
17 end
18
19 @store = Store.new()
20 @store.connect()
21
22 for arg in ARGV
23   handleArg(arg)
24 end
25
26 @store.init_db()
27
28 for arg in ARGV
29   if ! arg.start_with?("--")
30     puts 'Scanning directory "' + arg + '"...'
31     w = WalkDir.new(@store, arg)
32     book_ids += (w.books)
33   end
34 end
35
36 puts 'Creating output...'
37
38 atoz_counts = {}
39
40 ('A'..'Z').each do |letter| 
41   book_ids = @store.query_books_by_author(letter + '%')
42   puts 'Authors starting with "' + letter + '":  ' + book_ids.length.to_s() + ' books.'
43   atoz_counts[letter] = book_ids.length
44
45   page = Page.new(@store)
46   if 'A' != letter
47     page.back = ['../atoz/output_' + (letter.ord - 1).chr + '.html', 'Prev']
48   end
49   if 'Z' != letter
50     page.forward = ['../atoz/output_' + (letter.ord + 1).chr + '.html', 'Next']
51   end
52   page.output_dir = 'atoz'
53   page.index_file = 'output_' + letter + '.html'
54   page.title = "Authors starting with '" + letter + "'"
55   page.up = ['../atoz/index.html', 'Index']
56
57   page.write_html(book_ids)
58 end
59
60
61 ages = ['beginner', 'junior', 'ya', 'adult']
62
63 series_infos = []
64
65 ages.each do |age|
66   puts 'Series for "' + age + '" readers...'
67
68   series_ids = @store.query_series_by_age(age)
69
70   series_ids.each do |id|
71     series = @store.load_series(id)
72     book_ids = @store.query_books_by_series_id(id)
73     if nil != book_ids and book_ids.length > 0
74       series_infos.push( [series, book_ids] )
75     end
76   end
77
78   for idx in 0 .. (series_infos.length - 1) do 
79     #puts series.descr + ': ' + book_ids.length.to_s + ' books.'
80
81     back = nil
82     fwd = nil
83
84     if idx > 0
85       back = series_infos[idx-1]
86     end
87     if idx < (series_infos.length - 1)
88       fwd = series_infos[idx+1]
89     end
90
91     cur = series_infos[idx]
92     series = cur[0]
93     book_ids = cur[1]
94
95     page = Page.new(@store)
96     if nil != back
97       page.back = [back[0].key + '.html', 'Back']
98     end
99     if nil != fwd
100       page.forward = [fwd[0].key + '.html', 'Forward']
101     end
102     page.output_dir = 'series'
103     page.index_file = series.key + '.html'
104     page.title = 'Series &ldquo;' + series.descr + '&rdquo; (' + book_ids.length.to_s + ' books)'
105     page.up = ['../atoz/index.html', 'Index']
106   
107     page.write_html(book_ids)
108   end
109 end
110
111 content = '<table><tr><th>Author</th><th>Books</th></tr>'
112 ('A'..'Z').each do |letter|
113   content += '  <tr><td><a href="../atoz/output_' + letter + '.html">Starting with ' + letter + '</a></td><td>' + atoz_counts[letter].to_s + '</td></tr>'
114 end
115 page = Page.new(@store)
116 page.output_dir = 'atoz'
117 page.special = content
118 page.write_html( [] )
119   
120 @store.disconnect()
121