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_directive_types, #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, #include, #markdownify, #slugify, #split_into_metadata_and_contents, #yaml?, #yaml_split

Constructor Details

#initialize(parsed_schema, options) ⇒ Renderer

Returns a new instance of Renderer.



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/graphql-docs/renderer.rb', line 14

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

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

  @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.



12
13
14
# File 'lib/graphql-docs/renderer.rb', line 12

def options
  @options
end

Instance Method Details

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



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

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



52
53
54
# File 'lib/graphql-docs/renderer.rb', line 52

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