class Jdoc::Generator

Constants

DEFAULT_HTML_TEMPLATE_PATH
DEFAULT_MARKDOWN_TEMPLATE_PATH

Public Class Methods

call(*args) click to toggle source

Utility wrapper for Jdoc::Generator#call @return [String]

# File lib/jdoc/generator.rb, line 8
def self.call(*args)
  new(*args).call
end
new(schema, html: false, html_template_path: nil, markdown_template_path: nil) click to toggle source

@param schema [Hash] JSON Schema represented as a Hash @param html [true, false] Pass true to render HTML docs @param html_template_path [String] Path to ERB template to render HTML @param Markdown_template_path [String] Path to ERB template to render Markdown

# 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

Public Instance Methods

call() click to toggle source

Generates Markdown or HTML documentation from JSON schema @note Add some fix to adapt to GitHub anchor style @return [String] Generated text

# 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

Private Instance Methods

html_renderer() click to toggle source

@return [Erubis::Eruby] Renderer to render HTML that takes HTML string

# File lib/jdoc/generator.rb, line 42
def html_renderer
  Erubis::Eruby.new(html_template)
end
html_template() click to toggle source
# File lib/jdoc/generator.rb, line 51
def html_template
  File.read(html_template_path)
end
html_template_path() click to toggle source

@returns [String] Path to ERB template to render HTML

# File lib/jdoc/generator.rb, line 47
def html_template_path
  @html_template_path || DEFAULT_HTML_TEMPLATE_PATH
end
markdown_parser() click to toggle source

@return [Redcarpet::Markdown] Markdown parser to convert Markdown into HTML

# File lib/jdoc/generator.rb, line 56
def markdown_parser
  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
markdown_renderer() click to toggle source

@return [Erubis::Eruby] Renderer to render Markdown that takes schema data

# File lib/jdoc/generator.rb, line 70
def markdown_renderer
  Erubis::Eruby.new(markdown_template)
end
markdown_template() click to toggle source

@return [String] Content of specified Markdown template

# File lib/jdoc/generator.rb, line 75
def markdown_template
  File.read(markdown_template_path)
end
markdown_template_path() click to toggle source

@return [String] Path to ERB template to render Markdown

# File lib/jdoc/generator.rb, line 80
def markdown_template_path
  @markdown_template_path || DEFAULT_MARKDOWN_TEMPLATE_PATH
end
schema() click to toggle source

@return [Jdoc::Schema] @raise [JsonSchema::SchemaError] Raises if given invalid JSON Schema

# File lib/jdoc/generator.rb, line 86
def schema
  Jdoc::Schema.new(@raw_schema)
end