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



49
50
51
52
# File 'lib/jdoc/generator.rb', line 49

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)


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

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



36
37
38
# File 'lib/jdoc/generator.rb', line 36

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



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

def markdown_template
  File.read(MARKDOWN_TEMPLATE_PATH)
end

.redcarpetObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/jdoc/generator.rb', line 17

def redcarpet
  @redcarpet ||= Redcarpet::Markdown.new(
    Redcarpet::Render::HTML.new(
      filter_html: true,
      hard_wrap: true,
      with_toc_data: true,
    ),
    autolink: true,
    fenced_code_blocks: true,
    no_intra_emphasis: true,
  )
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)


57
58
59
60
61
62
63
64
65
# File 'lib/jdoc/generator.rb', line 57

def call
  result = self.class.markdown_renderer.result(schema: schema)
  if @html
    result = self.class.html_renderer.result(body: self.class.redcarpet.render(result))
    result.gsub(/id="(.+)"/) {|text| text.tr("/:", "") }
  else
    result
  end
end