Sha256: 42d51732e4cce89435a015b913b992843efc44a36b694142e2f5acca053e51b0
Contents?: true
Size: 1.73 KB
Versions: 12
Compression:
Stored size: 1.73 KB
Contents
# frozen_string_literal: true require "forwardable" module Tocer # Builds table of contents for a Markdown document. class Builder extend Forwardable CODE_BLOCK_PUNCTUATION = "```" def_delegators :comment_block, :start_index, :finish_index, :comments, :prependable? def initialize comment_block: Elements::CommentBlock.new, transformer: Transformers::Finder.new @comment_block = comment_block @transformer = transformer @url_count = Hash.new 0 @code_block = false end def unbuildable?(lines) = comment_block.empty?(lines) && headers(lines).empty? def call lines, label: Container[:configuration].label return "" if headers(lines).empty? url_count.clear assemble(lines, label).join end private attr_reader :comment_block, :transformer, :url_count attr_accessor :code_block def assemble lines, label [ "#{comment_block.start_tag}\n\n", "#{label}\n\n", links(lines).join("\n"), "\n\n#{comment_block.finish_tag}\n" ] end def links(lines) = headers(lines).map { |markdown| transform markdown } def headers lines lines.select do |line| toggle_code_block line line.start_with?(Parsers::Header::PUNCTUATION) && !code_block end end def toggle_code_block line return unless line.start_with? CODE_BLOCK_PUNCTUATION self.code_block = !code_block end def transform markdown transformer.call(markdown).then do |instance| url = instance.url link = instance.call url_suffix: url_suffix(url) url_count[url] += 1 link end end def url_suffix(url) = url_count[url].then { |count| count.zero? ? "" : count } end end
Version data entries
12 entries across 12 versions & 1 rubygems