Class: Jdoc::Generator

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

Constant Summary collapse

HTML_TEMPLATE_PATH =
File.expand_path("../../../template.html.erb", __FILE__)
MARKDOWN_TEMPLATE_PATH =
File.expand_path("../../../template.md.erb", __FILE__)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, html: false) ⇒ 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



44
45
46
47
# File 'lib/jdoc/generator.rb', line 44

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

Class Method Details

.call(*args) ⇒ String

Utility wrapper for Jdoc::Generator#call

Returns:

  • (String)


38
39
40
# File 'lib/jdoc/generator.rb', line 38

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

.html_rendererErubis::Eruby

Returns:

  • (Erubis::Eruby)


13
14
15
# File 'lib/jdoc/generator.rb', line 13

def html_renderer
  @html_renderer ||= Erubis::Eruby.new(html_template)
end

.html_templateString

Returns ERB template.

Returns:

  • (String)

    ERB template



31
32
33
# File 'lib/jdoc/generator.rb', line 31

def html_template
  File.read(HTML_TEMPLATE_PATH)
end

.markdown_rendererErubis::Eruby

Returns:

  • (Erubis::Eruby)


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

def markdown_renderer
  @markdown_renderer ||= Erubis::Eruby.new(markdown_template)
end

.markdown_templateString

Returns ERB template.

Returns:

  • (String)

    ERB template



26
27
28
# File 'lib/jdoc/generator.rb', line 26

def markdown_template
  File.read(MARKDOWN_TEMPLATE_PATH)
end

.redcarpetObject



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

def redcarpet
  @redcarpet ||= Redcarpet::Markdown.new(
    Redcarpet::Render::HTML.new(hard_wrap: true, filter_html: true),
    autolink: true,
    fenced_code_blocks: true,
  )
end

Instance Method Details

#callString

Generates Markdown or HTML documentation from JSON schema

Returns:

  • (String)


51
52
53
54
55
# File 'lib/jdoc/generator.rb', line 51

def call
  result = self.class.markdown_renderer.result(schema: schema)
  result = self.class.html_renderer.result(body: self.class.redcarpet.render(result)) if @html
  result
end