2015-09-25 12:07:36 +05:30
|
|
|
require 'gitlab/markdown'
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
module Gitlab
|
|
|
|
module Markdown
|
|
|
|
# HTML filter that replaces commit references with links.
|
|
|
|
#
|
|
|
|
# This filter supports cross-project references.
|
|
|
|
class CommitReferenceFilter < ReferenceFilter
|
|
|
|
include CrossProjectReference
|
|
|
|
|
|
|
|
# Public: Find commit references in text
|
|
|
|
#
|
|
|
|
# CommitReferenceFilter.references_in(text) do |match, commit, project_ref|
|
|
|
|
# "<a href=...>#{commit}</a>"
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# text - String text to search.
|
|
|
|
#
|
|
|
|
# Yields the String match, the String commit identifier, and an optional
|
|
|
|
# String of the external project reference.
|
|
|
|
#
|
|
|
|
# Returns a String replaced with the return of the block.
|
|
|
|
def self.references_in(text)
|
|
|
|
text.gsub(Commit.reference_pattern) do |match|
|
|
|
|
yield match, $~[:commit], $~[:project]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
def self.referenced_by(node)
|
|
|
|
project = Project.find(node.attr("data-project")) rescue nil
|
|
|
|
return unless project
|
|
|
|
|
|
|
|
id = node.attr("data-commit")
|
|
|
|
commit = commit_from_ref(project, id)
|
|
|
|
|
|
|
|
return unless commit
|
|
|
|
|
|
|
|
{ commit: commit }
|
|
|
|
end
|
|
|
|
|
2015-09-11 14:41:01 +05:30
|
|
|
def call
|
|
|
|
replace_text_nodes_matching(Commit.reference_pattern) do |content|
|
|
|
|
commit_link_filter(content)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Replace commit references in text with links to the commit specified.
|
|
|
|
#
|
|
|
|
# text - String text to replace references in.
|
|
|
|
#
|
|
|
|
# Returns a String with commit references replaced with links. All links
|
|
|
|
# have `gfm` and `gfm-commit` class names attached for styling.
|
|
|
|
def commit_link_filter(text)
|
2015-10-24 18:46:33 +05:30
|
|
|
self.class.references_in(text) do |match, id, project_ref|
|
2015-09-11 14:41:01 +05:30
|
|
|
project = self.project_from_ref(project_ref)
|
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
if commit = self.class.commit_from_ref(project, id)
|
2015-09-11 14:41:01 +05:30
|
|
|
url = url_for_commit(project, commit)
|
|
|
|
|
|
|
|
title = escape_once(commit.link_title)
|
|
|
|
klass = reference_class(:commit)
|
2015-10-24 18:46:33 +05:30
|
|
|
data = data_attribute(project: project.id, commit: id)
|
2015-09-11 14:41:01 +05:30
|
|
|
|
|
|
|
project_ref += '@' if project_ref
|
|
|
|
|
|
|
|
%(<a href="#{url}" #{data}
|
|
|
|
title="#{title}"
|
|
|
|
class="#{klass}">#{project_ref}#{commit.short_id}</a>)
|
|
|
|
else
|
|
|
|
match
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-24 18:46:33 +05:30
|
|
|
def self.commit_from_ref(project, id)
|
2015-09-11 14:41:01 +05:30
|
|
|
if project && project.valid_repo?
|
2015-10-24 18:46:33 +05:30
|
|
|
project.commit(id)
|
2015-09-11 14:41:01 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def url_for_commit(project, commit)
|
2015-09-25 12:07:36 +05:30
|
|
|
h = Gitlab::Application.routes.url_helpers
|
2015-09-11 14:41:01 +05:30
|
|
|
h.namespace_project_commit_url(project.namespace, project, commit,
|
|
|
|
only_path: context[:only_path])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|