Class: Banzai::Filter::AbstractReferenceFilter

Inherits:
ReferenceFilter
  • Object
show all
Includes:
CrossProjectReference
Defined in:
lib/banzai/filter/abstract_reference_filter.rb

Overview

Issues, Merge Requests, Snippets, Commits and Commit Ranges share similar functionality in reference filtering.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CrossProjectReference

#project_from_ref

Methods inherited from ReferenceFilter

#data_attribute, #each_node, #element_node?, #escape_once, #ignore_ancestor_query, #project, #reference_class, #replace_link_node_with_href, #replace_link_node_with_text, #replace_text_when_pattern_matches, #text_node?, user_can_reference?, user_can_see_reference?, #validate, #yield_valid_link

Class Method Details

.data_referenceObject


21
22
23
# File 'lib/banzai/filter/abstract_reference_filter.rb', line 21

def self.data_reference
  @data_reference ||= "data-#{object_name.dasherize}"
end

.object_classObject


8
9
10
11
# File 'lib/banzai/filter/abstract_reference_filter.rb', line 8

def self.object_class
  # Implement in child class
  # Example: MergeRequest
end

.object_class_titleObject


25
26
27
# File 'lib/banzai/filter/abstract_reference_filter.rb', line 25

def self.object_class_title
  @object_title ||= object_class.name.titleize
end

.object_nameObject


13
14
15
# File 'lib/banzai/filter/abstract_reference_filter.rb', line 13

def self.object_name
  @object_name ||= object_class.name.underscore
end

.object_symObject


17
18
19
# File 'lib/banzai/filter/abstract_reference_filter.rb', line 17

def self.object_sym
  @object_sym ||= object_name.to_sym
end

.referenced_by(node) ⇒ Object


48
49
50
# File 'lib/banzai/filter/abstract_reference_filter.rb', line 48

def self.referenced_by(node)
  { object_sym => LazyReference.new(object_class, node.attr(data_reference)) }
end

.references_in(text, pattern = object_class.reference_pattern) ⇒ Object

Public: Find references in text (like `!123` for merge requests)

AnyReferenceFilter.references_in(text) do |match, id, project_ref, matches|
  object = find_object(project_ref, id)
  "<a href=...>#{object.to_reference}</a>"
end

text - String text to search.

Yields the String match, the Integer referenced object ID, an optional String of the external project reference, and all of the matchdata.

Returns a String replaced with the return of the block.


42
43
44
45
46
# File 'lib/banzai/filter/abstract_reference_filter.rb', line 42

def self.references_in(text, pattern = object_class.reference_pattern)
  text.gsub(pattern) do |match|
    yield match, $~[object_sym].to_i, $~[:project], $~
  end
end

Instance Method Details

#callObject


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/banzai/filter/abstract_reference_filter.rb', line 108

def call
  return doc if project.nil?

  ref_pattern = object_class.reference_pattern
  link_pattern = object_class.link_reference_pattern

  each_node do |node|
    if text_node?(node) && ref_pattern
      replace_text_when_pattern_matches(node, ref_pattern) do |content|
        object_link_filter(content, ref_pattern)
      end

    elsif element_node?(node)
      yield_valid_link(node) do |link, text|
        if ref_pattern && link =~ /\A#{ref_pattern}\z/
          replace_link_node_with_href(node, link) do
            object_link_filter(link, ref_pattern, link_text: text)
          end

          next
        end

        next unless link_pattern

        if link == text && text =~ /\A#{link_pattern}/
          replace_link_node_with_text(node, link) do
            object_link_filter(text, link_pattern)
          end

          next
        end

        if link =~ /\A#{link_pattern}\z/
          replace_link_node_with_href(node, link) do
            object_link_filter(link, link_pattern, link_text: text)
          end

          next
        end
      end
    end
  end

  doc
end

#find_object(project, id) ⇒ Object


68
69
70
71
# File 'lib/banzai/filter/abstract_reference_filter.rb', line 68

def find_object(project, id)
  # Implement in child class
  # Example: project.merge_requests.find
end

#find_object_cached(project, id) ⇒ Object


73
74
75
76
77
78
79
80
81
# File 'lib/banzai/filter/abstract_reference_filter.rb', line 73

def find_object_cached(project, id)
  if RequestStore.active?
    cache = find_objects_cache[object_class][project.id]

    get_or_set_cache(cache, id) { find_object(project, id) }
  else
    find_object(project, id)
  end
end

#object_classObject


52
53
54
# File 'lib/banzai/filter/abstract_reference_filter.rb', line 52

def object_class
  self.class.object_class
end

#object_class_titleObject


60
61
62
# File 'lib/banzai/filter/abstract_reference_filter.rb', line 60

def object_class_title
  self.class.object_class_title
end

Replace references (like `!123` for merge requests) in text with links to the referenced object's details page.

text - String text to replace references in. pattern - Reference pattern to match against. link_text - Original content of the link being replaced.

Returns a String with references replaced with links. All links have `gfm` and `gfm-OBJECT_NAME` class names attached for styling.


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/banzai/filter/abstract_reference_filter.rb', line 163

def object_link_filter(text, pattern, link_text: nil)
  references_in(text, pattern) do |match, id, project_ref, matches|
    project = project_from_ref_cache(project_ref)

    if project && object = find_object_cached(project, id)
      title = object_link_title(object)
      klass = reference_class(object_sym)

      data  = data_attribute(
        original:     link_text || match,
        project:      project.id,
        object_sym => object.id
      )

      if matches.names.include?("url") && matches[:url]
        url = matches[:url]
      else
        url = url_for_object_cached(object, project)
      end

      text = link_text || object_link_text(object, matches)

      %(<a href="#{url}" #{data}
           title="#{escape_once(title)}"
           class="#{klass}">#{escape_once(text)}</a>)
    else
      match
    end
  end
end

208
209
210
211
212
213
214
215
# File 'lib/banzai/filter/abstract_reference_filter.rb', line 208

def object_link_text(object, matches)
  text = object.reference_link_text(context[:project])

  extras = object_link_text_extras(object, matches)
  text += " (#{extras.join(", ")})" if extras.any?

  text
end

194
195
196
197
198
199
200
201
202
# File 'lib/banzai/filter/abstract_reference_filter.rb', line 194

def object_link_text_extras(object, matches)
  extras = []

  if matches.names.include?("anchor") && matches[:anchor] && matches[:anchor] =~ /\A\#note_(\d+)\z/
    extras << "comment #{$1}"
  end

  extras
end

204
205
206
# File 'lib/banzai/filter/abstract_reference_filter.rb', line 204

def object_link_title(object)
  "#{object_class_title}: #{object.title}"
end

#object_symObject


56
57
58
# File 'lib/banzai/filter/abstract_reference_filter.rb', line 56

def object_sym
  self.class.object_sym
end

#project_from_ref_cache(ref) ⇒ Object


83
84
85
86
87
88
89
90
91
# File 'lib/banzai/filter/abstract_reference_filter.rb', line 83

def project_from_ref_cache(ref)
  if RequestStore.active?
    cache = project_refs_cache

    get_or_set_cache(cache, ref) { project_from_ref(ref) }
  else
    project_from_ref(ref)
  end
end

#references_in(*args, &block) ⇒ Object


64
65
66
# File 'lib/banzai/filter/abstract_reference_filter.rb', line 64

def references_in(*args, &block)
  self.class.references_in(*args, &block)
end

#url_for_object(object, project) ⇒ Object


93
94
95
96
# File 'lib/banzai/filter/abstract_reference_filter.rb', line 93

def url_for_object(object, project)
  # Implement in child class
  # Example: project_merge_request_url
end

#url_for_object_cached(object, project) ⇒ Object


98
99
100
101
102
103
104
105
106
# File 'lib/banzai/filter/abstract_reference_filter.rb', line 98

def url_for_object_cached(object, project)
  if RequestStore.active?
    cache = url_for_object_cache[object_class][project.id]

    get_or_set_cache(cache, object) { url_for_object(object, project) }
  else
    url_for_object(object, project)
  end
end