BigW Consortium Gitlab

reference_rewriter.rb 3.1 KB
Newer Older
1 2 3
module Gitlab
  module Gfm
    ##
4
    # Class that unfolds local references in text.
5
    #
6 7 8 9
    # 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
10 11 12
    # 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).
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
    #
    # 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`'
30
    #
31
    class ReferenceRewriter
32 33
      RewriteError = Class.new(StandardError)

34
      def initialize(text, source_project, current_user)
35
        @text = text
36 37 38
        @source_project = source_project
        @current_user = current_user
        @original_html = markdown(text)
39
        @pattern = Gitlab::ReferenceExtractor.references_pattern
40 41
      end

42
      def rewrite(target_project)
43 44
        return @text unless needs_rewrite?

45
        @text.gsub(@pattern) do |reference|
46
          unfold_reference(reference, Regexp.last_match, target_project)
47 48 49
        end
      end

50
      def needs_rewrite?
51
        @text =~ @pattern
52 53
      end

54 55
      private

56
      def unfold_reference(reference, match, target_project)
57
        before = @text[0...match.begin(0)]
58
        after = @text[match.end(0)..-1]
59

60
        referable = find_referable(reference)
61
        return reference unless referable
62

63
        cross_reference = build_cross_reference(referable, target_project)
64
        return reference if reference == cross_reference
65

66 67 68 69
        if cross_reference.nil?
          raise RewriteError, "Unspecified reference detected for #{referable.class.name}"
        end

70
        new_text = before + cross_reference + after
71 72 73
        substitution_valid?(new_text) ? cross_reference : reference
      end

74
      def find_referable(reference)
75 76
        extractor = Gitlab::ReferenceExtractor.new(@source_project,
                                                   @current_user)
77 78
        extractor.analyze(reference)
        extractor.all.first
79 80
      end

81 82 83 84
      def build_cross_reference(referable, target_project)
        if referable.respond_to?(:project)
          referable.to_reference(target_project)
        else
85
          referable.to_reference(@source_project, target_project: target_project)
86 87 88
        end
      end

89
      def substitution_valid?(substituted)
90
        @original_html == markdown(substituted)
91 92 93
      end

      def markdown(text)
94
        Banzai.render(text, project: @source_project, no_original_data: true)
95 96 97 98
      end
    end
  end
end