This commit is contained in:
Isha
2014-02-28 12:24:37 -05:00
parent 25cc69c3c0
commit 8f42e50959
3 changed files with 292 additions and 127 deletions
+145
View File
@@ -0,0 +1,145 @@
#include "liquid_ext.h"
VALUE cLiquidVariable;
extern VALUE mLiquid;
static void free_variable(void *ptr)
{
struct liquid_variable *variable = ptr;
xfree(variable);
}
static VALUE rb_variable_allocate(VALUE klass)
{
VALUE obj;
struct liquid_variable *variable;
obj = Data_Make_Struct(klass, struct liquid_variable, NULL, free_variable, variable);
return obj;
}
static void rb_variable_lax_parse(VALUE self, VALUE markup)
{
struct liquid_variable *variable;
variable->markup = RSTRING_PTR(markup);
variable->markup_len = RSTRING_LEN(markup);
regex_t regex, regex_f_args;
int reti;
regmatch_t match[3], f_match[5], f_arg_match[5];
regcomp(&regex, "\\s*(((\"[^\"]*\"|'[^']*')|([^\\s,\\|'\"]|(\"[^\"]*\"|'[^']*'))+))(.*)", REG_EXTENDED | REG_ICASE);
// regcomp(&regex, "\\s*((?:(?:\"[^\"]*\"|'[^']*')|(?:[^\\s,\\|'\"]|(?:\"[^\"]*\"|'[^']*'))+))(.*)", REG_ICASE | REG_ECMASCRIPT;
reti = regexec(&regex, variable->markup, 3, match, 0);
if( !reti ){
/* Extract name */
printf("\nWith the whole expression, a matched substring %.*s is found at position %d to %d. "
" and rest at %.*s is found at position %d to %d.\n",
match[1].rm_eo - match[1].rm_so, &variable->markup[match[1].rm_so], match[1].rm_so, match[1].rm_eo,
match[2].rm_eo - match[2].rm_so, &variable->markup[match[2].rm_so], match[2].rm_so, match[2].rm_eo);
variable->name = &variable->markup[match[1].rm_so];
variable->name_len = match[1].rm_eo - match[1].rm_so;
rb_iv_set(self, "@name", rb_str_new(variable->name, variable->name_len));
/* Extract filters */
// char * cursor = &variable->markup[match[2].rm_so]; int size = match[2].rm_eo - match[2].rm_so;
// while (cursor++ < &variable->markup[match[2].rm_eo]) {
// if (*cursor == ' ' || *cursor == '\n' || *cursor == '\f' || *cursor == '\t' || *cursor == '\r' || *cursor == '\v') continue;
// else if (*cursor == '|') {
// while (cursor++ < &variable->markup[match[2].rm_eo])
// if (*cursor == ' ' || *cursor == '\n' || *cursor == '\f' || *cursor == '\t' || *cursor == '\r' || *cursor == '\v') continue;
// else goto filters_present;
// } else return;
// }
// filters_present:
// if (cursor < &variable->markup[match[2].rm_eo]) {
// regcomp(&regex, "((|)|(\\s*(((\"[^\"]*\"|'[^']*')|([^\\s,\\|'\"]|(\"[^\"]*\"|'[^']*'))+)|(,))\\s*)+)", REG_EXTENDED | REG_ICASE);
// reti = regexec(&regex, cursor, 5, f_match, 0);
// regcomp(&regex_f_args, "((:)|(,))\\s*((\\w+\\s*\\:\\s*)?((\"[^\"]*\"|'[^']*')|([^\\s,\\|'\"]|(\"[^\"]*\"|'[^']*'))+))", REG_EXTENDED | REG_ICASE);
// if ( !reti ) {
// VALUE filters_array = rb_ary_new();
// int i = 1;
// while(i < 5) {
// // VALUE filter_data = rb_ary_new();
// char * filter = f_match[i].rm_so;
// // get filtername
// // get filter args into an array
// // regexec(&regex, filter, 3, f_arg_match, 0);
// rb_ary_push(filters_array, ID2SYM(rb_intern(filter)));
// // rb_ary_push(filters_array, filter_data);
// i++;
// }
// rb_iv_set(self, "@filters", filters_array);
// }
// }
}
}
// static void rb_easy_parse(VALUE self)
// {
// struct liquid_variable *variable;
// Data_Get_Struct(self, struct liquid_variable, variable);
// regex_t regex; int reti; regmatch_t match[2];
// reti = regcomp(&regex, " *(\\w+(\\.\\w+)*) *", REG_EXTENDED);
// reti = regexec(&regex, variable->markup, 2, match, 0);
// if( !reti ){
// variable->name = &variable->markup[match[1].rm_so];
// variable->name_len = match[1].rm_eo - match[1].rm_so;
// return;
// }
// VALUE p = rb_funcall(rb_path2class("Liquid::Parser"), rb_intern("new"), 1, rb_str_new(variable->markup, variable->markup_len));
// if (rb_funcall(p, rb_intern("look"), 1, ID2SYM(rb_intern("pipe")) ))
// {
// variable->name = NULL; variable->name_len = 0;
// }
// else
// {
// VALUE val = rb_funcall(p, rb_intern("expression"), 0);
// variable->name = RSTRING_PTR(val);
// variable->name_len = RSTRING_LEN(val);
// }
// }
// static VALUE rb_variable_initialize(VALUE self, VALUE markup)
// {
// Check_Type(markup, T_STRING);
// rb_iv_set(self, "@filters", rb_ary_new());
// rb_iv_set(self, "@markup", markup);
// // FIXME need to be able to accept :error_mode parameter when creating
// VALUE val = rb_funcall(rb_path2class("Liquid::Template"), rb_intern("error_mode"), 0);
// lax_parse(self, markup);
// // if (val == ID2SYM(rb_intern("strict"))) rb_funcall(self, rb_intern("strict_parse"), 1, markup);
// // else if (val == ID2SYM(rb_intern("lax"))) lax_parse(self, markup);
// // FIXME handle :warn case
// return self;
// }
void init_liquid_variable()
{
cLiquidVariable = rb_define_class_under(mLiquid, "Variable", rb_cObject);
rb_define_alloc_func(cLiquidVariable, rb_variable_allocate);
rb_define_method(cLiquidVariable, "lax_parse", rb_variable_lax_parse, 1);
}
+22
View File
@@ -0,0 +1,22 @@
#ifndef LIQUID_VARIABLE_H
#define LIQUID_VARIABLE_H
#include <regex.h>
enum error_mode {
STRICT,
LAX,
WARN
};
struct liquid_variable {
char *markup; long markup_len;
char *name; long name_len;
enum error_mode e_mode;
char **filters;
int *filter_len;
};
void init_liquid_variable();
#endif
+125 -127
View File
@@ -3,10 +3,10 @@ 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')
@@ -50,151 +50,149 @@ class VariableTest < Test::Unit::TestCase
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"!)
# assert_equal "'2006-06-06'", var.name
# # assert_equal [["date",["\"%m/%d/%Y\""]]], var.filters
# end
var = Variable.new(%! '2006-06-06' | date: "%m/%d/%Y"!) # def test_filters_without_whitespace
assert_equal "'2006-06-06'", var.name # var = Variable.new('hello | textileze | paragraph')
assert_equal [["date",["\"%m/%d/%Y\""]]], var.filters # assert_equal 'hello', var.name
# assert_equal [["textileze",[]], ["paragraph",[]]], var.filters
end # 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|replace:'foo','bar'|textileze")
var = Variable.new('hello | textileze | paragraph') # assert_equal 'hello', var.name
assert_equal 'hello', var.name # assert_equal [["replace", ["'foo'", "'bar'"]], ["textileze", []]], var.filters
assert_equal [["textileze",[]], ["paragraph",[]]], var.filters # end
var = Variable.new('hello|textileze|paragraph') # def test_symbol
assert_equal 'hello', var.name # var = Variable.new("http://disney.com/logo.gif | image: 'med' ", :error_mode => :lax)
assert_equal [["textileze",[]], ["paragraph",[]]], var.filters # assert_equal "http://disney.com/logo.gif", var.name
# assert_equal [["image",["'med'"]]], var.filters
# end
var = Variable.new("hello|replace:'foo','bar'|textileze") # def test_string_to_filter
assert_equal 'hello', var.name # var = Variable.new("'http://disney.com/logo.gif' | image: 'med' ")
assert_equal [["replace", ["'foo'", "'bar'"]], ["textileze", []]], var.filters # assert_equal "'http://disney.com/logo.gif'", var.name
end # # assert_equal [["image",["'med'"]]], var.filters
# end
def test_symbol # def test_string_single_quoted
var = Variable.new("http://disney.com/logo.gif | image: 'med' ", :error_mode => :lax) # var = Variable.new(%| "hello" |)
assert_equal "http://disney.com/logo.gif", var.name # assert_equal '"hello"', var.name
assert_equal [["image",["'med'"]]], var.filters # end
end
def test_string_to_filter # def test_string_double_quoted
var = Variable.new("'http://disney.com/logo.gif' | image: 'med' ") # var = Variable.new(%| 'hello' |)
assert_equal "'http://disney.com/logo.gif'", var.name # assert_equal "'hello'", var.name
assert_equal [["image",["'med'"]]], var.filters # end
end
def test_string_single_quoted # def test_integer
var = Variable.new(%| "hello" |) # var = Variable.new(%| 1000 |)
assert_equal '"hello"', var.name # assert_equal "1000", var.name
end # end
def test_string_double_quoted # def test_float
var = Variable.new(%| 'hello' |) # var = Variable.new(%| 1000.01 |)
assert_equal "'hello'", var.name # assert_equal "1000.01", var.name
end # end
def test_integer # def test_string_with_special_chars
var = Variable.new(%| 1000 |) # var = Variable.new(%| 'hello! $!@.;"ddasd" ' |)
assert_equal "1000", var.name # assert_equal %|'hello! $!@.;"ddasd" '|, var.name
end # end
def test_float # def test_string_dot
var = Variable.new(%| 1000.01 |) # var = Variable.new(%| test.test |)
assert_equal "1000.01", var.name # assert_equal 'test.test', var.name
end # end
def test_string_with_special_chars # def test_filter_with_keyword_arguments
var = Variable.new(%| 'hello! $!@.;"ddasd" ' |) # var = Variable.new(%! hello | things: greeting: "world", farewell: 'goodbye'!)
assert_equal %|'hello! $!@.;"ddasd" '|, var.name # assert_equal 'hello', var.name
end # # assert_equal [['things',["greeting: \"world\"","farewell: 'goodbye'"]]], var.filters
# end
def test_string_dot # def test_lax_filter_argument_parsing
var = Variable.new(%| test.test |) # var = Variable.new(%! number_of_comments | pluralize: 'comment': 'comments' !, :error_mode => :lax)
assert_equal 'test.test', var.name # assert_equal 'number_of_comments', var.name
end # assert_equal [['pluralize',["'comment'","'comments'"]]], var.filters
# end
def test_filter_with_keyword_arguments # def test_strict_filter_argument_parsing
var = Variable.new(%! hello | things: greeting: "world", farewell: 'goodbye'!) # with_error_mode(:strict) do
assert_equal 'hello', var.name # assert_raises(SyntaxError) do
assert_equal [['things',["greeting: \"world\"","farewell: 'goodbye'"]]], var.filters # Variable.new(%! number_of_comments | pluralize: 'comment': 'comments' !)
end # end
# end
def test_lax_filter_argument_parsing # end
var = Variable.new(%! number_of_comments | pluralize: 'comment': 'comments' !, :error_mode => :lax) # end
assert_equal 'number_of_comments', var.name
assert_equal [['pluralize',["'comment'","'comments'"]]], var.filters
end
def test_strict_filter_argument_parsing
with_error_mode(:strict) do
assert_raises(SyntaxError) do
Variable.new(%! number_of_comments | pluralize: 'comment': 'comments' !)
end
end
end
end
class VariableResolutionTest < Test::Unit::TestCase # class VariableResolutionTest < Test::Unit::TestCase
include Liquid # include Liquid
def test_simple_variable # def test_simple_variable
template = Template.parse(%|{{test}}|) # template = Template.parse(%|{{test}}|)
assert_equal 'worked', template.render('test' => 'worked') # assert_equal 'worked', template.render('test' => 'worked')
assert_equal 'worked wonderfully', template.render('test' => 'worked wonderfully') # assert_equal 'worked wonderfully', template.render('test' => 'worked wonderfully')
end # end
def test_simple_with_whitespaces # def test_simple_with_whitespaces
template = Template.parse(%| {{ test }} |) # template = Template.parse(%| {{ test }} |)
assert_equal ' worked ', template.render('test' => 'worked') # assert_equal ' worked ', template.render('test' => 'worked')
assert_equal ' worked wonderfully ', template.render('test' => 'worked wonderfully') # assert_equal ' worked wonderfully ', template.render('test' => 'worked wonderfully')
end # end
def test_ignore_unknown # def test_ignore_unknown
template = Template.parse(%|{{ test }}|) # template = Template.parse(%|{{ test }}|)
assert_equal '', template.render # assert_equal '', template.render
end # end
def test_hash_scoping # def test_hash_scoping
template = Template.parse(%|{{ test.test }}|) # template = Template.parse(%|{{ test.test }}|)
assert_equal 'worked', template.render('test' => {'test' => 'worked'}) # assert_equal 'worked', template.render('test' => {'test' => 'worked'})
end # end
def test_preset_assigns # def test_preset_assigns
template = Template.parse(%|{{ test }}|) # template = Template.parse(%|{{ test }}|)
template.assigns['test'] = 'worked' # template.assigns['test'] = 'worked'
assert_equal 'worked', template.render # assert_equal 'worked', template.render
end # end
def test_reuse_parsed_template # def test_reuse_parsed_template
template = Template.parse(%|{{ greeting }} {{ name }}|) # template = Template.parse(%|{{ greeting }} {{ name }}|)
template.assigns['greeting'] = 'Goodbye' # template.assigns['greeting'] = 'Goodbye'
assert_equal 'Hello Tobi', template.render('greeting' => 'Hello', 'name' => 'Tobi') # assert_equal 'Hello Tobi', template.render('greeting' => 'Hello', 'name' => 'Tobi')
assert_equal 'Hello ', template.render('greeting' => 'Hello', 'unknown' => 'Tobi') # assert_equal 'Hello ', template.render('greeting' => 'Hello', 'unknown' => 'Tobi')
assert_equal 'Hello Brian', template.render('greeting' => 'Hello', 'name' => 'Brian') # assert_equal 'Hello Brian', template.render('greeting' => 'Hello', 'name' => 'Brian')
assert_equal 'Goodbye Brian', template.render('name' => 'Brian') # assert_equal 'Goodbye Brian', template.render('name' => 'Brian')
assert_equal({'greeting'=>'Goodbye'}, template.assigns) # assert_equal({'greeting'=>'Goodbye'}, template.assigns)
end # end
def test_assigns_not_polluted_from_template # def test_assigns_not_polluted_from_template
template = Template.parse(%|{{ test }}{% assign test = 'bar' %}{{ test }}|) # template = Template.parse(%|{{ test }}{% assign test = 'bar' %}{{ test }}|)
template.assigns['test'] = 'baz' # template.assigns['test'] = 'baz'
assert_equal 'bazbar', template.render # assert_equal 'bazbar', template.render
assert_equal 'bazbar', template.render # assert_equal 'bazbar', template.render
assert_equal 'foobar', template.render('test' => 'foo') # assert_equal 'foobar', template.render('test' => 'foo')
assert_equal 'bazbar', template.render # assert_equal 'bazbar', template.render
end # end
def test_hash_with_default_proc # def test_hash_with_default_proc
template = Template.parse(%|Hello {{ test }}|) # template = Template.parse(%|Hello {{ test }}|)
assigns = Hash.new { |h,k| raise "Unknown variable '#{k}'" } # assigns = Hash.new { |h,k| raise "Unknown variable '#{k}'" }
assigns['test'] = 'Tobi' # assigns['test'] = 'Tobi'
assert_equal 'Hello Tobi', template.render!(assigns) # assert_equal 'Hello Tobi', template.render!(assigns)
assigns.delete('test') # assigns.delete('test')
e = assert_raises(RuntimeError) { # e = assert_raises(RuntimeError) {
template.render!(assigns) # template.render!(assigns)
} # }
assert_equal "Unknown variable 'test'", e.message # assert_equal "Unknown variable 'test'", e.message
end # end
end # VariableTest end # VariableTest