class Bridgetown::Tags::IncludeTag

Constants

FULL_VALID_SYNTAX
INVALID_SEQUENCES
VALID_FILENAME_CHARS
VALID_SYNTAX
VARIABLE_SYNTAX

Attributes

deprecation_message_shown[RW]

Public Class Methods

new(tag_name, markup, tokens) click to toggle source
Calls superclass method
# File lib/bridgetown-core/tags/include.rb, line 23
def initialize(tag_name, markup, tokens)
  super

  unless self.class.deprecation_message_shown
    Bridgetown.logger.warn "NOTICE: the {% include %} tag is deprecated and" \
                           " will be removed in Bridgetown 1.0. You should" \
                           " use the {% render %} tag instead."
    self.class.deprecation_message_shown = true
  end

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

Public Instance Methods

add_include_to_dependency(site, path, context) click to toggle source
# File lib/bridgetown-core/tags/include.rb, line 146
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
file_read_opts(context) click to toggle source

Grab file read opts in the context

# File lib/bridgetown-core/tags/include.rb, line 99
def file_read_opts(context)
  context.registers[:site].file_read_opts
end
load_cached_partial(path, context) click to toggle source
# File lib/bridgetown-core/tags/include.rb, line 155
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
locate_include_file(context, file) click to toggle source
# File lib/bridgetown-core/tags/include.rb, line 112
def locate_include_file(context, file)
  includes_dirs = tag_includes_dirs(context)
  includes_dirs.each do |dir|
    path = File.join(dir, file)
    return path if valid_include_file?(path, dir.to_s)
  end
  raise IOError, could_not_locate_message(file, includes_dirs)
end
parse_params(context) click to toggle source
# File lib/bridgetown-core/tags/include.rb, line 48
def parse_params(context)
  params = {}
  markup = @params

  while (match = VALID_SYNTAX.match(markup))
    markup = markup[match.end(0)..-1]

    value = if match[2]
              match[2].gsub('\\"', '"')
            elsif match[3]
              match[3].gsub("\\'", "'")
            elsif match[4]
              context[match[4]]
            end

    params[match[1]] = value
  end
  params
end
read_file(file, context) click to toggle source

This method allows to modify the file content by inheriting from the class.

# File lib/bridgetown-core/tags/include.rb, line 186
def read_file(file, context)
  File.read(file, **file_read_opts(context))
end
realpath_prefixed_with?(path, dir) click to toggle source
# File lib/bridgetown-core/tags/include.rb, line 179
def realpath_prefixed_with?(path, dir)
  File.exist?(path) && File.realpath(path).start_with?(dir)
rescue StandardError
  false
end
render(context) click to toggle source
# File lib/bridgetown-core/tags/include.rb, line 121
def render(context)
  site = context.registers[:site]

  file = render_variable(context) || @file
  validate_file_name(file)

  path = locate_include_file(context, file)
  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
render_variable(context) click to toggle source

Render the variable if required

# File lib/bridgetown-core/tags/include.rb, line 104
def render_variable(context)
  Liquid::Template.parse(@file).render(context) if VARIABLE_SYNTAX.match?(@file)
end
syntax_example() click to toggle source
# File lib/bridgetown-core/tags/include.rb, line 44
def syntax_example
  "{% #{@tag_name} file.ext param='value' param2='value' %}"
end
tag_includes_dirs(context) click to toggle source
# File lib/bridgetown-core/tags/include.rb, line 108
def tag_includes_dirs(context)
  context.registers[:site].includes_load_paths.freeze
end
valid_include_file?(path, _dir) click to toggle source
# File lib/bridgetown-core/tags/include.rb, line 175
def valid_include_file?(path, _dir)
  File.file?(path)
end
validate_file_name(file) click to toggle source
# File lib/bridgetown-core/tags/include.rb, line 68
      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
validate_params() click to toggle source
# File lib/bridgetown-core/tags/include.rb, line 83
      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

Private Instance Methods

could_not_locate_message(file, includes_dirs) click to toggle source
# File lib/bridgetown-core/tags/include.rb, line 192
def could_not_locate_message(file, includes_dirs)
  "Could not locate the included file '#{file}' in any of #{includes_dirs}." \
    " Ensure it exists in one of those directories."
end