From 3a0ee6ae9180e64f4fd12bbd54418d122fc25f94 Mon Sep 17 00:00:00 2001 From: Tristan Hume Date: Thu, 14 Aug 2014 15:06:54 -0400 Subject: [PATCH] Remove parser switching duplication --- lib/liquid.rb | 1 + lib/liquid/parser_switching.rb | 30 ++++++++++++++++++++++++++++++ lib/liquid/tag.rb | 25 +------------------------ lib/liquid/variable.rb | 21 ++++++--------------- 4 files changed, 38 insertions(+), 39 deletions(-) create mode 100644 lib/liquid/parser_switching.rb diff --git a/lib/liquid.rb b/lib/liquid.rb index cc56eb0..f719107 100644 --- a/lib/liquid.rb +++ b/lib/liquid.rb @@ -54,6 +54,7 @@ require 'liquid/interrupts' require 'liquid/strainer' require 'liquid/expression' require 'liquid/context' +require 'liquid/parser_switching' require 'liquid/tag' require 'liquid/block' require 'liquid/document' diff --git a/lib/liquid/parser_switching.rb b/lib/liquid/parser_switching.rb new file mode 100644 index 0000000..042419c --- /dev/null +++ b/lib/liquid/parser_switching.rb @@ -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 \ No newline at end of file diff --git a/lib/liquid/tag.rb b/lib/liquid/tag.rb index b4dfec4..d8d35c5 100644 --- a/lib/liquid/tag.rb +++ b/lib/liquid/tag.rb @@ -2,6 +2,7 @@ module Liquid class Tag attr_accessor :options, :line_number attr_reader :nodelist, :warnings + include ParserSwitching class << self def parse(tag_name, markup, tokens, options) @@ -37,29 +38,5 @@ module Liquid def blank? false 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 diff --git a/lib/liquid/variable.rb b/lib/liquid/variable.rb index 144b679..04d82fd 100644 --- a/lib/liquid/variable.rb +++ b/lib/liquid/variable.rb @@ -15,30 +15,24 @@ module Liquid EasyParse = /\A *(\w+(?:\.\w+)*) *\z/ attr_accessor :filters, :name, :warnings attr_accessor :line_number + include ParserSwitching def initialize(markup, options = {}) @markup = markup @name = nil @options = options || {} - case @options[:error_mode] || Template.error_mode - 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 + parse_with_selected_parser(markup) end def raw @markup end + def markup_context(markup) + " in \"{{#{markup}}}\"" + end + def lax_parse(markup) @filters = [] if markup =~ /\s*(#{QuotedFragment})(.*)/om @@ -74,9 +68,6 @@ module Liquid @filters << [filtername, filterargs] end p.consume(:end_of_string) - rescue SyntaxError => e - e.message << " in \"{{#{markup}}}\"" - raise e end def parse_filterargs(p)