Class: Markdownplus::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/markdownplus/parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ Parser

Returns a new instance of Parser.



16
17
18
# File 'lib/markdownplus/parser.rb', line 16

def initialize(value=nil)
  @input = value
end

Instance Attribute Details

#current_blockObject

Returns the value of attribute current_block.



8
9
10
# File 'lib/markdownplus/parser.rb', line 8

def current_block
  @current_block
end

#inputObject (readonly)

Returns the value of attribute input.



7
8
9
# File 'lib/markdownplus/parser.rb', line 7

def input
  @input
end

Class Method Details

.parse(value) ⇒ Object



10
11
12
13
14
# File 'lib/markdownplus/parser.rb', line 10

def self.parse(value)
  parser = Parser.new(value)
  parser.parse
  parser
end

Instance Method Details

#blocksObject



20
21
22
# File 'lib/markdownplus/parser.rb', line 20

def blocks
  @blocks ||= []
end

#blocks=(value) ⇒ Object



23
24
25
# File 'lib/markdownplus/parser.rb', line 23

def blocks=(value)
  @blocks = value
end

#code_blocksObject



27
28
29
# File 'lib/markdownplus/parser.rb', line 27

def code_blocks
  blocks.select { |b| b.class == CodeBlock }
end

#each_line(&block) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/markdownplus/parser.rb', line 67

def each_line(&block)
  if block_given?
    lines.each do |line|
      block.call(line)
    end
  end
end

#errorsObject



63
64
65
# File 'lib/markdownplus/parser.rb', line 63

def errors
  blocks.collect { |b| b.errors }.flatten
end

#executable_blocksObject



31
32
33
# File 'lib/markdownplus/parser.rb', line 31

def executable_blocks
  code_blocks.select(&:executable?)
end

#executeObject



96
97
98
99
100
# File 'lib/markdownplus/parser.rb', line 96

def execute
  self.executable_blocks.each do |block|
    block.execute(self.variables)
  end
end

#htmlObject



43
44
45
# File 'lib/markdownplus/parser.rb', line 43

def html
  markdown_renderer.render(output_markdown)
end

#input_markdownObject



35
36
37
# File 'lib/markdownplus/parser.rb', line 35

def input_markdown
  blocks.collect { |b| b.input_markdown }.join
end

#linesObject



51
52
53
# File 'lib/markdownplus/parser.rb', line 51

def lines
  @lines ||= input.split("\n")
end

#markdown_rendererObject



47
48
49
# File 'lib/markdownplus/parser.rb', line 47

def markdown_renderer
  Redcarpet::Markdown.new(GithubRenderer, fenced_code_blocks: true)
end

#output_markdownObject



39
40
41
# File 'lib/markdownplus/parser.rb', line 39

def output_markdown
  blocks.collect { |b| b.output_markdown }.join
end

#parseObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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

#variablesObject



55
56
57
# File 'lib/markdownplus/parser.rb', line 55

def variables
  @variables ||= {}
end

#warningsObject



59
60
61
# File 'lib/markdownplus/parser.rb', line 59

def warnings
  blocks.collect { |b| b.warnings }.flatten
end