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
# File 'lib/chapter.rb', line 10

def initialize(lines)
  self.meta = {}
  meta = true
  while meta 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.meta[matches[1].downcase] = matches[2]
    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

#metaObject

Returns the value of attribute meta.



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

def meta
  @meta
end

#numberObject

Returns the value of attribute number.



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

def number
  @number
end

Instance Method Details

#chapter_idObject



39
40
41
# File 'lib/chapter.rb', line 39

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

#htmlObject



34
35
36
37
# File 'lib/chapter.rb', line 34

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

#nameObject



47
48
49
# File 'lib/chapter.rb', line 47

def name
  meta['name'] || ""
end

#name_or_numberObject

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_wordObject



43
44
45
# File 'lib/chapter.rb', line 43

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



56
57
58
# File 'lib/chapter.rb', line 56

def number_or_name
  number ? "Chapter #{number_as_word}" : name
end

#templateObject



51
52
53
# File 'lib/chapter.rb', line 51

def template
  meta['template'] || nil
end

#word_countObject



30
31
32
# File 'lib/chapter.rb', line 30

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