Class: Gitlab::Gfm::ReferenceRewriter

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/gfm/reference_rewriter.rb

Overview

Class that unfolds local references in text.

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 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).

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`'

Instance Method Summary collapse

Constructor Details

#initialize(text, source_project, current_user) ⇒ ReferenceRewriter

Returns a new instance of ReferenceRewriter


32
33
34
35
36
37
38
# File 'lib/gitlab/gfm/reference_rewriter.rb', line 32

def initialize(text, source_project, current_user)
  @text = text
  @source_project = source_project
  @current_user = current_user
  @original_html = markdown(text)
  @pattern = Gitlab::ReferenceExtractor.references_pattern
end

Instance Method Details

#needs_rewrite?Boolean

Returns:

  • (Boolean)

48
49
50
# File 'lib/gitlab/gfm/reference_rewriter.rb', line 48

def needs_rewrite?
  @text =~ @pattern
end

#rewrite(target_project) ⇒ Object


40
41
42
43
44
45
46
# File 'lib/gitlab/gfm/reference_rewriter.rb', line 40

def rewrite(target_project)
  return @text unless needs_rewrite?

  @text.gsub(@pattern) do |reference|
    unfold_reference(reference, Regexp.last_match, target_project)
  end
end