Commit 323d328c authored by Grzegorz Bizon's avatar Grzegorz Bizon

Rename reference unfolder to rewriter, minor refactorings

parent 802b28e0
...@@ -77,9 +77,9 @@ module Issues ...@@ -77,9 +77,9 @@ module Issues
end end
def unfold_references(content) def unfold_references(content)
unfolder = Gitlab::Gfm::ReferenceUnfolder.new(content, @old_project, rewriter = Gitlab::Gfm::ReferenceRewriter.new(content, @old_project,
@current_user) @current_user)
unfolder.unfold(@new_project) rewriter.rewrite(@new_project)
end end
def notify_participants def notify_participants
......
...@@ -28,32 +28,31 @@ module Gitlab ...@@ -28,32 +28,31 @@ module Gitlab
# 'Merge request for issue gitlab-org/gitlab-ce#1234, se also link: # 'Merge request for issue gitlab-org/gitlab-ce#1234, se also link:
# http://gitlab.com/some/link/#1234, and code `puts #1234`' # http://gitlab.com/some/link/#1234, and code `puts #1234`'
# #
class ReferenceUnfolder class ReferenceRewriter
def initialize(text, project, user) def initialize(text, source_project, current_user)
@text = text @text = text
@project = project @source_project = source_project
@user = user @current_user = current_user
@original = markdown(text) @original_html = markdown(text)
end end
def unfold(from_project) def rewrite(target_project)
pattern = Gitlab::ReferenceExtractor.references_pattern pattern = Gitlab::ReferenceExtractor.references_pattern
return @text unless @text =~ pattern
@text.gsub(pattern) do |reference| @text.gsub(pattern) do |reference|
unfold_reference(reference, Regexp.last_match, from_project) unfold_reference(reference, Regexp.last_match, target_project)
end end
end end
private private
def unfold_reference(reference, match, from_project) def unfold_reference(reference, match, target_project)
before = @text[0...match.begin(0)] before = @text[0...match.begin(0)]
after = @text[match.end(0)...@text.length] after = @text[match.end(0)..-1]
referable = find_referable(reference) referable = find_local_referable(reference)
return reference unless referable return reference unless referable
cross_reference = referable.to_reference(from_project) cross_reference = referable.to_reference(target_project)
new_text = before + cross_reference + after new_text = before + cross_reference + after
substitution_valid?(new_text) ? cross_reference : reference substitution_valid?(new_text) ? cross_reference : reference
...@@ -62,21 +61,22 @@ module Gitlab ...@@ -62,21 +61,22 @@ module Gitlab
def referables def referables
return @referables if @referables return @referables if @referables
extractor = Gitlab::ReferenceExtractor.new(@project, @user) extractor = Gitlab::ReferenceExtractor.new(@source_project,
@current_user)
extractor.analyze(@text) extractor.analyze(@text)
@referables = extractor.all @referables = extractor.all
end end
def find_referable(reference) def find_local_referable(reference)
referables.find { |ref| ref.to_reference == reference } referables.find { |ref| ref.to_reference == reference }
end end
def substitution_valid?(substituted) def substitution_valid?(substituted)
@original == markdown(substituted) @original_html == markdown(substituted)
end end
def markdown(text) def markdown(text)
Banzai.render(text, project: @project, no_original_data: true) Banzai.render(text, project: @source_project, no_original_data: true)
end end
end end
end end
......
require 'spec_helper' require 'spec_helper'
describe Gitlab::Gfm::ReferenceUnfolder do describe Gitlab::Gfm::ReferenceRewriter do
let(:text) { 'some text' } let(:text) { 'some text' }
let(:old_project) { create(:project) } let(:old_project) { create(:project) }
let(:new_project) { create(:project) } let(:new_project) { create(:project) }
...@@ -8,9 +8,9 @@ describe Gitlab::Gfm::ReferenceUnfolder do ...@@ -8,9 +8,9 @@ describe Gitlab::Gfm::ReferenceUnfolder do
before { old_project.team << [user, :guest] } before { old_project.team << [user, :guest] }
describe '#unfold' do describe '#rewrite' do
subject do subject do
described_class.new(text, old_project, user).unfold(new_project) described_class.new(text, old_project, user).rewrite(new_project)
end end
context 'multiple issues and merge requests referenced' do context 'multiple issues and merge requests referenced' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment