Class: JsonSchemaDocs::Renderer

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/json-schema-docs/renderer.rb

Constant Summary

Constants included from Helpers

Helpers::SLUGIFY_PRETTY_REGEXP

Instance Attribute Summary collapse

Attributes included from Helpers

#templates

Instance Method Summary collapse

Methods included from Helpers

#include, #markdownify, #schemata, #slugify, #types

Constructor Details

#initialize(parsed_schema, options) ⇒ Renderer

Returns a new instance of Renderer.



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
39
40
# File 'lib/json-schema-docs/renderer.rb', line 12

def initialize(parsed_schema, options)
  @parsed_schema = parsed_schema
  @options = options

  unless @options[:templates][:default].nil?
    @default_layout = ERB.new(File.read(@options[:templates][:default]))
  end

  @pipeline_config = @options[:pipeline_config] || {}
  pipeline = @pipeline_config[:pipeline] || {}
  context = @pipeline_config[:context] || {}

  filters = pipeline.map do |f|
    if filter?(f)
      f
    else
      key = filter_key(f)
      filter = HTML::Pipeline.constants.find { |c| c.downcase == key }
      # possibly a custom filter
      if filter.nil?
        Kernel.const_get(f)
      else
        HTML::Pipeline.const_get(filter)
      end
    end
  end

  @pipeline = HTML::Pipeline.new(filters, context)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/json-schema-docs/renderer.rb', line 10

def options
  @options
end

Instance Method Details

#render(contents, meta: {}) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/json-schema-docs/renderer.rb', line 42

def render(contents, meta: {})
  opts = meta.merge({ base_url: @options[:base_url], output_dir: @options[:output_dir] }).merge(helper_methods)

  contents = to_html(contents, context: opts)
  return contents if @default_layout.nil?
  opts[:content] = contents
  @default_layout.result(OpenStruct.new(opts).instance_eval { binding })
end

#to_html(string, context: {}) ⇒ Object



51
52
53
# File 'lib/json-schema-docs/renderer.rb', line 51

def to_html(string, context: {})
  @pipeline.to_html(string, context)
end