Class: SixArm::Markdown::Headline

Inherits:
Object
  • Object
show all
Includes:
EqualInstanceVariables
Defined in:
lib/sixarm_ruby_markdown/sixarm/markdown/headline.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text: nil, anchor: nil, level: nil, link: nil) ⇒ Headline

Returns a new instance of Headline.

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sixarm_ruby_markdown/sixarm/markdown/headline.rb', line 16

def initialize(
  text: nil,
  anchor: nil,
  level: nil,
  link: nil
)
  raise ArgumentError if !text.kind_of?(String)
  @text = text
  @anchor = anchor || self.class.text_to_anchor(text)
  @level = level
  @link = link
end

Instance Attribute Details

#anchorObject

Returns the value of attribute anchor.



10
11
12
# File 'lib/sixarm_ruby_markdown/sixarm/markdown/headline.rb', line 10

def anchor
  @anchor
end

#levelObject

Returns the value of attribute level.



10
11
12
# File 'lib/sixarm_ruby_markdown/sixarm/markdown/headline.rb', line 10

def level
  @level
end

Returns the value of attribute link.



10
11
12
# File 'lib/sixarm_ruby_markdown/sixarm/markdown/headline.rb', line 10

def link
  @link
end

#textObject

Returns the value of attribute text.



10
11
12
# File 'lib/sixarm_ruby_markdown/sixarm/markdown/headline.rb', line 10

def text
  @text
end

Class Method Details

.parse_line(line) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/sixarm_ruby_markdown/sixarm/markdown/headline.rb', line 41

def self.parse_line(line)
  level = parse_line_level(line)
  text = trim_line(line)
  # TODO Add parser for anchor
  # TODO Add parser for Unicode normalization
  return SixArm::Markdown::Headline.new(text: text, level: level)
end

.parse_line_level(line) ⇒ Object



49
50
51
# File 'lib/sixarm_ruby_markdown/sixarm/markdown/headline.rb', line 49

def self.parse_line_level(line)
  return line =~/^\s*([#=]+)/ ? $1.length : nil
end

.text_to_anchor(text) ⇒ Object



57
58
59
# File 'lib/sixarm_ruby_markdown/sixarm/markdown/headline.rb', line 57

def self.text_to_anchor(text)
  text.downcase.gsub(/\W+/,'-')
end

.trim_line(line) ⇒ Object



53
54
55
# File 'lib/sixarm_ruby_markdown/sixarm/markdown/headline.rb', line 53

def self.trim_line(line)
  line.sub(/^\s*[#=]+\s+/, '').sub(/\s+[#=]+\s*$/, '')
end