Class: GraphQLDocs::Renderer

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/graphql-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

#graphql_enum_types, #graphql_input_object_types, #graphql_interface_types, #graphql_mutation_types, #graphql_object_types, #graphql_operation_types, #graphql_root_types, #graphql_scalar_types, #graphql_union_types, #has_yaml?, #include, #markdownify, #slugify, #split_into_metadata_and_contents, #yaml_split

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

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

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

  @pipeline_config = @options[:pipeline_config]

  filters = @pipeline_config[: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, @pipeline_config[:context])
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#render(contents, type: nil, name: nil, filename: nil) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/graphql-docs/renderer.rb', line 40

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

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

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



49
50
51
# File 'lib/graphql-docs/renderer.rb', line 49

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