Skip to content

Instantly share code, notes, and snippets.

@calebhearth
Created August 23, 2020 16:17
Show Gist options
  • Save calebhearth/0bacb6c19a60691586e28391531ec541 to your computer and use it in GitHub Desktop.
Save calebhearth/0bacb6c19a60691586e28391531ec541 to your computer and use it in GitHub Desktop.
class IncludeTag < Liquid::Tag
VALID_SYNTAX = %r!
([\w-]+)\s*=\s*
(?:"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)'|([\w\.-]+))
!x.freeze
VARIABLE_SYNTAX = %r!
(?<variable>[^{]*(\{\{\s*[\w\-\.]+\s*(\|.*)?\}\}[^\s{}]*)+)
(?<params>.*)
!mx.freeze
FULL_VALID_SYNTAX = %r!\A\s*(?:#{VALID_SYNTAX}(?=\s|\z)\s*)*\z!.freeze
VALID_FILENAME_CHARS = %r!^[\w/\.-]+$!.freeze
INVALID_SEQUENCES = %r![./]{2,}!.freeze
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(%r!\s+!, 2)
end
validate_params if @params
@tag_name = tag_name
end
def syntax_example
"{% #{@tag_name} file param='value' param2='value' %}"
end
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
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
unless FULL_VALID_SYNTAX.match?(@params)
raise ArgumentError, <<~MSG
Invalid syntax for include tag:
#{@params}
Valid syntax:
#{syntax_example}
MSG
end
end
# Render the variable if required
def render_variable(context)
Liquid::Template.parse(@file).render(context) if VARIABLE_SYNTAX.match?(@file)
end
def render(context)
file = render_variable(context) || @file
source = Liquid::Template.file_system.read_template_file(file)
#partial = Liquid::Template.parse(source)
context.stack do
context["include"] = parse_params(context) if @params
begin
path = Liquid::Template.file_system.full_path(file)
#partial.render!(context).html_safe
handler_for_template(path)
.call(find_template(file.sub(/\b_/, '')), source)
rescue Liquid::Error => e
e.template_name = path
e.markup_context = "included " if e.markup_context.nil?
raise e
end
end
end
def find_template(path)
lookup_context = ActionView::Base.build_lookup_context(nil)
prefixes = path.include?(?/) ? [] : lookup_context.prefixes
lookup_context.find_template(path, prefixes, true, {}, {})
end
def handler_for_template(path)
ApplicationController.new.view_paths.paths.first
.send(:extract_handler_and_format_and_variant, path)
.first
end
end
Liquid::Template.tags.delete('include')
Liquid::Template.register_tag("include", IncludeTag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment