class Madness::MarkdownDocument

Handle a pure markdown document.

Attributes

markdown[R]
title[R]

Public Class Methods

new(markdown, title: nil) click to toggle source
# File lib/madness/markdown_document.rb, line 11
def initialize(markdown, title: nil)
  @markdown = markdown
  @title = title || ''
end

Public Instance Methods

text() click to toggle source
# File lib/madness/markdown_document.rb, line 16
def text
  @text ||= begin
    result = markdown
    result = parse_toc(result) if config.auto_toc
    result = parse_shortlinks(result) if config.shortlinks
    result = prepend_h1(result) if config.auto_h1
    result
  end
end
to_html() click to toggle source
# File lib/madness/markdown_document.rb, line 26
def to_html
  @to_html ||= renderer.render text
end

Private Instance Methods

has_h1?(input) click to toggle source
# File lib/madness/markdown_document.rb, line 50
def has_h1?(input)
  lines = input.lines(chomp: true).reject(&:empty?)
  return false if lines.empty?

  lines[0].match(/^# \w+/) || (lines[1] && lines[0].match(/^\w+/) && lines[1].start_with?('='))
end
parse_toc(input) click to toggle source
# File lib/madness/markdown_document.rb, line 36
def parse_toc(input)
  input.gsub '<!-- TOC -->', toc
end
prepend_h1(input) click to toggle source
# File lib/madness/markdown_document.rb, line 44
def prepend_h1(input)
  return input if has_h1?(input)

  "# #{title}\n\n#{input}"
end
renderer() click to toggle source
# File lib/madness/markdown_document.rb, line 32
def renderer
  @renderer ||= Rendering::Handler.new config.renderer
end
toc() click to toggle source
# File lib/madness/markdown_document.rb, line 57
def toc
  @toc ||= toc_handler.markdown
end
toc_handler() click to toggle source
# File lib/madness/markdown_document.rb, line 61
def toc_handler
  @toc_handler ||= InlineTableOfContents.new markdown
end