wmoxam (owner)

Revisions

gist: 186862 Download_button fork
public
Public Clone URL: git://gist.github.com/186862.git
Embed All Files: show embed
Ruby #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def auto_link_html(html)
    substitutions = {}
    stripped_html = html.to_s.dup
    i = 0
    stripped_html.split(/<\/a>/).each do |line|
      next unless line.match(/(<a .*)/i)
      link = "#{$~[1]}</a>"
      tag_name = "::LINK_#{i}::"
      substitutions[tag_name] = link
      stripped_html.gsub!(Regexp.new(Regexp.escape(link), true), tag_name)
      i += 1
    end
 
    i = 0
    while stripped_html.match(/(<img [^>]*>)/i)
      img = $~[1]
      tag_name = "::IMG_#{i}::"
      substitutions[tag_name] = img
      stripped_html.gsub!(Regexp.new(Regexp.escape(img), true), tag_name)
      i += 1
    end
 
    uris = URI.extract(stripped_html, %w{ http https ftp dav gopher svn }) || []
    uris.uniq.each do |uri|
      stripped_html.gsub!(Regexp.new(/\b#{Regexp.escape(uri)}\b/, true), "<a href='#{uri}'>#{uri}</a>")
    end
    substitutions.each_pair do |tag_name, link|
      stripped_html.gsub!(tag_name, link)
    end
 
    url_re = /\s(www\.\w*\.(com|org|edu)(\/|\w)*)/i
    if stripped_html.match(url_re)
      # try to link non-protocol urls. May expand the scope of this in the future ..
      stripped_html.gsub!(url_re, " <a href='http://#{$~[1]}'>#{$~[1]}</a>")
    end
 
    stripped_html
  end