diff options
Diffstat (limited to 'lib/gitlab')
-rw-r--r-- | lib/gitlab/gfm/reference_unfolder.rb | 31 | ||||
-rw-r--r-- | lib/gitlab/reference_extractor.rb | 8 |
2 files changed, 38 insertions, 1 deletions
diff --git a/lib/gitlab/gfm/reference_unfolder.rb b/lib/gitlab/gfm/reference_unfolder.rb new file mode 100644 index 00000000000..4969e8ebe85 --- /dev/null +++ b/lib/gitlab/gfm/reference_unfolder.rb @@ -0,0 +1,31 @@ +module Gitlab + module Gfm + ## + # Class than unfolds local references in text. + # + # + class ReferenceUnfolder + def initialize(text, project) + @text = text + @project = project + end + + def unfold(from_project) + referables.each_with_object(@text.dup) do |referable, text| + next unless referable.respond_to?(:to_reference) + + pattern = /#{Regexp.escape(referable.to_reference)}/ + text.gsub!(pattern, referable.to_reference(from_project)) + end + end + + private + + def referables + extractor = Gitlab::ReferenceExtractor.new(@project) + extractor.analyze(@text) + extractor.all + end + end + end +end diff --git a/lib/gitlab/reference_extractor.rb b/lib/gitlab/reference_extractor.rb index 4d830aa45e1..8c698d43bef 100644 --- a/lib/gitlab/reference_extractor.rb +++ b/lib/gitlab/reference_extractor.rb @@ -1,6 +1,7 @@ module Gitlab # Extract possible GFM references from an arbitrary String for further processing. class ReferenceExtractor < Banzai::ReferenceExtractor + REFERABLES = %i(user issue label milestone merge_request snippet commit commit_range) attr_accessor :project, :current_user, :author def initialize(project, current_user = nil, author = nil) @@ -17,7 +18,7 @@ module Gitlab super(text, context.merge(project: project)) end - %i(user label milestone merge_request snippet commit commit_range).each do |type| + REFERABLES.each do |type| define_method("#{type}s") do @references[type] ||= references(type, reference_context) end @@ -31,6 +32,11 @@ module Gitlab end end + def all + REFERABLES.each { |referable| send(referable.to_s.pluralize) } + @references.values.flatten + end + private def reference_context |