class MarkdownSectionNumbering
Constants
- VERSION
Public Class Methods
config(max_level)
click to toggle source
# File lib/markdown_section_numbering.rb, line 5 def config(max_level) @max_level = max_level end
convert(input)
click to toggle source
# File lib/markdown_section_numbering.rb, line 9 def convert(input) @section_index = [-1] + [0] * max_level input.lines.map do |line| convert_line(line) end.join("\n") + "\n" end
Private Class Methods
add_section_number(line, target_level)
click to toggle source
# File lib/markdown_section_numbering.rb, line 33 def add_section_number(line, target_level) return line if target_level > max_level header_sign = '#' * target_level regex = Regexp.new("^#{header_sign}\\s*(\\d+(\\.\\d+)*)?\\s*(.+)") match = line.match(regex) number = calc_section_number(target_level) "#{header_sign} #{number} #{match[3]}" end
calc_section_number(target_level)
click to toggle source
# File lib/markdown_section_numbering.rb, line 45 def calc_section_number(target_level) @section_index[target_level] += 1 ((target_level + 1)..max_level).each do |child_level| @section_index[child_level] = 0 end (1..target_level).map { |level| @section_index[level].to_s }.join('.') end
convert_line(line)
click to toggle source
# File lib/markdown_section_numbering.rb, line 22 def convert_line(line) line.chomp! match = line.match(/^#+/) if !match line else level = match[0].length add_section_number(line, level) end end
max_level()
click to toggle source
# File lib/markdown_section_numbering.rb, line 18 def max_level @max_level || 10 end