Class: Kramdown::Parser::ISKram

Inherits:
Kramdown
  • Object
show all
Defined in:
lib/kramdown_parser/kram_parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.register_ast_element_hook(*elements, &hook) ⇒ Object



27
28
29
30
31
# File 'lib/kramdown_parser/kram_parser.rb', line 27

def register_ast_element_hook *elements, &hook
  @element_hooks ||= []
  @element_hooks << { elements: elements, hook: hook }
  hook
end

.register_post_parse_hook(&hook) ⇒ Object



8
9
10
11
12
# File 'lib/kramdown_parser/kram_parser.rb', line 8

def register_post_parse_hook &hook
  @hooks ||= []
  @hooks << hook
  hook
end

.trigger_ast_elements_hooks(parser) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kramdown_parser/kram_parser.rb', line 39

def trigger_ast_elements_hooks parser
  return if @element_hooks.nil? || @element_hooks.empty?
  traverse = lambda do |path|
    elem = path.last
    pp elem
    elem.children.each do |child|
      handled = false
      @element_hooks.each do |hook|
        if hook[:elements].include?(child.type)
          hooked = hook[:hook].call path, child
          handled ||= hooked
        end
      end
      traverse.call(path + [child]) unless handled
    end
  end
  traverse.call([parser.root])
end

.trigger_post_parse_hooks(parser) ⇒ Object



20
21
22
23
24
25
# File 'lib/kramdown_parser/kram_parser.rb', line 20

def trigger_post_parse_hooks parser
  return if @hooks.nil? || @hooks.empty?
  @hooks.each do |hook|
    hook.call parser
  end
end

.unregister_ast_element_hook(hook) ⇒ Object



33
34
35
36
37
# File 'lib/kramdown_parser/kram_parser.rb', line 33

def unregister_ast_element_hook hook
  if @element_hooks
    @element_hooks.delete_if { |rec| rec[:hook] == hook }
  end
end

.unregister_post_parse_hook(hook) ⇒ Object



14
15
16
17
18
# File 'lib/kramdown_parser/kram_parser.rb', line 14

def unregister_post_parse_hook hook
  if @hooks
    @hooks.delete hook
  end
end

Instance Method Details

#parseObject



60
61
62
63
64
# File 'lib/kramdown_parser/kram_parser.rb', line 60

def parse
  super
  trigger_post_parse_hooks
  trigger_ast_elements_hooks
end