# Copyright (c) 2010, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
module MarkdownifyHelper
def markdownify(message, options={})
message = h(message).html_safe
options[:newlines] = true if !options.has_key?(:newlines)
options[:emoticons] = true if !options.has_key?(:emoticons)
message = process_links(message)
message = process_autolinks(message)
message = process_emphasis(message)
message = process_youtube(message, options[:youtube_maps])
message = process_vimeo(message, options[:vimeo_maps])
message = process_emoticons(message) if options[:emoticons]
message.gsub!(/\n+/, '
') if options[:newlines]
message
end
def process_links(message)
message.gsub!(/\[\s*([^\[]+?)\s*\]\(\s*([^ ]+\s*) \"(([^&]|(&[^q])|(&q[^u])|(&qu[^o])|(&quo[^t])|("[^;]))+)\"\s*\)/) do |m|
escape = "\\"
link = $1
url = $2
title = $3
url.gsub!("_", "\\_")
url.gsub!("*", "\\*")
protocol = (url =~ /^\w+:\/\//) ? '' :'http://'
res = "#{link}"
res
end
message.gsub!(/\[\s*([^\[]+?)\s*\]\(\s*([^ ]+)\s*\)/) do |m|
escape = "\\"
link = $1
url = $2
url.gsub!("_", "\\_")
url.gsub!("*", "\\*")
protocol = (url =~ /^\w+:\/\//) ? '' :'http://'
res = "#{link}"
res
end
message
end
def process_youtube(message, youtube_maps)
processed_message = message.gsub(YoutubeTitles::YOUTUBE_ID_REGEX) do |matched_string|
match_data = matched_string.match(YoutubeTitles::YOUTUBE_ID_REGEX)
video_id = match_data[1]
anchor = match_data[2]
anchor ||= ''
if youtube_maps && youtube_maps[video_id]
title = h(CGI::unescape(youtube_maps[video_id]))
else
title = I18n.t 'application.helper.video_title.unknown'
end
' Youtube: ' + title + ''
end
processed_message
end
def process_autolinks(message)
message.gsub!(/( |^)(www\.[^\s]+\.[^\s])/, '\1http://\2')
message.gsub!(/(#{captures[2]}}
res.gsub!(/(\*|_)/) { |m| "\\#{$1}" }
res
end
end
message
end
def process_emphasis(message)
message.gsub!("\\**", "-^doublestar^-")
message.gsub!("\\__", "-^doublescore^-")
message.gsub!("\\*", "-^star^-")
message.gsub!("\\_", "-^score^-")
message.gsub!(/(\*\*\*|___)(.+?)\1/m, '\2')
message.gsub!(/(\*\*|__)(.+?)\1/m, '\2')
message.gsub!(/(\*|_)(.+?)\1/m, '\2')
message.gsub!("-^doublestar^-", "**")
message.gsub!("-^doublescore^-", "__")
message.gsub!("-^star^-", "*")
message.gsub!("-^score^-", "_")
message
end
def process_vimeo(message, vimeo_maps)
regex = /https?:\/\/(?:w{3}\.)?vimeo.com\/(\d{6,})\/?/
processed_message = message.gsub(regex) do |matched_string|
match_data = message.match(regex)
video_id = match_data[1]
if vimeo_maps && vimeo_maps[video_id]
title = h(CGI::unescape(vimeo_maps[video_id]))
else
title = I18n.t 'application.helper.video_title.unknown'
end
' Vimeo: ' + title + ''
end
processed_message
end
def process_emoticons(message)
map = [
["<3", "♥"],
[":(", "☹"],
[":-(", "☹"],
[":)", "☺"],
[":-)", "☺"],
["<->", "↔"],
["->", "→"],
["<-", "←"],
["...", "…"],
["(tm)", "™"],
["(r)", "®"],
["(c)", "©"]
]
map.each do |mapping|
message.gsub!(mapping[0], mapping[1])
end
message
end
end