Skip to content

Commit

Permalink
Add basic rendering of instance properties
Browse files Browse the repository at this point in the history
  • Loading branch information
lee-dohm committed May 13, 2019
1 parent 3ec7b61 commit ca5643e
Showing 1 changed file with 88 additions and 17 deletions.
105 changes: 88 additions & 17 deletions lib/filters/api_json_filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,101 @@
class ApiJsonFilter < Nanoc::Filter
identifier :api_json

def octicon(name)
"<span class=\"octicon octicon-#{name}\"></span>"
# ----- Helpers -----

def class_source_link(data, params)
source_link(data, "class")
end

def markdown(text)
GitHub::Markdown.render(text)
def property_source_link(data, params)
source_link(data, "property")
end

def class_source_link(data, params)
def source_link(item, type = nil)
<<~HTML
<a
class="document-source"
href="#{data["srcUrl"]}"
title="View class source">
href="#{item["srcUrl"]}"
#{type ? " title=\"View #{type} source\"" : ""}>
#{octicon("file-code")}
</a>
HTML
end

def markdown(text)
GitHub::Markdown.render(text)
end

def octicon(name)
"<span class=\"octicon octicon-#{name}\"></span>"
end

def argument(arg)
"<span class=\"argument\">#{arg["name"]}</span>"
end

def argument_list(func)
args = func["arguments"]

<<~HTML
<span class="argument-list">(#{args ? args.map { |arg| argument(arg) }.join(", ") : ""})</span>
HTML
end

def method(func, scope, params = {})
<<~HTML
<div
class="api-entry js-api-entry #{visibility_class(func["visibility"])}"
id="#{scope}-#{func["name"]}">
<h3 class="name">
#{func["name"]}#{argument_list(func)}
#{source_link(func)}
</h3>
</div>
HTML
end

def property(prop, scope, params = {})
<<~HTML
<div
class="api-entry js-api-entry #{visibility_class(prop["visibility"])}"
id="#{scope}-#{prop["name"]}">
<h3 class="name">
#{prop["name"]}
#{property_source_link(prop, params)}
</h3>
<div>
<div class="summary markdown-body">
#{markdown(prop["summary"])}
</div>
</div>
</div>
HTML
end

def visibility(viz)
case viz
when "Public" then "Essential"
else viz
end
end

def visibility_class(viz)
visibility(viz).downcase
end

def visibility_label(data, params)
<<~HTML
<span
class="label label-#{visibility_class(data["visibility"])}"
title="This class is in the #{visibility_class(data["visibility"])} API">
#{data["visibility"]}
</span>
HTML
end

# ----- Page Sections -----

def description(data, params)
markdown(data["description"])
end
Expand All @@ -39,24 +115,19 @@ def page_title(data, params = {})

def sections(data, params = {})
section_text = data["sections"].map do |section|
props = data["instanceProperties"].select { |prop| prop["sectionName"] == section["name"] }
methods = data["instanceMethods"].select { |func| func["sectionName"] == section["name"] }

<<~HTML
<h2 class="detail-section">#{section["name"]}</h2>
#{props.map { |prop| property(prop, "instance", params) }.join}
#{methods.map { |func| method(func, "instance", params) }.join}
HTML
end

section_text.join("\n")
end

def visibility_label(data, params)
<<~HTML
<span
class="label label-#{data["visibility"].downcase}"
title="This class is in the #{data["visibility"].downcase} API">
#{data["visibility"]}
</span>
HTML
end

def run(content, params = {})
data = JSON.parse(content)

Expand Down

0 comments on commit ca5643e

Please sign in to comment.