Class: Filter::URI

Inherits:
Filter show all
Defined in:
lib/filters/uri.rb

Constant Summary

Constants inherited from Filter

VERSION

Instance Attribute Summary collapse

Attributes inherited from Filter

#string

Instance Method Summary collapse

Methods inherited from Filter

Profanity, inherited

Constructor Details

#initialize(string, options = {}) ⇒ URI

Initialize with string and options.



13
14
15
16
# File 'lib/filters/uri.rb', line 13

def initialize string, options = {}
  super
  @new_window = options.fetch :new_window, false
end

Instance Attribute Details

#new_windowObject (readonly)

Weither or not to open in a new window.



8
9
10
# File 'lib/filters/uri.rb', line 8

def new_window
  @new_window
end

Instance Method Details

#filter!Object

Convert URIs to anchor tags.



21
22
23
24
25
26
27
28
29
30
# File 'lib/filters/uri.rb', line 21

def filter!
  string.gsub! /\w+:\/\/(.*?)(\s+|$)/ do |uri|
    if new_window
      %(<a href="#{uri.strip}" target="_blank">#{uri}</a>)
    else
      %(<a href="#{uri.strip}">#{uri}</a>)
    end
  end
  string
end