liquid_tag_with_params 0.0.1

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: 86c51173d39d8405318c75ee215ce6ae3259627b
4
+ data.tar.gz: 776e631917a9f5459c397c2dc5fb161e7a253072
5
+ SHA512:
6
+ metadata.gz: 06e4b41cd6c835f5391bb43edacba9d1a11793c30ad72be8686f032b7d45a9da0cd50bfede6bbdb27a01ed9e0c947c120e202a3b667fce4e146ed44bbb5b4785
7
+ data.tar.gz: 7b79a805b189812e96083f233b4ef208e94eed8ff32b1d26ef0d677339eb772bab1a92412ab8b8dcd9dbfae91183241b66e251e540836addd55a4d3130c0bca0
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem 'minitest'
7
+ gem 'rake'
8
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,23 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ liquid_tag_with_params (0.0.1)
5
+ liquid (>= 4.0.0)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ liquid (4.0.0)
11
+ minitest (5.10.3)
12
+ rake (12.1.0)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ liquid_tag_with_params!
19
+ minitest
20
+ rake
21
+
22
+ BUNDLED WITH
23
+ 1.14.6
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2017 Oleg Khabarov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # Liquid Tag With Params
2
+ [![Gem Version](https://img.shields.io/gem/v/liquid_tag_with_params.svg?style=flat)](http://rubygems.org/gems/liquid_tag_with_params) [![Gem Downloads](https://img.shields.io/gem/dt/liquid_tag_with_params.svg?style=flat)](http://rubygems.org/gems/liquid_tag_with_params) [![Build Status](https://img.shields.io/travis/comfy/liquid_tag_with_params.svg?style=flat)](https://travis-ci.org/comfy/liquid_tag_with_params)
3
+
4
+ This is a `Liquid::TagWithParams` that extends `Liquid::Tag` by adding support for, you guessed it, params.
5
+
6
+ ## Install
7
+
8
+ Add gem to your Gemfile:
9
+
10
+ ```ruby
11
+ gem 'liquid_tag_with_params', '~> 0.0.1'
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ This gem is primarily for extending Liquid [by creating your own tags](https://github.com/Shopify/liquid/wiki/Liquid-for-Programmers#create-your-own-tags). Out of the box `Liquid::Tag`
17
+ accepts a string and that's about it. `Liquid::TagWithParams` will take that
18
+ string and will make parameters out of it so you can use them during rendering.
19
+
20
+ Here's an example of such tag:
21
+
22
+ ```ruby
23
+ class ShuffleTag < Liquid::TagWithParams
24
+ def initialize(tag_name, params, tokens)
25
+ super
26
+ end
27
+
28
+ def render(context)
29
+ # assuming that last params were of key: value format
30
+ options = @params.extract_options!
31
+
32
+ # note: keys and values are always strings
33
+ times = options["times"] || 1
34
+
35
+ # shuffle really hard
36
+ times.to_i.times do
37
+ @params.shuffle!
38
+ end
39
+
40
+ @params.join(', ')
41
+ end
42
+ end
43
+
44
+ Liquid::Template.register_tag('shuffle', ShuffleTag)
45
+ ```
46
+
47
+ ```ruby
48
+ @template = Liquid::Template.parse("{% shuffle 1, 2, 3, 4, times: 5 %}")
49
+ @template.render # => "2, 1, 3, 4"
50
+ ```
51
+
52
+ ---
53
+ Copyright 2017, Oleg Khabarov
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'rake/testtask'
3
+ require 'bundler/setup'
4
+
5
+ Rake::TestTask.new(:test) do |test|
6
+ test.libs << 'lib' << 'test'
7
+ test.pattern = 'test/**/*_test.rb'
8
+ test.verbose = true
9
+ end
10
+
11
+ task default: :test
@@ -0,0 +1,16 @@
1
+ require 'liquid'
2
+ require_relative 'tag_with_params/parser'
3
+
4
+ class Liquid::TagWithParams < Liquid::Tag
5
+
6
+ attr_reader :params
7
+
8
+ def initialize(tag_name, markup, parse_context)
9
+ super
10
+ @params = LiquidTagWithParams::Parser.parse(markup)
11
+ end
12
+
13
+ def render(_context)
14
+ ""
15
+ end
16
+ end
@@ -0,0 +1,78 @@
1
+ module LiquidTagWithParams::Parser
2
+
3
+ SINGLE_STRING_LITERAL = /'[^\']*'/
4
+ DOUBLE_STRING_LITERAL = /"[^\"]*"/
5
+ IDENTIFIER = /[a-z][\w-]*/i
6
+ COLUMN = /\:/
7
+ COMMA = /\,/
8
+
9
+ def self.parse(text)
10
+ parameterize(slice(tokenize(text)))
11
+ end
12
+
13
+ def self.parameterize(token_groups)
14
+ params = [ ]
15
+ token_groups.each do |tokens|
16
+ case
17
+ when tokens.count == 1
18
+ collect_param_for_string!(params, tokens[0])
19
+ when tokens.count == 3
20
+ collect_param_for_hash!(params, tokens)
21
+ else
22
+ raise "Unexpected tokens found: #{tokens}"
23
+ end
24
+ end
25
+ params
26
+ end
27
+
28
+ def self.collect_param_for_string!(params, token)
29
+ type, value = token
30
+ raise "Unexpected token: #{token}" unless type == :string
31
+ params << value
32
+ end
33
+
34
+ def self.collect_param_for_hash!(params, tokens)
35
+ key, col, val = tokens
36
+ key_type, key_value = key
37
+ col_type, col_value = col
38
+ val_type, val_value = val
39
+
40
+ unless key_type == :string && col_type == :column && val_type == :string
41
+ raise "Unexpected tokens: #{tokens}"
42
+ end
43
+
44
+ hash = params.last.is_a?(Hash) ? params.pop : {}
45
+ hash[key_value] = val_value
46
+ params << hash
47
+ end
48
+
49
+ # grouping tokens based on the comma and also removing comma tokens
50
+ def self.slice(tokens)
51
+ tokens.slice_after do |token|
52
+ token[0] == :comma
53
+ end.map do |expression|
54
+ expression.reject{|t| t[0] == :comma}
55
+ end
56
+ end
57
+
58
+ # tokenizing input string into a list of touples
59
+ def self.tokenize(args_string)
60
+ tokens = []
61
+ ss = StringScanner.new(args_string)
62
+ until ss.eos?
63
+ ss.skip(/\s*/)
64
+ break if ss.eos?
65
+ token = case
66
+ when t = ss.scan(SINGLE_STRING_LITERAL) then [:string, t]
67
+ when t = ss.scan(DOUBLE_STRING_LITERAL) then [:string, t]
68
+ when t = ss.scan(IDENTIFIER) then [:string, t]
69
+ when t = ss.scan(COLUMN) then [:column, t]
70
+ when t = ss.scan(COMMA) then [:comma, t]
71
+ else
72
+ raise Error, "Unexpected char: #{ss.getch}"
73
+ end
74
+ tokens << token
75
+ end
76
+ tokens
77
+ end
78
+ end
@@ -0,0 +1,3 @@
1
+ module LiquidTagWithParams
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,18 @@
1
+ $:.unshift File.expand_path('../lib', __FILE__)
2
+ require 'liquid/tag_with_params/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "liquid_tag_with_params"
6
+ s.version = LiquidTagWithParams::VERSION
7
+ s.authors = ["Oleg Khabarov"]
8
+ s.email = ["oleg@khabarov.ca"]
9
+ s.homepage = "http://github.com/comfy/liquid_tag_with_params"
10
+ s.summary = "Liquid::TagWithParams"
11
+ s.description = "Tag class for Liquid markup that supports extra params"
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.platform = Gem::Platform::RUBY
15
+ s.require_paths = ['lib']
16
+
17
+ s.add_dependency 'liquid', ">= 4.0.0"
18
+ end
@@ -0,0 +1,108 @@
1
+ require_relative '../../test_helper'
2
+
3
+ class Liquid::TagWithParams::ParserTest < MiniTest::Test
4
+
5
+ def test_tokenizer
6
+ tokens = LiquidTagWithParams::Parser.tokenize("param")
7
+ assert_equal [[:string, "param"]], tokens
8
+ end
9
+
10
+ def test_tokenizer_with_commas
11
+ tokens = LiquidTagWithParams::Parser.tokenize("param_a, param_b")
12
+ assert_equal [[:string, "param_a"], [:comma, ","], [:string, "param_b"]], tokens
13
+ end
14
+
15
+ def test_tokenizer_with_columns
16
+ tokens = LiquidTagWithParams::Parser.tokenize("key: value")
17
+ assert_equal [[:string, "key"], [:column, ":"], [:string, "value"]], tokens
18
+ end
19
+
20
+ def test_tokenizer_with_quoted_value
21
+ tokens = LiquidTagWithParams::Parser.tokenize("key: 'v1, v2: v3'")
22
+ assert_equal [[:string, "key"], [:column, ":"], [:string, "'v1, v2: v3'"]], tokens
23
+
24
+ tokens = LiquidTagWithParams::Parser.tokenize('key: "v1, v2: v3"')
25
+ assert_equal [[:string, "key"], [:column, ":"], [:string, '"v1, v2: v3"']], tokens
26
+ end
27
+
28
+ def test_tokenizer_with_bad_input
29
+ assert_exception_raised do
30
+ LiquidTagWithParams::Parser.tokenize("%")
31
+ end
32
+ end
33
+
34
+ def test_slice
35
+ tokens = [[:string, "a"], [:comma, ","], [:string, "b"]]
36
+ token_groups = LiquidTagWithParams::Parser.slice(tokens)
37
+ assert_equal [[[:string, "a"]], [[:string, "b"]]], token_groups
38
+ end
39
+
40
+ def test_parameterize
41
+ token_groups = [[[:string, "a"]]]
42
+ params = LiquidTagWithParams::Parser.parameterize(token_groups)
43
+ assert_equal ["a"], params
44
+
45
+ token_groups = [[[:string, "a"], [:column, ":"], [:string, "b"]]]
46
+ params = LiquidTagWithParams::Parser.parameterize(token_groups)
47
+ assert_equal [{"a" => "b"}], params
48
+ end
49
+
50
+ def test_parameterize_with_bad_input
51
+ token_groups = [[[:string, "a"], [:string, "b"]]]
52
+ assert_exception_raised do
53
+ LiquidTagWithParams::Parser.parameterize(token_groups)
54
+ end
55
+ end
56
+
57
+ def test_collect_param_for_string!
58
+ params = []
59
+ LiquidTagWithParams::Parser.collect_param_for_string!(params, [:string, "a"])
60
+ assert_equal ["a"], params
61
+ end
62
+
63
+ def test_collect_param_for_string_with_bad_input
64
+ assert_exception_raised do
65
+ LiquidTagWithParams::Parser.collect_param_for_string!([], [:invalid, "a"])
66
+ end
67
+ end
68
+
69
+ def test_collect_param_for_hash!
70
+ params = []
71
+ tokens = [[:string, "a"], [:column, ":"], [:string, "b"]]
72
+ LiquidTagWithParams::Parser.collect_param_for_hash!(params, tokens)
73
+ assert_equal [{"a" => "b"}], params
74
+ end
75
+
76
+ def test_collect_param_for_hash_with_trailing_hash
77
+ params = ["x", {"y" => "z"}]
78
+ tokens = [[:string, "a"], [:column, ":"], [:string, "b"]]
79
+ LiquidTagWithParams::Parser.collect_param_for_hash!(params, tokens)
80
+ assert_equal ["x", {"y" => "z", "a" => "b"}], params
81
+ end
82
+
83
+ def test_collect_param_for_hash_with_trailing_param
84
+ params = ["x", {"y" => "z"}, "k"]
85
+ tokens = [[:string, "a"], [:column, ":"], [:string, "b"]]
86
+ LiquidTagWithParams::Parser.collect_param_for_hash!(params, tokens)
87
+ assert_equal ["x", {"y" => "z"}, "k", {"a" => "b"}], params
88
+ end
89
+
90
+ def test_collect_param_for_hash_with_bad_input
91
+ assert_exception_raised do
92
+ tokens = [[:string, "a"], [:invalid, ":"], [:string, "b"]]
93
+ LiquidTagWithParams::Parser.collect_param_for_hash!([], tokens)
94
+ end
95
+ end
96
+
97
+ def test_parse
98
+ text = "param_a, param_b, key_a: val_a, key_b: val_b, param_c, key_c: val_c"
99
+ params = LiquidTagWithParams::Parser.parse(text)
100
+ assert_equal [
101
+ "param_a",
102
+ "param_b",
103
+ {"key_a" => "val_a", "key_b" => "val_b"},
104
+ "param_c",
105
+ {"key_c" => "val_c"}
106
+ ], params
107
+ end
108
+ end
@@ -0,0 +1,15 @@
1
+ require_relative '../test_helper'
2
+
3
+ class Liquid::TagWithParamsTest < MiniTest::Test
4
+ include Liquid
5
+
6
+ def test_initialization
7
+ params = "param_1, param_2, key_1: val_1, key_2: val_2"
8
+ tag = Liquid::TagWithParams.send(:new, 'test_tag', params, ParseContext.new)
9
+
10
+ assert_equal 'test_tag', tag.tag_name
11
+ assert_equal params, tag.instance_variable_get("@markup")
12
+ assert_equal ['param_1', 'param_2', {'key_1' => 'val_1', 'key_2' => 'val_2'}],
13
+ tag.params
14
+ end
15
+ end
@@ -0,0 +1,29 @@
1
+ require 'bundler/setup'
2
+ require 'minitest/autorun'
3
+ require 'liquid'
4
+ require 'liquid/tag_with_params'
5
+
6
+ class MiniTest::Test
7
+
8
+ # Example usage:
9
+ # assert_exception_raised do ... end
10
+ # assert_exception_raised ActiveRecord::RecordInvalid do ... end
11
+ # assert_exception_raised Plugin::Error, 'error_message' do ... end
12
+ def assert_exception_raised(exception_class = nil, error_message = nil, &block)
13
+ exception_raised = nil
14
+ yield
15
+ rescue => exception_raised
16
+ ensure
17
+ if exception_raised
18
+ if exception_class
19
+ assert_equal exception_class, exception_raised.class, exception_raised.to_s
20
+ else
21
+ assert true
22
+ end
23
+ assert_equal error_message, exception_raised.to_s if error_message
24
+ else
25
+ flunk 'Exception was not raised'
26
+ end
27
+ end
28
+
29
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: liquid_tag_with_params
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Oleg Khabarov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-14 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: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
27
+ description: Tag class for Liquid markup that supports extra params
28
+ email:
29
+ - oleg@khabarov.ca
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - Gemfile
35
+ - Gemfile.lock
36
+ - LICENSE
37
+ - README.md
38
+ - Rakefile
39
+ - lib/liquid/tag_with_params.rb
40
+ - lib/liquid/tag_with_params/parser.rb
41
+ - lib/liquid/tag_with_params/version.rb
42
+ - liquid_tag_with_params.gemspec
43
+ - test/liquid/tag_with_params/parser_test.rb
44
+ - test/liquid/tag_with_params_test.rb
45
+ - test/test_helper.rb
46
+ homepage: http://github.com/comfy/liquid_tag_with_params
47
+ licenses: []
48
+ metadata: {}
49
+ post_install_message:
50
+ rdoc_options: []
51
+ require_paths:
52
+ - lib
53
+ required_ruby_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project:
65
+ rubygems_version: 2.6.11
66
+ signing_key:
67
+ specification_version: 4
68
+ summary: Liquid::TagWithParams
69
+ test_files: []