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

Attributes included from Helpers

#templates

Instance Method Summary collapse

Methods included from Helpers

#format_type, #graphql_enum_types, #graphql_input_object_types, #graphql_interface_types, #graphql_mutation_types, #graphql_object_types, #graphql_scalar_types, #graphql_union_types, #include, #markdown, #slugify

Constructor Details

#initialize(options, parsed_schema) ⇒ Renderer

Returns a new instance of Renderer.



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

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

  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 Method Details

#has_yaml?(contents) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_yaml?(contents)
  contents =~ /\A-{3,5}\s*$/
end

#render(type, name, contents) ⇒ Object



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

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

  if has_yaml?(contents)
    # Split data
    meta, contents = (contents)
    opts = opts.merge(meta)
  end

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

#split_into_metadata_and_contents(contents) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/graphql-docs/renderer.rb', line 60

def (contents)
  opts = {}
  pieces = yaml_split(contents)
  if pieces.size < 4
    raise RuntimeError.new(
      "The file '#{content_filename}' appears to start with a metadata section (three or five dashes at the top) but it does not seem to be in the correct format.",
    )
  end
  # Parse
  begin
    meta = YAML.load(pieces[2]) || {}
  rescue Exception => e # rubocop:disable Lint/RescueException
    raise "Could not parse YAML for #{name}: #{e.message}"
  end
  [meta, pieces[4]]
end

#yaml_split(contents) ⇒ Object



56
57
58
# File 'lib/graphql-docs/renderer.rb', line 56

def yaml_split(contents)
  contents.split(/^(-{5}|-{3})[ \t]*\r?\n?/, 3)
end