Class: Banzai::Filter::IncludeFilter

Inherits:
HTML::Pipeline::TextFilter
  • Object
show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
lib/banzai/filter/include_filter.rb

Overview

Detect include / transclusion syntax. Include the specified file/url, replacing the existing include instruction with the contents of the file/url.

  • does not recursively handle includes

  • only supports wikis or repository files

Syntax is ‘::includefile=FILE_OR_URL`

Based on lib/gitlab/asciidoc/include_processor.rb

Constant Summary collapse

REGEX =

This regex must be able to handle ‘n` or `rn` line endings

Regexp.new('^::include\{file=(?<include>.{1,1024})}([\r\n]?$|\z)',
timeout: Banzai::Filter::Concerns::TimeoutFilterHandler::RENDER_TIMEOUT)

Instance Method Summary collapse

Instance Method Details

#callObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/banzai/filter/include_filter.rb', line 20

def call
  return text unless wiki? || blob?

  @included_content = {}
  @total_included = 0

  @text = Gitlab::Utils::Gsub.gsub_with_limit(@text, REGEX, limit: max_includes) do |match_data|
    filename = match_data[:include]

    if filename
      read_lines(filename)
    else
      match_data[0]
    end
  end

  @text = Banzai::Filter::TruncateSourceFilter.new(@text, context).call if @total_included > 0

  @text
end