diff --git a/lib/liquid/block.rb b/lib/liquid/block.rb index e28cb4e..2d4e293 100644 --- a/lib/liquid/block.rb +++ b/lib/liquid/block.rb @@ -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 \ No newline at end of file + end +end diff --git a/lib/liquid/document.rb b/lib/liquid/document.rb index abffbde..bf95478 100644 --- a/lib/liquid/document.rb +++ b/lib/liquid/document.rb @@ -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 \ No newline at end of file +end diff --git a/lib/liquid/htmltags.rb b/lib/liquid/htmltags.rb index 8b88e49..c6db036 100644 --- a/lib/liquid/htmltags.rb +++ b/lib/liquid/htmltags.rb @@ -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 = ["\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 << [""] + render_all(@nodelist, context) + [''] - if col == cols and not (index == length - 1) + if col == cols and not (index == length - 1) col = 0 row += 1 - result << ["\n"] + result << ["\n"] end - + end end result + ["\n"] - end + end end - - Template.register_tag('tablerow', TableRow) -end \ No newline at end of file + + Template.register_tag('tablerow', TableRow) +end diff --git a/lib/liquid/module_ex.rb b/lib/liquid/module_ex.rb index e9d7b7b..c504ea1 100644 --- a/lib/liquid/module_ex.rb +++ b/lib/liquid/module_ex.rb @@ -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 ::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 diff --git a/lib/liquid/strainer.rb b/lib/liquid/strainer.rb index 9fa769b..b6dad26 100644 --- a/lib/liquid/strainer.rb +++ b/lib/liquid/strainer.rb @@ -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 = {} diff --git a/lib/liquid/tag.rb b/lib/liquid/tag.rb index e0bf35d..2750064 100644 --- a/lib/liquid/tag.rb +++ b/lib/liquid/tag.rb @@ -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 - \ No newline at end of file + diff --git a/lib/liquid/tags/capture.rb b/lib/liquid/tags/capture.rb index f4f6f3c..92b142f 100644 --- a/lib/liquid/tags/capture.rb +++ b/lib/liquid/tags/capture.rb @@ -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 # ... #

{{ monkeys }}

# - # 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 \ No newline at end of file +end diff --git a/lib/liquid/template.rb b/lib/liquid/template.rb index 03b3da1..c0f189c 100644 --- a/lib/liquid/template.rb +++ b/lib/liquid/template.rb @@ -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 render it. - # You can use a compiled template over and over again and keep it cached. + # After you have a compiled template you can then render 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 <Template object from liquid source code def parse(source) template = Template.new template.parse(source) template - end + end end # creates a new Template from an array of tokens. Use Template.parse 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 Template.register_filter - # + # # Following options can be passed: - # + # # * filters : array with local filters - # * registers : hash with register variables. Those can be accessed from - # filters and tags and might be useful to integrate liquid more with its host application + # * registers : 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 Liquid::TemplateParser 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