Sha256: 4a9f0d03efbc08fbea0fc0cff3e830256703ccd699f4febc2badc387b05f1301

Contents?: true

Size: 1.98 KB

Versions: 7

Compression:

Stored size: 1.98 KB

Contents

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

    class << self
      # @return [Erubis::Eruby]
      def markdown_renderer
        @markdown_renderer ||= Erubis::Eruby.new(markdown_template)
      end

      # @return [Erubis::Eruby]
      def html_renderer
        @html_renderer ||= Erubis::Eruby.new(html_template)
      end

      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

      # @return [String] ERB template
      def markdown_template
        File.read(MARKDOWN_TEMPLATE_PATH)
      end

      # @return [String] ERB template
      def html_template
        File.read(HTML_TEMPLATE_PATH)
      end
    end

    # Utility wrapper for Jdoc::Generator#call
    # @return [String]
    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
    def initialize(schema, html: false)
      @raw_schema = schema
      @html = html
    end

    # Generates Markdown or HTML documentation from JSON schema
    # @note Add some fix to adapt to GitHub anchor style
    # @return [String]
    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

    private

    # @return [Jdoc::Schema]
    # @raise [JsonSchema::SchemaError] Raises if given invalid JSON Schema
    def schema
      @schema ||= Jdoc::Schema.new(@raw_schema)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
jdoc-0.3.0 lib/jdoc/generator.rb
jdoc-0.2.1 lib/jdoc/generator.rb
jdoc-0.2.0 lib/jdoc/generator.rb
jdoc-0.1.9 lib/jdoc/generator.rb
jdoc-0.1.8 lib/jdoc/generator.rb
jdoc-0.1.7 lib/jdoc/generator.rb
jdoc-0.1.6 lib/jdoc/generator.rb