Class: PPZ::LeafSectionModel

Inherits:
AbstractSectionModel show all
Defined in:
lib/doc/model/section/leaf.rb

Constant Summary collapse

REG_EXP =
/^(\#{1,5}) (.+)/

Instance Attribute Summary collapse

Attributes inherited from AbstractModel

#father_model, #index, #left_model, #right_model

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractSectionModel

#append

Methods inherited from AbstractWrapperModel

#append

Methods inherited from AbstractModel

#transform_inline_element

Constructor Details

#initialize(title, level) ⇒ LeafSectionModel

Returns a new instance of LeafSectionModel.

Raises:

  • (TypeError)


4
5
6
7
8
9
# File 'lib/doc/model/section/leaf.rb', line 4

def initialize title, level
  raise TypeError unless title.is_a?(String) && level.is_a?(Integer) 
  super() # 不可以省略括号

  @title = transform_inline_element title
  @level = level
end

Instance Attribute Details

#levelObject

Returns the value of attribute level.



2
3
4
# File 'lib/doc/model/section/leaf.rb', line 2

def level
  @level
end

#section_dom_idObject

Returns the value of attribute section_dom_id.



2
3
4
# File 'lib/doc/model/section/leaf.rb', line 2

def section_dom_id
  @section_dom_id
end

#titleObject

Returns the value of attribute title.



2
3
4
# File 'lib/doc/model/section/leaf.rb', line 2

def title
  @title
end

Class Method Details

.from_line(line) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/doc/model/section/leaf.rb', line 12

def self.from_line line
  return nil unless REG_EXP.match(line)
  
  level = {
    1 => 1, # 一个井号是 一级

    5 => 3 # 五个井号是 三级

  }[$1.size] || 2 # 其余都是 两级

  PPZ::LeafSectionModel.new $2, level
end

Instance Method Details

#get_nav_htmlObject



22
23
24
# File 'lib/doc/model/section/leaf.rb', line 22

def get_nav_html
  return "<li><a href=\"##{section_dom_id}\">#{@title}</a><ul>#{super}</ul></li>"
end

#to_htmlObject



26
27
28
# File 'lib/doc/model/section/leaf.rb', line 26

def to_html
  "<section id=#{@section_dom_id}><h#{@level}>#{@title}</h#{@level}>#{super}</section>"
end