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.
-
#name ⇒ Object
Returns the value of attribute name.
-
#number ⇒ Object
Returns the value of attribute number.
-
#subhead ⇒ Object
Returns the value of attribute subhead.
Instance Method Summary collapse
- #chapter_id ⇒ Object
- #html ⇒ Object
-
#initialize(lines) ⇒ Chapter
constructor
A new instance of Chapter.
-
#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.
- #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 29 30 |
# File 'lib/chapter.rb', line 10 def initialize(lines) = true while and line = lines.shift do line.strip! if line =~ /^Chapter:/ = line.scan(/^Chapter:\s*(\d+)/).flatten.first self.number = .strip.to_i if elsif line =~ /^Name:/ = line.scan(/^Name:\s*(.+)/).flatten.first self.name = .strip if elsif line =~ /^Subhead:/ = line.scan(/^Subhead:\s*(.+)/).flatten.first self.subhead = .strip if 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 |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/chapter.rb', line 6 def name @name end |
#number ⇒ Object
Returns the value of attribute number.
6 7 8 |
# File 'lib/chapter.rb', line 6 def number @number end |
#subhead ⇒ Object
Returns the value of attribute subhead.
6 7 8 |
# File 'lib/chapter.rb', line 6 def subhead @subhead end |
Instance Method Details
#chapter_id ⇒ Object
41 42 43 |
# File 'lib/chapter.rb', line 41 def chapter_id @book_id ||= file_name.gsub(/\W/,'_').gsub('.html', '') end |
#html ⇒ Object
36 37 38 39 |
# File 'lib/chapter.rb', line 36 def html content.strip! @html ||= RedCloth.new(content).to_html end |
#name_or_number ⇒ Object
if there is a name, give us that; otherwise give the number written out as words
59 60 61 62 63 64 65 66 67 |
# File 'lib/chapter.rb', line 59 def name_or_number if name and !name.empty? name elsif number "Chapter #{number_as_word}" else "" end end |
#number_as_word ⇒ Object
45 46 47 |
# File 'lib/chapter.rb', line 45 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
50 51 52 53 54 55 56 |
# File 'lib/chapter.rb', line 50 def number_or_name if number "Chapter #{number_as_word}" else name || "" end end |
#word_count ⇒ Object
32 33 34 |
# File 'lib/chapter.rb', line 32 def word_count @word_count ||= (name || "" + content).gsub(/(_|\*|,|:)/, '').scan(/(\w|-|')+/).size end |