linkhum 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NDZmOTk1OTg3ZjU2MzYxMTc2ZTA1ZDM1YjM4NmI2NWJlYzdkMWUyYQ==
4
+ MjZjODJiMGM1N2Q2ZjdlOTkyN2IyMThkNGQ4MDI3NjgyMzJhYzExZA==
5
5
  data.tar.gz: !binary |-
6
- MmFjMWJlOTk3YjM5MGEyOTkzNWE2Y2E5NzYwZTFmM2U1Yjc5MjQwNw==
6
+ M2MwNTM2MGVmZjk5ZmZiMzMyOGU4YWFkN2Q2NDUyY2M0ODJlZWUzOA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZGE1YTI2MzFmY2E3OGY4OTJkNTg2Mjk5YzI3ZTFjZDAzNmFkNmI0MTMyMzVh
10
- MTJjMjgzZGU0MjYxMjZiNWRkYTZmYjYzOGVhY2U1ZmU0NWU4MzI3ZjQ5YzIx
11
- MjI1MTU4ZTM4NTAyMjc1N2Y3MzU2NGE2NGQzZGFhNjdlOTZkYjI=
9
+ OTY5ZGIzNzdkZjgzYWJkODcyYjJlZjYzMzM4NGRiNGViYjNhMjUxNTdmMTM0
10
+ NDNhMTY2NzE4NzNlMDY4ZWQ2NTA4OTE4ZmU0YzIyZGY1ZWY4Y2Y0NjZkNWFj
11
+ YTdkZGY1N2U3NmMzMTA0NjU2ZTlkYWRlYTgyZTcwNWI0NGU2NzA=
12
12
  data.tar.gz: !binary |-
13
- NzcwZjRmNDcwYTc4OTVjNTY4OTY2NTQ5OTg3NWIwZjZhOGU5MWFkOTExNGVm
14
- MThkNDAzYmFlMjRkYTkwNWNiMWE0MTk3NWY1NzBmM2U4NGQ5NjY0ODA2MmFi
15
- NGRjMGFkNzM0ZjY2NjU3NzRhZDNkMjEwNzYyMWI5YmZkYWJiYjY=
13
+ YWMwMDdhMzZjNzcxMDMyMTlhN2E0MmRkMGY1NDBmMzA0N2UwYWRjMjZjNGU5
14
+ MjljNGNmZTkxOTIxMDJlMjlkN2ZjZmY1MDhkODI0MjhmZTIyZjRmMWE0MzRm
15
+ N2NkMTM1OWQxYzQ1YTY1ZGFiYTlhODY3OWNhOTIwNzdhNGI5NDQ=
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## 0.0.2 (2015-07-16)
2
+
3
+ * Now supporting several `special` blocks in same class.
data/README.md CHANGED
@@ -123,19 +123,19 @@ MyLinks.urlify("Hey, @jude!")
123
123
  # => "Hey, <a href='http://oursite/users/jude'>@jude</a>!"
124
124
 
125
125
  # nil or false means no replacements:
126
- class MyLinks < LinkHum
126
+ class MyLinksConditional < LinkHum
127
127
  special /@(\S+)\b/ do |username|
128
128
  "http://oursite/users/#{username}" if User.where(name: username).exists?
129
129
  end
130
130
  end
131
131
 
132
- MyLinks.urlify("So, our @dude and @unknownguy walk into a bar...")
132
+ MyLinksConditional.urlify("So, our @dude and @unknownguy walk into a bar...")
133
133
  # => "So, our <a href='http://oursite/users/dude'>@dude</a> and @unknownguy walk into a bar..."
134
134
  ```
135
135
 
136
136
  Some `special` gotchas:
137
- * for now, only one `special` per class is supported (an attempt to define
138
- additional one will show warningn);
137
+ * in version 0.0.2, you can define any number of `special`s, but it's
138
+ totally up to you to have non-conflicting, clearly distinguished patterns;
139
139
  * it passes to the block values by the same logic as `String#scan` does:
140
140
 
141
141
  ```ruby
data/lib/linkhum.rb CHANGED
@@ -10,13 +10,12 @@ class LinkHum
10
10
  new(text).urlify(options.merge(link_processor: block))
11
11
  end
12
12
 
13
- def special(pattern = nil, &block)
14
- return @special unless pattern
15
-
16
- @special and
17
- puts("Warning: redefining #{self}.special from #{caller.first}")
13
+ def specials
14
+ @specials ||= []
15
+ end
18
16
 
19
- @special = [pattern, block]
17
+ def special(pattern = nil, &block)
18
+ specials << [pattern, block]
20
19
  end
21
20
  end
22
21
 
@@ -52,18 +51,26 @@ class LinkHum
52
51
  def process_text(str)
53
52
  str = CGI.escapeHTML(str)
54
53
 
55
- pattern, block = self.class.special
56
-
57
- if pattern
58
- str.gsub(pattern){|s|
59
- if (u = block.call(*arguments(pattern, s)))
60
- "<a href='#{screen_feet(u)}'>#{s}</a>"
61
- else
62
- s
63
- end
64
- }
65
- else
54
+ if self.class.specials.empty?
66
55
  str
56
+ else
57
+ replace_specials(str)
58
+ end
59
+ end
60
+
61
+ def replace_specials(str)
62
+ patterns = self.class.specials.map(&:first)
63
+ blocks = self.class.specials.map(&:last)
64
+
65
+ str.gsub(Regexp.union(patterns)) do |s|
66
+ pattern = patterns.detect{|p| s[p] == s}
67
+ idx = patterns.index(pattern)
68
+
69
+ if idx && (u = blocks[idx].call(*arguments(pattern, s)))
70
+ "<a href='#{screen_feet(u)}'>#{s}</a>"
71
+ else
72
+ s
73
+ end
67
74
  end
68
75
  end
69
76
 
data/linkhum.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'linkhum'
3
- s.version = '0.0.1'
3
+ s.version = '0.0.2'
4
4
  s.authors = ['Alexey Makhotkin', 'Victor Shepelev']
5
5
  s.email = 'zverok.offline@gmail.com'
6
6
  s.homepage = 'https://github.com/zverok/linkhum'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linkhum
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexey Makhotkin
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-06-30 00:00:00.000000000 Z
12
+ date: 2015-07-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: addressable
@@ -102,6 +102,7 @@ extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
104
  - .dokaz
105
+ - CHANGELOG.md
105
106
  - LICENSE.txt
106
107
  - README.md
107
108
  - lib/linkhum.rb