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-04-14 22:34:01 +00:00
|
|
|
class CommitReferenceFilter < ReferenceFilter
|
2015-04-03 00:46:43 +00:00
|
|
|
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)
|
2015-05-14 20:59:39 +00:00
|
|
|
text.gsub(Commit.reference_pattern) do |match|
|
2015-04-03 00:46:43 +00:00
|
|
|
yield match, $~[:commit], $~[:project]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def call
|
2015-05-14 20:59:39 +00:00
|
|
|
replace_text_nodes_matching(Commit.reference_pattern) do |content|
|
2015-04-14 22:34:01 +00:00
|
|
|
commit_link_filter(content)
|
2015-04-03 00:46:43 +00:00
|
|
|
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)
|
|
|
|
self.class.references_in(text) do |match, commit_ref, project_ref|
|
|
|
|
project = self.project_from_ref(project_ref)
|
|
|
|
|
2015-04-16 18:06:05 +00:00
|
|
|
if commit = commit_from_ref(project, commit_ref)
|
2015-04-15 19:56:45 +00:00
|
|
|
push_result(:commit, commit)
|
|
|
|
|
2015-04-03 00:46:43 +00:00
|
|
|
url = url_for_commit(project, commit)
|
|
|
|
|
2015-04-16 16:41:59 +00:00
|
|
|
title = escape_once(commit.link_title)
|
2015-04-14 22:34:01 +00:00
|
|
|
klass = reference_class(:commit)
|
2015-08-07 03:56:09 +00:00
|
|
|
data = data_attribute(project.id)
|
2015-04-03 00:46:43 +00:00
|
|
|
|
|
|
|
project_ref += '@' if project_ref
|
|
|
|
|
2015-08-07 03:56:09 +00:00
|
|
|
%(<a href="#{url}" #{data}
|
2015-04-03 00:46:43 +00:00
|
|
|
title="#{title}"
|
2015-04-21 20:20:24 +00:00
|
|
|
class="#{klass}">#{project_ref}#{commit.short_id}</a>)
|
2015-04-03 00:46:43 +00:00
|
|
|
else
|
|
|
|
match
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-16 18:06:05 +00:00
|
|
|
def commit_from_ref(project, commit_ref)
|
|
|
|
if project && project.valid_repo?
|
2015-04-21 13:13:40 +00:00
|
|
|
project.commit(commit_ref)
|
2015-04-16 18:06:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-03 00:46:43 +00:00
|
|
|
def url_for_commit(project, commit)
|
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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|