mirror of
https://github.com/kemko/liquid.git
synced 2026-01-04 09:15:41 +03:00
Literal
- added support for literals [closes #6] Code beautifier - indented some files
This commit is contained in:
@@ -45,6 +45,7 @@ module Liquid
|
||||
PartialTemplateParser = /#{TagStart}.*?#{TagEnd}|#{VariableStart}.*?#{VariableIncompleteEnd}/
|
||||
TemplateParser = /(#{PartialTemplateParser}|#{AnyStartingTag})/
|
||||
VariableParser = /\[[^\]]+\]|#{VariableSegment}+\??/
|
||||
LiteralShorthand = /^(?:{{{\s?)(.*?)(?:\s*}}})$/
|
||||
end
|
||||
|
||||
require 'liquid/drop'
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
lib/liquid/tags/literal.rb
Normal file
42
lib/liquid/tags/literal.rb
Normal file
@@ -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
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user