class Madness::MarkdownDocument
Handle a pure markdown document.
Attributes
Public Class Methods
Source
# File lib/madness/markdown_document.rb, line 11 def initialize(markdown, title: nil) @markdown = markdown @title = title || '' end
Public Instance Methods
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
Source
# File lib/madness/markdown_document.rb, line 26 def to_html @to_html ||= renderer.render text end
Private Instance Methods
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
Source
# File lib/madness/markdown_document.rb, line 40 def parse_shortlinks(input) input.gsub(/\[\[([^\]]+)\]\]/) { "[#{$1}](#{$1.to_href})" } end
Source
# File lib/madness/markdown_document.rb, line 36 def parse_toc(input) input.gsub '<!-- TOC -->', toc end
Source
# File lib/madness/markdown_document.rb, line 44 def prepend_h1(input) return input if has_h1?(input) "# #{title}\n\n#{input}" end
Source
# File lib/madness/markdown_document.rb, line 32 def renderer @renderer ||= Rendering::Handler.new config.renderer end
Source
# File lib/madness/markdown_document.rb, line 57 def toc @toc ||= toc_handler.markdown end
Source
# File lib/madness/markdown_document.rb, line 61 def toc_handler @toc_handler ||= InlineTableOfContents.new markdown end