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

automatically hyperlink references to top-level declarations #324

Merged
merged 3 commits into from
Oct 29, 2015
Merged
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: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

##### Enhancements

* Mentions of top-level declarations in documentation comments are now
automatically hyperlinked to their reference.

* Render special list items (e.g. Throws, See, etc.). See
http://ericasadun.com/2015/06/14/swift-header-documentation-in-xcode-7/ for
a complete list.
Expand Down
2 changes: 1 addition & 1 deletion lib/jazzy/doc_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ def self.document(source_module, doc_model, path_to_root)
doc[:github_url] = source_module.github_url
doc[:dash_url] = source_module.dash_url
doc[:path_to_root] = path_to_root
doc.render
doc.render.gsub(ELIDED_AUTOLINK_TOKEN, path_to_root)
end
end
end
35 changes: 34 additions & 1 deletion lib/jazzy/sourcekitten.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
require 'jazzy/source_declaration'
require 'jazzy/source_mark'

ELIDED_AUTOLINK_TOKEN = '36f8f5912051ae747ef441d6511ca4cb'.freeze

module Jazzy
# This module interacts with the sourcekitten command-line executable
module SourceKitten
Expand Down Expand Up @@ -309,6 +311,35 @@ def self.filter_excluded_files(json)
end.compact
end

def self.names_and_urls(docs)
docs.flat_map do |doc|
# FIXME: autolink more than just top-level items.
[{ name: doc.name, url: doc.url }] # + names_and_urls(doc.children)
end
end

def self.autolink_text(text, data, url)
regex = /\b(#{data.map { |d| Regexp.escape(d[:name]) }.join('|')})\b/
text.gsub(regex) do
name = Regexp.last_match(1)
auto_url = data.find { |d| d[:name] == name }[:url]
if auto_url == url # docs shouldn't autolink to themselves
Regexp.last_match(0)
else
"<a href=\"#{ELIDED_AUTOLINK_TOKEN}#{auto_url}\">#{name}</a>"
end
end
end

def self.autolink(docs, data)
docs.map do |doc|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be each

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. The intent is to return an array of transformed elements, not to mutate the elements in-place. So if this is what map is doing (passing mutable references to the elements), then making a copy of the doc block argument would be more "pure", but functionally the same I believe.

doc.abstract = autolink_text(doc.abstract, data, doc.url)
doc.return = autolink_text(doc.return, data, doc.url) if doc.return
doc.children = autolink(doc.children, data)
doc
end
end

# Parse sourcekitten STDOUT output as JSON
# @return [Hash] structured docs
def self.parse(sourcekitten_output, min_acl, skip_undocumented)
Expand All @@ -321,7 +352,9 @@ def self.parse(sourcekitten_output, min_acl, skip_undocumented)
# Remove top-level enum cases because it means they have an ACL lower
# than min_acl
docs = docs.reject { |doc| doc.type.enum_element? }
[make_doc_urls(docs, []), doc_coverage, @undocumented_tokens]
docs = make_doc_urls(docs, [])
docs = autolink(docs, names_and_urls(docs.flat_map(&:children)))
[docs, doc_coverage, @undocumented_tokens]
end
end
end
2 changes: 1 addition & 1 deletion spec/integration_specs
Submodule integration_specs updated 134 files