From f612330565f55fa4ecc52b1fe57a71e8832f9691 Mon Sep 17 00:00:00 2001 From: "maul.esel" Date: Fri, 24 May 2013 16:02:49 +0200 Subject: [PATCH] add support for parameters to {% include %} tags --- lib/jekyll/tags/include.rb | 66 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/lib/jekyll/tags/include.rb b/lib/jekyll/tags/include.rb index 3c2fa984..c4afac46 100644 --- a/lib/jekyll/tags/include.rb +++ b/lib/jekyll/tags/include.rb @@ -1,9 +1,70 @@ module Jekyll module Tags class IncludeTag < Liquid::Tag - def initialize(tag_name, file, tokens) + def initialize(tag_name, markup, tokens) super - @file = file.strip + markup.strip! + if markup.include?(' ') + separator = markup.index(' ') + @file = markup[0..separator].strip + parse_params(markup[separator..-1]) + else + @file = markup + end + end + + def parse_params(markup) + last_space = last_quote = pos = 0 + last_key = nil + in_quotes = false + @params = {} + + if !(/^(\s*\b\S+="(?:\\"|.)*?")*\s*$/ =~ markup) + raise SyntaxError.new <<-eos +Syntax error for 'include' while parsing the following markup: + + #{markup} + +Valid syntax: include param="value" +eos + end + + while pos = markup.index(/[=\"\s]/, pos) + str = markup[pos, 1] + if /\s/ =~ str + last_space = pos + elsif str == '=' + if !last_key.nil? + raise SyntaxError.new <<-eos +Syntax Error in tag 'include' (missing value) while parsing the following markup: + + #{markup} + +Valid syntax: include param="value" +eos + end + last_key = markup[last_space+1..pos-1] + elsif str == '"' and markup[pos-1, 1] != '\\' + in_quotes = !in_quotes + if !in_quotes + value = markup[last_quote+1..pos-1].gsub(/\\"/, '"') + @params[last_key] = value + last_key = nil + end + last_quote = pos + end + pos += 1 + end + + if in_quotes + raise SyntaxError.new <<-eos +Syntax Error in tag 'include' (unterminated value) while parsing the following markup: + + #{markup} + +Valid syntax: include param="value" +eos + end end def render(context) @@ -22,6 +83,7 @@ module Jekyll if choices.include?(@file) source = File.read(@file) partial = Liquid::Template.parse(source) + context['include'] = @params context.stack do partial.render(context) end