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.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
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
- #html ⇒ Object
- #includable_blocks ⇒ Object
- #include ⇒ Object
-
#initialize(value = nil) ⇒ Parser
constructor
A new instance of Parser.
- #lines ⇒ Object
- #markdown ⇒ Object
- #markdown_renderer ⇒ Object
- #parse ⇒ Object
- #warnings ⇒ Object
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_block ⇒ Object
Returns the value of attribute current_block.
10 11 12 |
# File 'lib/markdownplus/parser.rb', line 10 def current_block @current_block end |
#source ⇒ Object (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
#blocks ⇒ Object
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_blocks ⇒ Object
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 |
#errors ⇒ Object
57 58 59 |
# File 'lib/markdownplus/parser.rb', line 57 def errors blocks.collect { |b| b.errors }.flatten end |
#executable_blocks ⇒ Object
37 38 39 |
# File 'lib/markdownplus/parser.rb', line 37 def executable_blocks code_blocks.select(&:executable?) end |
#html ⇒ Object
45 46 47 |
# File 'lib/markdownplus/parser.rb', line 45 def html markdown_renderer.render(markdown) end |
#includable_blocks ⇒ Object
33 34 35 |
# File 'lib/markdownplus/parser.rb', line 33 def includable_blocks code_blocks.select(&:includable?) end |
#include ⇒ Object
92 93 94 95 96 |
# File 'lib/markdownplus/parser.rb', line 92 def include includable_blocks.each do |block| block.include end end |
#lines ⇒ Object
53 54 55 |
# File 'lib/markdownplus/parser.rb', line 53 def lines @lines ||= source.split("\n") end |
#markdown ⇒ Object
41 42 43 |
# File 'lib/markdownplus/parser.rb', line 41 def markdown blocks.collect { |b| b.markdown }.join end |
#markdown_renderer ⇒ Object
49 50 51 |
# File 'lib/markdownplus/parser.rb', line 49 def markdown_renderer Redcarpet::Markdown.new(Bootstrap2Renderer, fenced_code_blocks: true) end |
#parse ⇒ Object
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 |
#warnings ⇒ Object
61 62 63 |
# File 'lib/markdownplus/parser.rb', line 61 def warnings blocks.collect { |b| b.warnings }.flatten end |