class Markdownplus::Parser

Attributes

current_block[RW]
input[R]

Public Class Methods

new(value=nil) click to toggle source
# File lib/markdownplus/parser.rb, line 16
def initialize(value=nil)
  @input = value
end
parse(value) click to toggle source
# File lib/markdownplus/parser.rb, line 10
def self.parse(value)
  parser = Parser.new(value)
  parser.parse
  parser
end

Public Instance Methods

blocks() click to toggle source
# File lib/markdownplus/parser.rb, line 20
def blocks
  @blocks ||= []
end
blocks=(value) click to toggle source
# File lib/markdownplus/parser.rb, line 23
def blocks=(value)
  @blocks = value
end
code_blocks() click to toggle source
# File lib/markdownplus/parser.rb, line 27
def code_blocks
  blocks.select { |b| b.class == CodeBlock }
end
each_line(&block) click to toggle source
# File lib/markdownplus/parser.rb, line 67
def each_line(&block)
  if block_given?
    lines.each do |line|
      block.call(line)
    end
  end
end
errors() click to toggle source
# File lib/markdownplus/parser.rb, line 63
def errors
  blocks.collect { |b| b.errors }.flatten
end
executable_blocks() click to toggle source
# File lib/markdownplus/parser.rb, line 31
def executable_blocks
  code_blocks.select(&:executable?)
end
execute() click to toggle source
# File lib/markdownplus/parser.rb, line 96
def execute
  self.executable_blocks.each do |block|
    block.execute(self.variables)
  end
end
html() click to toggle source
# File lib/markdownplus/parser.rb, line 43
def html
  markdown_renderer.render(output_markdown)
end
input_markdown() click to toggle source
# File lib/markdownplus/parser.rb, line 35
def input_markdown
  blocks.collect { |b| b.input_markdown }.join
end
lines() click to toggle source
# File lib/markdownplus/parser.rb, line 51
def lines
  @lines ||= input.split("\n")
end
markdown_renderer() click to toggle source
# File lib/markdownplus/parser.rb, line 47
def markdown_renderer
  Redcarpet::Markdown.new(GithubRenderer, fenced_code_blocks: true)
end
output_markdown() click to toggle source
# File lib/markdownplus/parser.rb, line 39
def output_markdown
  blocks.collect { |b| b.output_markdown }.join
end
parse() click to toggle source
# File lib/markdownplus/parser.rb, line 75
def parse
  block_number = 0
  each_line do |line|
    matcher = line.match(/\s*`{3,}\s*(\S*)\s*/)
    if matcher
      if self.current_block && self.current_block.is_a?(CodeBlock)
        self.blocks << self.current_block
        self.current_block = nil
        block_number += 1
      else
        self.blocks << self.current_block if self.current_block
        self.current_block = CodeBlock.new(block_number, matcher[1])
      end
    else
      self.current_block ||= TextBlock.new(block_number)
      self.current_block.append line
    end
  end
  self.blocks << self.current_block if self.current_block      
end
variables() click to toggle source
# File lib/markdownplus/parser.rb, line 55
def variables
  @variables ||= {}
end
warnings() click to toggle source
# File lib/markdownplus/parser.rb, line 59
def warnings
  blocks.collect { |b| b.warnings }.flatten
end