2015-08-27 23:02:49 +00:00
|
|
|
require 'gitlab/markdown'
|
|
|
|
|
2015-04-03 00:46:43 +00:00
|
|
|
module Gitlab
|
|
|
|
module Markdown
|
2015-04-14 22:34:01 +00:00
|
|
|
# HTML filter that replaces commit references with links.
|
2015-04-03 00:46:43 +00:00
|
|
|
#
|
|
|
|
# This filter supports cross-project references.
|
2015-11-30 20:14:46 +00:00
|
|
|
class CommitReferenceFilter < AbstractReferenceFilter
|
|
|
|
def self.object_class
|
|
|
|
Commit
|
|
|
|
end
|
2015-04-03 00:46:43 +00:00
|
|
|
|
|
|
|
def self.references_in(text)
|
2015-05-14 20:59:39 +00:00
|
|
|
text.gsub(Commit.reference_pattern) do |match|
|
2015-11-30 20:14:46 +00:00
|
|
|
yield match, $~[:commit], $~[:project], $~
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-10-07 15:00:48 +00:00
|
|
|
def self.referenced_by(node)
|
|
|
|
project = Project.find(node.attr("data-project")) rescue nil
|
|
|
|
return unless project
|
|
|
|
|
|
|
|
id = node.attr("data-commit")
|
2015-11-30 20:14:46 +00:00
|
|
|
commit = find_object(project, id)
|
2015-10-07 15:00:48 +00:00
|
|
|
|
|
|
|
return unless commit
|
|
|
|
|
|
|
|
{ commit: commit }
|
|
|
|
end
|
|
|
|
|
2015-11-30 20:14:46 +00:00
|
|
|
def self.find_object(project, id)
|
2015-04-16 18:06:05 +00:00
|
|
|
if project && project.valid_repo?
|
2015-10-07 15:00:48 +00:00
|
|
|
project.commit(id)
|
2015-04-16 18:06:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-30 20:14:46 +00:00
|
|
|
def find_object(*args)
|
|
|
|
self.class.find_object(*args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def url_for_object(commit, project)
|
2015-08-26 01:42:46 +00:00
|
|
|
h = Gitlab::Application.routes.url_helpers
|
2015-04-03 00:46:43 +00:00
|
|
|
h.namespace_project_commit_url(project.namespace, project, commit,
|
|
|
|
only_path: context[:only_path])
|
|
|
|
end
|
2015-11-30 20:14:46 +00:00
|
|
|
|
|
|
|
def object_link_title(commit)
|
|
|
|
commit.link_title
|
|
|
|
end
|
2015-04-03 00:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|