liquid-tag-attribute_parser 0.1.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
+ SHA256:
3
+ metadata.gz: 910fc47e5c4f81438c3bb047d9efc21aa0a66999c9915c2f55db3f49fb4ad793
4
+ data.tar.gz: f109516c8d350a77d641ef9554173a2c2adb6f89a811c6cbe2f2df96bb8757b6
5
+ SHA512:
6
+ metadata.gz: 2dd04e8d6c1a987c699a0951688c1346c42c42013b4481276b1e329f8c8740b836e1a286af459932e34a06999a1aacd963cf9792d8ca424a34198b56782d935f
7
+ data.tar.gz: 397c35d8bfb52b246ab59fe5c0cec458ce2e4d576738a8718571c27e38de088683bfecab1223a44d41ded401cdf971bac2fcb3e56d1b5303fc33f1af41df0ad3
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,22 @@
1
+ require:
2
+ - rubocop-rake
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.5
7
+ NewCops: enable
8
+
9
+ Style/StringLiterals:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Style/StringLiteralsInInterpolation:
14
+ Enabled: true
15
+ EnforcedStyle: double_quotes
16
+
17
+ Layout/LineLength:
18
+ Max: 120
19
+
20
+ Metrics/BlockLength:
21
+ Exclude:
22
+ - spec/**/*
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-08-25
4
+
5
+ * Initial release
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in liquid-tag-attribute_parser.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.7"
13
+ gem "rubocop-rake", "~> 0.6"
14
+ gem "rubocop-rspec", "~> 2.4"
15
+
16
+ gem "simplecov", "~> 0.21"
17
+ gem "yard", "~> 0.9"
data/Gemfile.lock ADDED
@@ -0,0 +1,74 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ liquid-tag-attribute_parser (0.1.0)
5
+ liquid (>= 3.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ diff-lcs (1.4.4)
12
+ docile (1.4.0)
13
+ liquid (4.0.3)
14
+ parallel (1.20.1)
15
+ parser (3.0.2.0)
16
+ ast (~> 2.4.1)
17
+ rainbow (3.0.0)
18
+ rake (13.0.6)
19
+ regexp_parser (2.1.1)
20
+ rexml (3.2.5)
21
+ rspec (3.10.0)
22
+ rspec-core (~> 3.10.0)
23
+ rspec-expectations (~> 3.10.0)
24
+ rspec-mocks (~> 3.10.0)
25
+ rspec-core (3.10.1)
26
+ rspec-support (~> 3.10.0)
27
+ rspec-expectations (3.10.1)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.10.0)
30
+ rspec-mocks (3.10.2)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.10.0)
33
+ rspec-support (3.10.2)
34
+ rubocop (1.19.1)
35
+ parallel (~> 1.10)
36
+ parser (>= 3.0.0.0)
37
+ rainbow (>= 2.2.2, < 4.0)
38
+ regexp_parser (>= 1.8, < 3.0)
39
+ rexml
40
+ rubocop-ast (>= 1.9.1, < 2.0)
41
+ ruby-progressbar (~> 1.7)
42
+ unicode-display_width (>= 1.4.0, < 3.0)
43
+ rubocop-ast (1.10.0)
44
+ parser (>= 3.0.1.1)
45
+ rubocop-rake (0.6.0)
46
+ rubocop (~> 1.0)
47
+ rubocop-rspec (2.4.0)
48
+ rubocop (~> 1.0)
49
+ rubocop-ast (>= 1.1.0)
50
+ ruby-progressbar (1.11.0)
51
+ simplecov (0.21.2)
52
+ docile (~> 1.1)
53
+ simplecov-html (~> 0.11)
54
+ simplecov_json_formatter (~> 0.1)
55
+ simplecov-html (0.12.3)
56
+ simplecov_json_formatter (0.1.3)
57
+ unicode-display_width (2.0.0)
58
+ yard (0.9.26)
59
+
60
+ PLATFORMS
61
+ x86_64-darwin-20
62
+
63
+ DEPENDENCIES
64
+ liquid-tag-attribute_parser!
65
+ rake (~> 13.0)
66
+ rspec (~> 3.0)
67
+ rubocop (~> 1.7)
68
+ rubocop-rake (~> 0.6)
69
+ rubocop-rspec (~> 2.4)
70
+ simplecov (~> 0.21)
71
+ yard (~> 0.9)
72
+
73
+ BUNDLED WITH
74
+ 2.2.26
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Unai Abrisketa
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,177 @@
1
+ # Liquid::Tag::AttributeParser
2
+
3
+ `Liquid::Tag::AttributeParser` allows you to send familiar `html`-like attributes along with your Liquid tags to receive back a more manageable Ruby hash with all the given information.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'liquid-tag-attribute_parser'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```shell
16
+ $ bundle install
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```shell
22
+ $ gem install liquid-tag-attribute_parser
23
+ ```
24
+ ## Usage
25
+
26
+ Three easy steps:
27
+
28
+ 1. Take the raw argument data you get from the Liquid Tag
29
+ 1. Use `Liquid::Tag::AttributeParser` to parse the raw data
30
+ 1. Get back the data on a Hash format
31
+
32
+ #### Working example for [Jekyll tags](https://jekyllrb.com/docs/plugins/tags/):
33
+
34
+ ```ruby
35
+ require "liquid/tag/attribute_parser"
36
+
37
+ module Jekyll
38
+ class GreetingTag < Liquid::Tag
39
+ def initialize(_tag_name, text, _tokens)
40
+ super
41
+ @attributes = Liquid::Tag::AttributeParser.new(text).attributes
42
+ end
43
+
44
+ def render(_context)
45
+ "Hello! My name is #{@attributes[:name]} and I'm #{@attributes[:age]}"
46
+ end
47
+ end
48
+ end
49
+
50
+ Liquid::Template.register_tag('greeting', Jekyll::GreetingTag)
51
+ ```
52
+
53
+ ```html
54
+ {% greeting name="Jekyll" age=13 %}
55
+ ```
56
+
57
+ ## Supported data types
58
+
59
+ ### `String`
60
+
61
+ ```ruby
62
+ Liquid::Tag::AttributeParser.new('string="This is a string"').attributes
63
+
64
+ # => {:string=>"This is a string"}
65
+ ```
66
+
67
+ ### `Boolean`
68
+
69
+ ```ruby
70
+ Liquid::Tag::AttributeParser.new('boolean=true').attributes
71
+
72
+ # => {:boolean=>true}
73
+ ```
74
+
75
+ ```ruby
76
+ Liquid::Tag::AttributeParser.new('boolean=false').attributes
77
+
78
+ # => {:boolean=>false}
79
+ ```
80
+
81
+ ### `Integer`
82
+
83
+ ```ruby
84
+ Liquid::Tag::AttributeParser.new('integer=123').attributes
85
+
86
+ # => {:integer=>123}
87
+ ```
88
+
89
+ ```ruby
90
+ Liquid::Tag::AttributeParser.new('integer=+123').attributes
91
+
92
+ # => {:integer=>123}
93
+ ```
94
+
95
+ ```ruby
96
+ Liquid::Tag::AttributeParser.new('integer=-123').attributes
97
+
98
+ # => {:integer=>-123}
99
+ ```
100
+
101
+ ### `Float`
102
+
103
+ ```ruby
104
+ Liquid::Tag::AttributeParser.new('float=1.23').attributes
105
+
106
+ # => {:float=>1.23}
107
+ ```
108
+
109
+ ```ruby
110
+ Liquid::Tag::AttributeParser.new('float=+1.23').attributes
111
+
112
+ # => {:float=>1.23}
113
+ ```
114
+
115
+ ```ruby
116
+ Liquid::Tag::AttributeParser.new('float=-1.23').attributes
117
+
118
+ # => {:float=>-1.23}
119
+ ```
120
+
121
+ ```ruby
122
+ Liquid::Tag::AttributeParser.new('float=123.0').attributes
123
+
124
+ # => {:float=>123.0}
125
+ ```
126
+
127
+ ```ruby
128
+ Liquid::Tag::AttributeParser.new('float=+123.0').attributes
129
+
130
+ # => {:float=>123.0}
131
+ ```
132
+
133
+ ```ruby
134
+ Liquid::Tag::AttributeParser.new('float=-123.0').attributes
135
+
136
+ # => {:float=>-123.0}
137
+ ```
138
+
139
+ ```ruby
140
+ Liquid::Tag::AttributeParser.new('float=.123').attributes
141
+
142
+ # => {:float=>0.123}
143
+ ```
144
+
145
+ ```ruby
146
+ Liquid::Tag::AttributeParser.new('float=+.123').attributes
147
+
148
+ # => {:float=>0.123}
149
+ ```
150
+
151
+ ```ruby
152
+ Liquid::Tag::AttributeParser.new('float=-.123').attributes
153
+
154
+ # => {:float=>-0.123}
155
+ ```
156
+
157
+ ### Combination of different data types
158
+
159
+ ```ruby
160
+ Liquid::Tag::AttributeParser.new('string="This is a string" boolean=true integer=-123 float=.123').attributes
161
+
162
+ # => {:string=>"This is a string", :boolean=>true, :integer=>-123, :float=>0.123}
163
+ ```
164
+
165
+ ## Development
166
+
167
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
168
+
169
+ To install this gem onto your local machine, run `bundle exec rake install`.
170
+
171
+ ## Contributing
172
+
173
+ Bug reports and pull requests are welcome on GitHub at https://github.com/unabris/liquid-tag-attribute_parser.
174
+
175
+ ## License
176
+
177
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "liquid/tag/attribute_parser"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Liquid
4
+ class Tag
5
+ class AttributeParser
6
+ VERSION = "0.1.0"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "attribute_parser/version"
4
+
5
+ module Liquid
6
+ class Tag
7
+ class AttributeParser # :nodoc:
8
+ # TODO: Improve the key selecting regexp
9
+ STRING_ATTRIBUTES = /[^\s]+=(?:'[^']+')|[^\s]+=(?:"[^"]+")/.freeze
10
+ BOOLEAN_ATTRIBUTES = /[^\s]+=(?:true|false)/.freeze
11
+ NUMBER_ATTRIBUTES = /[^\s]+=(?:[+-]?(?:[0-9]*[.])?[0-9]+)/.freeze
12
+
13
+ attr_reader :attributes
14
+
15
+ def initialize(raw)
16
+ @raw = raw
17
+ @attributes = {}
18
+
19
+ parse
20
+ end
21
+
22
+ private
23
+
24
+ def parse
25
+ %i[string boolean number].each do |kind|
26
+ regexp = Object.const_get("#{self.class}::#{kind.upcase}_ATTRIBUTES")
27
+
28
+ instance_variable_set("@#{kind}_attributes", @raw.scan(regexp).map { |a| a.split("=") })
29
+ send("parse_#{kind}_attributes")
30
+ end
31
+ end
32
+
33
+ def parse_string_attributes
34
+ @string_attributes.each do |k, v|
35
+ # TODO: Fin a better way for parsing `v` without `eval`
36
+ @attributes[k.to_sym] = eval(v) # rubocop:disable Security/Eval
37
+ end
38
+ end
39
+
40
+ def parse_boolean_attributes
41
+ @boolean_attributes.each do |k, v|
42
+ # TODO: Fin a better way for parsing `v` without `eval`
43
+ @attributes[k.to_sym] = eval(v) # rubocop:disable Security/Eval
44
+ end
45
+ end
46
+
47
+ def parse_number_attributes
48
+ @number_attributes.each do |k, v|
49
+ # Add leading zero when `v` starts with `+.`, `-.` or just `.` because it breaks the `eval` statement
50
+ v.gsub!(/^[+-]?(\.)/) { |m| m.gsub!(Regexp.last_match(1), "0.") }
51
+
52
+ # TODO: Fin a better way for parsing `v` without `eval`
53
+ @attributes[k.to_sym] = eval(v) # rubocop:disable Security/Eval
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/liquid/tag/attribute_parser/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "liquid-tag-attribute_parser"
7
+ spec.version = Liquid::Tag::AttributeParser::VERSION
8
+ spec.authors = ["Unai Abrisketa"]
9
+ spec.email = ["github@unaiabrisketa.com"]
10
+
11
+ spec.summary = "Easily parse liquid tags"
12
+ spec.description = "Send html-like attributes along with your Liquid tag and parse them to get a hash back"
13
+ spec.homepage = "https://github.com/unabris/liquid-tag-attribute_parser"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.5.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/unabris/liquid-tag-attribute_parser"
19
+ spec.metadata["changelog_uri"] = "https://github.com/unabris/liquid-tag-attribute_parser/blob/main/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
26
+ end
27
+ end
28
+ spec.bindir = "exe"
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ["lib"]
31
+
32
+ spec.add_runtime_dependency("liquid", ">= 3.0")
33
+
34
+ # For more information and examples about making a new gem, checkout our
35
+ # guide at: https://bundler.io/guides/creating_gem.html
36
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: liquid-tag-attribute_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Unai Abrisketa
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-08-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: liquid
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ description: Send html-like attributes along with your Liquid tag and parse them to
28
+ get a hash back
29
+ email:
30
+ - github@unaiabrisketa.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".rspec"
36
+ - ".rubocop.yml"
37
+ - CHANGELOG.md
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - bin/console
44
+ - bin/setup
45
+ - lib/liquid/tag/attribute_parser.rb
46
+ - lib/liquid/tag/attribute_parser/version.rb
47
+ - liquid-tag-attribute_parser.gemspec
48
+ homepage: https://github.com/unabris/liquid-tag-attribute_parser
49
+ licenses:
50
+ - MIT
51
+ metadata:
52
+ homepage_uri: https://github.com/unabris/liquid-tag-attribute_parser
53
+ source_code_uri: https://github.com/unabris/liquid-tag-attribute_parser
54
+ changelog_uri: https://github.com/unabris/liquid-tag-attribute_parser/blob/main/CHANGELOG.md
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 2.5.0
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubygems_version: 3.2.26
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: Easily parse liquid tags
74
+ test_files: []