Class: Repubmark::Elems::Chapter

Inherits:
Base
  • Object
show all
Defined in:
lib/repubmark/elems/chapter.rb

Instance Attribute Summary

Attributes inherited from Base

#parent

Instance Method Summary collapse

Methods inherited from Base

#absolute_url, #config, #count_words, #html_class, #own_url, parent?, parents, #relative_url

Constructor Details

#initialize(parent, level = 1, slug = nil, title = nil) ⇒ Chapter

Returns a new instance of Chapter.



8
9
10
11
12
13
14
15
16
17
# File 'lib/repubmark/elems/chapter.rb', line 8

def initialize(parent, level = 1, slug = nil, title = nil)
  super parent
  self.level = level
  self.slug = slug
  self.title = title
  verify!

  @canvas = Canvas.new self
  @chapters = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/repubmark/elems/chapter.rb', line 92

def method_missing(method_name, ...)
  if @canvas.respond_to? method_name
    raise 'Intro after chapters' unless @chapters.empty?

    @canvas.public_send(method_name, ...)
  else
    super
  end
end

Instance Method Details

#anchorObject

Helper methods #



67
68
69
70
71
72
73
74
75
# File 'lib/repubmark/elems/chapter.rb', line 67

def anchor
  if parent.instance_of?(self.class) && @level > 2
    "#{parent.anchor}--#@slug"
  elsif anchor_prefix.empty?
    @slug
  else
    "#{anchor_prefix}--#@slug"
  end.freeze
end

#anchor_prefixObject (private)



143
144
145
# File 'lib/repubmark/elems/chapter.rb', line 143

def anchor_prefix
  @anchor_prefix ||= String(config[:chapter_anchor_prefix]).strip.freeze
end

#build_title_gemtextObject (private)



139
140
141
# File 'lib/repubmark/elems/chapter.rb', line 139

def build_title_gemtext
  "\n#{'#' * @level} #@title\n\n" if @level != 1
end

#build_title_htmlObject (private)



134
135
136
137
# File 'lib/repubmark/elems/chapter.rb', line 134

def build_title_html
  id_attr = %( id="#{anchor}") unless anchor_prefix.empty?
  %(<h#@level#{id_attr}>#@title</h#@level>\n) if @level != 1
end

#chapter(slug, title) {|chapter| ... } ⇒ Object

Builder methods #

Yields:



81
82
83
84
85
86
# File 'lib/repubmark/elems/chapter.rb', line 81

def chapter(slug, title)
  chapter = Chapter.new self, @level + 1, slug, title
  @chapters << chapter
  yield chapter
  nil
end

#chaptersObject



55
56
57
58
59
60
61
# File 'lib/repubmark/elems/chapter.rb', line 55

def chapters
  {
    slug: anchor,
    title: @title,
    chapters: @chapters.map(&:chapters).freeze,
  }.freeze
end

#level=(level) ⇒ Object (private)



104
105
106
107
108
109
# File 'lib/repubmark/elems/chapter.rb', line 104

def level=(level)
  level = Integer level
  raise unless level.positive?

  @level = level
end

#respond_to_missing?(method_name, _include_private) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/repubmark/elems/chapter.rb', line 88

def respond_to_missing?(method_name, _include_private)
  @canvas.respond_to?(method_name) || super
end

#slug=(slug) ⇒ Object (private)



111
112
113
# File 'lib/repubmark/elems/chapter.rb', line 111

def slug=(slug)
  @slug = Repubmark.validate_slug! slug if slug
end

#title=(title) ⇒ Object (private)



115
116
117
118
119
120
121
122
# File 'lib/repubmark/elems/chapter.rb', line 115

def title=(title)
  return @title = nil if title.nil?

  title = String(title).split.join(' ').strip.freeze
  raise 'Empty title' if title.empty?

  @title = title
end

#to_gemtextObject



47
48
49
50
51
52
53
# File 'lib/repubmark/elems/chapter.rb', line 47

def to_gemtext
  [
    build_title_gemtext,
    @canvas.to_gemtext,
    *@chapters.map(&:to_gemtext),
  ].join.freeze
end

#to_htmlObject



39
40
41
42
43
44
45
# File 'lib/repubmark/elems/chapter.rb', line 39

def to_html
  [
    build_title_html,
    @canvas.to_html,
    *@chapters.map(&:to_html),
  ].join.freeze
end

#to_summary_plainObject



29
30
31
32
33
34
35
36
37
# File 'lib/repubmark/elems/chapter.rb', line 29

def to_summary_plain
  str = [
    @title,
    @canvas.to_summary_plain,
    *@chapters.map(&:to_summary_plain),
  ].compact.join(' ').strip.freeze

  str.empty? ? nil : str
end

#verify!Object (private)



124
125
126
127
128
129
130
131
132
# File 'lib/repubmark/elems/chapter.rb', line 124

def verify!
  if @level == 1
    raise 'Non-empty slug for level 1'  if @slug
    raise 'Non-empty title for level 1' if @title
  else
    raise 'Empty slug for level >= 2'   if @slug.nil?
    raise 'Empty title for level >= 2'  if @title.nil?
  end
end

#word_countObject

Basic methods #



23
24
25
26
27
# File 'lib/repubmark/elems/chapter.rb', line 23

def word_count
  (@title ? count_words(@title) : 0) +
    @canvas.word_count +
    @chapters.sum(&:word_count)
end