Sha256: b276625dd2d7c88d77157952f80d7cf179cc9151a601e2c02eb38e33958b8631
Contents?: true
Size: 1.27 KB
Versions: 2
Compression:
Stored size: 1.27 KB
Contents
module Tocer # Builds a table of contents for a Markdown document. class Builder def initialize lines, label: "# Table of Contents", comment_block: Elements::CommentBlock @lines = lines @label = label @comment_block = comment_block.new @url_count = Hash.new { |hash, key| hash[key] = 0 } end def headers lines.select { |line| line.start_with? Parsers::Header.punctuation } end def build return "" if headers.empty? content = "#{comment_block.start}\n\n" content << "#{label}\n\n" content << headers_as_links.join("\n") content << "\n\n#{comment_block.finish}\n\n" end private attr_reader :lines, :label, :comment_block, :url_count def acquire_transfomer header case when header =~ /\[.+\]\(.+\)/ Transformers::Link.new header else Transformers::Text.new header end end def url_suffix url url_count[url].zero? ? "" : url_count[url] end def transform header transformer = acquire_transfomer header link = transformer.transform url_suffix: url_suffix(transformer.url) url_count[transformer.url] += 1 link end def headers_as_links headers.map { |header| transform header } end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
tocer-2.0.0 | lib/tocer/builder.rb |
tocer-1.0.0 | lib/tocer/builder.rb |