2013-05-30 23:16:49 +00:00
|
|
|
module Gitlab
|
|
|
|
# Extract possible GFM references from an arbitrary String for further processing.
|
|
|
|
class ReferenceExtractor
|
2015-06-02 11:17:11 +00:00
|
|
|
attr_accessor :project, :current_user
|
2013-05-30 23:16:49 +00:00
|
|
|
|
2015-03-27 10:38:22 +00:00
|
|
|
def initialize(project, current_user = nil)
|
2015-03-27 10:37:45 +00:00
|
|
|
@project = project
|
2015-03-27 11:58:23 +00:00
|
|
|
@current_user = current_user
|
2013-05-30 23:16:49 +00:00
|
|
|
end
|
|
|
|
|
2015-03-27 10:37:45 +00:00
|
|
|
def analyze(text)
|
2015-06-02 11:17:11 +00:00
|
|
|
references.clear
|
|
|
|
@text = markdown.render(text.dup)
|
2013-05-30 23:16:49 +00:00
|
|
|
end
|
|
|
|
|
2015-06-02 11:17:11 +00:00
|
|
|
%i(user label issue merge_request snippet commit commit_range).each do |type|
|
|
|
|
define_method("#{type}s") do
|
|
|
|
references[type]
|
|
|
|
end
|
2013-05-30 23:16:49 +00:00
|
|
|
end
|
|
|
|
|
2015-06-02 11:17:11 +00:00
|
|
|
private
|
2013-05-30 23:16:49 +00:00
|
|
|
|
2015-06-02 11:17:11 +00:00
|
|
|
def markdown
|
|
|
|
@markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML, GitlabMarkdownHelper::MARKDOWN_OPTIONS)
|
2013-05-30 23:16:49 +00:00
|
|
|
end
|
|
|
|
|
2015-06-02 11:17:11 +00:00
|
|
|
def references
|
|
|
|
@references ||= Hash.new do |references, type|
|
|
|
|
type = type.to_sym
|
|
|
|
return references[type] if references.has_key?(type)
|
2013-05-30 23:16:49 +00:00
|
|
|
|
2015-06-02 11:17:11 +00:00
|
|
|
references[type] = pipeline_result(type).uniq
|
|
|
|
end
|
2015-03-06 22:08:28 +00:00
|
|
|
end
|
|
|
|
|
2015-04-15 20:01:00 +00:00
|
|
|
# Instantiate and call HTML::Pipeline with a single reference filter type,
|
|
|
|
# returning the result
|
|
|
|
#
|
|
|
|
# filter_type - Symbol reference type (e.g., :commit, :issue, etc.)
|
|
|
|
#
|
2015-04-23 18:02:07 +00:00
|
|
|
# Returns the results Array for the requested filter type
|
2015-04-15 20:01:00 +00:00
|
|
|
def pipeline_result(filter_type)
|
|
|
|
klass = filter_type.to_s.camelize + 'ReferenceFilter'
|
|
|
|
filter = "Gitlab::Markdown::#{klass}".constantize
|
|
|
|
|
|
|
|
context = {
|
|
|
|
project: project,
|
|
|
|
current_user: current_user,
|
|
|
|
# We don't actually care about the links generated
|
2015-06-02 11:17:21 +00:00
|
|
|
only_path: true,
|
|
|
|
ignore_blockquotes: true
|
2015-04-15 20:01:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pipeline = HTML::Pipeline.new([filter], context)
|
2015-06-02 11:17:11 +00:00
|
|
|
result = pipeline.call(@text)
|
2015-04-23 18:02:07 +00:00
|
|
|
|
|
|
|
result[:references][filter_type]
|
2013-05-30 23:16:49 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|