Class: Ecrire::Markdown::Parsers::Code

Inherits:
Base
  • Object
show all
Defined in:
lib/ecrire/markdown/parsers/code.rb

Constant Summary collapse

OPENING_TAG =
/^((~{3,})([a-z]+)?)( (.*))?$/i

Instance Method Summary collapse

Methods inherited from Base

#initialize, parse!

Constructor Details

This class inherits a constructor from Ecrire::Markdown::Parsers::Base

Instance Method Details

#extract_code_nodes!(count) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ecrire/markdown/parsers/code.rb', line 22

def extract_code_nodes!(count)
  regex = Regexp.new("^(~{#{count}}$)")
  found_closing_tag = false
  i = @index + 1
  nodes = []

  while !found_closing_tag
    break if @document.nodes[i].nil?
    node = @document.nodes.delete_at(i)
    if regex.match(node.content)
      found_closing_tag = true
    else
      nodes.push node
    end
  end
  nodes
end

#parse!Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ecrire/markdown/parsers/code.rb', line 5

def parse!

  unless @node.instance_of?(Ecrire::Markdown::Node)
    return @node
  end

  if match = OPENING_TAG.match(@node.content)
    count = match[2].size
    language = match[3]
    title = match[5]
    nodes = extract_code_nodes!(count)
    @node = Ecrire::Markdown::Nodes::CodeBlock.new(language, title, nodes)
    @document.nodes[@index] = @node
  end
  return @node
end