Initial commit
[quanlib.git] / book.rb
1
2 require './author.rb'
3
4 class Book
5   def initialize(fileName)
6     @author = nil
7     @cover = nil
8     @series = nil
9     @title = nil
10     @volume = nil
11
12     parseFileName!(fileName)
13   end
14
15   def self.canHandle?(fileName)
16     if nil == fileName
17       return false
18     end
19
20     lowerName = fileName.downcase()
21
22     if lowerName.end_with?(".epub")
23       return true
24     end
25
26     return false
27   end
28
29   def inspect
30     data = []
31     if nil != @series
32       data.push('series="' + @series + '"')
33     end
34     if nil != @volume
35       data.push('volume="' + @volume + '"')
36     end
37     if nil != @title
38       data.push('title="' + @title + '"')
39     end
40     if nil != @author
41       data.push('author="' + @author + '"')
42     end
43     return '(Book:' + data.join(',') + ')'
44   end
45
46   def to_s
47     return inspect()
48   end
49
50   protected
51   def isUpper?(c)
52     return /[[:upper:]]/.match(c)
53   end
54
55   protected
56   def massageAuthor(input)
57     if nil == input
58       return nil
59     end
60
61     result = ""
62     input.each_char do |c|
63       if isUpper?(c) and (result.length > 0)
64         result += " "
65       end
66       result += c
67     end
68     
69     return result
70   end
71
72   # Returns (series, volumeNo, titleText)
73   protected
74   def processTitle(input)
75     if nil == input
76       return nil
77     end
78
79     arr = input.split('_')
80
81     series = nil
82     vol = nil
83
84     first = arr[0]
85     matchData = (arr[0]).match(/^([A-Z]+)([0-9]+)$/)
86     if nil != matchData
87       capt = matchData.captures
88       series = capt[0]
89       vol = capt[1]
90       arr.shift
91     end
92
93     pos = arr[-1].rindex('.')
94     if nil != pos
95       arr[-1] = arr[-1].slice(0, pos)
96     end
97
98     title = arr.join(' ')
99
100     return series, vol, title
101   end
102
103   protected
104   def parseFileName!(fileName)
105     parts = fileName.split('/')
106     (@series, @volume, @title) = processTitle(parts[-1])
107     if parts.length > 1
108       @author = massageAuthor(parts[-2])
109     end
110   end
111 end