Class: Journeybook::ComponentParser

Inherits:
Object
  • Object
show all
Defined in:
app/services/journeybook/component_parser.rb

Defined Under Namespace

Classes: ParsedComponent

Constant Summary collapse

HIDDEN_PROP =
:journeybook_hidden
COMPONENT_PATTERN =
/\{\{\s*(.*?)\s*\}\}/m
NAME_PATTERN =
/\A([a-zA-Z][\w-]*)/
PROP_PATTERN =
/([a-zA-Z_]\w*)\s*=\s*(?:"([^"]*)"|'([^']*)')/

Instance Method Summary collapse

Instance Method Details

#extract(markdown) ⇒ Array(String, Array<ParsedComponent>)

Parameters:

  • markdown (String)

Returns:



25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/services/journeybook/component_parser.rb', line 25

def extract(markdown)
  components = []
  extracted = markdown.to_s.gsub(COMPONENT_PATTERN) do |source|
    component = build_component(Regexp.last_match(1), components.length, source: source)
    next source unless component

    components << component
    component.token
  end

  [ extracted, components ]
end

#parse(markdown) ⇒ Array<ParsedComponent>

Parameters:

  • markdown (String)

Returns:



17
18
19
20
21
# File 'app/services/journeybook/component_parser.rb', line 17

def parse(markdown)
  markdown.to_s.scan(COMPONENT_PATTERN).each_with_index.filter_map do |(body), index|
    build_component(body, index)
  end
end