Class: JsonSchemaDocs::Generator

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/json-schema-docs/generator.rb

Constant Summary collapse

DYNAMIC_PAGES =
%i(links resource)
STATIC_PAGES =
%i(index)

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

#include, #markdownify, #schemata, #slugify, #types

Constructor Details

#initialize(parsed_schema, options) ⇒ Generator

Returns a new instance of Generator.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/json-schema-docs/generator.rb', line 13

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

  @renderer = @options[:renderer].new(@parsed_schema, @options)

  DYNAMIC_PAGES.each do |sym|
    if !File.exist?(@options[:templates][sym])
      raise IOError, "`#{sym}` template #{@options[:templates][sym]} was not found"
    end
    instance_variable_set("@json_schema_#{sym}_template", ERB.new(File.read(@options[:templates][sym]), nil, '-'))
  end

  STATIC_PAGES.each do |sym|
    if @options[:landing_pages][sym].nil?
      instance_variable_set("@#{sym}_landing_page", nil)
    elsif !File.exist?(@options[:landing_pages][sym])
      raise IOError, "`#{sym}` landing page #{@options[:landing_pages][sym]} was not found"
    end

    landing_page_contents = File.read(@options[:landing_pages][sym])

    instance_variable_set("@json_schema_#{sym}_landing_page", landing_page_contents)
  end
end

Instance Attribute Details

#parsed_schemaObject

Returns the value of attribute parsed_schema.



8
9
10
# File 'lib/json-schema-docs/generator.rb', line 8

def parsed_schema
  @parsed_schema
end

Instance Method Details

#generateObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/json-schema-docs/generator.rb', line 39

def generate
  FileUtils.rm_rf(@options[:output_dir]) if @options[:delete_output]

  @parsed_schema.each_pair do |resource, schemata|
    DYNAMIC_PAGES.each do |type|
      layout = instance_variable_get("@json_schema_#{type}_template")
      opts = @options.merge(helper_methods)

      opts[:schemata_resource] = resource
      opts[:schemata] = schemata

      contents = layout.result(OpenStruct.new(opts).instance_eval { binding })
      write_file(type, resource, schemata['title'], contents)
    end
  end

  STATIC_PAGES.each do |name|
    landing_page = instance_variable_get("@json_schema_#{name}_landing_page")

    unless landing_page.nil?
      write_file(:landing_page, name.to_s, nil, landing_page, trim: false)
    end
  end

  if @options[:use_default_styles]
    assets_dir = File.join(File.dirname(__FILE__), 'layouts', 'assets')
    FileUtils.mkdir_p(File.join(@options[:output_dir], 'assets'))

    sass = File.join(assets_dir, 'css', 'screen.scss')
    system `sass --sourcemap=none #{sass}:#{@options[:output_dir]}/assets/style.css`
  end

  true
end