Class: Gitlab::Gfm::ReferenceRewriter
- Inherits:
-
Object
- Object
- Gitlab::Gfm::ReferenceRewriter
- Includes:
- Utils::StrongMemoize
- 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`'
Constant Summary collapse
- RewriteError =
Class.new(StandardError)
Instance Method Summary collapse
-
#initialize(text, text_html, source_parent, current_user) ⇒ ReferenceRewriter
constructor
A new instance of ReferenceRewriter.
- #needs_rewrite? ⇒ Boolean
- #rewrite(target_parent) ⇒ Object
Constructor Details
#initialize(text, text_html, source_parent, current_user) ⇒ ReferenceRewriter
Returns a new instance of ReferenceRewriter.
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/gitlab/gfm/reference_rewriter.rb', line 38 def initialize(text, text_html, source_parent, current_user) @text = text # If for some reason cached html is not present it gets rendered here @text_html = text_html || original_html @source_parent = source_parent @current_user = current_user @pattern = Gitlab::ReferenceExtractor.references_pattern end |
Instance Method Details
#needs_rewrite? ⇒ Boolean
57 58 59 60 61 62 63 64 |
# File 'lib/gitlab/gfm/reference_rewriter.rb', line 57 def needs_rewrite? strong_memoize(:needs_rewrite) do reference_type_attribute = Banzai::Filter::References::ReferenceFilter::REFERENCE_TYPE_DATA_ATTRIBUTE @text_html.include?(reference_type_attribute) end end |
#rewrite(target_parent) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/gitlab/gfm/reference_rewriter.rb', line 49 def rewrite(target_parent) return @text unless needs_rewrite? @text.gsub!(@pattern) do |reference| unfold_reference(reference, Regexp.last_match, target_parent) end end |