Initial commit
[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.rb'
22
23 class WalkDir
24   def initialize(root)
25     @root = root
26     @files = walk(@root)
27   end
28
29   def books
30     result = []
31     for file in @files
32       if Book.canHandle?(file)
33         book = Book.new(file)
34         result.push(book)
35       end
36     end
37     return result
38   end
39
40   def walk(path)
41     result = []
42     children = Dir.entries(path)
43     for child in children
44       fullName = (path.chomp("/")) + "/" + child
45       if (File.directory?(fullName)) and (child != ".") and (child != "..")
46         sub = walk(fullName)
47         if (sub != nil) and (sub.length > 0)
48           result.concat(sub)
49         end
50       elsif (! File.directory?(fullName))
51         result.push(fullName)
52       end
53     end
54     return result
55   end
56 end