Class: Linky
- Inherits:
-
Object
- Object
- Linky
- Defined in:
- lib/linky/linky.rb
Class Method Summary collapse
-
.link(keyword, target, html, options = {}) ⇒ Object
Link a keyword to a target within some HTML.
Class Method Details
.link(keyword, target, html, options = {}) ⇒ Object
Link a keyword to a target within some HTML.
* keyword: what you want linked
* target: where you want it linked to
* html: the html you want to look for the keyword in
* options: any extra attributes (besides href) that you want on the link.
Here’s an example:
require 'linky'
html = "<p>link all occurances of the word "more."</p><div>some more text</div>"
Linky.link "more", "http://more.com", html, :class => "linky"
# 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 17 18 19 20 21 22 |
# File 'lib/linky/linky.rb', line 15 def link(keyword, target, html, ={}) = .map {|k,v| %{#{k}="#{v}"}}.join " " block = proc {|keyword| %{<a href="#{target}" #{}>#{keyword}</a>}} html_fragment = Nokogiri::HTML::DocumentFragment.parse html real_link keyword.to_s, html_fragment, &block raise "We lost some content in attempting to link it. Please report a bug" unless html_fragment.text == Nokogiri::HTML::DocumentFragment.parse(html).text html_fragment.to_xhtml end |