Sha256: 68a9a15a6c27b89c84139b05fb3e2cf23d7d32fe956702a0c2ed9a2292cd6ab8

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

require 'html/pipeline'
require 'yaml'
require 'extended-markdown-filter'

module GraphQLDocs
  class Renderer
    include Helpers

    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

    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

    def to_html(string)
      return '' if string.nil?
      CommonMarker.render_html(string, :DEFAULT).strip
    end

    private

    def filter_key(s)
      s.downcase
    end

    def filter?(f)
      f < HTML::Pipeline::Filter
    rescue LoadError, ArgumentError
      false
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
graphql-docs-1.0.0.pre2 lib/graphql-docs/renderer.rb