erb-trim 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (7) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +8 -0
  3. data/README.md +21 -0
  4. data/Rakefile +4 -0
  5. data/erb-trim.gemspec +34 -0
  6. data/lib/erb/trim.rb +49 -0
  7. metadata +48 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 58ecf24ef9ab51479c29e7bf2b457a0a2c4e46a01d0fa76f5204c2212187c6b9
4
+ data.tar.gz: f93889069f28295a8fbae59b7354d726055bbfee4e1db6b956f87e758cc1d07b
5
+ SHA512:
6
+ metadata.gz: c559234d57d5c50da51d222df0d098159a3000210369c2f20baad2156b63f855927484e72b31273b29901e557d838fe2ef5a5f4a6d151bfb6a4ea4092bc68794
7
+ data.tar.gz: 586039f977ba7e673c2e0bc3c84c28caa55998da25e46686a6b4fab13794ca0f89b2d23da2b8338b17fb1940754d1d0ae966de668e30823820e959bce5ff945e
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in erb-trim.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # ERB::Trim
2
+
3
+ An ERB extension that supports `<%-=`. [ruby/erb#24](https://github.com/ruby/erb/issues/24)
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ $ gem install erb-trim
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```rb
14
+ require 'erb/trim'
15
+
16
+ erb_notrim = " <%-= 'value' %>"
17
+ erb_trim = " <%-= 'value' if false %>"
18
+
19
+ ERB::Trim.new(erb_notrim).result #=> " value"
20
+ ERB::Trim.new(erb_trim).result #=> ""
21
+ ```
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
data/erb-trim.gemspec ADDED
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/erb/trim"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "erb-trim"
7
+ spec.version = ERB::Trim::VERSION
8
+ spec.authors = ["Takashi Kokubun"]
9
+ spec.email = ["takashikkbn@gmail.com"]
10
+
11
+ spec.summary = "ERB extension to add <%-="
12
+ spec.description = "ERB extension to add <%-="
13
+ spec.homepage = "https://github.com/k0kubun/erb-trim"
14
+ spec.required_ruby_version = ">= 2.6.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(__dir__) do
21
+ `git ls-files -z`.split("\x0").reject do |f|
22
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
23
+ end
24
+ end
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ # Uncomment to register a new dependency of your gem
30
+ # spec.add_dependency "example-gem", "~> 1.0"
31
+
32
+ # For more information and examples about making a new gem, check out our
33
+ # guide at: https://bundler.io/guides/creating_gem.html
34
+ end
data/lib/erb/trim.rb ADDED
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "erb"
4
+
5
+ class ERB
6
+ class Trim < ERB
7
+ VERSION = "0.1.0"
8
+
9
+ class TrimScanner < ERB::Compiler::TrimScanner
10
+ def stags
11
+ [*super, '[ \t]*<%-=']
12
+ end
13
+ end
14
+
15
+ class TrimCompiler < ERB::Compiler
16
+ def initialize(*)
17
+ super
18
+ end
19
+
20
+ def compile_stag(stag, out, scanner)
21
+ case stag
22
+ when /([ \t]*)<%-=/
23
+ scanner.stag = stag
24
+ add_put_cmd(out, content) if content.size > 0
25
+ self.content = +''
26
+ else
27
+ super
28
+ end
29
+ end
30
+
31
+ def compile_content(stag, out)
32
+ case stag
33
+ when /([ \t]*)<%-=/
34
+ add_insert_cmd(out, "-> (c) { c.to_s.empty? ? '' : '#{$1}' + c }.((#{content}))")
35
+ else
36
+ super
37
+ end
38
+ end
39
+
40
+ def make_scanner(src)
41
+ TrimScanner.new(src, @trim_mode, @percent)
42
+ end
43
+ end
44
+
45
+ def make_compiler(trim_mode)
46
+ TrimCompiler.new(trim_mode)
47
+ end
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: erb-trim
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Takashi Kokubun
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-11-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: ERB extension to add <%-=
14
+ email:
15
+ - takashikkbn@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - Gemfile
21
+ - README.md
22
+ - Rakefile
23
+ - erb-trim.gemspec
24
+ - lib/erb/trim.rb
25
+ homepage: https://github.com/k0kubun/erb-trim
26
+ licenses: []
27
+ metadata:
28
+ homepage_uri: https://github.com/k0kubun/erb-trim
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: 2.6.0
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubygems_version: 3.3.26
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: ERB extension to add <%-=
48
+ test_files: []