Class: Kramdown::Parser::KramdownMermaid

Inherits:
Kramdown
  • Object
show all
Defined in:
lib/kramdown-mermaid/parser/kramdown.rb

Constant Summary collapse

ER_DIAGRAM =
'erDiagram'
CONSTRAINTS =
i[PK FK UK].freeze
ER_DIAGRAM_START =
/#{ER_DIAGRAM}[^\n]*(?:%%)?[^\n]*\n/.freeze
ENTITY_START =
/[\s\t]*([a-z_]*)[\s\t]*\{\n((?:.*\n)+?)[\s\t]*\}/.freeze
RELATION_START =
/[\s\t]*([a-z_]*)[\s\t]*(\|o|\|\||\}o|\}\|)--(o\{|\|\||o\{|\|\{)[\s\t]*([a-z_]*):[\s\t]*".*"[\s\t]*\n/.freeze

Instance Method Summary collapse

Constructor Details

#initialize(source, options) ⇒ KramdownMermaid

Returns a new instance of KramdownMermaid.



8
9
10
11
12
13
# File 'lib/kramdown-mermaid/parser/kramdown.rb', line 8

def initialize(source, options)
  super
  @block_parsers.unshift(:er_diagram)
  @block_parsers.unshift(:entity)
  @block_parsers.unshift(:relation)
end

Instance Method Details

#attributesObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kramdown-mermaid/parser/kramdown.rb', line 33

def attributes
  arr = @src[2].split(/\n/).map { |a| a.gsub(/\A\s*/, '').gsub(/\s*\Z/, '') }
  arr.map do |a|
    s = a.split(/\s/)

    type = s[0]
    name = s[1]
    constraint = s.length > 2 ? s[2] : nil

    if constraint && !CONSTRAINTS.include?(constraint.to_sym)
      raise Kramdown::Error,
            "Invalid constraint #{constraint} for attribute #{s[1]}"
    end

    { type: type, name: name, constraint: constraint }
  end
end

#parse_entityObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/kramdown-mermaid/parser/kramdown.rb', line 51

def parse_entity
  @src.pos += @src.matched_size
  start_line_number = @src.current_line_number

  entity = @src[1]

  @tree.children << Element.new(:entity, @src.matched, nil, {
                                  entity: entity,
                                  attributes: attributes,
                                  location: start_line_number
                                })
end

#parse_er_diagramObject



23
24
25
26
27
28
29
30
# File 'lib/kramdown-mermaid/parser/kramdown.rb', line 23

def parse_er_diagram
  @src.pos += @src.matched_size
  start_line_number = @src.current_line_number

  @tree.children << Element.new(:er_diagram, @src.matched, nil, {
                                  location: start_line_number
                                })
end

#parse_relationObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/kramdown-mermaid/parser/kramdown.rb', line 65

def parse_relation
  @src.pos += @src.matched_size
  start_line_number = @src.current_line_number

  left_entity = @src[1]
  left_relation = @src[2]
  right_relation = @src[3]
  right_entity = @src[4]

  @tree.children << Element.new(:relation, @src.matched, nil, {
                                  left_entity: left_entity,
                                  left_relation: left_relation,
                                  right_relation: right_relation,
                                  right_entity: right_entity,
                                  location: start_line_number
                                })
end