debian-mirror-gitlab/tooling/graphql/docs/renderer.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

55 lines
1.5 KiB
Ruby
Raw Normal View History

2019-09-30 21:07:59 +05:30
# frozen_string_literal: true
2021-09-04 01:27:46 +05:30
require_relative 'helper'
2019-09-30 21:07:59 +05:30
2021-09-04 01:27:46 +05:30
module Tooling
2019-09-30 21:07:59 +05:30
module Graphql
module Docs
# Gitlab renderer for graphql-docs.
# Uses HAML templates to parse markdown and generate .md files.
# It uses graphql-docs helpers and schema parser, more information in https://github.com/gjtorikian/graphql-docs.
#
# Arguments:
2021-04-29 21:17:54 +05:30
# schema - the GraphQL schema definition. For GitLab should be: GitlabSchema
2019-09-30 21:07:59 +05:30
# output_dir: The folder where the markdown files will be saved
# template: The path of the haml template to be parsed
class Renderer
2021-09-04 01:27:46 +05:30
include Tooling::Graphql::Docs::Helper
2019-09-30 21:07:59 +05:30
2021-04-29 21:17:54 +05:30
attr_reader :schema
2019-09-30 21:07:59 +05:30
def initialize(schema, output_dir:, template:)
@output_dir = output_dir
@template = template
@layout = Haml::Engine.new(File.read(template))
2022-08-13 15:12:31 +05:30
@parsed_schema = GraphQLDocs::Parser.new(schema, {}).parse
2021-04-29 21:17:54 +05:30
@schema = schema
2021-06-08 01:23:25 +05:30
@seen = Set.new
2019-09-30 21:07:59 +05:30
end
2019-12-21 20:55:43 +05:30
def contents
# Render and remove an extra trailing new line
@contents ||= @layout.render(self).sub!(/\n(?=\Z)/, '')
2019-09-30 21:07:59 +05:30
end
2019-12-21 20:55:43 +05:30
def write
2019-09-30 21:07:59 +05:30
filename = File.join(@output_dir, 'index.md')
FileUtils.mkdir_p(@output_dir)
File.write(filename, contents)
end
2021-06-08 01:23:25 +05:30
private
def seen_type?(name)
@seen.include?(name)
end
def seen_type!(name)
@seen << name
end
2019-09-30 21:07:59 +05:30
end
end
end
end