All tests pass on Ruby 1.9.1

Signed-off-by: Tobias Lütke <tobi@leetsoft.com>
This commit is contained in:
Jakub Kuźma
2009-04-14 16:30:08 +08:00
committed by Tobias Lütke
parent 8d27864845
commit ed1b542abf
8 changed files with 130 additions and 130 deletions

View File

@@ -1,19 +1,19 @@
module Liquid
class Block < Tag
def parse(tokens)
@nodelist ||= []
@nodelist.clear
while token = tokens.shift
while token = tokens.shift
case token
when /^#{TagStart}/
when /^#{TagStart}/
if token =~ /^#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}$/
# if we found the proper block delimitor just end parsing here and let the outer block
# proceed
# proceed
if block_delimiter == $1
end_tag
return
@@ -23,10 +23,10 @@ module Liquid
if tag = Template.tags[$1]
@nodelist << tag.new($1, $2, tokens)
else
# this tag is not registered with the system
# this tag is not registered with the system
# pass it to the current block for special handling or error reporting
unknown_tag($1, $2, tokens)
end
end
else
raise SyntaxError, "Tag '#{token}' was not properly terminated with regexp: #{TagEnd.inspect} "
end
@@ -37,19 +37,19 @@ module Liquid
else
@nodelist << token
end
end
# Make sure that its ok to end parsing in the current block.
# Effectively this method will throw and exception unless the current block is
# of type Document
end
# Make sure that its ok to end parsing in the current block.
# Effectively this method will throw and exception unless the current block is
# of type Document
assert_missing_delimitation!
end
def end_tag
end
def end_tag
end
def unknown_tag(tag, params, tokens)
case tag
case tag
when 'else'
raise SyntaxError, "#{block_name} tag does not expect else tag"
when 'end'
@@ -61,7 +61,7 @@ module Liquid
def block_delimiter
"end#{block_name}"
end
end
def block_name
@tag_name
@@ -77,7 +77,7 @@ module Liquid
def render(context)
render_all(@nodelist, context)
end
protected
def assert_missing_delimitation!
@@ -86,12 +86,12 @@ module Liquid
def render_all(list, context)
list.collect do |token|
begin
begin
token.respond_to?(:render) ? token.render(context) : token
rescue Exception => e
rescue Exception => e
context.handle_error(e)
end
end
end
end
end
end
end
end
end

View File

@@ -1,17 +1,17 @@
module Liquid
class Document < Block
class Document < Block
# we don't need markup to open this block
def initialize(tokens)
parse(tokens)
end
# There isn't a real delimter
end
# There isn't a real delimter
def block_delimiter
[]
end
# Document blocks don't need to be terminated since they are not actually opened
def assert_missing_delimitation!
end
end
end
end
end

View File

@@ -1,7 +1,7 @@
module Liquid
class TableRow < Block
Syntax = /(\w+)\s+in\s+(#{VariableSignature}+)/
class TableRow < Block
Syntax = /(\w+)\s+in\s+(#{VariableSignature}+)/
def initialize(tag_name, markup, tokens)
if markup =~ Syntax
@variable_name = $1
@@ -13,62 +13,62 @@ module Liquid
else
raise SyntaxError.new("Syntax Error in 'table_row loop' - Valid syntax: table_row [item] in [collection] cols=3")
end
super
super
end
def render(context)
def render(context)
collection = context[@collection_name] or return ''
if @attributes['limit'] or @attributes['offset']
limit = context[@attributes['limit']] || -1
offset = context[@attributes['offset']] || 0
collection = collection[offset.to_i..(limit.to_i + offset.to_i - 1)]
end
length = collection.length
cols = context[@attributes['cols']].to_i
row = 1
col = 0
result = ["<tr class=\"row1\">\n"]
context.stack do
context.stack do
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,
'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
result << ["<td class=\"col#{col}\">"] + render_all(@nodelist, context) + ['</td>']
if col == cols and not (index == length - 1)
if col == cols and not (index == length - 1)
col = 0
row += 1
result << ["</tr>\n<tr class=\"row#{row}\">"]
result << ["</tr>\n<tr class=\"row#{row}\">"]
end
end
end
result + ["</tr>\n"]
end
end
end
Template.register_tag('tablerow', TableRow)
end
Template.register_tag('tablerow', TableRow)
end

View File

@@ -18,7 +18,7 @@
# end
# end
#
# if you want to extend the drop to other methods you can defines more methods
# if you want to extend the drop to other methods you can defines more methods
# in the class <YourClass>::LiquidDropClass
#
# class SomeClass::LiquidDropClass
@@ -37,11 +37,11 @@
# output:
# 'this comes from an allowed method and this from another allowed method'
#
# You can also chain associations, by adding the liquid_method call in the
# You can also chain associations, by adding the liquid_method call in the
# association models.
#
class Module
def liquid_methods(*allowed_methods)
drop_class = eval "class #{self.to_s}::LiquidDropClass < Liquid::Drop; self; end"
define_method :to_liquid do
@@ -58,5 +58,5 @@ class Module
end
end
end
end

View File

@@ -14,7 +14,7 @@ module Liquid
# One of the strainer's responsibilities is to keep malicious method calls out
class Strainer < parent_object #:nodoc:
INTERNAL_METHOD = /^__/
@@required_methods = Set.new([:__send__, :respond_to?, :extend, :methods, :class, :object_id])
@@required_methods = Set.new([:__id__, :__send__, :respond_to?, :extend, :methods, :class, :object_id])
@@filters = {}

View File

@@ -1,26 +1,26 @@
module Liquid
class Tag
attr_accessor :nodelist
def initialize(tag_name, markup, tokens)
@tag_name = tag_name
@markup = markup
parse(tokens)
end
def parse(tokens)
end
def name
self.class.name.downcase
end
def render(context)
''
end
end
end
end

View File

@@ -1,5 +1,5 @@
module Liquid
# Capture stores the result of a block into a variable without rendering it inplace.
#
# {% capture heading %}
@@ -8,28 +8,28 @@ module Liquid
# ...
# <h1>{{ monkeys }}</h1>
#
# Capture is useful for saving content for use later in your template, such as
# Capture is useful for saving content for use later in your template, such as
# in a sidebar or footer.
#
class Capture < Block
Syntax = /(\w+)/
def initialize(tag_name, markup, tokens)
def initialize(tag_name, markup, tokens)
if markup =~ Syntax
@to = $1
else
raise SyntaxError.new("Syntax Error in 'capture' - Valid syntax: capture [var]")
end
super
super
end
def render(context)
output = super
context[@to] = output.to_s
context[@to] = output.join
''
end
end
end
Template.register_tag('capture', Capture)
end
end

View File

@@ -1,120 +1,120 @@
module Liquid
# Templates are central to liquid.
# Interpretating templates is a two step process. First you compile the
# source code you got. During compile time some extensive error checking is performed.
# your code should expect to get some SyntaxErrors.
# Templates are central to liquid.
# Interpretating templates is a two step process. First you compile the
# source code you got. During compile time some extensive error checking is performed.
# your code should expect to get some SyntaxErrors.
#
# After you have a compiled template you can then <tt>render</tt> it.
# You can use a compiled template over and over again and keep it cached.
# After you have a compiled template you can then <tt>render</tt> it.
# You can use a compiled template over and over again and keep it cached.
#
# Example:
#
# Example:
#
# template = Liquid::Template.parse(source)
# template.render('user_name' => 'bob')
#
class Template
attr_accessor :root
@@file_system = BlankFileSystem.new
class <<self
class << self
def file_system
@@file_system
end
def file_system=(obj)
@@file_system = obj
end
def register_tag(name, klass)
def register_tag(name, klass)
tags[name.to_s] = klass
end
end
def tags
@tags ||= {}
end
# Pass a module with filter methods which should be available
# Pass a module with filter methods which should be available
# to all liquid views. Good for registering the standard library
def register_filter(mod)
def register_filter(mod)
Strainer.global_filter(mod)
end
end
# creates a new <tt>Template</tt> object from liquid source code
def parse(source)
template = Template.new
template.parse(source)
template
end
end
end
# creates a new <tt>Template</tt> from an array of tokens. Use <tt>Template.parse</tt> instead
def initialize
end
# Parse source code.
# Returns self for easy chaining
# Parse source code.
# Returns self for easy chaining
def parse(source)
@root = Document.new(tokenize(source))
self
end
def registers
def registers
@registers ||= {}
end
def assigns
@assigns ||= {}
end
def errors
@errors ||= []
end
# Render takes a hash with local variables.
#
# if you use the same filters over and over again consider registering them globally
# if you use the same filters over and over again consider registering them globally
# with <tt>Template.register_filter</tt>
#
#
# Following options can be passed:
#
#
# * <tt>filters</tt> : array with local filters
# * <tt>registers</tt> : hash with register variables. Those can be accessed from
# filters and tags and might be useful to integrate liquid more with its host application
# * <tt>registers</tt> : hash with register variables. Those can be accessed from
# filters and tags and might be useful to integrate liquid more with its host application
#
def render(*args)
return '' if @root.nil?
return '' if @root.nil?
context = case args.first
when Liquid::Context
args.shift
when Hash
self.assigns.merge!(args.shift)
self.assigns.merge!(args.shift)
Context.new(assigns, registers, @rethrow_errors)
when nil
Context.new(assigns, registers, @rethrow_errors)
else
raise ArgumentError, "Expect Hash or Liquid::Context as parameter"
end
case args.last
when Hash
options = args.pop
if options[:registers].is_a?(Hash)
self.registers.merge!(options[:registers])
self.registers.merge!(options[:registers])
end
if options[:filters]
context.add_filters(options[:filters])
end
end
when Module
context.add_filters(args.pop)
context.add_filters(args.pop)
when Array
context.add_filters(args.pop)
context.add_filters(args.pop)
end
begin
# render the nodelist.
# for performance reasons we get a array back here. join will make a string out of it
@@ -123,24 +123,24 @@ module Liquid
@errors = context.errors
end
end
def render!(*args)
@rethrow_errors = true; render(*args)
end
private
# Uses the <tt>Liquid::TemplateParser</tt> regexp to tokenize the passed source
def tokenize(source)
source = source.source if source.respond_to?(:source)
source = source.source if source.respond_to?(:source)
return [] if source.to_s.empty?
tokens = source.split(TemplateParser)
# removes the rogue empty element at the beginning of the array
tokens.shift if tokens[0] and tokens[0].empty?
tokens.shift if tokens[0] and tokens[0].empty?
tokens
end
end
end
end