gsub_filter 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gsub_filter.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 kyoendo
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,60 @@
1
+ # GsubFilter
2
+
3
+ This is a text filter for gsub chain. You can set or replace as many gsub filters as you like, then run them through a given text.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'gsub_filter'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install gsub_filter
18
+
19
+ ## Usage
20
+
21
+ require "gsub_filter"
22
+
23
+ gs = GsubFilter.new("ruby is a fantastic language!\nI love ruby.")
24
+
25
+ # make each words capitalize
26
+ gs.filter(/\w+/) {|md| md.to_s.capitalize }
27
+
28
+ # add asterisks only to the first 'ruby'
29
+ gs.filter(/ruby/i, global:false) { |md| "*#{md.to_s}*" }
30
+
31
+ # a MatchData object passes to the block as its first parameter
32
+ gs.filter(/a(.)/) { |md| "a-#{md[1]}" }
33
+
34
+ # excecute the above filters with run method
35
+ gs.run # => "*Ruby* Is A Fa-nta-stic La-ngua-ge!\nI Love Ruby."
36
+
37
+ GsubFilter#run can take another text for these filters.
38
+
39
+ gs.run("hello, world of ruby!") # => "Hello, World Of *Ruby*!"
40
+
41
+ Each filter can be replaced with GsubFilter#replace.
42
+
43
+ gs.replace(1, /ruby/i) { |md| "###{md.to_s}##" }
44
+
45
+ gs.run # => "##Ruby## Is A Fa-nta-stic La-ngua-ge!\nI Love ##Ruby##."
46
+
47
+ The MatchData object can be stocked through the second parameter of the block, which is accessed with GsubFilter#stocks.
48
+
49
+ gs.filter(/#(\w+)#/) { |md, stocks| stocks[:lang] << md[1]; "+#{md[1]}+" }
50
+
51
+ gs.run # => "#+Ruby+# Is A Fa-nta-stic La-ngua-ge!\nI Love #+Ruby+#."
52
+ gs.stocks # => {:lang=>["Ruby", "Ruby"]}
53
+
54
+ ## Contributing
55
+
56
+ 1. Fork it
57
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
58
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
59
+ 4. Push to the branch (`git push origin my-new-feature`)
60
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/gsub_filter/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["kyoendo"]
6
+ gem.email = ["postagie@gmail.com"]
7
+ gem.description = %q{Build gsub chain filter}
8
+ gem.summary = %q{
9
+ This is a text filter for gsub chain. You can set or replace as many gsub filters as you like, then run them through a given text.
10
+ }.strip
11
+ gem.homepage = "https://github.com/melborne/gsub-filter"
12
+
13
+ gem.files = `git ls-files`.split($\)
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = "gsub_filter"
16
+ gem.require_paths = ["lib"]
17
+ gem.version = GsubFilter::VERSION
18
+ gem.required_ruby_version = '>=1.9.2'
19
+ end
@@ -0,0 +1,46 @@
1
+ # encoding: UTF-8
2
+ require "gsub_filter/version"
3
+
4
+ class GsubFilter
5
+ attr_reader :stocks, :filters
6
+ def initialize(input=nil)
7
+ @input = input
8
+ @filters = []
9
+ @stocks = Hash.new{ |h,k| h[k] = [] }
10
+ end
11
+
12
+ # default opt: {global: true, at: -1, replace: false}
13
+ def filter(pattern, opt={}, &replace)
14
+ opt = opt_parse_for_filter(opt)
15
+ @filters[opt[:at], opt[:replace]] = [[pattern, replace, opt[:global]]]
16
+ nil
17
+ end
18
+
19
+ def replace(at, pattern, opt={}, &replace)
20
+ opt = {at: at, replace: true}.update(opt)
21
+ filter(pattern, opt, &replace)
22
+ end
23
+
24
+ def run(str=nil)
25
+ str ||= @input
26
+ @filters.compact.each do |pattern, replace, meth|
27
+ str = str.send(meth, pattern) { replace[$~, @stocks] }
28
+ end
29
+ str
30
+ end
31
+
32
+ def clear_stocks
33
+ @stocks.clear
34
+ end
35
+
36
+ private
37
+ def opt_parse_for_filter(opt)
38
+ opt = {global: true, at: -1, replace: false}.update(opt)
39
+ opt[:global] = opt[:global] ? :gsub : :sub
40
+ opt[:replace] = opt[:replace] ? 1 : 0
41
+ if opt[:replace].zero? && opt[:at] < 0
42
+ opt[:at] = @filters.size + opt[:at] + 1
43
+ end
44
+ opt
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ class GsubFilter
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,110 @@
1
+ # encoding: UTF-8
2
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
+
4
+ describe GsubFilter do
5
+ before do
6
+ str = <<EOS
7
+ *p1*こんにちは世界
8
+ hello, world
9
+ *123*I love Ruby
10
+ ruby is a lang.
11
+ EOS
12
+ @gs = GsubFilter.new(str)
13
+ end
14
+
15
+ context "run" do
16
+ it "should convert a string with one filter" do
17
+ res = <<EOS
18
+ ##こんにちは世界
19
+ hello, world
20
+ ##I love Ruby
21
+ ruby is a lang.
22
+ EOS
23
+ @gs.filter(/^\*p?\d+\*(.*)$/) { |md| "###{md[1]}" }
24
+ @gs.run.should == res
25
+ end
26
+
27
+ it "should convert a string given to run method" do
28
+ str =<<EOS
29
+ my friend is Tom.
30
+ He is very nice guy.
31
+ EOS
32
+ res =<<EOS
33
+ My Friend Is Tom.
34
+ He Is Very Nice Guy.
35
+ EOS
36
+ @gs.filter(/\w+/) { |md| md.to_s.capitalize }
37
+ @gs.run(str).should == res
38
+ end
39
+
40
+ it "should convert a string with two filters" do
41
+ res =<<EOS
42
+ ##こんにちは世界
43
+ HELLO, WORLD
44
+ ##I LOVE RUBY
45
+ RUBY IS A LANG.
46
+ EOS
47
+ @gs.filter(/^\*p?\d+\*(.*)$/) { |md| "###{md[1]}" }
48
+ @gs.filter(/\w+/) { |md| md.to_s.upcase }
49
+ @gs.run.should == res
50
+ end
51
+
52
+ it "should add a line with captured words" do
53
+ res =<<EOS
54
+ ---こんにちは世界---
55
+ HELLO, WORLD
56
+ ---I LOVE RUBY---
57
+ RUBY IS A LANG.
58
+ こんにちは世界,I love Ruby
59
+ EOS
60
+ @gs.filter(/^\*p?\d+\*(.*)$/) do |md, stocks|
61
+ stocks[:title] << md[1]
62
+ "---#{md[1]}---"
63
+ end
64
+ @gs.filter(/\w+/) { |md| md.to_s.upcase }
65
+ line = @gs.run
66
+ last = @gs.stocks[:title].join(',') + "\n"
67
+ (line + last).should == res
68
+ end
69
+ end
70
+
71
+ context "filter 'at' option" do
72
+ it "should add a filter at the last" do
73
+ regexps = [/\w+/, /\d+/]
74
+ @gs.filter(regexps[0]) { |md| md.to_s.upcase }
75
+ @gs.filter(regexps[1]) { |md| md.to_s.upcase }
76
+ @gs.filters.map(&:first) == regexps
77
+ end
78
+
79
+ it "should add a filter at the top" do
80
+ regexps = [/\w+/, /\d+/, /[_]/]
81
+ @gs.filter(regexps[0]) { |md| md.to_s.upcase }
82
+ @gs.filter(regexps[1], at: 0) { |md| md.to_s.upcase }
83
+ @gs.filters.map(&:first) == regexps.reverse
84
+ end
85
+ end
86
+
87
+ context "filter 'global' option" do
88
+ it "should act as gsub without global option" do
89
+ gs = GsubFilter.new("hello, world")
90
+ gs.filter(/\w+/) { |md| md.to_s.upcase }
91
+ gs.run.should eql "HELLO, WORLD"
92
+ end
93
+
94
+ it "should act as sub" do
95
+ gs = GsubFilter.new("hello, world")
96
+ gs.filter(/\w+/, global:false) { |md| md.to_s.upcase }
97
+ gs.run.should eql "HELLO, world"
98
+ end
99
+ end
100
+
101
+ context "replace method" do
102
+ it "should replace a filter" do
103
+ gs = GsubFilter.new("hello, world")
104
+ gs.filter(/\w+/) { |md| md.to_s.upcase }
105
+ gs.filter(/\d+/) { |md| md.to_s.upcase }
106
+ gs.replace(0, /[,]/) { |md| ":" }
107
+ gs.filters.map(&:first).should eql [/[,]/, /\d+/]
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,2 @@
1
+ require "rspec"
2
+ require "gsub_filter"
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gsub_filter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - kyoendo
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-23 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Build gsub chain filter
15
+ email:
16
+ - postagie@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - gsub_filter.gemspec
27
+ - lib/gsub_filter.rb
28
+ - lib/gsub_filter/version.rb
29
+ - spec/gsub_filter_spec.rb
30
+ - spec/spec_helper.rb
31
+ homepage: https://github.com/melborne/gsub-filter
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: 1.9.2
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.21
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: This is a text filter for gsub chain. You can set or replace as many gsub
55
+ filters as you like, then run them through a given text.
56
+ test_files:
57
+ - spec/gsub_filter_spec.rb
58
+ - spec/spec_helper.rb