class JsonSchemaDocs::Renderer

Attributes

options[R]

Public Class Methods

new(parsed_schema, options) click to toggle source
# 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

Public Instance Methods

render(contents, meta: {}) click to toggle source
# 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: {}) click to toggle source
# File lib/json-schema-docs/renderer.rb, line 51
def to_html(string, context: {})
  @pipeline.to_html(string, context)
end

Private Instance Methods

filter?(f) click to toggle source
# File lib/json-schema-docs/renderer.rb, line 61
def filter?(f)
  f < HTML::Pipeline::Filter
rescue LoadError, ArgumentError
  false
end
filter_key(s) click to toggle source
# File lib/json-schema-docs/renderer.rb, line 57
def filter_key(s)
  s.downcase
end