mirror of
https://github.com/Shopify/liquid.git
synced 2026-10-03 00:55:11 -07:00
extract name
This commit is contained in:
+42
-34
@@ -21,47 +21,55 @@ static VALUE rb_variable_allocate(VALUE klass)
|
|||||||
static int skip_whitespace(char * str, int len)
|
static int skip_whitespace(char * str, int len)
|
||||||
{
|
{
|
||||||
int i = 0; char * ptr = str;
|
int i = 0; char * ptr = str;
|
||||||
while (i < len && !isspace(*ptr))
|
while (i < len && isspace(*ptr))
|
||||||
i++;
|
{i++; ptr++;}
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
static char * cpy_string(char * str, int len)
|
static char * get_quoted_fragment(char * cursor, int len, VALUE self)
|
||||||
{
|
{
|
||||||
char * s = malloc(len*sizeof(char) + 1);
|
int count = 0; int start = -1, end = -1; char quoted = -1;
|
||||||
int i = 0;
|
|
||||||
while (i<len) s[i] = str[i];
|
|
||||||
s[i] = '\0';
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
static char * get_quoted_fragment(char * cursor, int len, char * name)
|
|
||||||
{
|
|
||||||
int count = 0; int start = -1, end = -1;
|
|
||||||
while (count < len) {
|
while (count < len) {
|
||||||
|
|
||||||
switch (cursor[count]) {
|
switch (cursor[count]) {
|
||||||
case '"':
|
case '"':
|
||||||
if (start == -1) start = count;
|
if (start == -1) {start = count; quoted = '"';}
|
||||||
else {end = count+1; goto form_name;}
|
else if (cursor[start] == '"') {end = count; goto form_name;}
|
||||||
|
else if (quoted == -1) quoted = '"';
|
||||||
|
else if (quoted == '"') quoted = -1;
|
||||||
break;
|
break;
|
||||||
case '\'':
|
case '\'':
|
||||||
if (start == -1) start = count;
|
if (start == -1) {start = count; quoted = '\'';}
|
||||||
else {end = count+1; goto form_name;}
|
else if (cursor[start] == '\'') {end = count; goto form_name;}
|
||||||
|
else if (quoted == -1) quoted = '\'';
|
||||||
|
else if (quoted == '\'') quoted = -1;
|
||||||
|
break;
|
||||||
|
case '|':
|
||||||
|
case ',':
|
||||||
|
case '\n':
|
||||||
|
case '\r':
|
||||||
|
case '\f':
|
||||||
|
case '\t':
|
||||||
|
case '\v':
|
||||||
|
case ' ':
|
||||||
|
if (start != -1 && quoted == -1) {end = count-1; goto form_name;}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (cursor[count] != '|' && cursor[count] != ':' && cursor[count] != ',' && cursor[count] != ' ' && !isspace(cursor[count]))
|
if (start == -1) start = count;
|
||||||
{ if (start == -1) start = count; }
|
break;
|
||||||
else
|
|
||||||
{ end = count+1; goto form_name;}
|
|
||||||
}
|
}
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
form_name:
|
if (count == len && start != -1 && end == -1) end = len-1;
|
||||||
if (end > start) name = cpy_string(&cursor[start], end-start);
|
|
||||||
|
|
||||||
if (end != -1) return &cursor[end];
|
form_name:
|
||||||
else return NULL;
|
if (end > start) {
|
||||||
|
rb_iv_set(self, "@name", rb_str_new(&cursor[start], end-start+1));
|
||||||
|
return &cursor[end+1];
|
||||||
|
} else {
|
||||||
|
rb_iv_set(self, "@name", Qnil);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void rb_variable_lax_parse_new(VALUE self, VALUE m)
|
static void rb_variable_lax_parse_new(VALUE self, VALUE m)
|
||||||
@@ -72,17 +80,17 @@ static void rb_variable_lax_parse_new(VALUE self, VALUE m)
|
|||||||
char * cursor = markup; int count = 0;
|
char * cursor = markup; int count = 0;
|
||||||
|
|
||||||
/* Extract name */
|
/* Extract name */
|
||||||
char * name;
|
count += skip_whitespace(markup, markup_len);
|
||||||
count += skip_whitespace(markup, markup_len); cursor = markup+count;
|
cursor = markup+count;
|
||||||
cursor = get_quoted_fragment(cursor, markup_len-count, name);
|
cursor = get_quoted_fragment(cursor, markup_len-count, self);
|
||||||
|
|
||||||
if (name == NULL) rb_iv_set(self, "@name", Qnil);
|
// if (*name == NULL) rb_iv_set(self, "@name", Qnil);
|
||||||
else
|
// else
|
||||||
{
|
// {
|
||||||
rb_iv_set(self, "@name", rb_str_new2(name));
|
// rb_iv_set(self, "@name", rb_str_new2(*name));
|
||||||
|
|
||||||
/* Extract filters */
|
// /* Extract filters */
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
void init_liquid_variable()
|
void init_liquid_variable()
|
||||||
|
|||||||
+11
-11
@@ -21,18 +21,18 @@ module Liquid
|
|||||||
@options = options || {}
|
@options = options || {}
|
||||||
|
|
||||||
|
|
||||||
case @options[:error_mode] || Template.error_mode
|
# case @options[:error_mode] || Template.error_mode
|
||||||
when :strict then strict_parse(markup)
|
# when :strict then strict_parse(markup)
|
||||||
when :lax then lax_parse(markup)
|
# when :lax then lax_parse(markup)
|
||||||
when :warn
|
# when :warn
|
||||||
begin
|
# begin
|
||||||
strict_parse(markup)
|
# strict_parse(markup)
|
||||||
rescue SyntaxError => e
|
# rescue SyntaxError => e
|
||||||
@warnings ||= []
|
# @warnings ||= []
|
||||||
@warnings << e
|
# @warnings << e
|
||||||
lax_parse(markup)
|
lax_parse(markup)
|
||||||
end
|
# end
|
||||||
end
|
# end
|
||||||
end
|
end
|
||||||
|
|
||||||
# def lax_parse(markup)
|
# def lax_parse(markup)
|
||||||
|
|||||||
@@ -3,72 +3,72 @@ require 'test_helper'
|
|||||||
class VariableTest < Test::Unit::TestCase
|
class VariableTest < Test::Unit::TestCase
|
||||||
include Liquid
|
include Liquid
|
||||||
|
|
||||||
# def test_variable
|
def test_variable
|
||||||
# var = Variable.new('hello')
|
var = Variable.new('hello')
|
||||||
# assert_equal 'hello', var.name
|
assert_equal 'hello', var.name
|
||||||
# end
|
end
|
||||||
|
|
||||||
# def test_filters
|
def test_filters
|
||||||
# var = Variable.new('hello | textileze')
|
var = Variable.new('hello | textileze')
|
||||||
# assert_equal 'hello', var.name
|
assert_equal 'hello', var.name
|
||||||
# assert_equal [["textileze",[]]], var.filters
|
# assert_equal [["textileze",[]]], var.filters
|
||||||
|
|
||||||
# var = Variable.new('hello | textileze | paragraph')
|
var = Variable.new('hello | textileze | paragraph')
|
||||||
# assert_equal 'hello', var.name
|
assert_equal 'hello', var.name
|
||||||
# assert_equal [["textileze",[]], ["paragraph",[]]], var.filters
|
# assert_equal [["textileze",[]], ["paragraph",[]]], var.filters
|
||||||
|
|
||||||
# var = Variable.new(%! hello | strftime: '%Y'!)
|
var = Variable.new(%! hello | strftime: '%Y'!)
|
||||||
# assert_equal 'hello', var.name
|
assert_equal 'hello', var.name
|
||||||
# assert_equal [["strftime",["'%Y'"]]], var.filters
|
# assert_equal [["strftime",["'%Y'"]]], var.filters
|
||||||
|
|
||||||
# var = Variable.new(%! 'typo' | link_to: 'Typo', true !)
|
var = Variable.new(%! 'typo' | link_to: 'Typo', true !)
|
||||||
# assert_equal %!'typo'!, var.name
|
assert_equal %!'typo'!, var.name
|
||||||
# assert_equal [["link_to",["'Typo'", "true"]]], var.filters
|
# assert_equal [["link_to",["'Typo'", "true"]]], var.filters
|
||||||
|
|
||||||
# var = Variable.new(%! 'typo' | link_to: 'Typo', false !)
|
var = Variable.new(%! 'typo' | link_to: 'Typo', false !)
|
||||||
# assert_equal %!'typo'!, var.name
|
assert_equal %!'typo'!, var.name
|
||||||
# assert_equal [["link_to",["'Typo'", "false"]]], var.filters
|
# assert_equal [["link_to",["'Typo'", "false"]]], var.filters
|
||||||
|
|
||||||
# var = Variable.new(%! 'foo' | repeat: 3 !)
|
var = Variable.new(%! 'foo' | repeat: 3 !)
|
||||||
# assert_equal %!'foo'!, var.name
|
assert_equal %!'foo'!, var.name
|
||||||
# assert_equal [["repeat",["3"]]], var.filters
|
# assert_equal [["repeat",["3"]]], var.filters
|
||||||
|
|
||||||
# var = Variable.new(%! 'foo' | repeat: 3, 3 !)
|
var = Variable.new(%! 'foo' | repeat: 3, 3 !)
|
||||||
# assert_equal %!'foo'!, var.name
|
assert_equal %!'foo'!, var.name
|
||||||
# assert_equal [["repeat",["3","3"]]], var.filters
|
# assert_equal [["repeat",["3","3"]]], var.filters
|
||||||
|
|
||||||
# var = Variable.new(%! 'foo' | repeat: 3, 3, 3 !)
|
var = Variable.new(%! 'foo' | repeat: 3, 3, 3 !)
|
||||||
# assert_equal %!'foo'!, var.name
|
assert_equal %!'foo'!, var.name
|
||||||
# assert_equal [["repeat",["3","3","3"]]], var.filters
|
# assert_equal [["repeat",["3","3","3"]]], var.filters
|
||||||
|
|
||||||
# var = Variable.new(%! hello | strftime: '%Y, okay?'!)
|
var = Variable.new(%! hello | strftime: '%Y, okay?'!)
|
||||||
# assert_equal 'hello', var.name
|
assert_equal 'hello', var.name
|
||||||
# assert_equal [["strftime",["'%Y, okay?'"]]], var.filters
|
# assert_equal [["strftime",["'%Y, okay?'"]]], var.filters
|
||||||
|
|
||||||
# var = Variable.new(%! hello | things: "%Y, okay?", 'the other one'!)
|
var = Variable.new(%! hello | things: "%Y, okay?", 'the other one'!)
|
||||||
# assert_equal 'hello', var.name
|
assert_equal 'hello', var.name
|
||||||
# assert_equal [["things",["\"%Y, okay?\"","'the other one'"]]], var.filters
|
# assert_equal [["things",["\"%Y, okay?\"","'the other one'"]]], var.filters
|
||||||
# end
|
end
|
||||||
|
|
||||||
# def test_filter_with_date_parameter
|
def test_filter_with_date_parameter
|
||||||
# var = Variable.new(%! '2006-06-06' | date: "%m/%d/%Y"!)
|
var = Variable.new(%! '2006-06-06' | date: "%m/%d/%Y"!)
|
||||||
# assert_equal "'2006-06-06'", var.name
|
assert_equal "'2006-06-06'", var.name
|
||||||
# assert_equal [["date",["\"%m/%d/%Y\""]]], var.filters
|
# assert_equal [["date",["\"%m/%d/%Y\""]]], var.filters
|
||||||
# end
|
end
|
||||||
|
|
||||||
# def test_filters_without_whitespace
|
def test_filters_without_whitespace
|
||||||
# var = Variable.new('hello | textileze | paragraph')
|
var = Variable.new('hello | textileze | paragraph')
|
||||||
# assert_equal 'hello', var.name
|
assert_equal 'hello', var.name
|
||||||
# assert_equal [["textileze",[]], ["paragraph",[]]], var.filters
|
# assert_equal [["textileze",[]], ["paragraph",[]]], var.filters
|
||||||
|
|
||||||
# var = Variable.new('hello|textileze|paragraph')
|
var = Variable.new('hello|textileze|paragraph')
|
||||||
# assert_equal 'hello', var.name
|
assert_equal 'hello', var.name
|
||||||
# assert_equal [["textileze",[]], ["paragraph",[]]], var.filters
|
# assert_equal [["textileze",[]], ["paragraph",[]]], var.filters
|
||||||
|
|
||||||
# var = Variable.new("hello|replace:'foo','bar'|textileze")
|
var = Variable.new("hello|replace:'foo','bar'|textileze")
|
||||||
# assert_equal 'hello', var.name
|
assert_equal 'hello', var.name
|
||||||
# assert_equal [["replace", ["'foo'", "'bar'"]], ["textileze", []]], var.filters
|
# assert_equal [["replace", ["'foo'", "'bar'"]], ["textileze", []]], var.filters
|
||||||
# end
|
end
|
||||||
|
|
||||||
def test_symbol
|
def test_symbol
|
||||||
var = Variable.new("http://disney.com/logo.gif | image: 'med'", :error_mode => :lax)
|
var = Variable.new("http://disney.com/logo.gif | image: 'med'", :error_mode => :lax)
|
||||||
@@ -76,51 +76,51 @@ class VariableTest < Test::Unit::TestCase
|
|||||||
# assert_equal [["image",["'med'"]]], var.filters
|
# assert_equal [["image",["'med'"]]], var.filters
|
||||||
end
|
end
|
||||||
|
|
||||||
# def test_string_to_filter
|
def test_string_to_filter
|
||||||
# var = Variable.new("'http://disney.com/logo.gif' | image: 'med' ")
|
var = Variable.new("'http://disney.com/logo.gif' | image: 'med' ")
|
||||||
# assert_equal "'http://disney.com/logo.gif'", var.name
|
assert_equal "'http://disney.com/logo.gif'", var.name
|
||||||
# assert_equal [["image",["'med'"]]], var.filters
|
# assert_equal [["image",["'med'"]]], var.filters
|
||||||
# end
|
end
|
||||||
|
|
||||||
# def test_string_single_quoted
|
def test_string_single_quoted
|
||||||
# var = Variable.new(%| "hello" |)
|
var = Variable.new(%| "hello" |)
|
||||||
# assert_equal '"hello"', var.name
|
assert_equal '"hello"', var.name
|
||||||
# end
|
end
|
||||||
|
|
||||||
# def test_string_double_quoted
|
def test_string_double_quoted
|
||||||
# var = Variable.new(%| 'hello' |)
|
var = Variable.new(%| 'hello' |)
|
||||||
# assert_equal "'hello'", var.name
|
assert_equal "'hello'", var.name
|
||||||
# end
|
end
|
||||||
|
|
||||||
# def test_integer
|
def test_integer
|
||||||
# var = Variable.new(%| 1000 |)
|
var = Variable.new(%| 1000 |)
|
||||||
# assert_equal "1000", var.name
|
assert_equal "1000", var.name
|
||||||
# end
|
end
|
||||||
|
|
||||||
# def test_float
|
def test_float
|
||||||
# var = Variable.new(%| 1000.01 |)
|
var = Variable.new(%| 1000.01 |)
|
||||||
# assert_equal "1000.01", var.name
|
assert_equal "1000.01", var.name
|
||||||
# end
|
end
|
||||||
|
|
||||||
# def test_string_with_special_chars
|
def test_string_with_special_chars
|
||||||
# var = Variable.new(%| 'hello! $!@.;"ddasd" ' |)
|
var = Variable.new(%| 'hello! $!@.;"ddasd" ' |)
|
||||||
# assert_equal %|'hello! $!@.;"ddasd" '|, var.name
|
assert_equal %|'hello! $!@.;"ddasd" '|, var.name
|
||||||
# end
|
end
|
||||||
|
|
||||||
# def test_string_dot
|
def test_string_dot
|
||||||
# var = Variable.new(%| test.test |)
|
var = Variable.new(%| test.test |)
|
||||||
# assert_equal 'test.test', var.name
|
assert_equal 'test.test', var.name
|
||||||
# end
|
end
|
||||||
|
|
||||||
# def test_filter_with_keyword_arguments
|
def test_filter_with_keyword_arguments
|
||||||
# var = Variable.new(%! hello | things: greeting: "world", farewell: 'goodbye'!)
|
var = Variable.new(%! hello | things: greeting: "world", farewell: 'goodbye'!)
|
||||||
# assert_equal 'hello', var.name
|
assert_equal 'hello', var.name
|
||||||
# assert_equal [['things',["greeting: \"world\"","farewell: 'goodbye'"]]], var.filters
|
# assert_equal [['things',["greeting: \"world\"","farewell: 'goodbye'"]]], var.filters
|
||||||
# end
|
end
|
||||||
|
|
||||||
def test_lax_filter_argument_parsing
|
def test_lax_filter_argument_parsing
|
||||||
# var = Variable.new(%! number_of_comments | pluralize: 'comment': 'comments' !, :error_mode => :lax)
|
var = Variable.new(%! number_of_comments | pluralize: 'comment': 'comments' !, :error_mode => :lax)
|
||||||
# assert_equal 'number_of_comments', var.name
|
assert_equal 'number_of_comments', var.name
|
||||||
# assert_equal [['pluralize',["'comment'","'comments'"]]], var.filters
|
# assert_equal [['pluralize',["'comment'","'comments'"]]], var.filters
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user