liquid-tag-parser 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b3ea6a7f6eeb0ef34a869786c7c4d131a49020ef
4
- data.tar.gz: 0f46918e58b9a95ee759fe72e511ea09ba00b67d
3
+ metadata.gz: ed525307344e34bf3973bfdec1cafa204c415444
4
+ data.tar.gz: 738a4cfc00bddcd073551a21e2328f7930451ef5
5
5
  SHA512:
6
- metadata.gz: f669d5adcdacba96e329474467f4edb7be5acd9ee333df5509539c5eb62912c65a77217588f2a9e31d23c6d5cd6775f5fd501e899f54b51d20d9e490f3b5982c
7
- data.tar.gz: 4e71217fa8d2789934f09f96c003687740c1ee2d56dae3844164b9d8b7097e5a52785f8e59bd3515cc2b356b7ce114013a1ada912c759f6536be6956124fcd29
6
+ metadata.gz: 863073381e60b53b0390d588b65799bb38250411e5ce26dfb4a5aa89a4123705fe6feaacf611f6fe1ba33085cca1d65b8ba9a85d145dec039183f467d3bab5c6
7
+ data.tar.gz: 8e8e8004f69e6f085714ae6634bd5e41e2b323ff8d26120b24ffa0bd8c91ba0ca3a0b711411cdf474a6c0c75fa10415faf1424a294060b70217c5d1acf7a866e
@@ -26,13 +26,17 @@ module Liquid
26
26
  rb_delegate :args_with_indifferent_access, to: :@args, \
27
27
  alias_of: :with_indifferent_access
28
28
 
29
- # --
30
- BOOLEAN_REGEXP = /^(?<!\\)(\!|@)/
31
- NEGATIVE_BOOLEAN_REGEXP = /^(?<!\\)\!/
32
- POSITIVE_BOOLEAN_REGEXP = /^(?<!\\)\@/
33
- KEY_REGEXP = /\b(?<!\\):/
29
+ FLOAT = /^\d+\.\d+$/
30
+ BOOLEAN = /^(?<!\\)(\!|@)/
31
+ NEGATIVE_BOOLEAN = /^(?<!\\)\!/
32
+ BOOLEAN_QUOTE_REPLACEMENT = "\\1\\\\\\2"
33
+ SHELLSPLIT = /\G\s*(?>([^\s\\\'\"]+)|'([^\']*)'|"((?:[^\"\\]|\\.)*)"|(\\.?)|(\S))(\s|\z)?/m
34
+ BOOLEAN_QUOTE = /^('|")((?<!\\)@|(?<!\\)\!)/
35
+ POSITIVE_BOOLEAN = /^(?<!\\)\@/
36
+ ESCAPED_BOOLEAN = /\\(@|\!)/
37
+ KEY = /\b(?<!\\):/
38
+ INT = /^\d+$/
34
39
 
35
- # --
36
40
  def initialize(raw, defaults: {}, sep: "=")
37
41
  @sep = sep
38
42
  @rsep = Regexp.escape(sep)
@@ -46,12 +50,13 @@ module Liquid
46
50
  parse
47
51
  end
48
52
 
49
- # --
50
53
  def to_html(skip: [], hash: false)
51
54
  out = @args.each_with_object(hash ? {} : []) do |(k, v), o|
52
- next if k == :argv1 || skip.include?(k) ||
53
- v == false || v.is_a?(Hash) ||
54
- v.is_a?(Array)
55
+ next if k == :argv1
56
+ next if v.is_a?(Array)
57
+ next if skip.include?(k)
58
+ next if v.is_a?(Hash)
59
+ next if v == false
55
60
 
56
61
  o[k] = v if hash
57
62
  unless hash
@@ -62,28 +67,32 @@ module Liquid
62
67
  hash ? out : out.join(" ")
63
68
  end
64
69
 
65
- # --
66
70
  private
67
- def parse
68
- args, hash = from_shellwords, {}
69
- if args.first !~ BOOLEAN_REGEXP && args.first !~ KEY_REGEXP \
70
- && args.first !~ @sep_regexp
71
+ def argv1(args)
72
+ val = args[0]
73
+ hash = {}
71
74
 
75
+ # !"@true" && !"key1:key2=val" but "argv1 @true key1:key2=val"
76
+ if val !~ BOOLEAN && val !~ KEY && val !~ @sep_regexp
72
77
  hash = {
73
- :argv1 => args.delete_at(0)
78
+ argv1: args.delete_at(0)
74
79
  }
75
80
  end
76
81
 
82
+ return args, hash
83
+ end
84
+
85
+ private
86
+ def parse
87
+ args, hash = argv1(from_shellwords)
77
88
  @args = @defaults.deep_merge(args.each_with_object(hash) do |k, h, out = h|
78
89
  keys, _, val = k.rpartition(@sep_regexp)
79
90
 
80
- val = val.gsub(@escaped_sep_regexp, @sep)
81
- if keys.empty? && val =~ BOOLEAN_REGEXP
82
- # @true, @false will not map right.
83
- keys = val.gsub(BOOLEAN_REGEXP, "")
84
- end
91
+ val = val.gsub(@escaped_sep_regexp, @sep) # Unescape \\=
92
+ keys = val.gsub(BOOLEAN, "") if keys.empty? && val =~ BOOLEAN # @true
93
+ keys, val = val, nil if keys.empty? # key val
94
+ keys = keys.split(KEY).map(&:to_sym)
85
95
 
86
- keys = keys.split(KEY_REGEXP).map(&:to_sym)
87
96
  if keys.size > 1
88
97
  h = h[keys[0]] ||= {}
89
98
  keys[1...-1].each do |sk|
@@ -91,29 +100,54 @@ module Liquid
91
100
  end
92
101
  end
93
102
 
94
- val = false if val == "false"
95
- val = val.to_f if val =~ /^\d+\.\d+$/
96
- val = false if val =~ NEGATIVE_BOOLEAN_REGEXP
97
- val = true if val =~ POSITIVE_BOOLEAN_REGEXP
98
- val = val.to_i if val =~ /^\d+$/
99
- val = true if val == "true"
103
+ val = val.to_i if val =~ INT # key=1
104
+ val = val.to_f if val =~ FLOAT # key=0.1
105
+ val = false if val == "false" # key=false
106
+ val = false if val =~ NEGATIVE_BOOLEAN # !false
107
+ val = true if val =~ POSITIVE_BOOLEAN # @true
108
+ val = true if val == "true" # key=true
109
+
110
+ if val.is_a?(String)
111
+ then val = val.gsub(ESCAPED_BOOLEAN, "\\1")
112
+ end
100
113
 
101
114
  key = keys.last.to_sym
102
115
  h[key] << val if h[key].is_a?(Array)
103
- h[key] = [h[key]] << val if h[key]
116
+ h[key] = [h[key]].flatten << val if h[key] && !h[key].is_a?(Array)
104
117
  h[key] = val unless h[key]
105
118
 
106
119
  out
107
120
  end)
108
121
  end
109
122
 
110
- # --
111
123
  private
112
124
  def from_shellwords
113
- Shellwords.shellwords(@raw.gsub(/('|")([^\1]+)\1/) do |v|
114
- v.gsub(@sep_regexp, @escaped_sep)
125
+ shellsplit(@raw.gsub(/('|")([^\1]+)\1/) do |v|
126
+ v.gsub(BOOLEAN_QUOTE, BOOLEAN_QUOTE_REPLACEMENT).gsub(@sep_regexp, @escaped_sep)
115
127
  end)
116
128
  end
129
+
130
+ # Because Shellwords.shellwords on < 2.4 has problems
131
+ # with quotes and \\, we ported this back, this pretty
132
+ # much the same thing except we replace some of the
133
+ # questionable code like `String.new`
134
+ private
135
+ def shellsplit(line)
136
+ out, field = [], ""
137
+
138
+ line.scan(SHELLSPLIT) do |w, s, d, e, g, se|
139
+ raise ArgumentError, "Unmatched double quote: #{line.inspect}" if g
140
+ field = field + (w || s || (d && d.gsub(/\\([$`"\\\n])/, '\\1')) \
141
+ || e.gsub(/\\(.)/, '\\1'))
142
+
143
+ if se
144
+ out << field
145
+ field = ""
146
+ end
147
+ end
148
+
149
+ out
150
+ end
117
151
  end
118
152
  end
119
153
  end
@@ -5,7 +5,7 @@
5
5
  module Liquid
6
6
  class Tag
7
7
  class Parser
8
- VERSION = "1.3.0"
8
+ VERSION = "1.4.0"
9
9
  end
10
10
  end
11
11
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: liquid-tag-parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jordon Bedwell
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-07 00:00:00.000000000 Z
11
+ date: 2017-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -102,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
102
  version: '0'
103
103
  requirements: []
104
104
  rubyforge_project:
105
- rubygems_version: 2.6.13
105
+ rubygems_version: 2.6.14
106
106
  signing_key:
107
107
  specification_version: 4
108
108
  summary: Parse liquid tags like a professional