liquid-tag-parser 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6dc35e4c2520d7dacf0080c387460e1ae3b2d8b8
4
+ data.tar.gz: 7e739fbae912858b89dbecec2bb8d7b0b2bf9437
5
+ SHA512:
6
+ metadata.gz: 1ef0adb8188931e2a7dd2e6b9072d1825ccbf862976e01541192380c1b3dfff5e90549529375a279ebfc156f871bfb0f417d762c857c2e1c273f6e0cffc63f96
7
+ data.tar.gz: 01fc8859b3cec052d49911a2ab2cb6c4a97f71f5799f8c31a1f722e1dd676b5af430cc74fc7365216367ab20d514ea035b066d81b63300c4eb385807ef9dd67a
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+ gemspec
3
+
4
+ gem "rake"
5
+ gem "pry"
@@ -0,0 +1,109 @@
1
+ # Frozen-string-literal: true
2
+ # Copyright: 2017 - MIT License
3
+ # Encoding: utf-8
4
+
5
+ require "forwardable"
6
+ require "extras/hash"
7
+
8
+ module Liquid
9
+ module Tag
10
+ class Parser
11
+ attr_reader :args, :raw_args
12
+ extend Forwardable
13
+
14
+ def_delegator :@args, :each
15
+ def_delegator :@args, :to_enum
16
+ def_delegator :@args, :key?
17
+ def_delegator :@args, :to_h
18
+ def_delegator :@args, :map
19
+ def_delegator :@args, :[]=
20
+ def_delegator :@args, :[]
21
+
22
+ # --
23
+ BOOLEAN_REGEXP = /^(?<!\\)(\!|@)/
24
+ NEGATIVE_BOOLEAN_REGEXP = /^(?<!\\)\!/
25
+ POSITIVE_BOOLEAN_REGEXP = /^(?<!\\)\@/
26
+ KEY_REGEXP = /\b(?<!\\):/
27
+
28
+ # --
29
+ def initialize(raw, defaults: {}, sep: "=")
30
+ @sep = sep
31
+ @rsep = Regexp.escape(sep)
32
+ @escaped_sep_regexp = /\\#{@rsep}/
33
+ @sep_regexp = /\b(?<!\\)#{@rsep}/
34
+ @escaped_sep = "\\#{@sep}"
35
+ @defaults = defaults
36
+ @args = {}
37
+ @raw = raw
38
+
39
+ parse
40
+ end
41
+
42
+ # --
43
+ def to_html(skip: [])
44
+ @args.each_with_object([]) do |(k, v), a|
45
+ next if k == :argv1 || skip.include?(k) ||
46
+ v == false || v.is_a?(Hash) ||
47
+ v.is_a?(Array)
48
+
49
+
50
+ a << (v == true ? k.to_s : "#{k}=\"#{v}\"")
51
+ end.join(" ")
52
+ end
53
+
54
+ # --
55
+ private
56
+ def parse
57
+ args, hash = from_shellwords, {}
58
+ if args.first !~ BOOLEAN_REGEXP && args.first !~ KEY_REGEXP \
59
+ && args.first !~ @sep_regexp
60
+
61
+ hash = {
62
+ :argv1 => args.delete_at(0)
63
+ }
64
+ end
65
+
66
+ @args = @defaults.deep_merge(args.each_with_object(hash) do |k, h, out = h|
67
+ keys, _, val = k.rpartition(@sep_regexp)
68
+ keys = keys.split(KEY_REGEXP).map(&:to_sym)
69
+ val = val.gsub(@escaped_sep_regexp, @sep)
70
+
71
+ # @true, @false will not split or map right.
72
+ if keys.size == 0 && val =~ BOOLEAN_REGEXP
73
+ keys = [
74
+ val
75
+ ]
76
+
77
+ elsif keys.size > 1
78
+ h = h[keys[0]] ||= {}
79
+ keys[1...-1].each do |sk|
80
+ h = h[sk] ||= {}
81
+ end
82
+ end
83
+
84
+ val = false if val == "false"
85
+ val = val.to_f if val =~ /^\d+\.\d+$/
86
+ val = false if val =~ NEGATIVE_BOOLEAN_REGEXP
87
+ val = true if val =~ POSITIVE_BOOLEAN_REGEXP
88
+ val = val.to_i if val =~ /^\d+$/
89
+ val = true if val == "true"
90
+
91
+ key = keys.last.to_s.gsub(BOOLEAN_REGEXP, "").to_sym
92
+ h[key] << val if h[key].is_a?(Array)
93
+ h[key] = [h[key]] << val if h[key]
94
+ h[key] = val unless h[key]
95
+
96
+ out
97
+ end)
98
+ end
99
+
100
+ # --
101
+ private
102
+ def from_shellwords
103
+ Shellwords.shellwords(@raw.gsub(/('|")([^\1]+)\1/) do |v|
104
+ v.gsub(@sep_regexp, @escaped_sep)
105
+ end)
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,11 @@
1
+ # Frozen-string-literal: true
2
+ # Copyright: 2017 - MIT License
3
+ # Encoding: utf-8
4
+
5
+ module Liquid
6
+ module Tag
7
+ class Parser
8
+ VERSION = "1.0.0"
9
+ end
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: liquid-tag-parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jordon Bedwell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '4'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '4'
33
+ - !ruby/object:Gem::Dependency
34
+ name: luna-rspec-formatters
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.7'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '3.7'
47
+ - !ruby/object:Gem::Dependency
48
+ name: liquid
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '4.0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '4.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: extras
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '0.3'
68
+ type: :runtime
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '0.3'
75
+ description: Parse liquid tags easily
76
+ email:
77
+ - jordon@envygeeks.io
78
+ executables: []
79
+ extensions: []
80
+ extra_rdoc_files: []
81
+ files:
82
+ - Gemfile
83
+ - lib/liquid/tag/parser.rb
84
+ - lib/liquid/tag/parser/version.rb
85
+ homepage: http://github.com/envygeeks/liquid-tag-parser
86
+ licenses:
87
+ - MIT
88
+ metadata: {}
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: 2.1.0
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubyforge_project:
105
+ rubygems_version: 2.6.13
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Parse liquid tags like a professional
109
+ test_files: []