Class: Markdownplus::Parser
- Inherits:
-
Object
- Object
- Markdownplus::Parser
- Defined in:
- lib/markdownplus/parser.rb
Instance Attribute Summary collapse
-
#current_block ⇒ Object
Returns the value of attribute current_block.
-
#input ⇒ Object
readonly
Returns the value of attribute input.
Class Method Summary collapse
Instance Method Summary collapse
- #blocks ⇒ Object
- #blocks=(value) ⇒ Object
- #code_blocks ⇒ Object
- #each_line(&block) ⇒ Object
- #errors ⇒ Object
- #executable_blocks ⇒ Object
- #execute ⇒ Object
- #html ⇒ Object
-
#initialize(value = nil) ⇒ Parser
constructor
A new instance of Parser.
- #input_markdown ⇒ Object
- #lines ⇒ Object
- #markdown_renderer ⇒ Object
- #output_markdown ⇒ Object
- #parse ⇒ Object
- #variables ⇒ Object
- #warnings ⇒ Object
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_block ⇒ Object
Returns the value of attribute current_block.
8 9 10 |
# File 'lib/markdownplus/parser.rb', line 8 def current_block @current_block end |
#input ⇒ Object (readonly)
Returns the value of attribute input.
7 8 9 |
# File 'lib/markdownplus/parser.rb', line 7 def input @input end |
Class Method Details
Instance Method Details
#blocks ⇒ Object
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_blocks ⇒ Object
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 |
#errors ⇒ Object
63 64 65 |
# File 'lib/markdownplus/parser.rb', line 63 def errors blocks.collect { |b| b.errors }.flatten end |
#executable_blocks ⇒ Object
31 32 33 |
# File 'lib/markdownplus/parser.rb', line 31 def executable_blocks code_blocks.select(&:executable?) end |
#execute ⇒ Object
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 |
#html ⇒ Object
43 44 45 |
# File 'lib/markdownplus/parser.rb', line 43 def html markdown_renderer.render(output_markdown) end |
#input_markdown ⇒ Object
35 36 37 |
# File 'lib/markdownplus/parser.rb', line 35 def input_markdown blocks.collect { |b| b.input_markdown }.join end |
#lines ⇒ Object
51 52 53 |
# File 'lib/markdownplus/parser.rb', line 51 def lines @lines ||= input.split("\n") end |
#markdown_renderer ⇒ Object
47 48 49 |
# File 'lib/markdownplus/parser.rb', line 47 def markdown_renderer Redcarpet::Markdown.new(GithubRenderer, fenced_code_blocks: true) end |
#output_markdown ⇒ Object
39 40 41 |
# File 'lib/markdownplus/parser.rb', line 39 def output_markdown blocks.collect { |b| b.output_markdown }.join end |
#parse ⇒ Object
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 |
#variables ⇒ Object
55 56 57 |
# File 'lib/markdownplus/parser.rb', line 55 def variables @variables ||= {} end |
#warnings ⇒ Object
59 60 61 |
# File 'lib/markdownplus/parser.rb', line 59 def warnings blocks.collect { |b| b.warnings }.flatten end |