From c00a650492f40fcc347ebc6c812255254e9d5bf4 Mon Sep 17 00:00:00 2001 From: DBA Date: Tue, 24 Aug 2010 05:39:07 +0800 Subject: [PATCH] Literal - added support for literals [closes #6] Code beautifier - indented some files --- lib/liquid.rb | 1 + lib/liquid/tag.rb | 6 ++-- lib/liquid/tags/comment.rb | 10 +++---- lib/liquid/tags/literal.rb | 42 ++++++++++++++++++++++++++++ lib/liquid/template.rb | 2 +- test/lib/liquid/regexp_test.rb | 4 +++ test/lib/liquid/tags/literal_test.rb | 39 ++++++++++++++++++++++++++ 7 files changed, 95 insertions(+), 9 deletions(-) create mode 100644 lib/liquid/tags/literal.rb create mode 100644 test/lib/liquid/tags/literal_test.rb diff --git a/lib/liquid.rb b/lib/liquid.rb index dccef10..950ac7e 100644 --- a/lib/liquid.rb +++ b/lib/liquid.rb @@ -45,6 +45,7 @@ module Liquid PartialTemplateParser = /#{TagStart}.*?#{TagEnd}|#{VariableStart}.*?#{VariableIncompleteEnd}/ TemplateParser = /(#{PartialTemplateParser}|#{AnyStartingTag})/ VariableParser = /\[[^\]]+\]|#{VariableSegment}+\??/ + LiteralShorthand = /^(?:{{{\s?)(.*?)(?:\s*}}})$/ end require 'liquid/drop' diff --git a/lib/liquid/tag.rb b/lib/liquid/tag.rb index 2750064..b7f2aa4 100644 --- a/lib/liquid/tag.rb +++ b/lib/liquid/tag.rb @@ -1,6 +1,7 @@ module Liquid class Tag + attr_accessor :nodelist def initialize(tag_name, markup, tokens) @@ -19,8 +20,7 @@ module Liquid def render(context) '' end - end + end # Tag -end - +end # Tag diff --git a/lib/liquid/tags/comment.rb b/lib/liquid/tags/comment.rb index 8ce7e0e..37fb4c8 100644 --- a/lib/liquid/tags/comment.rb +++ b/lib/liquid/tags/comment.rb @@ -1,9 +1,9 @@ module Liquid - class Comment < Block + class Comment < Block def render(context) '' - end + end end - - Template.register_tag('comment', Comment) -end \ No newline at end of file + + Template.register_tag('comment', Comment) +end diff --git a/lib/liquid/tags/literal.rb b/lib/liquid/tags/literal.rb new file mode 100644 index 0000000..67f1600 --- /dev/null +++ b/lib/liquid/tags/literal.rb @@ -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 diff --git a/lib/liquid/template.rb b/lib/liquid/template.rb index edfb980..d346c44 100644 --- a/lib/liquid/template.rb +++ b/lib/liquid/template.rb @@ -55,7 +55,7 @@ module Liquid # 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 diff --git a/test/lib/liquid/regexp_test.rb b/test/lib/liquid/regexp_test.rb index 280d62b..8643e2a 100644 --- a/test/lib/liquid/regexp_test.rb +++ b/test/lib/liquid/regexp_test.rb @@ -41,4 +41,8 @@ class RegexpTest < Test::Unit::TestCase 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 diff --git a/test/lib/liquid/tags/literal_test.rb b/test/lib/liquid/tags/literal_test.rb new file mode 100644 index 0000000..d5b970c --- /dev/null +++ b/test/lib/liquid/tags/literal_test.rb @@ -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 \ No newline at end of file