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.



18
19
20
# File 'lib/markdownplus/parser.rb', line 18

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

Instance Attribute Details

#current_blockObject

Returns the value of attribute current_block.



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

def current_block
  @current_block
end

#sourceObject (readonly)

Returns the value of attribute source.



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

def source
  @source
end

Class Method Details

.parse(value) ⇒ Object



12
13
14
15
16
# File 'lib/markdownplus/parser.rb', line 12

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

Instance Method Details

#blocksObject



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

def blocks
  @blocks ||= []
end

#blocks=(value) ⇒ Object



25
26
27
# File 'lib/markdownplus/parser.rb', line 25

def blocks=(value)
  @blocks = value
end

#code_blocksObject



29
30
31
# File 'lib/markdownplus/parser.rb', line 29

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

#each_line(&block) ⇒ Object



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

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

#errorsObject



57
58
59
# File 'lib/markdownplus/parser.rb', line 57

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

#executable_blocksObject



37
38
39
# File 'lib/markdownplus/parser.rb', line 37

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

#htmlObject



45
46
47
# File 'lib/markdownplus/parser.rb', line 45

def html
  markdown_renderer.render(markdown)
end

#includable_blocksObject



33
34
35
# File 'lib/markdownplus/parser.rb', line 33

def includable_blocks
  code_blocks.select(&:includable?)
end

#includeObject



92
93
94
95
96
# File 'lib/markdownplus/parser.rb', line 92

def include
  includable_blocks.each do |block|
    block.include
  end
end

#linesObject



53
54
55
# File 'lib/markdownplus/parser.rb', line 53

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

#markdownObject



41
42
43
# File 'lib/markdownplus/parser.rb', line 41

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

#markdown_rendererObject



49
50
51
# File 'lib/markdownplus/parser.rb', line 49

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

#parseObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/markdownplus/parser.rb', line 73

def parse
  each_line do |line|
    matcher = line.match(/\s*`{3,}\s*(\S*)\s*/)
    if matcher
      if self.current_block && self.current_block.class == CodeBlock
        self.blocks << self.current_block
        self.current_block = nil
      else
        self.blocks << self.current_block if self.current_block
        self.current_block = CodeBlock.new(matcher[1])
      end
    else
      self.current_block ||= TextBlock.new
      self.current_block.append line
    end
  end
  self.blocks << self.current_block if self.current_block      
end

#warningsObject



61
62
63
# File 'lib/markdownplus/parser.rb', line 61

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