Refactor page generations, and add hard-coded series naming.
[quanlib.git] / walkdir.rb
1 # Walk the directory (and subdirectories), identifying books.
2 #
3 # Expected format:
4 #   .../AuthorName/Title_of_the_Awesome_Book.ext
5 #
6 # Author is given as FirstLast.  For example, 
7 # Robert Anson Heinlein is RoberHeinlein, and 
8 # JKRowling is JoanneRowling.
9 #
10 # Book titles have spaces replaced with underscores,
11 # and punctuation [,!?'] replaced with hyphens.
12 #
13 # If the book forms part of a series, then an all-capitals 
14 # series designator, followed by a numeric volume number, 
15 # followed by an underscore, is prefixed to the name.
16 # For example, Hardy Boys' volume 1, The Tower Treasure, 
17 # is rendered as .../FranklinDixon/HB001_The_Tower_Treasure.epub
18 # and Mrs. Pollifax volume 6, On the China Station, is
19 # .../DorothyGilman/P06_On_the_China_Station.epub.
20
21 require 'book'
22 require 'store'
23
24 class WalkDir
25   def initialize(store, root)
26     @root = root
27     @store = store
28     @files = walk(@root)
29   end
30
31   def books
32     result = []
33     for file in @files.sort
34       if Book.canHandle?(file)
35         book = Book.new(@store)
36         book.loadFromFile(file)
37         id = @store.store_book(book)
38         result.push(id)
39       end
40     end
41     return result
42   end
43
44   def walk(path)
45     result = []
46     children = Dir.entries(path)
47     for child in children
48       fullName = (path.chomp("/")) + "/" + child
49       if (File.directory?(fullName)) and (child != ".") and (child != "..") and (!File.symlink?(fullName))
50         sub = walk(fullName)
51         if (sub != nil) and (sub.length > 0)
52           result.concat(sub)
53         end
54       elsif (! File.directory?(fullName))
55         result.push(fullName)
56       end
57     end
58     #puts result
59     return result
60   end
61 end