mirror of
https://github.com/kemko/liquid.git
synced 2026-01-05 01:35:41 +03:00
Compare commits
9 Commits
liquid_c_e
...
cache_var_
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
717174a57a | ||
|
|
b8fbd2b4fa | ||
|
|
ba5a9f2e47 | ||
|
|
1e309ba74b | ||
|
|
485340713a | ||
|
|
2af4ea1295 | ||
|
|
c5dfcd29b0 | ||
|
|
f9c289372d | ||
|
|
0b36540b78 |
5
.gitignore
vendored
5
.gitignore
vendored
@@ -4,8 +4,3 @@
|
|||||||
pkg
|
pkg
|
||||||
*.rbc
|
*.rbc
|
||||||
.rvmrc
|
.rvmrc
|
||||||
*.o
|
|
||||||
*.bundle
|
|
||||||
ext/liquid/Makefile
|
|
||||||
ext/liquid/liquid_context.c
|
|
||||||
|
|
||||||
|
|||||||
44
Rakefile
44
Rakefile
@@ -2,50 +2,12 @@
|
|||||||
|
|
||||||
require 'rubygems'
|
require 'rubygems'
|
||||||
require 'rake'
|
require 'rake'
|
||||||
require 'rake/clean'
|
|
||||||
require 'fileutils'
|
|
||||||
require 'rake/testtask'
|
require 'rake/testtask'
|
||||||
require 'rubygems/package_task'
|
require 'rubygems/package_task'
|
||||||
|
|
||||||
task :default => [:compile, :test]
|
task :default => 'test'
|
||||||
|
|
||||||
task :gen do
|
Rake::TestTask.new(:test) do |t|
|
||||||
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.libs << '.' << 'lib' << 'test'
|
||||||
t.test_files = FileList['test/liquid/**/*_test.rb']
|
t.test_files = FileList['test/liquid/**/*_test.rb']
|
||||||
t.verbose = false
|
t.verbose = false
|
||||||
@@ -80,7 +42,7 @@ namespace :profile do
|
|||||||
|
|
||||||
desc "Run KCacheGrind"
|
desc "Run KCacheGrind"
|
||||||
task :grind => :run do
|
task :grind => :run do
|
||||||
system "qcachegrind /tmp//callgrind.liquid.txt"
|
system "qcachegrind /tmp/liquid.rubyprof_calltreeprinter.txt"
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,6 +0,0 @@
|
|||||||
require 'mkmf'
|
|
||||||
|
|
||||||
dir_config("liquid_ext")
|
|
||||||
have_library("c", "main")
|
|
||||||
|
|
||||||
create_makefile("liquid_ext")
|
|
||||||
@@ -1,98 +0,0 @@
|
|||||||
|
|
||||||
%{
|
|
||||||
//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;
|
|
||||||
}
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
#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);
|
|
||||||
}
|
|
||||||
@@ -20,14 +20,15 @@
|
|||||||
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
module Liquid
|
module Liquid
|
||||||
|
WordRegex = RUBY_VERSION < "1.9" ? '\w' : '[[:word:]]'
|
||||||
FilterSeparator = /\|/
|
FilterSeparator = /\|/
|
||||||
ArgumentSeparator = ','
|
ArgumentSeparator = ','
|
||||||
FilterArgumentSeparator = ':'
|
FilterArgumentSeparator = ':'
|
||||||
VariableAttributeSeparator = '.'
|
VariableAttributeSeparator = '.'
|
||||||
TagStart = /\{\%/
|
TagStart = /\{\%/
|
||||||
TagEnd = /\%\}/
|
TagEnd = /\%\}/
|
||||||
VariableSignature = /\(?[\w\-\.\[\]]\)?/
|
VariableSignature = /\(?[#{WordRegex}\-\.\[\]]\)?/o
|
||||||
VariableSegment = /[\w\-]/
|
VariableSegment = /[#{WordRegex}\-]/o
|
||||||
VariableStart = /\{\{/
|
VariableStart = /\{\{/
|
||||||
VariableEnd = /\}\}/
|
VariableEnd = /\}\}/
|
||||||
VariableIncompleteEnd = /\}\}?/
|
VariableIncompleteEnd = /\}\}?/
|
||||||
@@ -38,7 +39,7 @@ module Liquid
|
|||||||
OtherFilterArgument = /#{ArgumentSeparator}(?:#{StrictQuotedFragment})/o
|
OtherFilterArgument = /#{ArgumentSeparator}(?:#{StrictQuotedFragment})/o
|
||||||
SpacelessFilter = /^(?:'[^']+'|"[^"]+"|[^'"])*#{FilterSeparator}(?:#{StrictQuotedFragment})(?:#{FirstFilterArgument}(?:#{OtherFilterArgument})*)?/o
|
SpacelessFilter = /^(?:'[^']+'|"[^"]+"|[^'"])*#{FilterSeparator}(?:#{StrictQuotedFragment})(?:#{FirstFilterArgument}(?:#{OtherFilterArgument})*)?/o
|
||||||
Expression = /(?:#{QuotedFragment}(?:#{SpacelessFilter})*)/o
|
Expression = /(?:#{QuotedFragment}(?:#{SpacelessFilter})*)/o
|
||||||
TagAttributes = /(\w+)\s*\:\s*(#{QuotedFragment})/o
|
TagAttributes = /(#{WordRegex}+)\s*\:\s*(#{QuotedFragment})/o
|
||||||
AnyStartingTag = /\{\{|\{\%/
|
AnyStartingTag = /\{\{|\{\%/
|
||||||
PartialTemplateParser = /#{TagStart}.*?#{TagEnd}|#{VariableStart}.*?#{VariableIncompleteEnd}/o
|
PartialTemplateParser = /#{TagStart}.*?#{TagEnd}|#{VariableStart}.*?#{VariableIncompleteEnd}/o
|
||||||
TemplateParser = /(#{PartialTemplateParser}|#{AnyStartingTag})/o
|
TemplateParser = /(#{PartialTemplateParser}|#{AnyStartingTag})/o
|
||||||
@@ -62,7 +63,6 @@ require 'liquid/standardfilters'
|
|||||||
require 'liquid/condition'
|
require 'liquid/condition'
|
||||||
require 'liquid/module_ex'
|
require 'liquid/module_ex'
|
||||||
require 'liquid/utils'
|
require 'liquid/utils'
|
||||||
require 'liquid_ext'
|
|
||||||
|
|
||||||
# Load all the tags of the standard library
|
# Load all the tags of the standard library
|
||||||
#
|
#
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ module Liquid
|
|||||||
class Block < Tag
|
class Block < Tag
|
||||||
IsTag = /^#{TagStart}/o
|
IsTag = /^#{TagStart}/o
|
||||||
IsVariable = /^#{VariableStart}/o
|
IsVariable = /^#{VariableStart}/o
|
||||||
FullToken = /^#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}$/o
|
FullToken = /^#{TagStart}\s*(#{WordRegex}+)\s*(.*)?#{TagEnd}$/o
|
||||||
ContentOfVariable = /^#{VariableStart}(.*)#{VariableEnd}$/o
|
ContentOfVariable = /^#{VariableStart}(.*)#{VariableEnd}$/o
|
||||||
|
|
||||||
def parse(tokens)
|
def parse(tokens)
|
||||||
@@ -109,7 +109,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
output
|
output.join
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ module Liquid
|
|||||||
squash_instance_assigns_with_environments
|
squash_instance_assigns_with_environments
|
||||||
|
|
||||||
@interrupts = []
|
@interrupts = []
|
||||||
|
@variable_cache = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
def strainer
|
def strainer
|
||||||
@@ -78,17 +79,20 @@ module Liquid
|
|||||||
|
|
||||||
# Push new local scope on the stack. use <tt>Context#stack</tt> instead
|
# Push new local scope on the stack. use <tt>Context#stack</tt> instead
|
||||||
def push(new_scope={})
|
def push(new_scope={})
|
||||||
|
@variable_cache = {}
|
||||||
@scopes.unshift(new_scope)
|
@scopes.unshift(new_scope)
|
||||||
raise StackLevelError, "Nesting too deep" if @scopes.length > 100
|
raise StackLevelError, "Nesting too deep" if @scopes.length > 100
|
||||||
end
|
end
|
||||||
|
|
||||||
# Merge a hash of variables in the current local scope
|
# Merge a hash of variables in the current local scope
|
||||||
def merge(new_scopes)
|
def merge(new_scopes)
|
||||||
|
@variable_cache = {}
|
||||||
@scopes[0].merge!(new_scopes)
|
@scopes[0].merge!(new_scopes)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Pop from the stack. use <tt>Context#stack</tt> instead
|
# Pop from the stack. use <tt>Context#stack</tt> instead
|
||||||
def pop
|
def pop
|
||||||
|
@variable_cache = {}
|
||||||
raise ContextError if @scopes.size == 1
|
raise ContextError if @scopes.size == 1
|
||||||
@scopes.shift
|
@scopes.shift
|
||||||
end
|
end
|
||||||
@@ -102,13 +106,16 @@ module Liquid
|
|||||||
#
|
#
|
||||||
# context['var] #=> nil
|
# context['var] #=> nil
|
||||||
def stack(new_scope={})
|
def stack(new_scope={})
|
||||||
|
@variable_cache = {}
|
||||||
push(new_scope)
|
push(new_scope)
|
||||||
yield
|
yield
|
||||||
ensure
|
ensure
|
||||||
pop
|
pop
|
||||||
|
@variable_cache = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
def clear_instance_assigns
|
def clear_instance_assigns
|
||||||
|
@variable_cache = {}
|
||||||
@scopes[0] = {}
|
@scopes[0] = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -171,6 +178,7 @@ module Liquid
|
|||||||
|
|
||||||
# Only allow String, Numeric, Hash, Array, Proc, Boolean or <tt>Liquid::Drop</tt>
|
# Only allow String, Numeric, Hash, Array, Proc, Boolean or <tt>Liquid::Drop</tt>
|
||||||
def []=(key, value)
|
def []=(key, value)
|
||||||
|
@variable_cache[key] = value
|
||||||
@scopes[0][key] = value
|
@scopes[0][key] = value
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -200,6 +208,10 @@ module Liquid
|
|||||||
|
|
||||||
# Fetches an object starting at the local scope and then moving up the hierachy
|
# Fetches an object starting at the local scope and then moving up the hierachy
|
||||||
def find_variable(key)
|
def find_variable(key)
|
||||||
|
if val = @variable_cache[key]
|
||||||
|
return val
|
||||||
|
end
|
||||||
|
|
||||||
scope = @scopes.find { |s| s.has_key?(key) }
|
scope = @scopes.find { |s| s.has_key?(key) }
|
||||||
|
|
||||||
if scope.nil?
|
if scope.nil?
|
||||||
@@ -213,6 +225,7 @@ module Liquid
|
|||||||
|
|
||||||
scope ||= @environments.last || @scopes.last
|
scope ||= @environments.last || @scopes.last
|
||||||
variable ||= lookup_and_evaluate(scope, key)
|
variable ||= lookup_and_evaluate(scope, key)
|
||||||
|
@variable_cache[key] = variable
|
||||||
|
|
||||||
return variable
|
return variable
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,13 +1,11 @@
|
|||||||
module Liquid
|
module Liquid
|
||||||
class TableRow < Block
|
class TableRow < Block
|
||||||
Syntax = /(\w+)\s+in\s+(#{QuotedFragment}+)/o
|
Syntax = /(#{WordRegex}+)\s+in\s+(#{QuotedFragment}+)/o
|
||||||
|
|
||||||
def initialize(tag_name, markup, tokens)
|
def initialize(tag_name, markup, tokens)
|
||||||
if markup =~ Syntax
|
if markup =~ Syntax
|
||||||
@variable_name = $1
|
@variable_name = $1
|
||||||
@collection_name = $2
|
@collection_name = $2
|
||||||
@idx_i = "#{$1}-#{$2}-i"
|
|
||||||
@idx_col = "#{$1}-#{$2}-c"
|
|
||||||
@attributes = {}
|
@attributes = {}
|
||||||
markup.scan(TagAttributes) do |key, value|
|
markup.scan(TagAttributes) do |key, value|
|
||||||
@attributes[key] = value
|
@attributes[key] = value
|
||||||
@@ -20,8 +18,6 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render(context)
|
def render(context)
|
||||||
context.registers[:tablerowloop] ||= Hash.new(0)
|
|
||||||
|
|
||||||
collection = context[@collection_name] or return ''
|
collection = context[@collection_name] or return ''
|
||||||
|
|
||||||
from = @attributes['offset'] ? context[@attributes['offset']].to_i : 0
|
from = @attributes['offset'] ? context[@attributes['offset']].to_i : 0
|
||||||
@@ -36,15 +32,26 @@ module Liquid
|
|||||||
row = 1
|
row = 1
|
||||||
col = 0
|
col = 0
|
||||||
|
|
||||||
result = ["<tr class=\"row1\">\n"]
|
result = "<tr class=\"row1\">\n"
|
||||||
context.stack do
|
context.stack do
|
||||||
|
|
||||||
context.registers[:tablerowloop][@idx]
|
|
||||||
context['tablerowloop'] = lambda { Tablerowloop.new(@idx_i, @idx_col, length) }
|
|
||||||
collection.each_with_index do |item, index|
|
collection.each_with_index do |item, index|
|
||||||
context.registers[:tablerowloop][@idx_i] = index
|
context[@variable_name] = item
|
||||||
context.registers[:tablerowloop][@idx_col] = col
|
context['tablerowloop'] = {
|
||||||
context[@variable_name] = item
|
'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)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
col += 1
|
col += 1
|
||||||
|
|
||||||
@@ -61,58 +68,6 @@ module Liquid
|
|||||||
result << "</tr>\n"
|
result << "</tr>\n"
|
||||||
result
|
result
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
Template.register_tag('tablerow', TableRow)
|
Template.register_tag('tablerow', TableRow)
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ module Liquid
|
|||||||
# in a sidebar or footer.
|
# in a sidebar or footer.
|
||||||
#
|
#
|
||||||
class Capture < Block
|
class Capture < Block
|
||||||
Syntax = /(\w+)/
|
Syntax = /(#{WordRegex}+)/o
|
||||||
|
|
||||||
def initialize(tag_name, markup, tokens)
|
def initialize(tag_name, markup, tokens)
|
||||||
if markup =~ Syntax
|
if markup =~ Syntax
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ module Liquid
|
|||||||
context.stack do
|
context.stack do
|
||||||
execute_else_block = true
|
execute_else_block = true
|
||||||
|
|
||||||
output = []
|
output = ''
|
||||||
@blocks.each do |block|
|
@blocks.each do |block|
|
||||||
if block.else?
|
if block.else?
|
||||||
return render_all(block.attachment, context) if execute_else_block
|
return render_all(block.attachment, context) if execute_else_block
|
||||||
|
|||||||
@@ -44,14 +44,13 @@ module Liquid
|
|||||||
# forloop.last:: Returns true if the item is the last item.
|
# forloop.last:: Returns true if the item is the last item.
|
||||||
#
|
#
|
||||||
class For < Block
|
class For < Block
|
||||||
Syntax = /(\w+)\s+in\s+(#{QuotedFragment}+)\s*(reversed)?/o
|
Syntax = /(#{WordRegex}+)\s+in\s+(#{QuotedFragment}+)\s*(reversed)?/ou
|
||||||
|
|
||||||
def initialize(tag_name, markup, tokens)
|
def initialize(tag_name, markup, tokens)
|
||||||
if markup =~ Syntax
|
if markup =~ Syntax
|
||||||
@variable_name = $1
|
@variable_name = $1
|
||||||
@collection_name = $2
|
@collection_name = $2
|
||||||
@name = "#{$1}-#{$2}"
|
@name = "#{$1}-#{$2}"
|
||||||
@idx = "#{@name}-i"
|
|
||||||
@reversed = $3
|
@reversed = $3
|
||||||
@attributes = {}
|
@attributes = {}
|
||||||
markup.scan(TagAttributes) do |key, value|
|
markup.scan(TagAttributes) do |key, value|
|
||||||
@@ -88,13 +87,14 @@ module Liquid
|
|||||||
limit = context[@attributes['limit']]
|
limit = context[@attributes['limit']]
|
||||||
to = limit ? limit.to_i + from : nil
|
to = limit ? limit.to_i + from : nil
|
||||||
|
|
||||||
|
|
||||||
segment = Utils.slice_collection_using_each(collection, from, to)
|
segment = Utils.slice_collection_using_each(collection, from, to)
|
||||||
|
|
||||||
return render_else(context) if segment.empty?
|
return render_else(context) if segment.empty?
|
||||||
|
|
||||||
segment.reverse! if @reversed
|
segment.reverse! if @reversed
|
||||||
|
|
||||||
result = []
|
result = ''
|
||||||
|
|
||||||
length = segment.length
|
length = segment.length
|
||||||
|
|
||||||
@@ -102,10 +102,17 @@ module Liquid
|
|||||||
context.registers[:for][@name] = from + segment.length
|
context.registers[:for][@name] = from + segment.length
|
||||||
|
|
||||||
context.stack do
|
context.stack do
|
||||||
context['forloop'] = lambda { Forloop.new(@name, @idx, length) }
|
|
||||||
segment.each_with_index do |item, index|
|
segment.each_with_index do |item, index|
|
||||||
context.registers[:for][@idx] = index
|
|
||||||
context[@variable_name] = item
|
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)
|
result << render_all(@for_block, context)
|
||||||
|
|
||||||
@@ -122,39 +129,6 @@ module Liquid
|
|||||||
|
|
||||||
private
|
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)
|
def render_else(context)
|
||||||
return @else_block ? [render_all(@else_block, context)] : ''
|
return @else_block ? [render_all(@else_block, context)] : ''
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -2,10 +2,10 @@ module Liquid
|
|||||||
class Include < Tag
|
class Include < Tag
|
||||||
Syntax = /(#{QuotedFragment}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?/o
|
Syntax = /(#{QuotedFragment}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?/o
|
||||||
|
|
||||||
def initialize(tag_name, markup, tokens)
|
def initialize(tag_name, markup, tokens)
|
||||||
if markup =~ Syntax
|
if markup =~ Syntax
|
||||||
|
|
||||||
@template_name = $1
|
@template_name = $1
|
||||||
@variable_name = $3
|
@variable_name = $3
|
||||||
@attributes = {}
|
@attributes = {}
|
||||||
|
|
||||||
@@ -24,8 +24,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render(context)
|
def render(context)
|
||||||
source = _read_template_from_file_system(context)
|
partial = load_cached_partial(context)
|
||||||
partial = Liquid::Template.parse(source)
|
|
||||||
variable = context[@variable_name || @template_name[1..-2]]
|
variable = context[@variable_name || @template_name[1..-2]]
|
||||||
|
|
||||||
context.stack do
|
context.stack do
|
||||||
@@ -46,7 +45,21 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def _read_template_from_file_system(context)
|
def load_cached_partial(context)
|
||||||
|
cached_partials = context.registers[:cached_partials] || {}
|
||||||
|
template_name = context[@template_name]
|
||||||
|
|
||||||
|
if cached = cached_partials[template_name]
|
||||||
|
return cached
|
||||||
|
end
|
||||||
|
source = read_template_from_file_system(context)
|
||||||
|
partial = Liquid::Template.parse(source)
|
||||||
|
cached_partials[template_name] = partial
|
||||||
|
context.registers[:cached_partials] = cached_partials
|
||||||
|
partial
|
||||||
|
end
|
||||||
|
|
||||||
|
def read_template_from_file_system(context)
|
||||||
file_system = context.registers[:file_system] || Liquid::Template.file_system
|
file_system = context.registers[:file_system] || Liquid::Template.file_system
|
||||||
|
|
||||||
# make read_template_file call backwards-compatible.
|
# make read_template_file call backwards-compatible.
|
||||||
|
|||||||
@@ -121,7 +121,8 @@ module Liquid
|
|||||||
begin
|
begin
|
||||||
# render the nodelist.
|
# render the nodelist.
|
||||||
# for performance reasons we get a array back here. join will make a string out of it
|
# for performance reasons we get a array back here. join will make a string out of it
|
||||||
@root.render(context).join
|
result = @root.render(context)
|
||||||
|
result.respond_to?(:join) ? result.join : result
|
||||||
ensure
|
ensure
|
||||||
@errors = context.errors
|
@errors = context.errors
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ module Liquid
|
|||||||
if match[2].match(/#{FilterSeparator}\s*(.*)/o)
|
if match[2].match(/#{FilterSeparator}\s*(.*)/o)
|
||||||
filters = Regexp.last_match(1).scan(FilterParser)
|
filters = Regexp.last_match(1).scan(FilterParser)
|
||||||
filters.each do |f|
|
filters.each do |f|
|
||||||
if matches = f.match(/\s*(\w+)(?:\s*#{FilterArgumentSeparator}(.*))?/)
|
if matches = f.match(/\s*(#{WordRegex}+)(?:\s*#{FilterArgumentSeparator}(.*))?/)
|
||||||
filtername = matches[1]
|
filtername = matches[1]
|
||||||
filterargs = matches[2].to_s.scan(/(?:\A|#{ArgumentSeparator})\s*((?:\w+\s*\:\s*)?#{QuotedFragment})/o).flatten
|
filterargs = matches[2].to_s.scan(/(?:\A|#{ArgumentSeparator})\s*((?:\w+\s*\:\s*)?#{QuotedFragment})/o).flatten
|
||||||
@filters << [filtername, filterargs]
|
@filters << [filtername, filterargs]
|
||||||
|
|||||||
@@ -13,12 +13,9 @@ Gem::Specification.new do |s|
|
|||||||
s.required_rubygems_version = ">= 1.3.7"
|
s.required_rubygems_version = ">= 1.3.7"
|
||||||
|
|
||||||
s.test_files = Dir.glob("{test}/**/*")
|
s.test_files = Dir.glob("{test}/**/*")
|
||||||
s.files = Dir.glob("{lib}/**/*") +
|
s.files = Dir.glob("{lib}/**/*") + %w(MIT-LICENSE README.md)
|
||||||
Dir.glob("{ext}/**/*") + %w(MIT-LICENSE README.md)
|
|
||||||
|
|
||||||
s.extensions = ['ext/liquid/extconf.rb']
|
|
||||||
|
|
||||||
s.extra_rdoc_files = ["History.md", "README.md"]
|
s.extra_rdoc_files = ["History.md", "README.md"]
|
||||||
|
|
||||||
s.require_paths = ["lib", "ext"]
|
s.require_path = "lib"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ require File.dirname(__FILE__) + '/theme_runner'
|
|||||||
|
|
||||||
profiler = ThemeRunner.new
|
profiler = ThemeRunner.new
|
||||||
|
|
||||||
Benchmark.bm do |x|
|
Benchmark.bmbm do |x|
|
||||||
# x.report("parse:") { 100.times { profiler.compile } }
|
x.report("parse:") { 100.times { profiler.compile } }
|
||||||
x.report("parse & run:") { 100.times { profiler.run } }
|
x.report("parse & run:") { 100.times { profiler.run } }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -6,12 +6,14 @@ profiler = ThemeRunner.new
|
|||||||
|
|
||||||
puts 'Running profiler...'
|
puts 'Running profiler...'
|
||||||
|
|
||||||
results = profiler.run_profile
|
results = profiler.run
|
||||||
|
|
||||||
puts 'Success'
|
puts 'Success'
|
||||||
|
puts
|
||||||
|
|
||||||
filename = (ENV['TMP'] || '/tmp') + "/callgrind.liquid.txt"
|
[RubyProf::FlatPrinter, RubyProf::GraphPrinter, RubyProf::GraphHtmlPrinter, RubyProf::CallTreePrinter].each do |klass|
|
||||||
File.open(filename, "w+") do |fp|
|
filename = (ENV['TMP'] || '/tmp') + (klass.name.include?('Html') ? "/liquid.#{klass.name.downcase}.html" : "/callgrind.liquid.#{klass.name.downcase}.txt")
|
||||||
RubyProf::CallTreePrinter.new(results).print(fp, :print_file => true)
|
filename.gsub!(/:+/, '_')
|
||||||
|
File.open(filename, "w+") { |fp| klass.new(results).print(fp, :print_file => true) }
|
||||||
|
$stderr.puts "wrote #{klass.name} output to #{filename}"
|
||||||
end
|
end
|
||||||
$stderr.puts "wrote RubyProf::CallTreePrinter output to #{filename}"
|
|
||||||
|
|||||||
@@ -14,6 +14,17 @@ require File.dirname(__FILE__) + '/shopify/liquid'
|
|||||||
require File.dirname(__FILE__) + '/shopify/database.rb'
|
require File.dirname(__FILE__) + '/shopify/database.rb'
|
||||||
|
|
||||||
class ThemeRunner
|
class ThemeRunner
|
||||||
|
class FileSystem
|
||||||
|
|
||||||
|
def initialize(path)
|
||||||
|
@path = path
|
||||||
|
end
|
||||||
|
|
||||||
|
# Called by Liquid to retrieve a template file
|
||||||
|
def read_template_file(template_path, context)
|
||||||
|
File.read(@path + '/' + template_path + '.liquid')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Load all templates into memory, do this now so that
|
# Load all templates into memory, do this now so that
|
||||||
# we don't profile IO.
|
# we don't profile IO.
|
||||||
@@ -31,6 +42,7 @@ class ThemeRunner
|
|||||||
# Dup assigns because will make some changes to them
|
# Dup assigns because will make some changes to them
|
||||||
|
|
||||||
@tests.each do |liquid, layout, template_name|
|
@tests.each do |liquid, layout, template_name|
|
||||||
|
|
||||||
tmpl = Liquid::Template.new
|
tmpl = Liquid::Template.new
|
||||||
tmpl.parse(liquid)
|
tmpl.parse(liquid)
|
||||||
tmpl = Liquid::Template.new
|
tmpl = Liquid::Template.new
|
||||||
@@ -46,14 +58,14 @@ class ThemeRunner
|
|||||||
|
|
||||||
# Compute page_tempalte outside of profiler run, uninteresting to profiler
|
# Compute page_tempalte outside of profiler run, uninteresting to profiler
|
||||||
page_template = File.basename(template_name, File.extname(template_name))
|
page_template = File.basename(template_name, File.extname(template_name))
|
||||||
compile_and_render(liquid, layout, assigns, page_template)
|
compile_and_render(liquid, layout, assigns, page_template, template_name)
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def run_profile
|
def run_profile
|
||||||
RubyProf.measure_mode = RubyProf::PROCESS_TIME
|
RubyProf.measure_mode = RubyProf::WALL_TIME
|
||||||
|
|
||||||
# Dup assigns because will make some changes to them
|
# Dup assigns because will make some changes to them
|
||||||
assigns = Database.tables.dup
|
assigns = Database.tables.dup
|
||||||
@@ -73,7 +85,7 @@ class ThemeRunner
|
|||||||
html = nil
|
html = nil
|
||||||
|
|
||||||
RubyProf.resume
|
RubyProf.resume
|
||||||
html = compile_and_render(liquid, layout, assigns, page_template)
|
html = compile_and_render(liquid, layout, assigns, page_template, template_name)
|
||||||
RubyProf.pause
|
RubyProf.pause
|
||||||
|
|
||||||
|
|
||||||
@@ -87,10 +99,11 @@ class ThemeRunner
|
|||||||
RubyProf.stop
|
RubyProf.stop
|
||||||
end
|
end
|
||||||
|
|
||||||
def compile_and_render(template, layout, assigns, page_template)
|
def compile_and_render(template, layout, assigns, page_template, template_file)
|
||||||
tmpl = Liquid::Template.new
|
tmpl = Liquid::Template.new
|
||||||
tmpl.assigns['page_title'] = 'Page title'
|
tmpl.assigns['page_title'] = 'Page title'
|
||||||
tmpl.assigns['template'] = page_template
|
tmpl.assigns['template'] = page_template
|
||||||
|
tmpl.registers[:file_system] = ThemeRunner::FileSystem.new(File.dirname(template_file))
|
||||||
|
|
||||||
content_for_layout = tmpl.parse(template).render(assigns)
|
content_for_layout = tmpl.parse(template).render(assigns)
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,13 @@ class AssignTest < Test::Unit::TestCase
|
|||||||
'{% assign foo = values %}.{{ foo[1] }}.',
|
'{% assign foo = values %}.{{ foo[1] }}.',
|
||||||
'values' => %w{foo bar baz})
|
'values' => %w{foo bar baz})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_assigned_utf8_variable
|
||||||
|
assert_template_result('.bar.',
|
||||||
|
"{% assign foo\u6000 = values %}.{{ foo\u6000[1] }}.",
|
||||||
|
'values' => %w{foo bar baz})
|
||||||
|
end
|
||||||
|
|
||||||
def test_assign_with_filter
|
def test_assign_with_filter
|
||||||
assert_template_result('.bar.',
|
assert_template_result('.bar.',
|
||||||
'{% assign foo = values | split: "," %}.{{ foo[1] }}.',
|
'{% assign foo = values | split: "," %}.{{ foo[1] }}.',
|
||||||
|
|||||||
@@ -51,6 +51,14 @@ class BlockTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_with_custom_utf8_tag
|
||||||
|
Liquid::Template.register_tag("testtag\u6000", Block)
|
||||||
|
|
||||||
|
assert_nothing_thrown do
|
||||||
|
template = Liquid::Template.parse( "{% testtag\u6000 something\u6000 %} {% endtesttag\u6000 %}")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def block_types(nodelist)
|
def block_types(nodelist)
|
||||||
nodelist.collect { |node| node.class }
|
nodelist.collect { |node| node.class }
|
||||||
|
|||||||
@@ -7,6 +7,10 @@ class CaptureTest < Test::Unit::TestCase
|
|||||||
assert_template_result("test string", "{% capture 'var' %}test string{% endcapture %}{{var}}", {})
|
assert_template_result("test string", "{% capture 'var' %}test string{% endcapture %}{{var}}", {})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_captures_block_content_in_utf8_variable
|
||||||
|
assert_template_result("test string", "{% capture var\u6000 %}test string{% endcapture %}{{var\u6000}}", {})
|
||||||
|
end
|
||||||
|
|
||||||
def test_capture_to_variable_from_outer_scope_if_existing
|
def test_capture_to_variable_from_outer_scope_if_existing
|
||||||
template_source = <<-END_TEMPLATE
|
template_source = <<-END_TEMPLATE
|
||||||
{% assign var = '' %}
|
{% assign var = '' %}
|
||||||
|
|||||||
@@ -98,6 +98,11 @@ class ContextTest < Test::Unit::TestCase
|
|||||||
assert_equal nil, @context['nil']
|
assert_equal nil, @context['nil']
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_utf8_variables
|
||||||
|
@context["chinese\u6000variable"] = 'chinese'
|
||||||
|
assert_equal 'chinese', @context["chinese\u6000variable"]
|
||||||
|
end
|
||||||
|
|
||||||
def test_variables_not_existing
|
def test_variables_not_existing
|
||||||
assert_equal nil, @context['does_not_exist']
|
assert_equal nil, @context['does_not_exist']
|
||||||
end
|
end
|
||||||
@@ -254,16 +259,12 @@ class ContextTest < Test::Unit::TestCase
|
|||||||
@context['test'] = {'test' => [1,2,3,4,5]}
|
@context['test'] = {'test' => [1,2,3,4,5]}
|
||||||
|
|
||||||
assert_equal 1, @context['test.test[0]']
|
assert_equal 1, @context['test.test[0]']
|
||||||
end
|
|
||||||
|
|
||||||
def test_recoursive_array_notation_for_hash
|
|
||||||
@context['test'] = [{'test' => 'worked'}]
|
@context['test'] = [{'test' => 'worked'}]
|
||||||
|
|
||||||
assert_equal 'worked', @context['test[0].test']
|
assert_equal 'worked', @context['test[0].test']
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def test_hash_to_array_transition
|
def test_hash_to_array_transition
|
||||||
@context['colors'] = {
|
@context['colors'] = {
|
||||||
'Blue' => ['003366','336699', '6699CC', '99CCFF'],
|
'Blue' => ['003366','336699', '6699CC', '99CCFF'],
|
||||||
@@ -319,7 +320,7 @@ class ContextTest < Test::Unit::TestCase
|
|||||||
@context['nested'] = {'var' => 'tags'}
|
@context['nested'] = {'var' => 'tags'}
|
||||||
@context['products'] = {'count' => 5, 'tags' => ['deepsnow', 'freestyle'] }
|
@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']
|
assert_equal 'freestyle', @context['products[nested.var].last']
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,93 +0,0 @@
|
|||||||
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
|
|
||||||
@@ -39,7 +39,7 @@ class ParsingQuirksTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
def test_meaningless_parens
|
def test_meaningless_parens
|
||||||
assigns = {'b' => 'bar', 'c' => 'baz'}
|
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)
|
assert_template_result(' YES ',"{% if #{markup} %} YES {% endif %}", assigns)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,11 @@ HERE
|
|||||||
assert_template_result(expected,template,'array' => [1,2,3])
|
assert_template_result(expected,template,'array' => [1,2,3])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_utf8_for
|
||||||
|
assigns = {"array\u6000chinese" => [1,2,3]}
|
||||||
|
assert_template_result('123', "{% for item\u6000chinese in array\u6000chinese %}{{ item\u6000chinese }}{% endfor %}", assigns)
|
||||||
|
end
|
||||||
|
|
||||||
def test_for_reversed
|
def test_for_reversed
|
||||||
assigns = {'array' => [ 1, 2, 3] }
|
assigns = {'array' => [ 1, 2, 3] }
|
||||||
assert_template_result('321','{%for item in array reversed %}{{item}}{%endfor%}',assigns)
|
assert_template_result('321','{%for item in array reversed %}{{item}}{%endfor%}',assigns)
|
||||||
|
|||||||
@@ -26,6 +26,12 @@ class HtmlTagTest < Test::Unit::TestCase
|
|||||||
'numbers' => [])
|
'numbers' => [])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_utf8_html_table
|
||||||
|
assert_template_result("<tr class=\"row1\">\n<td class=\"col1\"> 1 </td></tr>\n",
|
||||||
|
"{% tablerow n\u6000 in numbers\u6000 %} {{n\u6000}} {% endtablerow %}",
|
||||||
|
"numbers\u6000" => [1])
|
||||||
|
end
|
||||||
|
|
||||||
def test_html_table_with_different_cols
|
def test_html_table_with_different_cols
|
||||||
assert_template_result("<tr class=\"row1\">\n<td class=\"col1\"> 1 </td><td class=\"col2\"> 2 </td><td class=\"col3\"> 3 </td><td class=\"col4\"> 4 </td><td class=\"col5\"> 5 </td></tr>\n<tr class=\"row2\"><td class=\"col1\"> 6 </td></tr>\n",
|
assert_template_result("<tr class=\"row1\">\n<td class=\"col1\"> 1 </td><td class=\"col2\"> 2 </td><td class=\"col3\"> 3 </td><td class=\"col4\"> 4 </td><td class=\"col5\"> 5 </td></tr>\n<tr class=\"row2\"><td class=\"col1\"> 6 </td></tr>\n",
|
||||||
'{% tablerow n in numbers cols:5%} {{n}} {% endtablerow %}',
|
'{% tablerow n in numbers cols:5%} {{n}} {% endtablerow %}',
|
||||||
|
|||||||
@@ -39,6 +39,15 @@ class OtherFileSystem
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class CountingFileSystem
|
||||||
|
attr_reader :count
|
||||||
|
def read_template_file(template_path, context)
|
||||||
|
@count ||= 0
|
||||||
|
@count += 1
|
||||||
|
'from CountingFileSystem'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class IncludeTagTest < Test::Unit::TestCase
|
class IncludeTagTest < Test::Unit::TestCase
|
||||||
include Liquid
|
include Liquid
|
||||||
|
|
||||||
@@ -136,4 +145,22 @@ class IncludeTagTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
assert_equal "Product: Draft 151cm ", Template.parse("{% include template for product %}").render("template" => 'product', 'product' => { 'title' => 'Draft 151cm'})
|
assert_equal "Product: Draft 151cm ", Template.parse("{% include template for product %}").render("template" => 'product', 'product' => { 'title' => 'Draft 151cm'})
|
||||||
end
|
end
|
||||||
end # IncludeTagTest
|
|
||||||
|
def test_include_tag_caches_second_read_of_same_partial
|
||||||
|
file_system = CountingFileSystem.new
|
||||||
|
assert_equal 'from CountingFileSystemfrom CountingFileSystem',
|
||||||
|
Template.parse("{% include 'pick_a_source' %}{% include 'pick_a_source' %}").render({}, :registers => {:file_system => file_system})
|
||||||
|
assert_equal 1, file_system.count
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_include_tag_doesnt_cache_partials_across_renders
|
||||||
|
file_system = CountingFileSystem.new
|
||||||
|
assert_equal 'from CountingFileSystem',
|
||||||
|
Template.parse("{% include 'pick_a_source' %}").render({}, :registers => {:file_system => file_system})
|
||||||
|
assert_equal 1, file_system.count
|
||||||
|
|
||||||
|
assert_equal 'from CountingFileSystem',
|
||||||
|
Template.parse("{% include 'pick_a_source' %}").render({}, :registers => {:file_system => file_system})
|
||||||
|
assert_equal 2, file_system.count
|
||||||
|
end
|
||||||
|
end # IncludeTagTest
|
||||||
|
|||||||
@@ -50,6 +50,12 @@ 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_utf8_filters
|
||||||
|
var = Variable.new("foo | chinese\u6000filter: value\u6000")
|
||||||
|
assert_equal 'foo', var.name
|
||||||
|
assert_equal [["chinese\u6000filter",["value\u6000"]]], var.filters
|
||||||
|
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"!)
|
||||||
|
|||||||
Reference in New Issue
Block a user