linky 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/linky/linky.rb +8 -5
  2. data/spec/linky_spec.rb +6 -0
  3. metadata +6 -6
data/lib/linky/linky.rb CHANGED
@@ -24,13 +24,13 @@ class Linky
24
24
  def real_link(needle, haystack, &block)
25
25
  # if we've drilled down all the way to a raw text node,
26
26
  # and it contains the needle we're looking for
27
- if haystack.text? && (match = haystack.content.match(/(#{needle})/i))
27
+ if haystack.text? && (match = haystack.content.match(/^.*?\b(#{needle})(?:\b.*)?$/i))
28
28
 
29
29
  # call the block to determine what we'll replace the needle with
30
- new_needle = block.call match[0]
30
+ new_needle = block.call match[1]
31
31
 
32
32
  # break the string into three parts
33
- prefix, keyword, postfix = break_on_boundary match[0], haystack.to_s
33
+ prefix, keyword, postfix = break_on_boundary match[1], haystack.to_s
34
34
 
35
35
  haystack.content = prefix
36
36
  haystack.add_next_sibling Nokogiri::HTML::DocumentFragment.parse(new_needle)
@@ -38,15 +38,18 @@ class Linky
38
38
  haystack.next_sibling.add_next_sibling Nokogiri::HTML::DocumentFragment.parse(postfix)
39
39
  haystack.next_sibling.next_sibling.content = postfix
40
40
  end
41
+ true
41
42
  elsif haystack.name != "a" && haystack.respond_to?(:children)
43
+ found = false
42
44
  haystack.children.each do |child|
43
- real_link needle, child, &block
45
+ found = real_link needle, child, &block unless found
44
46
  end
47
+ found
45
48
  end
46
49
  end
47
50
 
48
51
  def break_on_boundary(boundary, string)
49
- match = string.match /^(.*)(#{boundary})(.*)$/
52
+ match = string.match /^(.*?\b)(#{boundary})(\b.*)?$/
50
53
  return match[1], match[2], match[3]
51
54
  end
52
55
  end
data/spec/linky_spec.rb CHANGED
@@ -18,4 +18,10 @@ describe Linky, "#link" do
18
18
  new_html = Linky.link(@keyword, "http://link.to.somewhere", @html, :class => "autolinked")
19
19
  new_html.should == "<p>some html with <a href=\"http://link.to.somewhere\" class=\"autolinked\">keyword</a> and Keyword and <a href=\"#\">keyword</a></p>"
20
20
  end
21
+
22
+ it "should match on whole words (i.e., don't link partial words within words)" do
23
+ html = "hi there, he is funny and he is a he.<div>more he stuff.</div>"
24
+ new_html = Linky.link "he", "#he", html
25
+ new_html.should == "hi there, <a href=\"#he\">he</a> is funny and he is a he.<div>more he stuff.</div>"
26
+ end
21
27
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linky
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matt Parker
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-10 00:00:00 -05:00
18
+ date: 2010-12-05 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -53,8 +53,8 @@ homepage: http://github.com/moonmaster9000/linky
53
53
  licenses: []
54
54
 
55
55
  post_install_message:
56
- rdoc_options:
57
- - --charset=UTF-8
56
+ rdoc_options: []
57
+
58
58
  require_paths:
59
59
  - lib
60
60
  required_ruby_version: !ruby/object:Gem::Requirement