Class: Chapter

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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)
  meta = true
  while meta and line = lines.shift do
    line.strip!
    if line =~ /^Chapter:/
      meta_num = line.scan(/^Chapter:\s*(\d+)/).flatten.first
      self.number = meta_num.strip.to_i if meta_num
    elsif line =~ /^Name:/
      meta_name = line.scan(/^Name:\s*(.+)/).flatten.first
      self.name = meta_name.strip if meta_name
    elsif line =~ /^Subhead:/
      meta_sub = line.scan(/^Subhead:\s*(.+)/).flatten.first
      self.subhead = meta_sub.strip if meta_sub
    else
      lines = [line] + lines if line
      meta = false 
    end
  end

  self.content = lines.join
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



6
7
8
# File 'lib/chapter.rb', line 6

def content
  @content
end

#file_nameObject

Returns the value of attribute file_name.



6
7
8
# File 'lib/chapter.rb', line 6

def file_name
  @file_name
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/chapter.rb', line 6

def name
  @name
end

#numberObject

Returns the value of attribute number.



6
7
8
# File 'lib/chapter.rb', line 6

def number
  @number
end

#subheadObject

Returns the value of attribute subhead.



6
7
8
# File 'lib/chapter.rb', line 6

def subhead
  @subhead
end

Instance Method Details

#chapter_idObject



41
42
43
# File 'lib/chapter.rb', line 41

def chapter_id
  @book_id ||= file_name.gsub(/\W/,'_').gsub('.html', '')
end

#htmlObject



36
37
38
39
# File 'lib/chapter.rb', line 36

def html
  content.strip!
  @html ||= RedCloth.new(content).to_html
end

#name_or_numberObject

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_wordObject



45
46
47
# File 'lib/chapter.rb', line 45

def number_as_word
  number ? Linguistics::EN.numwords(number).capitalize : nil
end

#number_or_nameObject

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_countObject



32
33
34
# File 'lib/chapter.rb', line 32

def word_count
  @word_count ||= (name || "" + content).gsub(/(_|\*|,|:)/, '').scan(/(\w|-|')+/).size
end