Class: JsonValueLoadInlineMacro

Inherits:
Asciidoctor::Extensions::InlineMacroProcessor
  • Object
show all
Defined in:
lib/asciidoctor-json-value-loader.rb

Instance Method Summary collapse

Instance Method Details

#process(parent, target, attrs) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/asciidoctor-json-value-loader.rb', line 11

def process parent, target, attrs

  path, query = target.split(':')

  query = query.split('.').map do |q|
    decoded = URI.decode_www_form_component(q)
    if decoded.match(/^\d+$/) then
      decoded.to_i
    else
      decoded
    end
  end

  json = JSON.parse(File.open(path, 'r:utf-8', &:read))

  content = json.dig(*query)

  content = case content
  when String
    content
  when Numeric
    content.to_s
  else
    JSON.generate(content)
  end

  create_inline_pass parent, content, attributes: { 'subs' => :normal } 
end