Skip to content

Commit

Permalink
Literal
Browse files Browse the repository at this point in the history
  - added support for literals [closes Shopify#6]

Code beautifier
  - indented some files
  • Loading branch information
DBA authored and Tobias Lütke committed Aug 24, 2010
1 parent 4819eb1 commit c00a650
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/liquid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module Liquid
PartialTemplateParser = /#{TagStart}.*?#{TagEnd}|#{VariableStart}.*?#{VariableIncompleteEnd}/
TemplateParser = /(#{PartialTemplateParser}|#{AnyStartingTag})/
VariableParser = /\[[^\]]+\]|#{VariableSegment}+\??/
LiteralShorthand = /^(?:{{{\s?)(.*?)(?:\s*}}})$/
end

require 'liquid/drop'
Expand Down
6 changes: 3 additions & 3 deletions lib/liquid/tag.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Liquid

class Tag

attr_accessor :nodelist

def initialize(tag_name, markup, tokens)
Expand All @@ -19,8 +20,7 @@ def name
def render(context)
''
end
end


end
end # Tag

end # Tag
10 changes: 5 additions & 5 deletions lib/liquid/tags/comment.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module Liquid
class Comment < Block
class Comment < Block
def render(context)
''
end
end
end
Template.register_tag('comment', Comment)
end

Template.register_tag('comment', Comment)
end
42 changes: 42 additions & 0 deletions lib/liquid/tags/literal.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
module Liquid

class Literal < Block

# Class methods

# Converts a shorthand Liquid literal into its long representation.
#
# Currently the Template parser only knows how to handle the long version.
# So, it always checks if it is in the presence of a literal, in which case it gets converted through this method.
#
# Example:
# Liquid::Literal "{{{ hello world }}}" #=> "{% literal %} hello world {% endliteral %}"
def self.from_shorthand(literal)
literal =~ LiteralShorthand ? "{% literal %}#{$1}{% endliteral %}" : literal
end

# Public instance methods

def parse(tokens) # :nodoc:
@nodelist ||= []
@nodelist.clear

while token = tokens.shift
if token =~ FullToken && block_delimiter == $1
end_tag
return
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
assert_missing_delimitation!
end # parse

end

Template.register_tag('literal', Literal)
end
2 changes: 1 addition & 1 deletion lib/liquid/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def initialize
# Parse source code.
# Returns self for easy chaining
def parse(source)
@root = Document.new(tokenize(source))
@root = Document.new(tokenize(Liquid::Literal.from_shorthand(source)))
self
end

Expand Down
4 changes: 4 additions & 0 deletions test/lib/liquid/regexp_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ def test_variable_parser
assert_equal ['var', '["method"]', '[0]'], 'var["method"][0]'.scan(VariableParser)
assert_equal ['var', '[method]', '[0]', 'method'], 'var[method][0].method'.scan(VariableParser)
end

def test_literal_shorthand_regexp
assert_match "{{{ {% if 'gnomeslab' contains 'liquid' %}yes{% endif %} }}}", LiteralShorthand
end
end # RegexpTest
39 changes: 39 additions & 0 deletions test/lib/liquid/tags/literal_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'test_helper'

class LiteralTagTest < Test::Unit::TestCase
include Liquid

def test_empty_literal
assert_template_result '', '{% literal %}{% endliteral %}'
assert_template_result '', '{{{}}}'
end

def test_simple_literal_value
assert_template_result 'howdy',
'{% literal %}howdy{% endliteral %}'
end

def test_literals_ignore_liquid_markup
expected = %({% if 'gnomeslab' contain 'liquid' %}yes{ % endif %})
template = %({% literal %}#{expected}{% endliteral %})

assert_template_result expected, template
end

def test_shorthand_syntax
expected = %({% if 'gnomeslab' contain 'liquid' %}yes{ % endif %})
template = %({{{#{expected}}}})

assert_template_result expected, template
end

# Class methods
def test_from_shorthand
assert_equal '{% literal %}gnomeslab{% endliteral %}', Liquid::Literal.from_shorthand('{{{gnomeslab}}}')
end

def test_from_shorthand_ignores_improper_syntax
text = "{% if 'hi' == 'hi' %}hi{% endif %}"
assert_equal text, Liquid::Literal.from_shorthand(text)
end
end # AssignTest

0 comments on commit c00a650

Please sign in to comment.