Compare commits

..

14 Commits

Author SHA1 Message Date
Tom Burns
0c6bc2b2a3 Allow floats > 9 2013-03-19 15:26:58 -04:00
Tom Burns
56d1913b37 Remove extension Makefile 2013-03-18 17:22:58 -04:00
Tobias Lutke
2f6b67798e fixed but with single char identifiers 2012-10-30 21:41:56 -04:00
Tobias Lutke
82c222ef0f enable weird method endings 2012-10-30 21:17:35 -04:00
Tobias Lutke
2baee3de3e enabled hard mode 2012-10-30 21:12:43 -04:00
Tobias Lutke
a96cfd1297 conquered parser_tests 2012-10-30 21:12:01 -04:00
Tobias Lütke
cf765ebed3 wip 2012-10-30 15:49:13 -04:00
Tobias Lütke
8d234098aa Initial checkin 2012-10-30 13:27:18 -04:00
Tobias Lutke
43b44c007a remove binaries 2012-10-29 21:16:25 -04:00
Tobias Lutke
858cb62c4f various speedups 2012-10-29 21:15:50 -04:00
Tobias Lütke
80be33884e Convert parser to C99 2012-10-29 16:43:42 -04:00
Tobias Lutke
cd040dabd8 Implement naive recusrive descent
Ragel doesn't allow us to recurse so we simply
reinvoke the parser for each step.
2012-10-28 21:55:20 -04:00
Tobias Lütke
18b83a58bd Replace regexpes with Ragel grammer
context parsing was handrolled and pretty ad-hoc
this branch exists to explore parsing the context
through a defined fsm as produced by Ragel
2012-10-28 21:50:18 -04:00
Tobias Lutke
6b64bfb53e fix benchmarks 2012-10-28 21:37:07 -04:00
31 changed files with 1309 additions and 331 deletions

4
.gitignore vendored
View File

@@ -4,3 +4,7 @@
pkg
*.rbc
.rvmrc
*.o
*.bundle
ext/liquid/Makefile

View File

@@ -1,13 +1,5 @@
# Liquid Version History
## 2.5.0 / 2013-03-06
* Prevent Object methods from being called on drops
* Avoid symbol injection from liquid
* Added break and continue statements
* Fix filter parser for args without space separators
* Add support for filter keyword arguments
## 2.4.0 / 2012-08-03
* Performance improvements

View File

@@ -2,12 +2,50 @@
require 'rubygems'
require 'rake'
require 'rake/clean'
require 'fileutils'
require 'rake/testtask'
require 'rubygems/package_task'
task :default => 'test'
task :default => [:compile, :test]
Rake::TestTask.new(:test) do |t|
task :gen do
sh "leg -oext/liquid/liquid_context.c ext/liquid/liquid_context.leg"
end
task :compile => [:gen, :liquid_ext]
extension = "liquid_ext"
ext = "ext/liquid"
ext_so = "#{ext}/#{extension}.#{RbConfig::CONFIG['DLEXT']}"
ext_files = FileList[
"#{ext}/*.c",
"#{ext}/*.h",
"#{ext}/*.leg",
"#{ext}/extconf.rb",
"#{ext}/Makefile",
"lib"
]
task "lib" do
directory "lib"
end
desc "Builds just the #{extension} extension"
task extension.to_sym => [:gen, "#{ext}/Makefile", ext_so ]
file "#{ext}/Makefile" => ["#{ext}/extconf.rb"] do
Dir.chdir(ext) do ruby "extconf.rb" end
end
file ext_so => ext_files do
Dir.chdir(ext) do
sh "make"
end
cp ext_so, "lib"
end
Rake::TestTask.new(:test => [:gen, 'liquid_ext']) do |t|
t.libs << '.' << 'lib' << 'test'
t.test_files = FileList['test/liquid/**/*_test.rb']
t.verbose = false
@@ -42,7 +80,7 @@ namespace :profile do
desc "Run KCacheGrind"
task :grind => :run do
system "qcachegrind /tmp/liquid.rubyprof_calltreeprinter.txt"
system "qcachegrind /tmp//callgrind.liquid.txt"
end
end

6
ext/liquid/extconf.rb Normal file
View File

@@ -0,0 +1,6 @@
require 'mkmf'
dir_config("liquid_ext")
have_library("c", "main")
create_makefile("liquid_ext")

764
ext/liquid/liquid_context.c Normal file
View File

@@ -0,0 +1,764 @@
/* A recursive-descent parser generated by peg 0.1.9 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define YYRULECOUNT 14
//uncomment to get more debug instrumentation
//
//#define YY_DEBUG
#include <ruby.h>
#define EMIT(sym, data) \
rb_ary_push(ctx->rb_tokens, rb_ary_new3(2, ID2SYM(rb_intern(sym)), data));
#define yy_rb_str rb_str_new(yytext, yyleng)
#define YYSTYPE VALUE
#define YY_CTX_LOCAL
#define YY_CTX_MEMBERS VALUE rb_tokens; char *p; int p_len;
#define YY_INPUT(buf, result, max_size) { \
result = ctx->p_len; \
if (result>0 || EOF == ctx->p[0]) { \
if (max_size < result) { result = max_size; } \
strncpy(buf, ctx->p, result); \
buf[result] = '\0'; \
yyprintf((stderr, "\nREFILLING %d chars now:<%s>", result, buf)); \
ctx->p += result; ctx->p_len -= result; \
yyprintf((stderr, "\nREFILLING DONE size left: %d <%s>", ctx->p_len, ctx->p)); \
} \
}
#ifndef YY_LOCAL
#define YY_LOCAL(T) static T
#endif
#ifndef YY_ACTION
#define YY_ACTION(T) static T
#endif
#ifndef YY_RULE
#define YY_RULE(T) static T
#endif
#ifndef YY_PARSE
#define YY_PARSE(T) T
#endif
#ifndef YYPARSE
#define YYPARSE yyparse
#endif
#ifndef YYPARSEFROM
#define YYPARSEFROM yyparsefrom
#endif
#ifndef YY_INPUT
#define YY_INPUT(buf, result, max_size) \
{ \
int yyc= getchar(); \
result= (EOF == yyc) ? 0 : (*(buf)= yyc, 1); \
yyprintf((stderr, "<%c>", yyc)); \
}
#endif
#ifndef YY_BEGIN
#define YY_BEGIN ( ctx->begin= ctx->pos, 1)
#endif
#ifndef YY_END
#define YY_END ( ctx->end= ctx->pos, 1)
#endif
#ifdef YY_DEBUG
# define yyprintf(args) fprintf args
#else
# define yyprintf(args)
#endif
#ifndef YYSTYPE
#define YYSTYPE int
#endif
#ifndef YY_PART
typedef struct _yycontext yycontext;
typedef void (*yyaction)(yycontext *ctx, char *yytext, int yyleng);
typedef struct _yythunk { int begin, end; yyaction action; struct _yythunk *next; } yythunk;
struct _yycontext {
char *buf;
int buflen;
int pos;
int limit;
char *text;
int textlen;
int begin;
int end;
int textmax;
yythunk *thunks;
int thunkslen;
int thunkpos;
YYSTYPE yy;
YYSTYPE *val;
YYSTYPE *vals;
int valslen;
#ifdef YY_CTX_MEMBERS
YY_CTX_MEMBERS
#endif
};
#ifdef YY_CTX_LOCAL
#define YY_CTX_PARAM_ yycontext *yyctx,
#define YY_CTX_PARAM yycontext *yyctx
#define YY_CTX_ARG_ yyctx,
#define YY_CTX_ARG yyctx
#else
#define YY_CTX_PARAM_
#define YY_CTX_PARAM
#define YY_CTX_ARG_
#define YY_CTX_ARG
yycontext yyctx0;
yycontext *yyctx= &yyctx0;
#endif
YY_LOCAL(int) yyrefill(yycontext *ctx)
{
int yyn;
while (ctx->buflen - ctx->pos < 512)
{
ctx->buflen *= 2;
ctx->buf= (char *)realloc(ctx->buf, ctx->buflen);
}
YY_INPUT((ctx->buf + ctx->pos), yyn, (ctx->buflen - ctx->pos));
if (!yyn) return 0;
ctx->limit += yyn;
return 1;
}
YY_LOCAL(int) yymatchDot(yycontext *ctx)
{
if (ctx->pos >= ctx->limit && !yyrefill(ctx)) return 0;
++ctx->pos;
return 1;
}
YY_LOCAL(int) yymatchChar(yycontext *ctx, int c)
{
if (ctx->pos >= ctx->limit && !yyrefill(ctx)) return 0;
if ((unsigned char)ctx->buf[ctx->pos] == c)
{
++ctx->pos;
yyprintf((stderr, " ok yymatchChar(ctx, %c) @ %s\n", c, ctx->buf+ctx->pos));
return 1;
}
yyprintf((stderr, " fail yymatchChar(ctx, %c) @ %s\n", c, ctx->buf+ctx->pos));
return 0;
}
YY_LOCAL(int) yymatchString(yycontext *ctx, char *s)
{
int yysav= ctx->pos;
while (*s)
{
if (ctx->pos >= ctx->limit && !yyrefill(ctx)) return 0;
if (ctx->buf[ctx->pos] != *s)
{
ctx->pos= yysav;
return 0;
}
++s;
++ctx->pos;
}
return 1;
}
YY_LOCAL(int) yymatchClass(yycontext *ctx, unsigned char *bits)
{
int c;
if (ctx->pos >= ctx->limit && !yyrefill(ctx)) return 0;
c= (unsigned char)ctx->buf[ctx->pos];
if (bits[c >> 3] & (1 << (c & 7)))
{
++ctx->pos;
yyprintf((stderr, " ok yymatchClass @ %s\n", ctx->buf+ctx->pos));
return 1;
}
yyprintf((stderr, " fail yymatchClass @ %s\n", ctx->buf+ctx->pos));
return 0;
}
YY_LOCAL(void) yyDo(yycontext *ctx, yyaction action, int begin, int end)
{
while (ctx->thunkpos >= ctx->thunkslen)
{
ctx->thunkslen *= 2;
ctx->thunks= (yythunk *)realloc(ctx->thunks, sizeof(yythunk) * ctx->thunkslen);
}
ctx->thunks[ctx->thunkpos].begin= begin;
ctx->thunks[ctx->thunkpos].end= end;
ctx->thunks[ctx->thunkpos].action= action;
++ctx->thunkpos;
}
YY_LOCAL(int) yyText(yycontext *ctx, int begin, int end)
{
int yyleng= end - begin;
if (yyleng <= 0)
yyleng= 0;
else
{
while (ctx->textlen < (yyleng + 1))
{
ctx->textlen *= 2;
ctx->text= (char *)realloc(ctx->text, ctx->textlen);
}
memcpy(ctx->text, ctx->buf + begin, yyleng);
}
ctx->text[yyleng]= '\0';
return yyleng;
}
YY_LOCAL(void) yyDone(yycontext *ctx)
{
int pos;
for (pos= 0; pos < ctx->thunkpos; ++pos)
{
yythunk *thunk= &ctx->thunks[pos];
int yyleng= thunk->end ? yyText(ctx, thunk->begin, thunk->end) : thunk->begin;
yyprintf((stderr, "DO [%d] %p %s\n", pos, thunk->action, ctx->text));
thunk->action(ctx, ctx->text, yyleng);
}
ctx->thunkpos= 0;
}
YY_LOCAL(void) yyCommit(yycontext *ctx)
{
if ((ctx->limit -= ctx->pos))
{
memmove(ctx->buf, ctx->buf + ctx->pos, ctx->limit);
}
ctx->begin -= ctx->pos;
ctx->end -= ctx->pos;
ctx->pos= ctx->thunkpos= 0;
}
YY_LOCAL(int) yyAccept(yycontext *ctx, int tp0)
{
if (tp0)
{
fprintf(stderr, "accept denied at %d\n", tp0);
return 0;
}
else
{
yyDone(ctx);
yyCommit(ctx);
}
return 1;
}
YY_LOCAL(void) yyPush(yycontext *ctx, char *text, int count) { ctx->val += count; }
YY_LOCAL(void) yyPop(yycontext *ctx, char *text, int count) { ctx->val -= count; }
YY_LOCAL(void) yySet(yycontext *ctx, char *text, int count) { ctx->val[count]= ctx->yy; }
#endif /* YY_PART */
#define YYACCEPT yyAccept(ctx, yythunkpos0)
YY_RULE(int) yy_digit(yycontext *ctx); /* 14 */
YY_RULE(int) yy_float(yycontext *ctx); /* 13 */
YY_RULE(int) yy_integer(yycontext *ctx); /* 12 */
YY_RULE(int) yy_rangelet(yycontext *ctx); /* 11 */
YY_RULE(int) yy_identifier(yycontext *ctx); /* 10 */
YY_RULE(int) yy_accessors(yycontext *ctx); /* 9 */
YY_RULE(int) yy_numeric(yycontext *ctx); /* 8 */
YY_RULE(int) yy_string(yycontext *ctx); /* 7 */
YY_RULE(int) yy_const(yycontext *ctx); /* 6 */
YY_RULE(int) yy_hash(yycontext *ctx); /* 5 */
YY_RULE(int) yy_range(yycontext *ctx); /* 4 */
YY_RULE(int) yy_entity(yycontext *ctx); /* 3 */
YY_RULE(int) yy_primary(yycontext *ctx); /* 2 */
YY_RULE(int) yy_grammar(yycontext *ctx); /* 1 */
YY_ACTION(void) yy_4_const(yycontext *ctx, char *yytext, int yyleng)
{
#define yy ctx->yy
#define yypos ctx->pos
#define yythunkpos ctx->thunkpos
yyprintf((stderr, "do yy_4_const\n"));
yy = Qnil; ;
#undef yythunkpos
#undef yypos
#undef yy
}
YY_ACTION(void) yy_3_const(yycontext *ctx, char *yytext, int yyleng)
{
#define yy ctx->yy
#define yypos ctx->pos
#define yythunkpos ctx->thunkpos
yyprintf((stderr, "do yy_3_const\n"));
yy = Qnil; ;
#undef yythunkpos
#undef yypos
#undef yy
}
YY_ACTION(void) yy_2_const(yycontext *ctx, char *yytext, int yyleng)
{
#define yy ctx->yy
#define yypos ctx->pos
#define yythunkpos ctx->thunkpos
yyprintf((stderr, "do yy_2_const\n"));
yy = Qfalse; ;
#undef yythunkpos
#undef yypos
#undef yy
}
YY_ACTION(void) yy_1_const(yycontext *ctx, char *yytext, int yyleng)
{
#define yy ctx->yy
#define yypos ctx->pos
#define yythunkpos ctx->thunkpos
yyprintf((stderr, "do yy_1_const\n"));
yy = Qtrue; ;
#undef yythunkpos
#undef yypos
#undef yy
}
YY_ACTION(void) yy_1_integer(yycontext *ctx, char *yytext, int yyleng)
{
#define yy ctx->yy
#define yypos ctx->pos
#define yythunkpos ctx->thunkpos
yyprintf((stderr, "do yy_1_integer\n"));
yy = rb_funcall(rb_cObject, rb_intern("Integer"), 1, yy_rb_str); ;
#undef yythunkpos
#undef yypos
#undef yy
}
YY_ACTION(void) yy_1_float(yycontext *ctx, char *yytext, int yyleng)
{
#define yy ctx->yy
#define yypos ctx->pos
#define yythunkpos ctx->thunkpos
yyprintf((stderr, "do yy_1_float\n"));
yy = rb_funcall(rb_cObject, rb_intern("Float"), 1, yy_rb_str); ;
#undef yythunkpos
#undef yypos
#undef yy
}
YY_ACTION(void) yy_2_string(yycontext *ctx, char *yytext, int yyleng)
{
#define yy ctx->yy
#define yypos ctx->pos
#define yythunkpos ctx->thunkpos
yyprintf((stderr, "do yy_2_string\n"));
yy = yy_rb_str; ;
#undef yythunkpos
#undef yypos
#undef yy
}
YY_ACTION(void) yy_1_string(yycontext *ctx, char *yytext, int yyleng)
{
#define yy ctx->yy
#define yypos ctx->pos
#define yythunkpos ctx->thunkpos
yyprintf((stderr, "do yy_1_string\n"));
yy = yy_rb_str; ;
#undef yythunkpos
#undef yypos
#undef yy
}
YY_ACTION(void) yy_1_range(yycontext *ctx, char *yytext, int yyleng)
{
#define yy ctx->yy
#define yypos ctx->pos
#define yythunkpos ctx->thunkpos
yyprintf((stderr, "do yy_1_range\n"));
EMIT("range", Qnil); ;
#undef yythunkpos
#undef yypos
#undef yy
}
YY_ACTION(void) yy_1_rangelet(yycontext *ctx, char *yytext, int yyleng)
{
#define var ctx->val[-1]
#define yy ctx->yy
#define yypos ctx->pos
#define yythunkpos ctx->thunkpos
yyprintf((stderr, "do yy_1_rangelet\n"));
EMIT("id", var); ;
#undef yythunkpos
#undef yypos
#undef yy
#undef var
}
YY_ACTION(void) yy_1_entity(yycontext *ctx, char *yytext, int yyleng)
{
#define yy ctx->yy
#define yypos ctx->pos
#define yythunkpos ctx->thunkpos
yyprintf((stderr, "do yy_1_entity\n"));
EMIT("id", yy_rb_str); EMIT("lookup", Qnil); ;
#undef yythunkpos
#undef yypos
#undef yy
}
YY_ACTION(void) yy_5_accessors(yycontext *ctx, char *yytext, int yyleng)
{
#define yy ctx->yy
#define yypos ctx->pos
#define yythunkpos ctx->thunkpos
yyprintf((stderr, "do yy_5_accessors\n"));
EMIT("id", yy_rb_str); EMIT("call", Qnil); ;
#undef yythunkpos
#undef yypos
#undef yy
}
YY_ACTION(void) yy_4_accessors(yycontext *ctx, char *yytext, int yyleng)
{
#define yy ctx->yy
#define yypos ctx->pos
#define yythunkpos ctx->thunkpos
yyprintf((stderr, "do yy_4_accessors\n"));
EMIT("buildin", rb_str_new2("size")); ;
#undef yythunkpos
#undef yypos
#undef yy
}
YY_ACTION(void) yy_3_accessors(yycontext *ctx, char *yytext, int yyleng)
{
#define yy ctx->yy
#define yypos ctx->pos
#define yythunkpos ctx->thunkpos
yyprintf((stderr, "do yy_3_accessors\n"));
EMIT("buildin", rb_str_new2("last")); ;
#undef yythunkpos
#undef yypos
#undef yy
}
YY_ACTION(void) yy_2_accessors(yycontext *ctx, char *yytext, int yyleng)
{
#define yy ctx->yy
#define yypos ctx->pos
#define yythunkpos ctx->thunkpos
yyprintf((stderr, "do yy_2_accessors\n"));
EMIT("buildin", rb_str_new2("first")); ;
#undef yythunkpos
#undef yypos
#undef yy
}
YY_ACTION(void) yy_1_accessors(yycontext *ctx, char *yytext, int yyleng)
{
#define yy ctx->yy
#define yypos ctx->pos
#define yythunkpos ctx->thunkpos
yyprintf((stderr, "do yy_1_accessors\n"));
EMIT("call", Qnil); ;
#undef yythunkpos
#undef yypos
#undef yy
}
YY_ACTION(void) yy_3_primary(yycontext *ctx, char *yytext, int yyleng)
{
#define var ctx->val[-1]
#define yy ctx->yy
#define yypos ctx->pos
#define yythunkpos ctx->thunkpos
yyprintf((stderr, "do yy_3_primary\n"));
EMIT("id", var); ;
#undef yythunkpos
#undef yypos
#undef yy
#undef var
}
YY_ACTION(void) yy_2_primary(yycontext *ctx, char *yytext, int yyleng)
{
#define var ctx->val[-1]
#define yy ctx->yy
#define yypos ctx->pos
#define yythunkpos ctx->thunkpos
yyprintf((stderr, "do yy_2_primary\n"));
EMIT("id", var); ;
#undef yythunkpos
#undef yypos
#undef yy
#undef var
}
YY_ACTION(void) yy_1_primary(yycontext *ctx, char *yytext, int yyleng)
{
#define var ctx->val[-1]
#define yy ctx->yy
#define yypos ctx->pos
#define yythunkpos ctx->thunkpos
yyprintf((stderr, "do yy_1_primary\n"));
EMIT("id", var); ;
#undef yythunkpos
#undef yypos
#undef yy
#undef var
}
YY_ACTION(void) yy_1_grammar(yycontext *ctx, char *yytext, int yyleng)
{
#define yy ctx->yy
#define yypos ctx->pos
#define yythunkpos ctx->thunkpos
yyprintf((stderr, "do yy_1_grammar\n"));
EMIT("lookup", Qnil); ;
#undef yythunkpos
#undef yypos
#undef yy
}
YY_RULE(int) yy_digit(yycontext *ctx)
{ int yypos0= ctx->pos, yythunkpos0= ctx->thunkpos;
yyprintf((stderr, "%s\n", "digit")); if (!yymatchClass(ctx, (unsigned char *)"\000\000\000\000\000\000\377\003\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000")) goto l1;
yyprintf((stderr, " ok %s @ %s\n", "digit", ctx->buf+ctx->pos));
return 1;
l1:; ctx->pos= yypos0; ctx->thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "digit", ctx->buf+ctx->pos));
return 0;
}
YY_RULE(int) yy_float(yycontext *ctx)
{ int yypos0= ctx->pos, yythunkpos0= ctx->thunkpos;
yyprintf((stderr, "%s\n", "float")); yyText(ctx, ctx->begin, ctx->end); if (!(YY_BEGIN)) goto l2;
{ int yypos3= ctx->pos, yythunkpos3= ctx->thunkpos; if (!yymatchChar(ctx, '-')) goto l3; goto l4;
l3:; ctx->pos= yypos3; ctx->thunkpos= yythunkpos3;
}
l4:; if (!yy_digit(ctx)) goto l2; if (!yymatchChar(ctx, '.')) goto l2; if (!yy_digit(ctx)) goto l2;
l5:;
{ int yypos6= ctx->pos, yythunkpos6= ctx->thunkpos; if (!yy_digit(ctx)) goto l6; goto l5;
l6:; ctx->pos= yypos6; ctx->thunkpos= yythunkpos6;
} yyText(ctx, ctx->begin, ctx->end); if (!(YY_END)) goto l2; yyDo(ctx, yy_1_float, ctx->begin, ctx->end);
yyprintf((stderr, " ok %s @ %s\n", "float", ctx->buf+ctx->pos));
return 1;
l2:; ctx->pos= yypos0; ctx->thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "float", ctx->buf+ctx->pos));
return 0;
}
YY_RULE(int) yy_integer(yycontext *ctx)
{ int yypos0= ctx->pos, yythunkpos0= ctx->thunkpos;
yyprintf((stderr, "%s\n", "integer")); yyText(ctx, ctx->begin, ctx->end); if (!(YY_BEGIN)) goto l7;
{ int yypos8= ctx->pos, yythunkpos8= ctx->thunkpos; if (!yymatchChar(ctx, '-')) goto l8; goto l9;
l8:; ctx->pos= yypos8; ctx->thunkpos= yythunkpos8;
}
l9:; if (!yy_digit(ctx)) goto l7;
l10:;
{ int yypos11= ctx->pos, yythunkpos11= ctx->thunkpos; if (!yy_digit(ctx)) goto l11; goto l10;
l11:; ctx->pos= yypos11; ctx->thunkpos= yythunkpos11;
} yyText(ctx, ctx->begin, ctx->end); if (!(YY_END)) goto l7; yyDo(ctx, yy_1_integer, ctx->begin, ctx->end);
yyprintf((stderr, " ok %s @ %s\n", "integer", ctx->buf+ctx->pos));
return 1;
l7:; ctx->pos= yypos0; ctx->thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "integer", ctx->buf+ctx->pos));
return 0;
}
YY_RULE(int) yy_rangelet(yycontext *ctx)
{ int yypos0= ctx->pos, yythunkpos0= ctx->thunkpos; yyDo(ctx, yyPush, 1, 0);
yyprintf((stderr, "%s\n", "rangelet"));
{ int yypos13= ctx->pos, yythunkpos13= ctx->thunkpos; if (!yy_integer(ctx)) goto l14; yyDo(ctx, yySet, -1, 0); yyDo(ctx, yy_1_rangelet, ctx->begin, ctx->end); goto l13;
l14:; ctx->pos= yypos13; ctx->thunkpos= yythunkpos13; if (!yy_entity(ctx)) goto l12;
}
l13:;
yyprintf((stderr, " ok %s @ %s\n", "rangelet", ctx->buf+ctx->pos)); yyDo(ctx, yyPop, 1, 0);
return 1;
l12:; ctx->pos= yypos0; ctx->thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "rangelet", ctx->buf+ctx->pos));
return 0;
}
YY_RULE(int) yy_identifier(yycontext *ctx)
{ int yypos0= ctx->pos, yythunkpos0= ctx->thunkpos;
yyprintf((stderr, "%s\n", "identifier")); if (!yymatchClass(ctx, (unsigned char *)"\000\000\000\000\000\000\000\000\376\377\377\007\376\377\377\007\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000")) goto l15;
l16:;
{ int yypos17= ctx->pos, yythunkpos17= ctx->thunkpos; if (!yymatchClass(ctx, (unsigned char *)"\000\000\000\000\000\040\377\003\376\377\377\207\376\377\377\007\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000")) goto l17; goto l16;
l17:; ctx->pos= yypos17; ctx->thunkpos= yythunkpos17;
}
{ int yypos18= ctx->pos, yythunkpos18= ctx->thunkpos; if (!yymatchClass(ctx, (unsigned char *)"\000\000\000\000\002\000\000\200\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000")) goto l18; goto l19;
l18:; ctx->pos= yypos18; ctx->thunkpos= yythunkpos18;
}
l19:;
yyprintf((stderr, " ok %s @ %s\n", "identifier", ctx->buf+ctx->pos));
return 1;
l15:; ctx->pos= yypos0; ctx->thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "identifier", ctx->buf+ctx->pos));
return 0;
}
YY_RULE(int) yy_accessors(yycontext *ctx)
{ int yypos0= ctx->pos, yythunkpos0= ctx->thunkpos;
yyprintf((stderr, "%s\n", "accessors"));
{ int yypos21= ctx->pos, yythunkpos21= ctx->thunkpos; if (!yy_hash(ctx)) goto l22; yyDo(ctx, yy_1_accessors, ctx->begin, ctx->end); goto l21;
l22:; ctx->pos= yypos21; ctx->thunkpos= yythunkpos21; if (!yymatchString(ctx, ".first")) goto l23; yyDo(ctx, yy_2_accessors, ctx->begin, ctx->end); goto l21;
l23:; ctx->pos= yypos21; ctx->thunkpos= yythunkpos21; if (!yymatchString(ctx, ".last")) goto l24; yyDo(ctx, yy_3_accessors, ctx->begin, ctx->end); goto l21;
l24:; ctx->pos= yypos21; ctx->thunkpos= yythunkpos21; if (!yymatchString(ctx, ".size")) goto l25; yyDo(ctx, yy_4_accessors, ctx->begin, ctx->end); goto l21;
l25:; ctx->pos= yypos21; ctx->thunkpos= yythunkpos21; if (!yymatchChar(ctx, '.')) goto l20; yyText(ctx, ctx->begin, ctx->end); if (!(YY_BEGIN)) goto l20; if (!yy_identifier(ctx)) goto l20; yyText(ctx, ctx->begin, ctx->end); if (!(YY_END)) goto l20; yyDo(ctx, yy_5_accessors, ctx->begin, ctx->end);
}
l21:;
yyprintf((stderr, " ok %s @ %s\n", "accessors", ctx->buf+ctx->pos));
return 1;
l20:; ctx->pos= yypos0; ctx->thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "accessors", ctx->buf+ctx->pos));
return 0;
}
YY_RULE(int) yy_numeric(yycontext *ctx)
{ int yypos0= ctx->pos, yythunkpos0= ctx->thunkpos;
yyprintf((stderr, "%s\n", "numeric"));
{ int yypos27= ctx->pos, yythunkpos27= ctx->thunkpos; if (!yy_float(ctx)) goto l28; goto l27;
l28:; ctx->pos= yypos27; ctx->thunkpos= yythunkpos27; if (!yy_integer(ctx)) goto l26;
}
l27:;
yyprintf((stderr, " ok %s @ %s\n", "numeric", ctx->buf+ctx->pos));
return 1;
l26:; ctx->pos= yypos0; ctx->thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "numeric", ctx->buf+ctx->pos));
return 0;
}
YY_RULE(int) yy_string(yycontext *ctx)
{ int yypos0= ctx->pos, yythunkpos0= ctx->thunkpos;
yyprintf((stderr, "%s\n", "string"));
{ int yypos30= ctx->pos, yythunkpos30= ctx->thunkpos; if (!yymatchClass(ctx, (unsigned char *)"\000\000\000\000\200\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000")) goto l31; yyText(ctx, ctx->begin, ctx->end); if (!(YY_BEGIN)) goto l31;
l32:;
{ int yypos33= ctx->pos, yythunkpos33= ctx->thunkpos;
{ int yypos34= ctx->pos, yythunkpos34= ctx->thunkpos; if (!yymatchClass(ctx, (unsigned char *)"\000\000\000\000\200\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000")) goto l34; goto l33;
l34:; ctx->pos= yypos34; ctx->thunkpos= yythunkpos34;
} if (!yymatchDot(ctx)) goto l33; goto l32;
l33:; ctx->pos= yypos33; ctx->thunkpos= yythunkpos33;
} yyText(ctx, ctx->begin, ctx->end); if (!(YY_END)) goto l31; if (!yymatchClass(ctx, (unsigned char *)"\000\000\000\000\200\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000")) goto l31; yyDo(ctx, yy_1_string, ctx->begin, ctx->end); goto l30;
l31:; ctx->pos= yypos30; ctx->thunkpos= yythunkpos30; if (!yymatchClass(ctx, (unsigned char *)"\000\000\000\000\004\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000")) goto l29; yyText(ctx, ctx->begin, ctx->end); if (!(YY_BEGIN)) goto l29;
l35:;
{ int yypos36= ctx->pos, yythunkpos36= ctx->thunkpos;
{ int yypos37= ctx->pos, yythunkpos37= ctx->thunkpos; if (!yymatchClass(ctx, (unsigned char *)"\000\000\000\000\004\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000")) goto l37; goto l36;
l37:; ctx->pos= yypos37; ctx->thunkpos= yythunkpos37;
} if (!yymatchDot(ctx)) goto l36; goto l35;
l36:; ctx->pos= yypos36; ctx->thunkpos= yythunkpos36;
} yyText(ctx, ctx->begin, ctx->end); if (!(YY_END)) goto l29; if (!yymatchClass(ctx, (unsigned char *)"\000\000\000\000\004\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000")) goto l29; yyDo(ctx, yy_2_string, ctx->begin, ctx->end);
}
l30:;
yyprintf((stderr, " ok %s @ %s\n", "string", ctx->buf+ctx->pos));
return 1;
l29:; ctx->pos= yypos0; ctx->thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "string", ctx->buf+ctx->pos));
return 0;
}
YY_RULE(int) yy_const(yycontext *ctx)
{ int yypos0= ctx->pos, yythunkpos0= ctx->thunkpos;
yyprintf((stderr, "%s\n", "const"));
{ int yypos39= ctx->pos, yythunkpos39= ctx->thunkpos; if (!yymatchString(ctx, "true")) goto l40; yyDo(ctx, yy_1_const, ctx->begin, ctx->end); goto l39;
l40:; ctx->pos= yypos39; ctx->thunkpos= yythunkpos39; if (!yymatchString(ctx, "false")) goto l41; yyDo(ctx, yy_2_const, ctx->begin, ctx->end); goto l39;
l41:; ctx->pos= yypos39; ctx->thunkpos= yythunkpos39; if (!yymatchString(ctx, "nil")) goto l42; yyDo(ctx, yy_3_const, ctx->begin, ctx->end); goto l39;
l42:; ctx->pos= yypos39; ctx->thunkpos= yythunkpos39; if (!yymatchString(ctx, "null")) goto l38; yyDo(ctx, yy_4_const, ctx->begin, ctx->end);
}
l39:;
yyprintf((stderr, " ok %s @ %s\n", "const", ctx->buf+ctx->pos));
return 1;
l38:; ctx->pos= yypos0; ctx->thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "const", ctx->buf+ctx->pos));
return 0;
}
YY_RULE(int) yy_hash(yycontext *ctx)
{ int yypos0= ctx->pos, yythunkpos0= ctx->thunkpos;
yyprintf((stderr, "%s\n", "hash")); if (!yymatchChar(ctx, '[')) goto l43;
{ int yypos44= ctx->pos, yythunkpos44= ctx->thunkpos; if (!yy_primary(ctx)) goto l45; goto l44;
l45:; ctx->pos= yypos44; ctx->thunkpos= yythunkpos44; if (!yy_entity(ctx)) goto l43;
}
l44:; if (!yymatchChar(ctx, ']')) goto l43;
yyprintf((stderr, " ok %s @ %s\n", "hash", ctx->buf+ctx->pos));
return 1;
l43:; ctx->pos= yypos0; ctx->thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "hash", ctx->buf+ctx->pos));
return 0;
}
YY_RULE(int) yy_range(yycontext *ctx)
{ int yypos0= ctx->pos, yythunkpos0= ctx->thunkpos;
yyprintf((stderr, "%s\n", "range")); if (!yymatchChar(ctx, '(')) goto l46; if (!yy_rangelet(ctx)) goto l46; if (!yymatchString(ctx, "..")) goto l46; if (!yy_rangelet(ctx)) goto l46; if (!yymatchChar(ctx, ')')) goto l46; yyDo(ctx, yy_1_range, ctx->begin, ctx->end);
yyprintf((stderr, " ok %s @ %s\n", "range", ctx->buf+ctx->pos));
return 1;
l46:; ctx->pos= yypos0; ctx->thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "range", ctx->buf+ctx->pos));
return 0;
}
YY_RULE(int) yy_entity(yycontext *ctx)
{ int yypos0= ctx->pos, yythunkpos0= ctx->thunkpos;
yyprintf((stderr, "%s\n", "entity")); yyText(ctx, ctx->begin, ctx->end); if (!(YY_BEGIN)) goto l47; if (!yy_identifier(ctx)) goto l47; yyText(ctx, ctx->begin, ctx->end); if (!(YY_END)) goto l47; yyDo(ctx, yy_1_entity, ctx->begin, ctx->end);
l48:;
{ int yypos49= ctx->pos, yythunkpos49= ctx->thunkpos; if (!yy_accessors(ctx)) goto l49; goto l48;
l49:; ctx->pos= yypos49; ctx->thunkpos= yythunkpos49;
}
yyprintf((stderr, " ok %s @ %s\n", "entity", ctx->buf+ctx->pos));
return 1;
l47:; ctx->pos= yypos0; ctx->thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "entity", ctx->buf+ctx->pos));
return 0;
}
YY_RULE(int) yy_primary(yycontext *ctx)
{ int yypos0= ctx->pos, yythunkpos0= ctx->thunkpos; yyDo(ctx, yyPush, 1, 0);
yyprintf((stderr, "%s\n", "primary"));
{ int yypos51= ctx->pos, yythunkpos51= ctx->thunkpos; if (!yy_const(ctx)) goto l52; yyDo(ctx, yySet, -1, 0); yyDo(ctx, yy_1_primary, ctx->begin, ctx->end); goto l51;
l52:; ctx->pos= yypos51; ctx->thunkpos= yythunkpos51; if (!yy_string(ctx)) goto l53; yyDo(ctx, yySet, -1, 0); yyDo(ctx, yy_2_primary, ctx->begin, ctx->end); goto l51;
l53:; ctx->pos= yypos51; ctx->thunkpos= yythunkpos51; if (!yy_numeric(ctx)) goto l50; yyDo(ctx, yySet, -1, 0); yyDo(ctx, yy_3_primary, ctx->begin, ctx->end);
}
l51:;
yyprintf((stderr, " ok %s @ %s\n", "primary", ctx->buf+ctx->pos)); yyDo(ctx, yyPop, 1, 0);
return 1;
l50:; ctx->pos= yypos0; ctx->thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "primary", ctx->buf+ctx->pos));
return 0;
}
YY_RULE(int) yy_grammar(yycontext *ctx)
{ int yypos0= ctx->pos, yythunkpos0= ctx->thunkpos;
yyprintf((stderr, "%s\n", "grammar"));
{ int yypos55= ctx->pos, yythunkpos55= ctx->thunkpos; if (!yy_primary(ctx)) goto l56; goto l55;
l56:; ctx->pos= yypos55; ctx->thunkpos= yythunkpos55; if (!yy_entity(ctx)) goto l57; goto l55;
l57:; ctx->pos= yypos55; ctx->thunkpos= yythunkpos55; if (!yy_range(ctx)) goto l58; goto l55;
l58:; ctx->pos= yypos55; ctx->thunkpos= yythunkpos55; if (!yy_hash(ctx)) goto l54; yyDo(ctx, yy_1_grammar, ctx->begin, ctx->end);
}
l55:;
yyprintf((stderr, " ok %s @ %s\n", "grammar", ctx->buf+ctx->pos));
return 1;
l54:; ctx->pos= yypos0; ctx->thunkpos= yythunkpos0;
yyprintf((stderr, " fail %s @ %s\n", "grammar", ctx->buf+ctx->pos));
return 0;
}
#ifndef YY_PART
typedef int (*yyrule)(yycontext *ctx);
YY_PARSE(int) YYPARSEFROM(YY_CTX_PARAM_ yyrule yystart)
{
int yyok;
if (!yyctx->buflen)
{
yyctx->buflen= 1024;
yyctx->buf= (char *)malloc(yyctx->buflen);
yyctx->textlen= 1024;
yyctx->text= (char *)malloc(yyctx->textlen);
yyctx->thunkslen= 32;
yyctx->thunks= (yythunk *)malloc(sizeof(yythunk) * yyctx->thunkslen);
yyctx->valslen= 32;
yyctx->vals= (YYSTYPE *)malloc(sizeof(YYSTYPE) * yyctx->valslen);
yyctx->begin= yyctx->end= yyctx->pos= yyctx->limit= yyctx->thunkpos= 0;
}
yyctx->begin= yyctx->end= yyctx->pos;
yyctx->thunkpos= 0;
yyctx->val= yyctx->vals;
yyok= yystart(yyctx);
if (yyok) yyDone(yyctx);
yyCommit(yyctx);
return yyok;
}
YY_PARSE(int) YYPARSE(YY_CTX_PARAM)
{
return YYPARSEFROM(YY_CTX_ARG_ yy_grammar);
}
#endif
VALUE liquid_context_parse_impl(VALUE self, VALUE text) {
char *p;
int len;
yycontext ctx;
memset(&ctx, 0, sizeof(yycontext));
ctx.p = RSTRING_PTR(text);
ctx.p_len = (int) RSTRING_LEN(text);
ctx.rb_tokens = rb_ary_new();
yyparse(&ctx);
return ctx.rb_tokens;
}

View File

@@ -0,0 +1,98 @@
%{
//uncomment to get more debug instrumentation
//
//#define YY_DEBUG
#include <ruby.h>
#define EMIT(sym, data) \
rb_ary_push(ctx->rb_tokens, rb_ary_new3(2, ID2SYM(rb_intern(sym)), data));
#define yy_rb_str rb_str_new(yytext, yyleng)
#define YYSTYPE VALUE
#define YY_CTX_LOCAL
#define YY_CTX_MEMBERS VALUE rb_tokens; char *p; int p_len;
#define YY_INPUT(buf, result, max_size) { \
result = ctx->p_len; \
if (result>0 || EOF == ctx->p[0]) { \
if (max_size < result) { result = max_size; } \
strncpy(buf, ctx->p, result); \
buf[result] = '\0'; \
yyprintf((stderr, "\nREFILLING %d chars now:<%s>", result, buf)); \
ctx->p += result; ctx->p_len -= result; \
yyprintf((stderr, "\nREFILLING DONE size left: %d <%s>", ctx->p_len, ctx->p)); \
} \
}
%}
grammar = primary
| entity
| range
| hash { EMIT("lookup", Qnil); }
;
hash = '[' (primary|entity) ']';
primary = var:const { EMIT("id", var); }
| var:string { EMIT("id", var); }
| var:numeric { EMIT("id", var); }
;
accessors = hash { EMIT("call", Qnil); }
| '.first' { EMIT("buildin", rb_str_new2("first")); }
| '.last' { EMIT("buildin", rb_str_new2("last")); }
| '.size' { EMIT("buildin", rb_str_new2("size")); }
| '.' <identifier> { EMIT("id", yy_rb_str); EMIT("call", Qnil); }
;
entity = <identifier> { EMIT("id", yy_rb_str); EMIT("lookup", Qnil); }
accessors*
;
rangelet = var:integer { EMIT("id", var); }
| entity
;
range = '(' rangelet '..' rangelet ')' { EMIT("range", Qnil); }
string = ['] < ( !['] . )* > ['] { $$ = yy_rb_str; }
| ["] < ( !["] . )* > ["] { $$ = yy_rb_str; }
;
numeric = float
| integer
;
float = <'-'? digit+'.'digit+> { $$ = rb_funcall(rb_cObject, rb_intern("Float"), 1, yy_rb_str); }
integer = <'-'? digit+> { $$ = rb_funcall(rb_cObject, rb_intern("Integer"), 1, yy_rb_str); }
const = "true" { $$ = Qtrue; }
| 'false' { $$ = Qfalse; }
| 'nil' { $$ = Qnil; }
| 'null' { $$ = Qnil; }
;
digit = [0-9];
identifier = [a-zA-Z][a-zA-Z0-9_\-]*[?!]?;
%%
VALUE liquid_context_parse_impl(VALUE self, VALUE text) {
char *p;
int len;
yycontext ctx;
memset(&ctx, 0, sizeof(yycontext));
ctx.p = RSTRING_PTR(text);
ctx.p_len = (int) RSTRING_LEN(text);
ctx.rb_tokens = rb_ary_new();
yyparse(&ctx);
return ctx.rb_tokens;
}

13
ext/liquid/liquid_ext.c Normal file
View File

@@ -0,0 +1,13 @@
#include <ruby.h>
static VALUE rb_Liquid;
static VALUE rb_Parser;
VALUE liquid_context_parse_impl(VALUE text);
void Init_liquid_ext()
{
rb_Liquid = rb_define_module("Liquid");
rb_Parser = rb_define_class_under(rb_Liquid, "Parser", rb_cObject);
rb_define_singleton_method(rb_Parser, "parse", liquid_context_parse_impl, 1);
}

View File

@@ -62,6 +62,7 @@ require 'liquid/standardfilters'
require 'liquid/condition'
require 'liquid/module_ex'
require 'liquid/utils'
require 'liquid_ext'
# Load all the tags of the standard library
#

View File

@@ -109,7 +109,7 @@ module Liquid
end
end
output.join
output
end
end
end

View File

@@ -1,5 +1,6 @@
module Liquid
# Context keeps the variable stack and resolves variables, as well as keywords
#
# context['variable'] = 'testing'
@@ -39,7 +40,6 @@ module Liquid
filters.each do |f|
raise ArgumentError, "Expected module but got: #{f.class}" unless f.is_a?(Module)
Strainer.add_known_filter(f)
strainer.extend(f)
end
end
@@ -72,7 +72,11 @@ module Liquid
end
def invoke(method, *args)
strainer.invoke(method, *args)
if strainer.respond_to?(method)
strainer.__send__(method, *args)
else
args.first
end
end
# Push new local scope on the stack. use <tt>Context#stack</tt> instead
@@ -111,54 +115,89 @@ module Liquid
@scopes[0] = {}
end
# Look up variable, either resolve directly after considering the name. We can directly handle
# Strings, digits, floats and booleans (true,false).
# If no match is made we lookup the variable in the current scope and
# later move up to the parent blocks to see if we can resolve the variable somewhere up the tree.
# Some special keywords return symbols. Those symbols are to be called on the rhs object in expressions
#
# Example:
# products == empty #=> products.empty?
def resolve(key)
case key
when nil, ""
return nil
when "blank"
return :blank?
when "empty"
return :empty?
end
result = Parser.parse(key)
stack = []
result.each do |(sym, value)|
case sym
when :id
stack.push value
when :lookup
left = stack.pop
value = find_variable(left)
stack.push(harden(value))
when :range
right = stack.pop.to_i
left = stack.pop.to_i
stack.push (left..right)
when :buildin
left = stack.pop
value = invoke_buildin(left, value)
stack.push(harden(value))
when :call
left = stack.pop
right = stack.pop
value = lookup_and_evaluate(right, left)
stack.push(harden(value))
else
raise "unknown #{sym}"
end
end
return stack.first
end
# Only allow String, Numeric, Hash, Array, Proc, Boolean or <tt>Liquid::Drop</tt>
def []=(key, value)
@scopes[0][key] = value
end
def [](key)
resolve(key)
end
def has_key?(key)
resolve(key) != nil
end
private
LITERALS = {
nil => nil, 'nil' => nil, 'null' => nil, '' => nil,
'true' => true,
'false' => false,
'blank' => :blank?,
'empty' => :empty?
}
alias_method :[], :resolve
# Look up variable, either resolve directly after considering the name. We can directly handle
# Strings, digits, floats and booleans (true,false).
# If no match is made we lookup the variable in the current scope and
# later move up to the parent blocks to see if we can resolve the variable somewhere up the tree.
# Some special keywords return symbols. Those symbols are to be called on the rhs object in expressions
#
# Example:
# products == empty #=> products.empty?
def resolve(key)
if LITERALS.key?(key)
LITERALS[key]
private
def invoke_buildin(obj, key)
# as weird as this is, liquid unit tests demand that we prioritize hash lookups
# to buildins. So if we got a hash and it has a :first element we need to call that
# instead of sending the first message...
if obj.respond_to?(:has_key?) && obj.has_key?(key)
return lookup_and_evaluate(obj, key)
end
if obj.respond_to?(key)
return obj.send(key)
else
case key
when /^'(.*)'$/ # Single quoted strings
$1
when /^"(.*)"$/ # Double quoted strings
$1
when /^(-?\d+)$/ # Integer and floats
$1.to_i
when /^\((\S+)\.\.(\S+)\)$/ # Ranges
(resolve($1).to_i..resolve($2).to_i)
when /^(-?\d[\d\.]+)$/ # Floats
$1.to_f
else
variable(key)
end
return nil
end
end
@@ -178,71 +217,34 @@ module Liquid
scope ||= @environments.last || @scopes.last
variable ||= lookup_and_evaluate(scope, key)
variable = variable.to_liquid
variable.context = self if variable.respond_to?(:context=)
return variable
end
# Resolves namespaced queries gracefully.
#
# Example
# @context['hash'] = {"name" => 'tobi'}
# assert_equal 'tobi', @context['hash.name']
# assert_equal 'tobi', @context['hash["name"]']
def variable(markup)
parts = markup.scan(VariableParser)
square_bracketed = /^\[(.*)\]$/
first_part = parts.shift
if first_part =~ square_bracketed
first_part = resolve($1)
end
if object = find_variable(first_part)
parts.each do |part|
part = resolve($1) if part_resolved = (part =~ square_bracketed)
# If object is a hash- or array-like object we look for the
# presence of the key and if its available we return it
if object.respond_to?(:[]) and
((object.respond_to?(:has_key?) and object.has_key?(part)) or
(object.respond_to?(:fetch) and part.is_a?(Integer)))
# if its a proc we will replace the entry with the proc
res = lookup_and_evaluate(object, part)
object = res.to_liquid
# Some special cases. If the part wasn't in square brackets and
# no key with the same name was found we interpret following calls
# as commands and call them on the current object
elsif !part_resolved and object.respond_to?(part) and ['size', 'first', 'last'].include?(part)
object = object.send(part.intern).to_liquid
# No key was present with the desired value and it wasn't one of the directly supported
# keywords either. The only thing we got left is to return nil
else
return nil
end
# If we are dealing with a drop here we have to
object.context = self if object.respond_to?(:context=)
end
end
object
end # variable
def lookup_and_evaluate(obj, key)
if (value = obj[key]).is_a?(Proc) && obj.respond_to?(:[]=)
obj[key] = (value.arity == 0) ? value.call : value.call(self)
else
value
return nil unless obj.respond_to?(:[])
if obj.is_a?(Array)
return nil unless key.is_a?(Integer)
end
end # lookup_and_evaluate
value = obj[key]
if value.is_a?(Proc)
# call the proc
value = (value.arity == 0) ? value.call : value.call(self)
# memozie if possible
obj[key] = value if obj.respond_to?(:[]=)
end
value
end
def harden(value)
value = value.to_liquid
value.context = self if value.respond_to?(:context=)
return value
end
def squash_instance_assigns_with_environments
@scopes.last.each_key do |k|
@@ -254,6 +256,7 @@ module Liquid
end
end
end # squash_instance_assigns_with_environments
end # Context
end # Liquid

View File

@@ -1,5 +1,3 @@
require 'set'
module Liquid
# A drop in liquid is a class which allows you to export DOM like things to liquid.
@@ -33,8 +31,8 @@ module Liquid
# called by liquid to invoke a drop
def invoke_drop(method_or_key)
if method_or_key && method_or_key != EMPTY_STRING && self.class.invokable?(method_or_key)
send(method_or_key)
if method_or_key && method_or_key != EMPTY_STRING && self.class.public_method_defined?(method_or_key.to_s.to_sym)
send(method_or_key.to_s.to_sym)
else
before_method(method_or_key)
end
@@ -49,13 +47,5 @@ module Liquid
end
alias :[] :invoke_drop
private
# Check for method existence without invoking respond_to?, which creates symbols
def self.invokable?(method_name)
@invokable_methods ||= Set.new((public_instance_methods - Liquid::Drop.public_instance_methods).map(&:to_s))
@invokable_methods.include?(method_name.to_s)
end
end
end

View File

@@ -6,6 +6,8 @@ module Liquid
if markup =~ Syntax
@variable_name = $1
@collection_name = $2
@idx_i = "#{$1}-#{$2}-i"
@idx_col = "#{$1}-#{$2}-c"
@attributes = {}
markup.scan(TagAttributes) do |key, value|
@attributes[key] = value
@@ -18,6 +20,8 @@ module Liquid
end
def render(context)
context.registers[:tablerowloop] ||= Hash.new(0)
collection = context[@collection_name] or return ''
from = @attributes['offset'] ? context[@attributes['offset']].to_i : 0
@@ -32,26 +36,15 @@ module Liquid
row = 1
col = 0
result = "<tr class=\"row1\">\n"
result = ["<tr class=\"row1\">\n"]
context.stack do
context.registers[:tablerowloop][@idx]
context['tablerowloop'] = lambda { Tablerowloop.new(@idx_i, @idx_col, length) }
collection.each_with_index do |item, index|
context[@variable_name] = item
context['tablerowloop'] = {
'length' => length,
'index' => index + 1,
'index0' => index,
'col' => col + 1,
'col0' => col,
'index0' => index,
'rindex' => length - index,
'rindex0' => length - index - 1,
'first' => (index == 0),
'last' => (index == length - 1),
'col_first' => (col == 0),
'col_last' => (col == cols - 1)
}
context.registers[:tablerowloop][@idx_i] = index
context.registers[:tablerowloop][@idx_col] = col
context[@variable_name] = item
col += 1
@@ -68,6 +61,58 @@ module Liquid
result << "</tr>\n"
result
end
private
class Tablerowloop < Liquid::Drop
attr_accessor :length
def initialize(idx_i, idx_col, length)
@idx_i, @idx_col, @length = idx_i, idx_col, length
end
def index
@context.registers[:tablerowloop][@idx_i] + 1
end
def index0
@context.registers[:tablerowloop][@idx_i]
end
def rindex
length - @context.registers[:tablerowloop][@idx_i]
end
def rindex0
length - @context.registers[:tablerowloop][@idx_i] - 1
end
def first
(@context.registers[:tablerowloop][@idx_i] == 0)
end
def last
(@context.registers[:tablerowloop][@idx_i] == length - 1)
end
def col
@context.registers[:tablerowloop][@idx_col] + 1
end
def col0
@context.registers[:tablerowloop][@idx_col]
end
def col_first
(@context.registers[:tablerowloop][@idx_col] == 0)
end
def col_last
(@context.registers[:tablerowloop][@idx_col] == cols - 1)
end
end
end
Template.register_tag('tablerow', TableRow)

View File

@@ -93,8 +93,10 @@ module Liquid
# map/collect on a given property
def map(input, property)
ary = [input].flatten
ary.map do |e|
e.respond_to?('[]') ? e[property] : nil
if ary.first.respond_to?('[]') and !ary.first[property].nil?
ary.map {|e| e[property] }
elsif ary.first.respond_to?(property)
ary.map {|e| e.send(property) }
end
end

View File

@@ -2,15 +2,24 @@ require 'set'
module Liquid
parent_object = if defined? BlankObject
BlankObject
else
Object
end
# Strainer is the parent class for the filters system.
# New filters are mixed into the strainer class which is then instantiated for each liquid template render run.
# New filters are mixed into the strainer class which is then instanciated for each liquid template render run.
#
# The Strainer only allows method calls defined in filters given to it via Strainer.global_filter,
# Context#add_filters or Template.register_filter
class Strainer #:nodoc:
# One of the strainer's responsibilities is to keep malicious method calls out
class Strainer < parent_object #:nodoc:
INTERNAL_METHOD = /^__/
@@required_methods = Set.new([:__id__, :__send__, :respond_to?, :kind_of?, :extend, :methods, :singleton_methods, :class, :object_id])
# Ruby 1.9.2 introduces Object#respond_to_missing?, which is invoked by Object#respond_to?
@@required_methods << :respond_to_missing? if Object.respond_to? :respond_to_missing?
@@filters = {}
@@known_filters = Set.new
@@known_methods = Set.new
def initialize(context)
@context = context
@@ -18,36 +27,28 @@ module Liquid
def self.global_filter(filter)
raise ArgumentError, "Passed filter is not a module" unless filter.is_a?(Module)
add_known_filter(filter)
@@filters[filter.name] = filter
end
def self.add_known_filter(filter)
unless @@known_filters.include?(filter)
@@method_blacklist ||= Set.new(Strainer.instance_methods.map(&:to_s))
new_methods = filter.instance_methods.map(&:to_s)
new_methods.reject!{ |m| @@method_blacklist.include?(m) }
@@known_methods.merge(new_methods)
@@known_filters.add(filter)
end
end
def self.create(context)
strainer = Strainer.new(context)
@@filters.each { |k,m| strainer.extend(m) }
strainer
end
def invoke(method, *args)
if invokable?(method)
send(method, *args)
else
args.first
end
def respond_to?(method, include_private = false)
method_name = method.to_s
return false if method_name =~ INTERNAL_METHOD
return false if @@required_methods.include?(method_name)
super
end
def invokable?(method)
@@known_methods.include?(method.to_s) && respond_to?(method)
# remove all standard methods from the bucket so circumvent security
# problems
instance_methods.each do |m|
unless @@required_methods.include?(m.to_sym)
undef_method m
end
end
end
end

View File

@@ -31,7 +31,7 @@ module Liquid
context.stack do
execute_else_block = true
output = ''
output = []
@blocks.each do |block|
if block.else?
return render_all(block.attachment, context) if execute_else_block

View File

@@ -50,7 +50,8 @@ module Liquid
if markup =~ Syntax
@variable_name = $1
@collection_name = $2
@name = "#{$1}-#{$2}"
@name = "#{$1}-#{$2}"
@idx = "#{@name}-i"
@reversed = $3
@attributes = {}
markup.scan(TagAttributes) do |key, value|
@@ -87,14 +88,13 @@ module Liquid
limit = context[@attributes['limit']]
to = limit ? limit.to_i + from : nil
segment = Utils.slice_collection_using_each(collection, from, to)
return render_else(context) if segment.empty?
segment.reverse! if @reversed
result = ''
result = []
length = segment.length
@@ -102,17 +102,10 @@ module Liquid
context.registers[:for][@name] = from + segment.length
context.stack do
context['forloop'] = lambda { Forloop.new(@name, @idx, length) }
segment.each_with_index do |item, index|
context.registers[:for][@idx] = index
context[@variable_name] = item
context['forloop'] = {
'name' => @name,
'length' => length,
'index' => index + 1,
'index0' => index,
'rindex' => length - index,
'rindex0' => length - index - 1,
'first' => (index == 0),
'last' => (index == length - 1) }
result << render_all(@for_block, context)
@@ -129,6 +122,39 @@ module Liquid
private
class Forloop < Liquid::Drop
attr_accessor :name, :length
def initialize(name, idx, length)
@name, @idx, @length = name, idx, length
end
def index
@context.registers[:for][@idx] + 1
end
def index0
@context.registers[:for][@idx]
end
def rindex
length - @context.registers[:for][@idx]
end
def rindex0
length - @context.registers[:for][@idx] - 1
end
def first
(@context.registers[:for][@idx] == 0)
end
def last
(@context.registers[:for][@idx] == length - 1)
end
end
def render_else(context)
return @else_block ? [render_all(@else_block, context)] : ''
end

View File

@@ -121,8 +121,7 @@ module Liquid
begin
# render the nodelist.
# for performance reasons we get a array back here. join will make a string out of it
result = @root.render(context)
result.respond_to?(:join) ? result.join : result
@root.render(context).join
ensure
@errors = context.errors
end

View File

@@ -11,7 +11,7 @@ module Liquid
# {{ user | link }}
#
class Variable
FilterParser = /(?:#{FilterSeparator}|(?:\s*(?:#{QuotedFragment}|#{ArgumentSeparator})\s*)+)/o
FilterParser = /(?:#{FilterSeparator}|(?:\s*(?!(?:#{FilterSeparator}))(?:#{QuotedFragment}|\S+)\s*)+)/o
attr_accessor :filters, :name
def initialize(markup)
@@ -23,10 +23,10 @@ module Liquid
if match[2].match(/#{FilterSeparator}\s*(.*)/o)
filters = Regexp.last_match(1).scan(FilterParser)
filters.each do |f|
if matches = f.match(/\s*(\w+)(?:\s*#{FilterArgumentSeparator}(.*))?/)
if matches = f.match(/\s*(\w+)/)
filtername = matches[1]
filterargs = matches[2].to_s.scan(/(?:\A|#{ArgumentSeparator})\s*((?:\w+\s*\:\s*)?#{QuotedFragment})/o).flatten
@filters << [filtername, filterargs]
filterargs = f.scan(/(?:#{FilterArgumentSeparator}|#{ArgumentSeparator})\s*(#{QuotedFragment})/o).flatten
@filters << [filtername.to_sym, filterargs]
end
end
end
@@ -36,16 +36,9 @@ module Liquid
def render(context)
return '' if @name.nil?
@filters.inject(context[@name]) do |output, filter|
filterargs = []
keyword_args = {}
filter[1].to_a.each do |a|
if matches = a.match(/\A#{TagAttributes}\z/o)
keyword_args[matches[1]] = context[matches[2]]
else
filterargs << context[a]
end
filterargs = filter[1].to_a.collect do |a|
context[a]
end
filterargs << keyword_args unless keyword_args.empty?
begin
output = context.invoke(filter[0], output, *filterargs)
rescue FilterNotFound

View File

@@ -2,7 +2,7 @@
Gem::Specification.new do |s|
s.name = "liquid"
s.version = "2.5.1"
s.version = "2.4.1"
s.platform = Gem::Platform::RUBY
s.summary = "A secure, non-evaling end user template engine with aesthetic markup."
s.authors = ["Tobias Luetke"]

View File

@@ -4,8 +4,8 @@ require File.dirname(__FILE__) + '/theme_runner'
profiler = ThemeRunner.new
Benchmark.bmbm do |x|
x.report("parse:") { 100.times { profiler.compile } }
Benchmark.bm do |x|
# x.report("parse:") { 100.times { profiler.compile } }
x.report("parse & run:") { 100.times { profiler.run } }
end

View File

@@ -6,14 +6,12 @@ profiler = ThemeRunner.new
puts 'Running profiler...'
results = profiler.run
results = profiler.run_profile
puts 'Success'
puts
[RubyProf::FlatPrinter, RubyProf::GraphPrinter, RubyProf::GraphHtmlPrinter, RubyProf::CallTreePrinter].each do |klass|
filename = (ENV['TMP'] || '/tmp') + (klass.name.include?('Html') ? "/liquid.#{klass.name.downcase}.html" : "/callgrind.liquid.#{klass.name.downcase}.txt")
filename.gsub!(/:+/, '_')
File.open(filename, "w+") { |fp| klass.new(results).print(fp, :print_file => true) }
$stderr.puts "wrote #{klass.name} output to #{filename}"
filename = (ENV['TMP'] || '/tmp') + "/callgrind.liquid.txt"
File.open(filename, "w+") do |fp|
RubyProf::CallTreePrinter.new(results).print(fp, :print_file => true)
end
$stderr.puts "wrote RubyProf::CallTreePrinter output to #{filename}"

View File

@@ -31,7 +31,6 @@ class ThemeRunner
# Dup assigns because will make some changes to them
@tests.each do |liquid, layout, template_name|
tmpl = Liquid::Template.new
tmpl.parse(liquid)
tmpl = Liquid::Template.new
@@ -54,7 +53,7 @@ class ThemeRunner
def run_profile
RubyProf.measure_mode = RubyProf::WALL_TIME
RubyProf.measure_mode = RubyProf::PROCESS_TIME
# Dup assigns because will make some changes to them
assigns = Database.tables.dup

View File

@@ -189,10 +189,10 @@ class ContextTest < Test::Unit::TestCase
end
context = Context.new
assert_equal "Wookie", context.invoke("hi", "Wookie")
methods_before = context.strainer.methods.map { |method| method.to_s }
context.add_filters(filter)
assert_equal "Wookie hi!", context.invoke("hi", "Wookie")
methods_after = context.strainer.methods.map { |method| method.to_s }
assert_equal (methods_before + ["hi"]).sort, methods_after.sort
end
def test_add_item_in_outer_scope
@@ -254,12 +254,16 @@ class ContextTest < Test::Unit::TestCase
@context['test'] = {'test' => [1,2,3,4,5]}
assert_equal 1, @context['test.test[0]']
end
def test_recoursive_array_notation_for_hash
@context['test'] = [{'test' => 'worked'}]
assert_equal 'worked', @context['test[0].test']
end
def test_hash_to_array_transition
@context['colors'] = {
'Blue' => ['003366','336699', '6699CC', '99CCFF'],
@@ -315,7 +319,7 @@ class ContextTest < Test::Unit::TestCase
@context['nested'] = {'var' => 'tags'}
@context['products'] = {'count' => 5, 'tags' => ['deepsnow', 'freestyle'] }
assert_equal 'deepsnow', @context['products[var].first']
#assert_equal 'deepsnow', @context['products[var].first']
assert_equal 'freestyle', @context['products[nested.var].last']
end

View File

@@ -71,29 +71,23 @@ class DropsTest < Test::Unit::TestCase
include Liquid
def test_product_drop
assert_nothing_raised do
tpl = Liquid::Template.parse( ' ' )
tpl.render('product' => ProductDrop.new)
end
end
def test_drop_does_only_respond_to_whitelisted_methods
assert_equal "", Liquid::Template.parse("{{ product.inspect }}").render('product' => ProductDrop.new)
assert_equal "", Liquid::Template.parse("{{ product.pretty_inspect }}").render('product' => ProductDrop.new)
assert_equal "", Liquid::Template.parse("{{ product.whatever }}").render('product' => ProductDrop.new)
assert_equal "", Liquid::Template.parse('{{ product | map: "inspect" }}').render('product' => ProductDrop.new)
assert_equal "", Liquid::Template.parse('{{ product | map: "pretty_inspect" }}').render('product' => ProductDrop.new)
assert_equal "", Liquid::Template.parse('{{ product | map: "whatever" }}').render('product' => ProductDrop.new)
end
def test_text_drop
output = Liquid::Template.parse( ' {{ product.texts.text }} ' ).render('product' => ProductDrop.new)
assert_equal ' text1 ', output
end
def test_unknown_method
output = Liquid::Template.parse( ' {{ product.catchall.unknown }} ' ).render('product' => ProductDrop.new)
assert_equal ' method: unknown ', output
end
def test_integer_argument_drop
@@ -121,13 +115,6 @@ class DropsTest < Test::Unit::TestCase
assert_equal ' ', output
end
def test_object_methods_not_allowed
[:dup, :clone, :singleton_class, :eval, :class_eval, :inspect].each do |method|
output = Liquid::Template.parse(" {{ product.#{method} }} ").render('product' => ProductDrop.new)
assert_equal ' ', output
end
end
def test_scope
assert_equal '1', Liquid::Template.parse( '{{ context.scopes }}' ).render('context' => ContextDrop.new)
assert_equal '2', Liquid::Template.parse( '{%for i in dummy%}{{ context.scopes }}{%endfor%}' ).render('context' => ContextDrop.new, 'dummy' => [1])

View File

@@ -16,12 +16,6 @@ module CanadianMoneyFilter
end
end
module SubstituteFilter
def substitute(input, params={})
input.gsub(/%\{(\w+)\}/) { |match| params[$1] }
end
end
class FiltersTest < Test::Unit::TestCase
include Liquid
@@ -98,13 +92,6 @@ class FiltersTest < Test::Unit::TestCase
assert_equal 1000, Variable.new("var | xyzzy").render(@context)
end
def test_filter_with_keyword_arguments
@context['surname'] = 'john'
@context.add_filters(SubstituteFilter)
output = Variable.new(%! 'hello %{first_name}, %{last_name}' | substitute: first_name: surname, last_name: 'doe' !).render(@context)
assert_equal 'hello john, doe', output
end
end
class FiltersInTemplate < Test::Unit::TestCase

View File

@@ -0,0 +1,93 @@
require 'test_helper'
class ParserTest < Test::Unit::TestCase
include Liquid
def test_strings
assert_equal [[:id, "string"]], Parser.parse('"string"')
assert_equal [[:id, "string"]], Parser.parse('\'string\'')
end
def test_integer
assert_equal [[:id, 1]], Parser.parse('1')
assert_equal [[:id, 100001]], Parser.parse('100001')
end
def test_float
assert_equal [[:id, 1.1]], Parser.parse('1.1')
assert_equal [[:id, 1.55435]], Parser.parse('1.55435')
end
def test_null
assert_equal [[:id, nil]], Parser.parse('null')
assert_equal [[:id, nil]], Parser.parse('nil')
end
def test_bool
assert_equal [[:id, true]], Parser.parse('true')
assert_equal [[:id, false]], Parser.parse('false')
end
def test_ranges
assert_equal [[:id, 1], [:id, 5], [:range, nil]], Parser.parse('(1..5)')
assert_equal [[:id, 100], [:id, 500], [:range, nil]], Parser.parse('(100..500)')
end
def test_ranges_with_lookups
assert_equal [[:id, 1], [:id, "test"], [:lookup, nil], [:range, nil]], Parser.parse('(1..test)')
end
def test_lookups
assert_equal [[:id, "variable"], [:lookup, nil]], Parser.parse('variable')
assert_equal [[:id, "underscored_variable"], [:lookup, nil]], Parser.parse('underscored_variable')
assert_equal [[:id, "c"], [:lookup, nil]], Parser.parse('c')
end
def test_global_hash
assert_equal [[:id, true], [:lookup, nil]], Parser.parse('[true]')
assert_equal [[:id, "string"], [:lookup, nil]], Parser.parse('["string"]')
assert_equal [[:id, 5.55], [:lookup, nil]], Parser.parse('[5.55]')
assert_equal [[:id, 0], [:lookup, nil]], Parser.parse('[0]')
assert_equal [[:id, "variable"], [:lookup, nil], [:lookup, nil]], Parser.parse('[variable]')
end
def test_descent
assert_equal [[:id, "variable1"], [:lookup, nil], [:id, "variable2"], [:call, nil]], Parser.parse('variable1.variable2')
assert_equal [[:id, "variable1"], [:lookup, nil], [:id, "variable2"], [:call, nil], [:id, "variable3"], [:call, nil]], Parser.parse('variable1.variable2.variable3')
assert_equal [[:id, "variable1"], [:lookup, nil], [:id, "under_score"], [:call, nil]], Parser.parse('variable1.under_score')
assert_equal [[:id, "variable1"], [:lookup, nil], [:id, "question?"], [:call, nil]], Parser.parse('variable1.question?')
assert_equal [[:id, "variable1"], [:lookup, nil], [:id, "exclaimation!"], [:call, nil]], Parser.parse('variable1.exclaimation!')
end
def test_descent_hash
assert_equal [[:id, "variable1"], [:lookup, nil], [:id, "variable2"], [:call, nil]], Parser.parse('variable1["variable2"]')
assert_equal [[:id, "variable1"], [:lookup, nil], [:id, "variable2"], [:lookup, nil], [:call, nil]], Parser.parse('variable1[variable2]')
end
def test_buildin
assert_equal [[:id, "first"], [:lookup, nil]], Parser.parse('first')
assert_equal [[:id, "var"], [:lookup, nil], [:buildin, "first"]], Parser.parse('var.first')
assert_equal [[:id, "var"], [:lookup, nil], [:buildin, "last"]], Parser.parse('var.last')
assert_equal [[:id, "var"], [:lookup, nil], [:buildin, "size"]], Parser.parse('var.size')
end
def test_descent_hash_descent
assert_equal [[:id, "variable1"], [:lookup, nil], [:id, "test1"], [:lookup, nil], [:id, "test2"], [:call, nil], [:call, nil]],
Parser.parse('variable1[test1.test2]'), "resolove: variable1[test1.test2]"
assert_equal [[:id, "variable1"], [:lookup, nil], [:id, "test1"], [:lookup, nil], [:id, "test2"], [:call, nil], [:call, nil]],
Parser.parse('variable1[test1["test2"]]'), 'resolove: variable1[test1["test2"]]'
assert_equal [[:id, "variable1"], [:lookup, nil], [:id, "test1"], [:lookup, nil], [:id, "test2"], [:lookup, nil], [:call, nil], [:call, nil]],
Parser.parse('variable1[test1[test2]]'), "resolove: variable1[test1[test2]]"
end
end

View File

@@ -39,7 +39,7 @@ class ParsingQuirksTest < Test::Unit::TestCase
def test_meaningless_parens
assigns = {'b' => 'bar', 'c' => 'baz'}
markup = "a == 'foo' or (b == 'bar' and c == 'baz') or false"
markup = "a == 'foo' or b == 'bar' and c == 'baz' or false"
assert_template_result(' YES ',"{% if #{markup} %} YES {% endif %}", assigns)
end

View File

@@ -38,27 +38,4 @@ class SecurityTest < Test::Unit::TestCase
assert_equal expected, Template.parse(text).render(@assigns, :filters => SecurityFilter)
end
def test_does_not_add_filters_to_symbol_table
current_symbols = Symbol.all_symbols
test = %( {{ "some_string" | a_bad_filter }} )
template = Template.parse(test)
assert_equal [], (Symbol.all_symbols - current_symbols)
template.render
assert_equal [], (Symbol.all_symbols - current_symbols)
end
def test_does_not_add_drop_methods_to_symbol_table
current_symbols = Symbol.all_symbols
drop = Drop.new
drop.invoke_drop("custom_method_1")
drop.invoke_drop("custom_method_2")
drop.invoke_drop("custom_method_3")
assert_equal [], (Symbol.all_symbols - current_symbols)
end
end # SecurityTest

View File

@@ -86,11 +86,6 @@ class StandardFiltersTest < Test::Unit::TestCase
'ary' => [{'foo' => {'bar' => 'a'}}, {'foo' => {'bar' => 'b'}}, {'foo' => {'bar' => 'c'}}]
end
def test_map_doesnt_call_arbitrary_stuff
assert_equal "", Liquid::Template.parse('{{ "foo" | map: "__id__" }}').render
assert_equal "", Liquid::Template.parse('{{ "foo" | map: "inspect" }}').render
end
def test_date
assert_equal 'May', @filters.date(Time.parse("2006-05-05 10:00:00"), "%B")
assert_equal 'June', @filters.date(Time.parse("2006-06-05 10:00:00"), "%B")

View File

@@ -3,50 +3,23 @@ require 'test_helper'
class StrainerTest < Test::Unit::TestCase
include Liquid
module AccessScopeFilters
def public_filter
"public"
end
def private_filter
"private"
end
private :private_filter
end
Strainer.global_filter(AccessScopeFilters)
def test_strainer
strainer = Strainer.create(nil)
assert_equal 5, strainer.invoke('size', 'input')
assert_equal "public", strainer.invoke("public_filter")
assert_equal false, strainer.respond_to?('__test__')
assert_equal false, strainer.respond_to?('test')
assert_equal false, strainer.respond_to?('instance_eval')
assert_equal false, strainer.respond_to?('__send__')
assert_equal true, strainer.respond_to?('size') # from the standard lib
end
def test_strainer_only_invokes_public_filter_methods
def test_should_respond_to_two_parameters
strainer = Strainer.create(nil)
assert_equal false, strainer.invokable?('__test__')
assert_equal false, strainer.invokable?('test')
assert_equal false, strainer.invokable?('instance_eval')
assert_equal false, strainer.invokable?('__send__')
assert_equal true, strainer.invokable?('size') # from the standard lib
assert_equal true, strainer.respond_to?('size', false)
end
def test_strainer_returns_nil_if_no_filter_method_found
strainer = Strainer.create(nil)
assert_nil strainer.invoke("private_filter")
assert_nil strainer.invoke("undef_the_filter")
# Asserts that Object#respond_to_missing? is not being undefined in Ruby versions where it has been implemented
# Currently this method is only present in Ruby v1.9.2, or higher
def test_object_respond_to_missing
assert_equal Object.respond_to?(:respond_to_missing?), Strainer.create(nil).respond_to?(:respond_to_missing?)
end
def test_strainer_returns_first_argument_if_no_method_and_arguments_given
strainer = Strainer.create(nil)
assert_equal "password", strainer.invoke("undef_the_method", "password")
end
def test_strainer_only_allows_methods_defined_in_filters
strainer = Strainer.create(nil)
assert_equal "1 + 1", strainer.invoke("instance_eval", "1 + 1")
assert_equal "puts", strainer.invoke("__send__", "puts", "Hi Mom")
assert_equal "has_method?", strainer.invoke("invoke", "has_method?", "invoke")
end
end # StrainerTest

View File

@@ -11,71 +11,67 @@ class VariableTest < Test::Unit::TestCase
def test_filters
var = Variable.new('hello | textileze')
assert_equal 'hello', var.name
assert_equal [["textileze",[]]], var.filters
assert_equal [[:textileze,[]]], var.filters
var = Variable.new('hello | textileze | paragraph')
assert_equal 'hello', var.name
assert_equal [["textileze",[]], ["paragraph",[]]], var.filters
assert_equal [[:textileze,[]], [:paragraph,[]]], var.filters
var = Variable.new(%! hello | strftime: '%Y'!)
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 !)
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 !)
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 !)
assert_equal %!'foo'!, var.name
assert_equal [["repeat",["3"]]], var.filters
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
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
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
assert_equal [[:strftime,["'%Y, okay?'"]]], var.filters
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
assert_equal [[:things,["\"%Y, okay?\"","'the other one'"]]], 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
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
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
assert_equal [[:textileze,[]], [:paragraph,[]]], var.filters
end
def test_symbol
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
assert_equal [[:image,["'med'"]]], var.filters
end
def test_string_single_quoted
@@ -107,12 +103,6 @@ class VariableTest < Test::Unit::TestCase
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
end