class Jekyll::Tags::IncludeTag

Constants

VALID_SYNTAX
VARIABLE_SYNTAX

Attributes

includes_dir[R]

Public Class Methods

new(tag_name, markup, tokens) click to toggle source
Calls superclass method
# File lib/jekyll/tags/include.rb, line 19
def initialize(tag_name, markup, tokens)
  super
  matched = markup.strip.match(VARIABLE_SYNTAX)
  if matched
    @file = matched['variable'].strip
    @params = matched['params'].strip
  else
    @file, @params = markup.strip.split(' ', 2)
  end
  validate_params if @params
  @tag_name = tag_name
end

Public Instance Methods

file_read_opts(context) click to toggle source

Grab file read opts in the context

# File lib/jekyll/tags/include.rb, line 88
def file_read_opts(context)
  context.registers[:site].file_read_opts
end
load_cached_partial(path, context) click to toggle source
# File lib/jekyll/tags/include.rb, line 135
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
    cached_partial[path] = context.registers[:site].liquid_renderer.file(path).parse(read_file(path, context))
  end
end
parse_params(context) click to toggle source
# File lib/jekyll/tags/include.rb, line 36
def parse_params(context)
  params = {}
  markup = @params

  while match = VALID_SYNTAX.match(markup) do
    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
path_relative_to_source(dir, path) click to toggle source
# File lib/jekyll/tags/include.rb, line 158
def path_relative_to_source(dir, path)
  File.join(@includes_dir, path.sub(Regexp.new("^#{dir}"), ""))
end
read_file(file, context) click to toggle source

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

# File lib/jekyll/tags/include.rb, line 167
def read_file(file, context)
  File.read(file, file_read_opts(context))
end
realpath_prefixed_with?(path, dir) click to toggle source
# File lib/jekyll/tags/include.rb, line 162
def realpath_prefixed_with?(path, dir)
  File.exist?(path) && File.realpath(path).start_with?(dir)
end
render(context) click to toggle source
# File lib/jekyll/tags/include.rb, line 104
def render(context)
  site = context.registers[:site]
  @includes_dir = tag_includes_dir(context)
  dir = resolved_includes_dir(context)

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

  path = File.join(dir, file)
  validate_path(path, dir, site.safe)

  # Add include to dependency tree
  if context.registers[:page] && context.registers[:page].key?("path")
    site.regenerator.add_dependency(
      site.in_source_dir(context.registers[:page]["path"]),
      path
    )
  end

  begin
    partial = load_cached_partial(path, context)

    context.stack do
      context['include'] = parse_params(context) if @params
      partial.render!(context)
    end
  rescue => e
    raise IncludeTagError.new e.message, File.join(@includes_dir, @file)
  end
end
render_variable(context) click to toggle source

Render the variable if required

# File lib/jekyll/tags/include.rb, line 93
def render_variable(context)
  if @file.match(VARIABLE_SYNTAX)
    partial = context.registers[:site].liquid_renderer.file("(variable)").parse(@file)
    partial.render!(context)
  end
end
resolved_includes_dir(context) click to toggle source
# File lib/jekyll/tags/include.rb, line 146
def resolved_includes_dir(context)
  context.registers[:site].in_source_dir(@includes_dir)
end
syntax_example() click to toggle source
# File lib/jekyll/tags/include.rb, line 32
def syntax_example
  "{% #{@tag_name} file.ext param='value' param2='value' %}"
end
tag_includes_dir(context) click to toggle source
# File lib/jekyll/tags/include.rb, line 100
def tag_includes_dir(context)
  context.registers[:site].config['includes_dir'].freeze
end
validate_file_name(file) click to toggle source
# File lib/jekyll/tags/include.rb, line 56
      def validate_file_name(file)
        if file !~ /^[a-zA-Z0-9_\/\.-]+$/ || file =~ /\.\// || file =~ /\/\./
          raise ArgumentError.new <<-eos
Invalid syntax for include tag. File contains invalid characters or sequences:

  #{file}

Valid syntax:

  #{syntax_example}

eos
        end
      end
validate_params() click to toggle source
# File lib/jekyll/tags/include.rb, line 71
      def validate_params
        full_valid_syntax = Regexp.compile('\A\s*(?:' + VALID_SYNTAX.to_s + '(?=\s|\z)\s*)*\z')
        unless @params =~ full_valid_syntax
          raise ArgumentError.new <<-eos
Invalid syntax for include tag:

  #{@params}

Valid syntax:

  #{syntax_example}

eos
        end
      end
validate_path(path, dir, safe) click to toggle source
# File lib/jekyll/tags/include.rb, line 150
def validate_path(path, dir, safe)
  if safe && !realpath_prefixed_with?(path, dir)
    raise IOError.new "The included file '#{path}' should exist and should not be a symlink"
  elsif !File.exist?(path)
    raise IOError.new "Included file '#{path_relative_to_source(dir, path)}' not found"
  end
end