Skip to content

Commit

Permalink
Alright, links!
Browse files Browse the repository at this point in the history
  • Loading branch information
Amos Wenger committed Mar 21, 2012
1 parent b8228e0 commit d33bdc5
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,35 @@ class ApplicationController < ActionController::Base
before_filter :require_login

private
URL_REGEXP = /[^"^'^>]((http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/[^ ^<]*)?)/
LINK_REGEXP = /<a href=['"](.*)['"]>(.*)<\/a>/

def twitter(nick)
"<a href='https://twitter.com/#{nick}'><span class='symbol'>t</span>&nbsp;#{nick}</a>"
end

def pretty_link(link, text)
uri = URI(link).normalize
actual_text = if (link == text)
"#{uri.host}#{uri.path}"
else
text
end
"<a class='url' href='#{link}'><img src='http://getfavicon.appspot.com/#{link}'> #{actual_text}</a>"
end

def markup(markdown)
html = RDiscount.new(markdown).to_html
html = RDiscount.new(' ' + markdown).to_html
with_twitters = html.gsub(/@(\w*)/, twitter('\1'))
with_twitters
with_implicit_links = with_twitters.gsub(URL_REGEXP, ' <a href="\1">\1</a>')
with_pretty_links = with_implicit_links.gsub(LINK_REGEXP) do
pretty_link($1, $2)
end
end

helper_method :markup
helper_method :twitter
helper_method :pretty_url

def require_login
redirect_to '/login' if not current_user
Expand Down

0 comments on commit d33bdc5

Please sign in to comment.