linky 0.0.8 → 0.0.8.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.
- data/lib/linky/linky.rb +8 -3
- data/spec/linky_spec.rb +7 -0
- metadata +4 -3
data/lib/linky/linky.rb
CHANGED
@@ -3,7 +3,7 @@ class Linky
|
|
3
3
|
# Link a keyword to a target within some HTML.
|
4
4
|
# * keyword: what you want linked
|
5
5
|
# * target: where you want it linked to
|
6
|
-
# * html: the html you want to look for the keyword in
|
6
|
+
# * html: the html you want to look for the keyword in, or Nokogiri::HTML::DocumentFragment object.
|
7
7
|
# * options: any extra attributes (besides href) that you want on the link.
|
8
8
|
#
|
9
9
|
# Here's an example:
|
@@ -12,12 +12,17 @@ class Linky
|
|
12
12
|
# html = "<p>link all occurances of the word "more."</p><div>some more text</div>"
|
13
13
|
# Linky.link "more", "http://more.com", html, :class => "linky"
|
14
14
|
# # returns "<p>link the word "<a href="http://more.com" class="linky">more</a>."</p><div>some <a href="http://more.com" class="linky">more</a> text</div>"
|
15
|
+
#
|
16
|
+
# if you provide parameter html as DocumentFragment object, after calling link(), that object is updated as well.
|
17
|
+
# Means the output of link(keyword, target, html) == html.to_xhtml
|
15
18
|
def link(keyword, target, html, options={})
|
16
19
|
options = options.map {|k,v| %{#{k}="#{v}"}}.join " "
|
17
20
|
block = proc {|keyword| %{<a href="#{target}" #{options}>#{keyword}</a>}}
|
18
|
-
html_fragment = Nokogiri::HTML::DocumentFragment.parse
|
21
|
+
html_fragment = html.kind_of?(Nokogiri::HTML::DocumentFragment) ? html : Nokogiri::HTML::DocumentFragment.parse(html)
|
22
|
+
html_fragment_orig = html_fragment.dup
|
23
|
+
|
19
24
|
real_link keyword.to_s, html_fragment, &block
|
20
|
-
raise "We lost some content in attempting to link it. Please report a bug" unless html_fragment.text ==
|
25
|
+
raise "We lost some content in attempting to link it. Please report a bug" unless html_fragment.text == html_fragment_orig.text
|
21
26
|
html_fragment.to_xhtml
|
22
27
|
end
|
23
28
|
|
data/spec/linky_spec.rb
CHANGED
@@ -48,4 +48,11 @@ describe Linky, "#link" do
|
|
48
48
|
new_html = Linky.link "apple", "topics/apple", html
|
49
49
|
new_html.should == %(<a href="topics/apple">apple</a> & orange)
|
50
50
|
end
|
51
|
+
|
52
|
+
it "should process pre parsed html fragment object" do
|
53
|
+
html = "apple & orange"
|
54
|
+
frag = Nokogiri::HTML::DocumentFragment.parse(html)
|
55
|
+
new_html = Linky.link "apple", "topics/apple", frag
|
56
|
+
new_html.should == %(<a href="topics/apple">apple</a> & orange)
|
57
|
+
end
|
51
58
|
end
|
metadata
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: linky
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 109
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
9
|
- 8
|
10
|
-
|
10
|
+
- 1
|
11
|
+
version: 0.0.8.1
|
11
12
|
platform: ruby
|
12
13
|
authors:
|
13
14
|
- Matt Parker
|
@@ -15,7 +16,7 @@ autorequire:
|
|
15
16
|
bindir: bin
|
16
17
|
cert_chain: []
|
17
18
|
|
18
|
-
date: 2011-02-
|
19
|
+
date: 2011-02-27 00:00:00 -05:00
|
19
20
|
default_executable:
|
20
21
|
dependencies:
|
21
22
|
- !ruby/object:Gem::Dependency
|