Sha256: f35ba8c6f7ec5b5f1223116c122c80f4211f72773f0fb16d6a9d9c6914690ee9

Contents?: true

Size: 1.82 KB

Versions: 3

Compression:

Stored size: 1.82 KB

Contents

require_relative 'method_documentation'
require 'github/markup'

class Apiculture::AppDocumentation
  class TaggedMarkdown < Struct.new(:string, :section_class)
    def to_markdown
      string.to_markdown.to_s rescue string.to_s
    end
    
    def to_html
      '<section class="%s">%s</section>' % [Rack::Utils.escape_html(section_class), render_markdown(to_markdown)]
    end
    
    def render_markdown(s)
      GitHub::Markup.render('section.markdown', s.to_s)
    end
  end
  
  def initialize(app, mountpoint, action_definitions_and_markdown_segments)
    @app_title = app.to_s
    @mountpoint = mountpoint
    @chunks = action_definitions_and_markdown_segments
  end
  
  # Generates a Markdown string that contains the entire API documentation
  def to_markdown
    (['## %s' % @app_title] + to_markdown_slices).join("\n\n")
  end 

  def to_openapi
    OpenApiDocumentation::Base.new(@app_title, @mountpoint, @chunks)
  end
  
  # Generates an HTML fragment string that can be included into another HTML document
  def to_html_fragment
    to_markdown_slices.map do |tagged_markdown|
      tagged_markdown.to_html
    end.join("\n\n")
  end
  
  def to_markdown_slices
    markdown_slices = @chunks.map do | action_def_or_doc |
      if action_def_or_doc.respond_to?(:http_verb) # ActionDefinition
        s = Apiculture::MethodDocumentation.new(action_def_or_doc, @mountpoint).to_markdown
        TaggedMarkdown.new(s, 'apiculture-method')
      elsif action_def_or_doc.respond_to?(:to_markdown)
        TaggedMarkdown.new(action_def_or_doc, 'apiculture-verbatim')
      end
    end
  end
  
  # Generates a complete HTML document string that can be saved into a file
  def to_html
    require 'mustache'
    template = File.read(__dir__ + '/app_documentation_tpl.mustache')
    Mustache.render(template, :html_fragment => to_html_fragment)
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
apiculture-0.2.1 lib/apiculture/app_documentation.rb
apiculture-0.2.0 lib/apiculture/app_documentation.rb
apiculture-0.1.7 lib/apiculture/app_documentation.rb