Sha256: 3579b6efceb147489db9225b2d07fbaf1015f07ec4c2bfb04eb3cd1a6919e930

Contents?: true

Size: 1.09 KB

Versions: 1

Compression:

Stored size: 1.09 KB

Contents

# -*- coding: utf-8 -*-
=begin rdoc
=end

module SixArm; module Markdown; module TOC; end; end; end

class SixArm::Markdown::TOC::Generator

  attr_accessor :settings

  def initialize(
    settings: SixArm::Markdown::TOC::Settings.new
    )
    @settings = settings
  end

  def headline_to_markdown(headline)
    settings.line_prefix + ("  " * (headline.level - 2)) + settings.bullet + " " + headline.link + "\n"
  end

  def scan_headlines(markdown_string)
    headlines = []
    markdown_string.scan(settings.headline_regexp).each{|markers, text| 
      level = markers.length
      if  level >= settings.headline_minimum_level && level <= settings.headline_maximum_level 
        headlines << SixArm::Markdown::Headline.new(level: level, text: text)
      end
    }
    return headlines
  end

  def create_toc(markdown_string)
    return scan_headlines(markdown_string).map{|headline| headline_to_markdown(headline)}.join
  end

  def rewrite_toc(markdown_string)
    m = markdown_string.match(settings.contents_regexp)
    return m ? m.pre_match + create_toc(markdown_string) + m.post_match : nil
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sixarm_ruby_markdown_table_of_contents-2.4.0 lib/sixarm_ruby_markdown_table_of_contents/sixarm/markdown/toc/generator.rb