diaspora/lib/diaspora/markdownify_email.rb
Steven Hancock 99a2865e23 Make hashtags clickable in emails
Refactor the Redcarpet renderer created in c2bc7272f to parse hashtags
into clickable links (for HTML emails only).
2012-05-29 10:01:58 -07:00

29 lines
No EOL
835 B
Ruby

require Rails.root.join("app", "models", "acts_as_taggable_on", "tag")
module Diaspora
module Markdownify
class Email < Redcarpet::Render::HTML
include Rails.application.routes.url_helpers
TAG_REGEX = /(?:^|\s)#([#{ActsAsTaggableOn::Tag.tag_text_regexp}]+)/u
def preprocess(text)
process_tags(text)
end
private
def tags(text)
text.scan(TAG_REGEX).map { |match| match[0] }
end
def process_tags(text)
return text unless text.match(TAG_REGEX)
tags(text).each do |tag|
text.gsub!(/##{tag}/) do |tag|
opts = {:name => ActsAsTaggableOn::Tag.normalize(tag)}.merge(Rails.application.config.action_mailer.default_url_options)
"[#{tag}](#{tag_url(opts)})"
end
end
text
end
end
end
end