Class: GraphQLDocs::Renderer
- Inherits:
-
Object
- Object
- GraphQLDocs::Renderer
- 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
Instance Method Summary collapse
- #has_yaml?(contents) ⇒ Boolean
-
#initialize(options, parsed_schema) ⇒ Renderer
constructor
A new instance of Renderer.
- #render(type, name, contents) ⇒ Object
- #yaml_split(contents) ⇒ Object
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(, parsed_schema) = @parsed_schema = parsed_schema unless [:templates][:default].nil? @graphql_default_layout = ERB.new(File.read([:templates][:default])) end @pipeline_config = [: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
64 65 66 |
# File 'lib/graphql-docs/renderer.rb', line 64 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 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/graphql-docs/renderer.rb', line 37 def render(type, name, contents) opts = { base_url: [:base_url] }.merge({ type: type, name: name}).merge(helper_methods) if has_yaml?(contents) # Split data 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 = YAML.load(pieces[2]) || {} opts = opts.merge() rescue Exception => e # rubocop:disable Lint/RescueException raise "Could not parse YAML for #{name}: #{e.message}" end contents = pieces[4] 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 |
#yaml_split(contents) ⇒ Object
68 69 70 |
# File 'lib/graphql-docs/renderer.rb', line 68 def yaml_split(contents) contents.split(/^(-{5}|-{3})[ \t]*\r?\n?/, 3) end |