extract name

This commit is contained in:
Isha
2014-03-02 23:24:31 +00:00
parent a119e86fd0
commit 02525f258e
3 changed files with 142 additions and 134 deletions

View File

@@ -21,47 +21,55 @@ static VALUE rb_variable_allocate(VALUE klass)
static int skip_whitespace(char * str, int len)
{
int i = 0; char * ptr = str;
while (i < len && !isspace(*ptr))
i++;
while (i < len && isspace(*ptr))
{i++; ptr++;}
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 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;
int count = 0; int start = -1, end = -1; char quoted = -1;
while (count < len) {
switch (cursor[count]) {
case '"':
if (start == -1) start = count;
else {end = count+1; goto form_name;}
if (start == -1) {start = count; quoted = '"';}
else if (cursor[start] == '"') {end = count; goto form_name;}
else if (quoted == -1) quoted = '"';
else if (quoted == '"') quoted = -1;
break;
case '\'':
if (start == -1) start = count;
else {end = count+1; goto form_name;}
if (start == -1) {start = count; quoted = '\'';}
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;
default:
if (cursor[count] != '|' && cursor[count] != ':' && cursor[count] != ',' && cursor[count] != ' ' && !isspace(cursor[count]))
{ if (start == -1) start = count; }
else
{ end = count+1; goto form_name;}
if (start == -1) start = count;
break;
}
count++;
}
form_name:
if (end > start) name = cpy_string(&cursor[start], end-start);
if (count == len && start != -1 && end == -1) end = len-1;
if (end != -1) return &cursor[end];
else return NULL;
form_name:
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)
@@ -70,19 +78,19 @@ static void rb_variable_lax_parse_new(VALUE self, VALUE m)
int markup_len = RSTRING_LEN(m);
char * cursor = markup; int count = 0;
/* Extract name */
char * name;
count += skip_whitespace(markup, markup_len); cursor = markup+count;
cursor = get_quoted_fragment(cursor, markup_len-count, name);
count += skip_whitespace(markup, markup_len);
cursor = markup+count;
cursor = get_quoted_fragment(cursor, markup_len-count, self);
if (name == NULL) rb_iv_set(self, "@name", Qnil);
else
{
rb_iv_set(self, "@name", rb_str_new2(name));
// if (*name == NULL) rb_iv_set(self, "@name", Qnil);
// else
// {
// rb_iv_set(self, "@name", rb_str_new2(*name));
/* Extract filters */
}
// /* Extract filters */
// }
}
void init_liquid_variable()

View File

@@ -21,18 +21,18 @@ module Liquid
@options = options || {}
case @options[:error_mode] || Template.error_mode
when :strict then strict_parse(markup)
when :lax then lax_parse(markup)
when :warn
begin
strict_parse(markup)
rescue SyntaxError => e
@warnings ||= []
@warnings << e
# case @options[:error_mode] || Template.error_mode
# when :strict then strict_parse(markup)
# when :lax then lax_parse(markup)
# when :warn
# begin
# strict_parse(markup)
# rescue SyntaxError => e
# @warnings ||= []
# @warnings << e
lax_parse(markup)
end
end
# end
# end
end
# def lax_parse(markup)

View File

@@ -3,124 +3,124 @@ require 'test_helper'
class VariableTest < Test::Unit::TestCase
include Liquid
# def test_variable
# var = Variable.new('hello')
# assert_equal 'hello', var.name
# end
def test_variable
var = Variable.new('hello')
assert_equal 'hello', var.name
end
# def test_filters
# var = Variable.new('hello | textileze')
# assert_equal 'hello', var.name
# assert_equal [["textileze",[]]], var.filters
def test_filters
var = Variable.new('hello | textileze')
assert_equal 'hello', var.name
# assert_equal [["textileze",[]]], var.filters
# var = Variable.new('hello | textileze | paragraph')
# assert_equal 'hello', var.name
# assert_equal [["textileze",[]], ["paragraph",[]]], var.filters
var = Variable.new('hello | textileze | paragraph')
assert_equal 'hello', var.name
# assert_equal [["textileze",[]], ["paragraph",[]]], var.filters
# var = Variable.new(%! hello | strftime: '%Y'!)
# assert_equal 'hello', var.name
# assert_equal [["strftime",["'%Y'"]]], var.filters
var = Variable.new(%! hello | strftime: '%Y'!)
assert_equal 'hello', var.name
# assert_equal [["strftime",["'%Y'"]]], var.filters
# var = Variable.new(%! 'typo' | link_to: 'Typo', true !)
# assert_equal %!'typo'!, var.name
# assert_equal [["link_to",["'Typo'", "true"]]], var.filters
var = Variable.new(%! 'typo' | link_to: 'Typo', true !)
assert_equal %!'typo'!, var.name
# assert_equal [["link_to",["'Typo'", "true"]]], var.filters
# var = Variable.new(%! 'typo' | link_to: 'Typo', false !)
# assert_equal %!'typo'!, var.name
# assert_equal [["link_to",["'Typo'", "false"]]], var.filters
var = Variable.new(%! 'typo' | link_to: 'Typo', false !)
assert_equal %!'typo'!, var.name
# assert_equal [["link_to",["'Typo'", "false"]]], var.filters
# var = Variable.new(%! 'foo' | repeat: 3 !)
# assert_equal %!'foo'!, var.name
# assert_equal [["repeat",["3"]]], var.filters
var = Variable.new(%! 'foo' | repeat: 3 !)
assert_equal %!'foo'!, var.name
# assert_equal [["repeat",["3"]]], var.filters
# var = Variable.new(%! 'foo' | repeat: 3, 3 !)
# assert_equal %!'foo'!, var.name
# assert_equal [["repeat",["3","3"]]], var.filters
var = Variable.new(%! 'foo' | repeat: 3, 3 !)
assert_equal %!'foo'!, var.name
# assert_equal [["repeat",["3","3"]]], var.filters
# var = Variable.new(%! 'foo' | repeat: 3, 3, 3 !)
# assert_equal %!'foo'!, var.name
# assert_equal [["repeat",["3","3","3"]]], var.filters
var = Variable.new(%! 'foo' | repeat: 3, 3, 3 !)
assert_equal %!'foo'!, var.name
# assert_equal [["repeat",["3","3","3"]]], var.filters
# var = Variable.new(%! hello | strftime: '%Y, okay?'!)
# assert_equal 'hello', var.name
# assert_equal [["strftime",["'%Y, okay?'"]]], var.filters
var = Variable.new(%! hello | strftime: '%Y, okay?'!)
assert_equal 'hello', var.name
# assert_equal [["strftime",["'%Y, okay?'"]]], var.filters
# var = Variable.new(%! hello | things: "%Y, okay?", 'the other one'!)
# assert_equal 'hello', var.name
var = Variable.new(%! hello | things: "%Y, okay?", 'the other one'!)
assert_equal 'hello', var.name
# assert_equal [["things",["\"%Y, okay?\"","'the other one'"]]], var.filters
# end
end
# def test_filter_with_date_parameter
# var = Variable.new(%! '2006-06-06' | date: "%m/%d/%Y"!)
# assert_equal "'2006-06-06'", var.name
# assert_equal [["date",["\"%m/%d/%Y\""]]], var.filters
# end
def test_filter_with_date_parameter
var = Variable.new(%! '2006-06-06' | date: "%m/%d/%Y"!)
assert_equal "'2006-06-06'", var.name
# assert_equal [["date",["\"%m/%d/%Y\""]]], var.filters
end
# def test_filters_without_whitespace
# var = Variable.new('hello | textileze | paragraph')
# assert_equal 'hello', var.name
# assert_equal [["textileze",[]], ["paragraph",[]]], var.filters
def test_filters_without_whitespace
var = Variable.new('hello | textileze | paragraph')
assert_equal 'hello', var.name
# assert_equal [["textileze",[]], ["paragraph",[]]], var.filters
# var = Variable.new('hello|textileze|paragraph')
# assert_equal 'hello', var.name
# assert_equal [["textileze",[]], ["paragraph",[]]], var.filters
var = Variable.new('hello|textileze|paragraph')
assert_equal 'hello', var.name
# assert_equal [["textileze",[]], ["paragraph",[]]], var.filters
# var = Variable.new("hello|replace:'foo','bar'|textileze")
# assert_equal 'hello', var.name
# assert_equal [["replace", ["'foo'", "'bar'"]], ["textileze", []]], var.filters
# end
var = Variable.new("hello|replace:'foo','bar'|textileze")
assert_equal 'hello', var.name
# assert_equal [["replace", ["'foo'", "'bar'"]], ["textileze", []]], var.filters
end
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)
assert_equal "http://disney.com/logo.gif", var.name
# assert_equal [["image",["'med'"]]], var.filters
end
# def test_string_to_filter
# var = Variable.new("'http://disney.com/logo.gif' | image: 'med' ")
# assert_equal "'http://disney.com/logo.gif'", var.name
# assert_equal [["image",["'med'"]]], var.filters
# end
def test_string_to_filter
var = Variable.new("'http://disney.com/logo.gif' | image: 'med' ")
assert_equal "'http://disney.com/logo.gif'", var.name
# assert_equal [["image",["'med'"]]], var.filters
end
# def test_string_single_quoted
# var = Variable.new(%| "hello" |)
# assert_equal '"hello"', var.name
# end
def test_string_single_quoted
var = Variable.new(%| "hello" |)
assert_equal '"hello"', var.name
end
# def test_string_double_quoted
# var = Variable.new(%| 'hello' |)
# assert_equal "'hello'", var.name
# end
def test_string_double_quoted
var = Variable.new(%| 'hello' |)
assert_equal "'hello'", var.name
end
# def test_integer
# var = Variable.new(%| 1000 |)
# assert_equal "1000", var.name
# end
def test_integer
var = Variable.new(%| 1000 |)
assert_equal "1000", var.name
end
# def test_float
# var = Variable.new(%| 1000.01 |)
# assert_equal "1000.01", var.name
# end
def test_float
var = Variable.new(%| 1000.01 |)
assert_equal "1000.01", var.name
end
# def test_string_with_special_chars
# var = Variable.new(%| 'hello! $!@.;"ddasd" ' |)
# assert_equal %|'hello! $!@.;"ddasd" '|, var.name
# end
def test_string_with_special_chars
var = Variable.new(%| 'hello! $!@.;"ddasd" ' |)
assert_equal %|'hello! $!@.;"ddasd" '|, var.name
end
# def test_string_dot
# var = Variable.new(%| test.test |)
# assert_equal 'test.test', var.name
# end
def test_string_dot
var = Variable.new(%| test.test |)
assert_equal 'test.test', var.name
end
# def test_filter_with_keyword_arguments
# var = Variable.new(%! hello | things: greeting: "world", farewell: 'goodbye'!)
# assert_equal 'hello', var.name
# assert_equal [['things',["greeting: \"world\"","farewell: 'goodbye'"]]], var.filters
# end
def test_filter_with_keyword_arguments
var = Variable.new(%! hello | things: greeting: "world", farewell: 'goodbye'!)
assert_equal 'hello', var.name
# assert_equal [['things',["greeting: \"world\"","farewell: 'goodbye'"]]], var.filters
end
def test_lax_filter_argument_parsing
# var = Variable.new(%! number_of_comments | pluralize: 'comment': 'comments' !, :error_mode => :lax)
# assert_equal 'number_of_comments', var.name
var = Variable.new(%! number_of_comments | pluralize: 'comment': 'comments' !, :error_mode => :lax)
assert_equal 'number_of_comments', var.name
# assert_equal [['pluralize',["'comment'","'comments'"]]], var.filters
end