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_scalar_types, #graphql_union_types, #has_yaml?, #include, #markdownify, #slugify, #split_into_metadata_and_contents, #yaml_split

Constructor Details

#initialize(parsed_schema, options) ⇒ Renderer



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

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.



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

def options
  @options
end

Instance Method Details

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



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

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

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

#to_html(string) ⇒ Object



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

def to_html(string)
  @pipeline.to_html(string)
end