Class: Liquid::Tags::Include

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/liquid/tags/lr_include.rb

Constant Summary collapse

SYNTAX_EXAMPLE =
"{% include 'file.ext' param='value' param2='value' %}"
VALID_SYNTAX =
/([\w-]+)\s*=\s*(?:"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)'|([\w\.-]+))/
VARIABLE_SYNTAX =
/(?<variable>[^{]*\{\{\s*(?<name>[\w\-\.]+)\s*(\|.*)?\}\}[^\s}]*)(?<params>.*)/

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ Include

Returns a new instance of Include.



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/liquid/tags/lr_include.rb', line 27

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
end

Instance Method Details

#includes_dirObject



23
24
25
# File 'lib/liquid/tags/lr_include.rb', line 23

def includes_dir
  File.join(File.realpath(Limitedrun::Themekit::Config.theme_path), Limitedrun::Themekit::Config.snippets_dir)
end

#parse_params(context) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/liquid/tags/lr_include.rb', line 39

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) ⇒ Object



127
128
129
# File 'lib/liquid/tags/lr_include.rb', line 127

def path_relative_to_source(dir, path)
  File.join(INCLUDES_DIR, path.sub(Regexp.new("^#{dir}"), ""))
end

#realpath_prefixed_with?(path, dir) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/liquid/tags/lr_include.rb', line 131

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

#render(context) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/liquid/tags/lr_include.rb', line 98

def render(context)
  dir = includes_dir

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

  path = File.join(dir, file[1...-1])
  validate_path(path, dir, true)

  begin
    partial = Liquid::Template.parse(source(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) ⇒ Object

Render the variable if required



91
92
93
94
95
96
# File 'lib/liquid/tags/lr_include.rb', line 91

def render_variable(context)
  if @file.match(VARIABLE_SYNTAX)
    partial = Liquid::Template.parse(@file)
    partial.render!(context)
  end
end

#source(file, context) ⇒ Object

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



136
137
138
# File 'lib/liquid/tags/lr_include.rb', line 136

def source(file, context)
  File.read(file)
end

#validate_file_name(file) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/liquid/tags/lr_include.rb', line 59

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_paramsObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/liquid/tags/lr_include.rb', line 74

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) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/liquid/tags/lr_include.rb', line 119

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