class Pione::LiterateAction::MarkdownParser

MarkdownParser is a parser for literate action document.

Public Class Methods

new(src) click to toggle source
# File lib/pione/literate-action/markdown-parser.rb, line 10
def initialize(src)
  @src = src
end
parse(src) click to toggle source

Parse the source string and return the result.

# File lib/pione/literate-action/markdown-parser.rb, line 6
def self.parse(src)
  new(src).parse
end

Public Instance Methods

parse() click to toggle source

Parse the source string.

# File lib/pione/literate-action/markdown-parser.rb, line 15
def parse
  @parsed = Kramdown::Parser::GFM.parse(@src)
  current_name = nil
  root = @parsed.first
  root.children.each_with_object(Hash.new) do |elt, action|
    if name = find_rule_name(elt)
      current_name = name
      next
    end

    if current_name
      lang, content = find_action(elt)
      if content
        action[current_name] ||= {content: ""}
        action[current_name][:lang] = lang
        action[current_name][:content] << content
      end
    end
  end
end

Private Instance Methods

find_action(elt) click to toggle source

Find an action from the document element.

# File lib/pione/literate-action/markdown-parser.rb, line 46
def find_action(elt)
  if elt.type == :codeblock
    if elt.attr["class"] and elt.attr["class"].start_with?("language-")
      # with language
      return [elt.attr["class"].sub("language-", ""), elt.value]
    else
      # without language
      return [nil, elt.value]
    end
  end
end
find_rule_name(elt) click to toggle source

Find a rule name from the document element.

# File lib/pione/literate-action/markdown-parser.rb, line 39
def find_rule_name(elt)
  if elt.type == :header and elt.options[:level] == 2
    elt.options[:raw_text]
  end
end