Class: Banzai::ReferenceExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/banzai/reference_extractor.rb

Overview

Extract possible GFM references from an arbitrary String for further processing.

Direct Known Subclasses

Gitlab::ReferenceExtractor

Constant Summary collapse

LAZY_KEY =
:banzai_reference_extractor_lazy

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeReferenceExtractor

Returns a new instance of ReferenceExtractor


26
27
28
# File 'lib/banzai/reference_extractor.rb', line 26

def initialize
  @texts = []
end

Class Method Details

.lazily(values = nil, &block) ⇒ Object


11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/banzai/reference_extractor.rb', line 11

def lazily(values = nil, &block)
  return (values || block.call).uniq if lazy?

  begin
    Thread.current[LAZY_KEY] = true

    values ||= block.call

    Banzai::LazyReference.load(values.uniq).uniq
  ensure
    Thread.current[LAZY_KEY] = false
  end
end

.lazy?Boolean

Returns:

  • (Boolean)

7
8
9
# File 'lib/banzai/reference_extractor.rb', line 7

def lazy?
  Thread.current[LAZY_KEY]
end

Instance Method Details

#analyze(text, context = {}) ⇒ Object


30
31
32
# File 'lib/banzai/reference_extractor.rb', line 30

def analyze(text, context = {})
  @texts << Renderer.render(text, context)
end

#references(type, context = {}) ⇒ Object


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/banzai/reference_extractor.rb', line 34

def references(type, context = {})
  filter = Banzai::Filter["#{type}_reference"]

  context.merge!(
    pipeline: :reference_extraction,

    # ReferenceGathererFilter
    reference_filter: filter
  )

  self.class.lazily do
    @texts.flat_map do |html|
      text_context = context.dup
      result = Renderer.render_result(html, text_context)
      result[:references][type]
    end.uniq
  end
end