Class: MarkdownIncluder::Heading

Inherits:
Object
  • Object
show all
Defined in:
lib/markdown_helper/markdown_includer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level, title) ⇒ Heading

Returns a new instance of Heading.



216
217
218
219
# File 'lib/markdown_helper/markdown_includer.rb', line 216

def initialize(level, title)
  self.level = level
  self.title = title
end

Instance Attribute Details

#levelObject

Returns the value of attribute level.



214
215
216
# File 'lib/markdown_helper/markdown_includer.rb', line 214

def level
  @level
end

#titleObject

Returns the value of attribute title.



214
215
216
# File 'lib/markdown_helper/markdown_includer.rb', line 214

def title
  @title
end

Class Method Details

.parse(line) ⇒ Object



221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/markdown_helper/markdown_includer.rb', line 221

def self.parse(line)
  # Four leading spaces not allowed (but three are allowed).
  return nil if line.start_with?(' ' * 4)
  stripped_line = line.sub(/^ */, '')
  # Now must begin with hash marks and space.
  return nil unless stripped_line.match(/^#+ /)
  hash_marks, title = stripped_line.split(' ', 2)
  level = hash_marks.size
  # Seventh level heading not allowed.
  return nil if level > 6
  self.new(level, title)
end

Instance Method Details



235
236
237
238
239
240
241
242
243
244
245
# File 'lib/markdown_helper/markdown_includer.rb', line 235

def link(anchor_counts = Hash.new(0))
  anchor = title.downcase
  anchor.gsub!(/[^\p{Word}\- ]/u, '') # remove punctuation
  anchor.gsub!(' ', '-') # replace spaces with dash

  anchor_count = anchor_counts[anchor]
  anchor_counts[anchor] += 1
  suffix = (anchor_count == 0) ? '' : "-#{anchor_count}"

  "[#{title}](##{anchor}#{suffix})"
end