2016-03-07 07:09:53 -05:00
|
|
|
module Gitlab
|
|
|
|
module Gfm
|
|
|
|
##
|
2016-03-16 05:24:44 -04:00
|
|
|
# Class that unfolds local references in text.
|
2016-03-07 07:09:53 -05:00
|
|
|
#
|
2016-03-16 05:24:44 -04:00
|
|
|
# The initializer takes text in Markdown and project this text is valid
|
|
|
|
# in context of.
|
|
|
|
#
|
|
|
|
# `unfold` method tries to find all local references and unfold each of
|
2016-03-18 09:48:55 -04:00
|
|
|
# those local references to cross reference format, assuming that the
|
|
|
|
# argument passed to this method is a project that references will be
|
|
|
|
# viewed from (see `Referable#to_reference method).
|
2016-03-16 05:24:44 -04:00
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
#
|
|
|
|
# 'Hello, this issue is related to #123 and
|
|
|
|
# other issues labeled with ~"label"', will be converted to:
|
|
|
|
#
|
|
|
|
# 'Hello, this issue is related to gitlab-org/gitlab-ce#123 and
|
|
|
|
# other issue labeled with gitlab-org/gitlab-ce~"label"'.
|
|
|
|
#
|
|
|
|
# It does respect markdown lexical rules, so text in code block will not be
|
|
|
|
# replaced, see another example:
|
|
|
|
#
|
|
|
|
# 'Merge request for issue #1234, see also link:
|
|
|
|
# http://gitlab.com/some/link/#1234, and code `puts #1234`' =>
|
|
|
|
#
|
|
|
|
# 'Merge request for issue gitlab-org/gitlab-ce#1234, se also link:
|
|
|
|
# http://gitlab.com/some/link/#1234, and code `puts #1234`'
|
2016-03-07 07:09:53 -05:00
|
|
|
#
|
|
|
|
class ReferenceUnfolder
|
|
|
|
def initialize(text, project)
|
|
|
|
@text = text
|
|
|
|
@project = project
|
2016-03-11 05:49:04 -05:00
|
|
|
@original = markdown(text)
|
2016-03-07 07:09:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def unfold(from_project)
|
2016-03-18 09:48:55 -04:00
|
|
|
pattern = Gitlab::ReferenceExtractor.references_pattern
|
|
|
|
return @text unless @text =~ pattern
|
2016-03-07 07:09:53 -05:00
|
|
|
|
2016-03-18 09:48:55 -04:00
|
|
|
@text.gsub(pattern) do |reference|
|
2016-03-11 05:49:04 -05:00
|
|
|
unfold_reference(reference, Regexp.last_match, from_project)
|
2016-03-07 07:09:53 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-03-11 05:49:04 -05:00
|
|
|
def unfold_reference(reference, match, from_project)
|
|
|
|
before = @text[0...match.begin(0)]
|
|
|
|
after = @text[match.end(0)...@text.length]
|
|
|
|
referable = find_referable(reference)
|
|
|
|
|
|
|
|
return reference unless referable
|
|
|
|
cross_reference = referable.to_reference(from_project)
|
|
|
|
new_text = before + cross_reference + after
|
|
|
|
|
|
|
|
substitution_valid?(new_text) ? cross_reference : reference
|
|
|
|
end
|
|
|
|
|
2016-03-07 07:09:53 -05:00
|
|
|
def referables
|
2016-03-11 05:49:04 -05:00
|
|
|
return @referables if @referables
|
|
|
|
|
2016-03-07 07:09:53 -05:00
|
|
|
extractor = Gitlab::ReferenceExtractor.new(@project)
|
|
|
|
extractor.analyze(@text)
|
2016-03-11 05:49:04 -05:00
|
|
|
@referables = extractor.all
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_referable(reference)
|
|
|
|
referables.find { |ref| ref.to_reference == reference }
|
|
|
|
end
|
|
|
|
|
|
|
|
def substitution_valid?(substituted)
|
|
|
|
@original == markdown(substituted)
|
|
|
|
end
|
|
|
|
|
|
|
|
def markdown(text)
|
2016-03-16 05:24:44 -04:00
|
|
|
Banzai.render(text, project: @project, no_original_data: true)
|
2016-03-07 07:09:53 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|