Class: Jdoc::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/jdoc/generator.rb

Constant Summary collapse

DEFAULT_HTML_TEMPLATE_PATH =
File.expand_path("../../../template.html.erb", __FILE__)
DEFAULT_MARKDOWN_TEMPLATE_PATH =
File.expand_path("../../../template.md.erb", __FILE__)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, html: false, html_template_path: nil, markdown_template_path: nil) ⇒ Generator

Returns a new instance of Generator.

Parameters:

  • schema (Hash)

    JSON Schema represented as a Hash

  • html (true, false) (defaults to: false)

    Pass true to render HTML docs

  • html_template_path (String) (defaults to: nil)

    Path to ERB template to render HTML

  • Markdown_template_path (String)

    Path to ERB template to render Markdown



16
17
18
19
20
21
# File 'lib/jdoc/generator.rb', line 16

def initialize(schema, html: false, html_template_path: nil, markdown_template_path: nil)
  @raw_schema = schema
  @html = html
  @html_template_path = html_template_path
  @markdown_template_path = markdown_template_path
end

Class Method Details

.call(*args) ⇒ String

Utility wrapper for Jdoc::Generator#call

Returns:

  • (String)


8
9
10
# File 'lib/jdoc/generator.rb', line 8

def self.call(*args)
  new(*args).call
end

Instance Method Details

#callString

Note:

Add some fix to adapt to GitHub anchor style

Generates Markdown or HTML documentation from JSON schema

Returns:

  • (String)

    Generated text



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jdoc/generator.rb', line 26

def call
  markdown = markdown_renderer.result(schema: schema)
  if @html
    html = markdown_parser.render(markdown)
    html =  html_renderer.result(body: html)
    html.gsub(/id="(.+)"/) {|text| text.tr("/:", "") }
  else
    markdown
  end
rescue Jdoc::Link::ExampleNotFound => exception
  abort("Error: #{exception.to_s}")
end