class Jekyll::Tags::IncludeTag

def add_include_to_dependency(site, path, context)

def add_include_to_dependency(site, path, context)
  if context.registers[:page]&.key?("path")
    site.regenerator.add_dependency(
      site.in_source_dir(context.registers[:page]["path"]),
      path
    )
  end
end

def could_not_locate_message(file, includes_dirs, safe)

def could_not_locate_message(file, includes_dirs, safe)
  message = "Could not locate the included file '#{file}' in any of #{includes_dirs}. " \
            "Ensure it exists in one of those directories and"
  message + if safe
              " is not a symlink as those are not allowed in safe mode."
            else
              ", if it is a symlink, does not point outside your site source."
            end
end

def file_read_opts(context)

Grab file read opts in the context
def file_read_opts(context)
  context.registers[:site].file_read_opts
end

def initialize(tag_name, markup, tokens)

def initialize(tag_name, markup, tokens)
  super
  markup  = markup.strip
  matched = markup.match(VARIABLE_SYNTAX)
  if matched
    @file = matched["variable"].strip
    @params = matched["params"].strip
  else
    @file, @params = markup.split(%r!\s+!, 2)
  end
  validate_params if @params
  @tag_name = tag_name
end

def load_cached_partial(path, context)

def load_cached_partial(path, context)
  context.registers[:cached_partials] ||= {}
  cached_partial = context.registers[:cached_partials]
  if cached_partial.key?(path)
    cached_partial[path]
  else
    unparsed_file = context.registers[:site]
      .liquid_renderer
      .file(path)
    begin
      cached_partial[path] = unparsed_file.parse(read_file(path, context))
    rescue Liquid::Error => e
      e.template_name = path
      e.markup_context = "included " if e.markup_context.nil?
      raise e
    end
  end
end

def locate_include_file(context, file, safe)

def locate_include_file(context, file, safe)
  includes_dirs = tag_includes_dirs(context)
  includes_dirs.each do |dir|
    path = PathManager.join(dir, file)
    return path if valid_include_file?(path, dir.to_s, safe)
  end
  raise IOError, could_not_locate_message(file, includes_dirs, safe)
end

def outside_site_source?(path, dir, safe)

def outside_site_source?(path, dir, safe)
  safe && !realpath_prefixed_with?(path, dir)
end

def parse_params(context)

def parse_params(context)
  params = {}
  @params.scan(VALID_SYNTAX) do |key, d_quoted, s_quoted, variable|
    value = if d_quoted
              d_quoted.include?('\\"') ? d_quoted.gsub('\\"', '"') : d_quoted
            elsif s_quoted
              s_quoted.include?("\\'") ? s_quoted.gsub("\\'", "'") : s_quoted
            elsif variable
              context[variable]
            end
    params[key] = value
  end
  params
end

def read_file(file, context)

This method allows to modify the file content by inheriting from the class.
def read_file(file, context)
  File.read(file, **file_read_opts(context))
end

def realpath_prefixed_with?(path, dir)

def realpath_prefixed_with?(path, dir)
  File.exist?(path) && File.realpath(path).start_with?(dir)
rescue StandardError
  false
end

def render(context)

def render(context)
  site = context.registers[:site]
  file = render_variable(context) || @file
  validate_file_name(file)
  path = locate_include_file(context, file, site.safe)
  return unless path
  add_include_to_dependency(site, path, context)
  partial = load_cached_partial(path, context)
  context.stack do
    context["include"] = parse_params(context) if @params
    begin
      partial.render!(context)
    rescue Liquid::Error => e
      e.template_name = path
      e.markup_context = "included " if e.markup_context.nil?
      raise e
    end
  end
end

def render_variable(context)

Render the variable if required
def render_variable(context)
  Liquid::Template.parse(@file).render(context) if VARIABLE_SYNTAX.match?(@file)
end

def syntax_example

def syntax_example
  "{% #{@tag_name} file.ext param='value' param2='value' %}"
end

def tag_includes_dirs(context)

def tag_includes_dirs(context)
  context.registers[:site].includes_load_paths.freeze
end

def valid_include_file?(path, dir, safe)

def valid_include_file?(path, dir, safe)
  !outside_site_source?(path, dir, safe) && File.file?(path)
end

def validate_file_name(file)

def validate_file_name(file)
  if INVALID_SEQUENCES.match?(file) || !VALID_FILENAME_CHARS.match?(file)
    raise ArgumentError, <<~MSG
      Invalid syntax for include tag. File contains invalid characters or sequences:
        #{file}
      Valid syntax:
        #{syntax_example}
    MSG
  end
end

def validate_params

def validate_params
  unless FULL_VALID_SYNTAX.match?(@params)
    raise ArgumentError, <<~MSG
      Invalid syntax for include tag:
      #{@params}
      Valid syntax:
      #{syntax_example}
    MSG
  end
end