Class: Bypass::HTMLFilter

Inherits:
Filter
  • Object
show all
Defined in:
lib/bypass/html_filter.rb

Constant Summary

Constants inherited from Filter

Filter::URL_PATTERN

Instance Attribute Summary

Attributes inherited from Filter

#fragment

Instance Method Summary collapse

Methods inherited from Filter

#initialize, #to_s

Constructor Details

This class inherits a constructor from Bypass::Filter

Instance Method Details



17
18
19
20
21
22
23
24
# File 'lib/bypass/html_filter.rb', line 17

def auto_link(options = {})
  each_text_node do |node|
    text = gsub_urls(node.text.to_s) do |url| 
      build_anchor_tag(url, url, options)
    end
    node.replace(parse_html_fragment(text))
  end
end

#build_anchor_tag(text, href, options = {}) ⇒ Object



26
27
28
29
30
# File 'lib/bypass/html_filter.rb', line 26

def build_anchor_tag(text, href, options = {})
  attributes = []
  options.each { |k, v| attributes << " #{k}=\"#{v}\"" }
  "<a href=\"#{href}\"#{attributes.join(' ')}>#{text}</a>"
end

#contentObject



32
33
34
# File 'lib/bypass/html_filter.rb', line 32

def content
  parsed_content.to_s
end

#replace(&block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/bypass/html_filter.rb', line 6

def replace(&block)
  parsed_content.traverse do |node|
    if node.name == "a" && href = node.get_attribute("href")
      if parsed_href = parse_uri(href)
        url = yield(parsed_href)
        node.set_attribute("href", url.to_s)
      end
    end
  end
end