mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
Remove parser switching duplication
This commit is contained in:
committed by
Florian Weingarten
parent
5eff375094
commit
3a0ee6ae91
@@ -54,6 +54,7 @@ require 'liquid/interrupts'
|
|||||||
require 'liquid/strainer'
|
require 'liquid/strainer'
|
||||||
require 'liquid/expression'
|
require 'liquid/expression'
|
||||||
require 'liquid/context'
|
require 'liquid/context'
|
||||||
|
require 'liquid/parser_switching'
|
||||||
require 'liquid/tag'
|
require 'liquid/tag'
|
||||||
require 'liquid/block'
|
require 'liquid/block'
|
||||||
require 'liquid/document'
|
require 'liquid/document'
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
module Liquid
|
||||||
|
module ParserSwitching
|
||||||
|
def parse_with_selected_parser(markup)
|
||||||
|
case @options[:error_mode] || Template.error_mode
|
||||||
|
when :strict then strict_parse_with_error_context(markup)
|
||||||
|
when :lax then lax_parse(markup)
|
||||||
|
when :warn
|
||||||
|
begin
|
||||||
|
return strict_parse_with_error_context(markup)
|
||||||
|
rescue SyntaxError => e
|
||||||
|
@warnings ||= []
|
||||||
|
@warnings << e
|
||||||
|
return lax_parse(markup)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def strict_parse_with_error_context(markup)
|
||||||
|
strict_parse(markup)
|
||||||
|
rescue SyntaxError => e
|
||||||
|
e.message << markup_context(markup)
|
||||||
|
raise e
|
||||||
|
end
|
||||||
|
|
||||||
|
def markup_context(markup)
|
||||||
|
" in \"#{markup.strip}\""
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
+1
-24
@@ -2,6 +2,7 @@ module Liquid
|
|||||||
class Tag
|
class Tag
|
||||||
attr_accessor :options, :line_number
|
attr_accessor :options, :line_number
|
||||||
attr_reader :nodelist, :warnings
|
attr_reader :nodelist, :warnings
|
||||||
|
include ParserSwitching
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
def parse(tag_name, markup, tokens, options)
|
def parse(tag_name, markup, tokens, options)
|
||||||
@@ -37,29 +38,5 @@ module Liquid
|
|||||||
def blank?
|
def blank?
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_with_selected_parser(markup)
|
|
||||||
case @options[:error_mode] || Template.error_mode
|
|
||||||
when :strict then strict_parse_with_error_context(markup)
|
|
||||||
when :lax then lax_parse(markup)
|
|
||||||
when :warn
|
|
||||||
begin
|
|
||||||
return strict_parse_with_error_context(markup)
|
|
||||||
rescue SyntaxError => e
|
|
||||||
@warnings ||= []
|
|
||||||
@warnings << e
|
|
||||||
return lax_parse(markup)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def strict_parse_with_error_context(markup)
|
|
||||||
strict_parse(markup)
|
|
||||||
rescue SyntaxError => e
|
|
||||||
e.message << " in \"#{markup.strip}\""
|
|
||||||
raise e
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
+6
-15
@@ -15,30 +15,24 @@ module Liquid
|
|||||||
EasyParse = /\A *(\w+(?:\.\w+)*) *\z/
|
EasyParse = /\A *(\w+(?:\.\w+)*) *\z/
|
||||||
attr_accessor :filters, :name, :warnings
|
attr_accessor :filters, :name, :warnings
|
||||||
attr_accessor :line_number
|
attr_accessor :line_number
|
||||||
|
include ParserSwitching
|
||||||
|
|
||||||
def initialize(markup, options = {})
|
def initialize(markup, options = {})
|
||||||
@markup = markup
|
@markup = markup
|
||||||
@name = nil
|
@name = nil
|
||||||
@options = options || {}
|
@options = options || {}
|
||||||
|
|
||||||
case @options[:error_mode] || Template.error_mode
|
parse_with_selected_parser(markup)
|
||||||
when :strict then strict_parse(markup)
|
|
||||||
when :lax then lax_parse(markup)
|
|
||||||
when :warn
|
|
||||||
begin
|
|
||||||
strict_parse(markup)
|
|
||||||
rescue SyntaxError => e
|
|
||||||
@warnings ||= []
|
|
||||||
@warnings << e
|
|
||||||
lax_parse(markup)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def raw
|
def raw
|
||||||
@markup
|
@markup
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def markup_context(markup)
|
||||||
|
" in \"{{#{markup}}}\""
|
||||||
|
end
|
||||||
|
|
||||||
def lax_parse(markup)
|
def lax_parse(markup)
|
||||||
@filters = []
|
@filters = []
|
||||||
if markup =~ /\s*(#{QuotedFragment})(.*)/om
|
if markup =~ /\s*(#{QuotedFragment})(.*)/om
|
||||||
@@ -74,9 +68,6 @@ module Liquid
|
|||||||
@filters << [filtername, filterargs]
|
@filters << [filtername, filterargs]
|
||||||
end
|
end
|
||||||
p.consume(:end_of_string)
|
p.consume(:end_of_string)
|
||||||
rescue SyntaxError => e
|
|
||||||
e.message << " in \"{{#{markup}}}\""
|
|
||||||
raise e
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse_filterargs(p)
|
def parse_filterargs(p)
|
||||||
|
|||||||
Reference in New Issue
Block a user