2015-12-15 09:51:16 -05:00
|
|
|
module Banzai
|
|
|
|
# Extract possible GFM references from an arbitrary String for further processing.
|
|
|
|
class ReferenceExtractor
|
|
|
|
def initialize
|
2016-07-21 13:15:31 -04:00
|
|
|
@texts_and_contexts = []
|
2015-12-15 09:51:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def analyze(text, context = {})
|
2016-07-21 13:15:31 -04:00
|
|
|
@texts_and_contexts << { text: text, context: context }
|
2015-12-15 09:51:16 -05:00
|
|
|
end
|
|
|
|
|
2016-05-26 07:16:43 -04:00
|
|
|
def references(type, project, current_user = nil)
|
2018-04-03 09:45:17 -04:00
|
|
|
context = RenderContext.new(project, current_user)
|
|
|
|
processor = Banzai::ReferenceParser[type].new(context)
|
2016-05-26 07:16:43 -04:00
|
|
|
|
|
|
|
processor.process(html_documents)
|
|
|
|
end
|
2015-12-15 09:51:16 -05:00
|
|
|
|
2016-12-05 07:12:22 -05:00
|
|
|
def reset_memoized_values
|
|
|
|
@html_documents = nil
|
|
|
|
@texts_and_contexts = []
|
|
|
|
end
|
|
|
|
|
2016-05-26 07:16:43 -04:00
|
|
|
private
|
2015-12-15 09:51:16 -05:00
|
|
|
|
2016-05-26 07:16:43 -04:00
|
|
|
def html_documents
|
|
|
|
# This ensures that we don't memoize anything until we have a number of
|
|
|
|
# text blobs to parse.
|
2016-07-21 13:15:31 -04:00
|
|
|
return [] if @texts_and_contexts.empty?
|
2015-12-15 09:51:16 -05:00
|
|
|
|
2016-07-21 13:15:31 -04:00
|
|
|
@html_documents ||= Renderer.cache_collection_render(@texts_and_contexts)
|
|
|
|
.map { |html| Nokogiri::HTML.fragment(html) }
|
2015-12-15 09:51:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|