Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor AppDocumentation #18

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions lib/apiculture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def serve_api_documentation_at(url)
# MyApi.api_documentation.to_html #=> "..."
def api_documentation
require_relative 'apiculture/app_documentation'
AppDocumentation.new(self, @apiculture_mounted_at.to_s, @apiculture_actions_and_docs || [])
AppDocumentation.new(self, @apiculture_mounted_at.to_s, apiculture_stack)
end

# Define an API method. Under the hood will call the related methods in Sinatra
Expand Down Expand Up @@ -277,6 +277,5 @@ def api_method(http_verb, path, options={}, &blk)

def apiculture_stack
@apiculture_actions_and_docs ||= []
@apiculture_actions_and_docs
end
end
5 changes: 5 additions & 0 deletions lib/apiculture/action_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ def defines_route_params?
def initialize
@parameters, @route_parameters, @responses = [], [], []
end

def to_tagged_markdown(mountpoint)
md = Apiculture::MethodDocumentation.new(self, mountpoint).to_markdown
TaggedMarkdown.new(md, 'apiculture-method')
end
end
50 changes: 17 additions & 33 deletions lib/apiculture/app_documentation.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
require_relative 'method_documentation'
require 'github/markup'
require_relative 'tagged_markdown'

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
JOINER = "\n\n"
TEMPLATE_PATH = '/app_documentation_tpl.mustache'

def initialize(app, mountpoint, action_definitions_and_markdown_segments)
@app_title = app.to_s
Expand All @@ -24,31 +13,26 @@ def initialize(app, mountpoint, action_definitions_and_markdown_segments)

# Generates a Markdown string that contains the entire API documentation
def to_markdown
(['## %s' % @app_title] + to_markdown_slices).join("\n\n")
(['## %s' % @app_title] + to_tagged_markdowns).join(JOINER)
end

# Generates a complete HTML document string that can be saved into a file
def to_html
require 'mustache'
template = File.read(__dir__ + TEMPLATE_PATH)
Mustache.render(template, html_fragment: to_html_fragment)
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")
to_tagged_markdowns.map(&:to_html).join(JOINER)
end

private

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')
def to_tagged_markdowns
@chunks.map do |action_def_or_doc|
action_def_or_doc.to_tagged_markdown(@mountpoint)
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
4 changes: 4 additions & 0 deletions lib/apiculture/markdown_segment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ class Apiculture::MarkdownSegment < Struct.new(:string)
def to_markdown
string.to_s
end

def to_tagged_markdown(_mountpoint)
TaggedMarkdown.new(self, 'apiculture-verbatim')
end
end
15 changes: 15 additions & 0 deletions lib/apiculture/tagged_markdown.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'github/markup'

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
4 changes: 4 additions & 0 deletions lib/apiculture/timestamp_promise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ def self.to_markdown
ts = Time.now.utc.strftime "%Y-%m-%d %H:%M"
"Documentation built on #{ts}"
end

def self.to_tagged_markdown(_mountpoint)
TaggedMarkdown.new(self, 'apiculture-verbatim')
end
end