class Rundoc::Parser

Constants

CODEBLOCK_REGEX
COMMAND_REGEX
DEFAULT_KEYWORD
GITHUB_BLOCK
INDENT_BLOCK

Attributes

contents[R]
context[R]
keyword[R]
stack[R]

Public Class Methods

new(contents, context:, keyword: DEFAULT_KEYWORD) click to toggle source
# File lib/rundoc/parser.rb, line 13
def initialize(contents, context:, keyword: DEFAULT_KEYWORD)
  @context = context
  @contents = contents
  @original = contents.dup
  @keyword = keyword
  @stack = []
  partition
end

Public Instance Methods

partition() click to toggle source

split into [before_code, code, after_code], process code, and re-run until tail is empty

# File lib/rundoc/parser.rb, line 38
def partition
  until contents.empty?
    head, code, tail = contents.partition(CODEBLOCK_REGEX)
    @stack << head unless head.empty?
    unless code.empty?
      match = code.match(CODEBLOCK_REGEX)
      @stack << CodeSection.new(
        match,
        keyword: keyword,
        context: context
      )
    end
    @contents = tail
  end
end
to_md() click to toggle source
# File lib/rundoc/parser.rb, line 22
def to_md
  result = []
  @stack.each do |s|
    result << if s.respond_to?(:render)
      s.render
    else
      s
    end
  end
  result.join("")
rescue => e
  File.write("README.md", result.join(""))
  raise e
end