linky 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
@@ -0,0 +1,20 @@
1
+ # Linky
2
+
3
+ A simple gem for safely linking keywords within HTML (i.e., it's more than a `gsub`). Built on top of Nokogiri.
4
+
5
+ ## Installation
6
+
7
+ $ gem install linky
8
+
9
+ ## Usage
10
+
11
+ Call the `Linky.link` method:
12
+
13
+ require 'linky'
14
+ html = "<p>link all occurances of the word "more."</p><div>some more text</div>"
15
+ Linky.link "more", "http://more.com", html, :class => "linky"
16
+ # 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>"
17
+
18
+ ## Docs
19
+
20
+ Docs are on "rdoc.info":http://rdoc.info
@@ -0,0 +1,53 @@
1
+ class Linky
2
+ class << self
3
+ # Link a keyword to a target within some HTML.
4
+ # * keyword: what you want linked
5
+ # * target: where you want it linked to
6
+ # * html: the html you want to look for the keyword in
7
+ # * options: any extra attributes (besides href) that you want on the link.
8
+ #
9
+ # Here's an example:
10
+ #
11
+ # require 'linky'
12
+ # html = "<p>link all occurances of the word "more."</p><div>some more text</div>"
13
+ # Linky.link "more", "http://more.com", html, :class => "linky"
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
+ def link(keyword, target, html, options={})
16
+ options = options.map {|k,v| %{#{k}="#{v}"}}.join " "
17
+ block = proc {|keyword| %{<a href="#{target}" #{options}>#{keyword}</a>}}
18
+ html_fragment = Nokogiri::HTML::DocumentFragment.parse html
19
+ real_link keyword.to_s, html_fragment, &block
20
+ html_fragment.to_html
21
+ end
22
+
23
+ private
24
+ def real_link(needle, haystack, &block)
25
+ # if we've drilled down all the way to a raw text node,
26
+ # and it contains the needle we're looking for
27
+ if haystack.text? && (match = haystack.content.match(/(#{needle})/i))
28
+
29
+ # call the block to determine what we'll replace the needle with
30
+ new_needle = block.call match[0]
31
+
32
+ # break the string into three parts
33
+ prefix, keyword, postfix = break_on_boundary match[0], haystack.to_s
34
+
35
+ haystack.content = prefix
36
+ haystack.add_next_sibling Nokogiri::HTML::DocumentFragment.parse(new_needle)
37
+ if !postfix.nil? and !postfix.empty?
38
+ haystack.next_sibling.add_next_sibling Nokogiri::HTML::DocumentFragment.parse(postfix)
39
+ haystack.next_sibling.next_sibling.content = postfix
40
+ end
41
+ elsif haystack.name != "a" && haystack.respond_to?(:children)
42
+ haystack.children.each do |child|
43
+ real_link needle, child, &block
44
+ end
45
+ end
46
+ end
47
+
48
+ def break_on_boundary(boundary, string)
49
+ match = string.match /^(.*)(#{boundary})(.*)$/
50
+ return match[1], match[2], match[3]
51
+ end
52
+ end
53
+ end
data/lib/linky.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'nokogiri'
2
+ require 'linky/linky'
@@ -0,0 +1,21 @@
1
+ $LOAD_PATH.unshift './lib'
2
+ require 'linky'
3
+
4
+ describe Linky, "#link" do
5
+ before do
6
+ @keyword = :keyword
7
+ @html = "<p>some html with keyword and Keyword and <a href='#'>keyword</a></p>"
8
+ end
9
+
10
+ it "should require a keyword, a target, some html, and potentially options" do
11
+ proc {Linky.link}.should raise_error
12
+ proc {Linky.link :keyword}.should raise_error
13
+ proc {Linky.link :keyword, "http://link.to_somewhere"}.should raise_error
14
+ proc {Linky.link :keyword, "http://link.to.somewhere", @html, :class => "autolinked"}.should_not raise_error
15
+ end
16
+
17
+ it "should link any keywords not already linked" do
18
+ new_html = Linky.link(@keyword, "http://link.to.somewhere", @html, :class => "autolinked")
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
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: linky
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Matt Parker
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-10 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: nokogiri
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 113
30
+ segments:
31
+ - 1
32
+ - 4
33
+ - 3
34
+ - 1
35
+ version: 1.4.3.1
36
+ type: :runtime
37
+ version_requirements: *id001
38
+ description: A library for safely linking keywords to urls within HTML content.
39
+ email: moonmaster9000@gmail.com
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files:
45
+ - README.markdown
46
+ files:
47
+ - README.markdown
48
+ - lib/linky.rb
49
+ - lib/linky/linky.rb
50
+ - spec/linky_spec.rb
51
+ has_rdoc: true
52
+ homepage: http://github.com/moonmaster9000/linky
53
+ licenses: []
54
+
55
+ post_install_message:
56
+ rdoc_options:
57
+ - --charset=UTF-8
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ hash: 3
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project:
81
+ rubygems_version: 1.3.7
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: A library for safely linking keywords to urls within HTML content.
85
+ test_files:
86
+ - spec/linky_spec.rb