class Jdoc::Generator
Constants
- DEFAULT_HTML_TEMPLATE_PATH
- DEFAULT_MARKDOWN_TEMPLATE_PATH
Public Class Methods
Utility wrapper for Jdoc::Generator#call
@return [String]
# File lib/jdoc/generator.rb, line 8 def self.call(*args) new(*args).call end
@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
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
@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
# File lib/jdoc/generator.rb, line 51 def html_template File.read(html_template_path) end
@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
@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
@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
@return [String] Content of specified Markdown template
# File lib/jdoc/generator.rb, line 75 def markdown_template File.read(markdown_template_path) end
@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
@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