Class: Chapter
- Inherits:
-
Object
- Object
- Chapter
- Defined in:
- lib/chapter.rb
Overview
Chapter contains name, content (text-only) generates html file_name should reference html file after it is written
Instance Attribute Summary collapse
-
#content ⇒ Object
Returns the value of attribute content.
-
#file_name ⇒ Object
Returns the value of attribute file_name.
-
#meta ⇒ Object
Returns the value of attribute meta.
-
#number ⇒ Object
Returns the value of attribute number.
Instance Method Summary collapse
- #chapter_id ⇒ Object
- #html ⇒ Object
-
#initialize(lines) ⇒ Chapter
constructor
A new instance of Chapter.
- #name ⇒ Object
-
#name_or_number ⇒ Object
if there is a name, give us that; otherwise give the number written out as words.
- #number_as_word ⇒ Object
-
#number_or_name ⇒ Object
if there is a number, give us that written out as words; otherwise give the chapter name.
- #template ⇒ Object
- #word_count ⇒ Object
Constructor Details
#initialize(lines) ⇒ Chapter
Returns a new instance of Chapter.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/chapter.rb', line 10 def initialize(lines) self. = {} = true while and line = lines.shift do line.strip! matches = line.match /^(.+):\s+(.+)/ if matches if matches[1] =~ /(Chapter|Number|Position)/i and matches[2] =~ /\d+/ and number.nil? self.number = matches[2].strip.to_i end self.[matches[1].downcase] = matches[2] else lines = [line] + lines if line = false end end self.content = lines.join end |
Instance Attribute Details
#content ⇒ Object
Returns the value of attribute content.
6 7 8 |
# File 'lib/chapter.rb', line 6 def content @content end |
#file_name ⇒ Object
Returns the value of attribute file_name.
6 7 8 |
# File 'lib/chapter.rb', line 6 def file_name @file_name end |
#meta ⇒ Object
Returns the value of attribute meta.
6 7 8 |
# File 'lib/chapter.rb', line 6 def @meta end |
#number ⇒ Object
Returns the value of attribute number.
6 7 8 |
# File 'lib/chapter.rb', line 6 def number @number end |
Instance Method Details
#chapter_id ⇒ Object
39 40 41 |
# File 'lib/chapter.rb', line 39 def chapter_id @book_id ||= file_name.gsub(/\W/,'_').gsub('.html', '') end |
#html ⇒ Object
34 35 36 37 |
# File 'lib/chapter.rb', line 34 def html content.strip! @html ||= RedCloth.new(content).to_html end |
#name ⇒ Object
47 48 49 |
# File 'lib/chapter.rb', line 47 def name ['name'] || "" end |
#name_or_number ⇒ Object
if there is a name, give us that; otherwise give the number written out as words
61 62 63 64 65 66 67 68 69 |
# File 'lib/chapter.rb', line 61 def name_or_number if !name.empty? name elsif number "Chapter #{number_as_word}" else "" end end |
#number_as_word ⇒ Object
43 44 45 |
# File 'lib/chapter.rb', line 43 def number_as_word number ? Linguistics::EN.numwords(number).capitalize : nil end |
#number_or_name ⇒ Object
if there is a number, give us that written out as words; otherwise give the chapter name
56 57 58 |
# File 'lib/chapter.rb', line 56 def number_or_name number ? "Chapter #{number_as_word}" : name end |
#template ⇒ Object
51 52 53 |
# File 'lib/chapter.rb', line 51 def template ['template'] || nil end |
#word_count ⇒ Object
30 31 32 |
# File 'lib/chapter.rb', line 30 def word_count @word_count ||= (name + content).gsub(/(_|\*|,|:)/, '').scan(/(\w|-|')+/).size end |