Class: Linkup::Linker
- Inherits:
-
Object
- Object
- Linkup::Linker
- Defined in:
- lib/linkup.rb
Constant Summary collapse
- LINK_REGEX =
/\b(([\w-]+:\/\/?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s<]|\/)))/u
Instance Attribute Summary collapse
-
#original ⇒ Object
Returns the value of attribute original.
Instance Method Summary collapse
- #already_linked? ⇒ Boolean
-
#initialize(string) ⇒ Linker
constructor
A new instance of Linker.
- #linkify_text(text) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(string) ⇒ Linker
Returns a new instance of Linker.
20 21 22 |
# File 'lib/linkup.rb', line 20 def initialize(string) self.original = string end |
Instance Attribute Details
#original ⇒ Object
Returns the value of attribute original.
16 17 18 |
# File 'lib/linkup.rb', line 16 def original @original end |
Instance Method Details
#already_linked? ⇒ Boolean
24 25 26 |
# File 'lib/linkup.rb', line 24 def already_linked? original.match(/href=/) end |
#linkify_text(text) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/linkup.rb', line 28 def linkify_text(text) text.gsub(LINK_REGEX) do |match| prefix = if (match =~/\Awww\./) prefix = 'http://' end %Q[<a href="#{prefix}#{match}">#{match}</a>] end end |
#to_s ⇒ Object
37 38 39 |
# File 'lib/linkup.rb', line 37 def to_s already_linked? ? original : linkify_text(original) end |