2015-09-01 13:28:19 -04:00
|
|
|
require 'gitlab/markdown'
|
|
|
|
|
2013-05-30 19:16:49 -04:00
|
|
|
module Gitlab
|
|
|
|
# Extract possible GFM references from an arbitrary String for further processing.
|
|
|
|
class ReferenceExtractor
|
2015-10-14 03:17:05 -04:00
|
|
|
attr_accessor :project, :current_user, :load_lazy_references
|
2013-05-30 19:16:49 -04:00
|
|
|
|
2015-10-14 03:17:05 -04:00
|
|
|
def initialize(project, current_user = nil, load_lazy_references: true)
|
2015-03-27 06:37:45 -04:00
|
|
|
@project = project
|
2015-03-27 07:58:23 -04:00
|
|
|
@current_user = current_user
|
2015-10-14 03:17:05 -04:00
|
|
|
@load_lazy_references = load_lazy_references
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2015-10-14 03:17:05 -04:00
|
|
|
def analyze(text)
|
2015-06-02 07:17:11 -04:00
|
|
|
references.clear
|
2015-10-14 03:17:05 -04:00
|
|
|
@text = Gitlab::Markdown.render_without_gfm(text)
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2015-06-02 07:17:11 -04: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 19:16:49 -04:00
|
|
|
end
|
|
|
|
|
2015-06-02 07:17:11 -04:00
|
|
|
private
|
2013-05-30 19:16:49 -04:00
|
|
|
|
2015-06-02 07:17:11 -04:00
|
|
|
def references
|
|
|
|
@references ||= Hash.new do |references, type|
|
|
|
|
type = type.to_sym
|
2015-10-18 08:05:27 -04:00
|
|
|
next references[type] if references.has_key?(type)
|
2013-05-30 19:16:49 -04:00
|
|
|
|
2015-10-14 03:17:05 -04:00
|
|
|
references[type] = pipeline_result(type)
|
2015-06-02 07:17:11 -04:00
|
|
|
end
|
2015-03-06 17:08:28 -05:00
|
|
|
end
|
|
|
|
|
2015-04-15 16:01:00 -04: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 14:02:07 -04:00
|
|
|
# Returns the results Array for the requested filter type
|
2015-04-15 16:01:00 -04:00
|
|
|
def pipeline_result(filter_type)
|
2015-10-12 05:54:46 -04:00
|
|
|
return [] if @text.blank?
|
|
|
|
|
2015-10-14 03:46:06 -04:00
|
|
|
klass = "#{filter_type.to_s.camelize}ReferenceFilter"
|
2015-08-27 16:09:01 -04:00
|
|
|
filter = Gitlab::Markdown.const_get(klass)
|
2015-04-15 16:01:00 -04:00
|
|
|
|
|
|
|
context = {
|
|
|
|
project: project,
|
|
|
|
current_user: current_user,
|
2015-10-14 03:46:06 -04:00
|
|
|
|
2015-04-15 16:01:00 -04:00
|
|
|
# We don't actually care about the links generated
|
2015-06-02 07:17:21 -04:00
|
|
|
only_path: true,
|
2015-10-13 16:53:05 -04:00
|
|
|
ignore_blockquotes: true,
|
2015-10-14 03:46:06 -04:00
|
|
|
|
|
|
|
# ReferenceGathererFilter
|
|
|
|
load_lazy_references: false,
|
|
|
|
reference_filter: filter
|
2015-04-15 16:01:00 -04:00
|
|
|
}
|
|
|
|
|
2015-10-07 11:00:48 -04:00
|
|
|
pipeline = HTML::Pipeline.new([filter, Gitlab::Markdown::ReferenceGathererFilter], context)
|
2015-10-14 03:17:05 -04:00
|
|
|
result = pipeline.call(@text)
|
2015-04-23 14:02:07 -04:00
|
|
|
|
2015-10-14 03:17:05 -04:00
|
|
|
values = result[:references][filter_type].uniq
|
2015-10-13 16:53:05 -04:00
|
|
|
|
2015-10-14 03:17:05 -04:00
|
|
|
if @load_lazy_references
|
|
|
|
values = Gitlab::Markdown::ReferenceFilter::LazyReference.load(values).uniq
|
2015-10-13 16:53:05 -04:00
|
|
|
end
|
|
|
|
|
2015-10-14 03:17:05 -04:00
|
|
|
values
|
2013-05-30 19:16:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|