2012-07-07 00:52:32 +02:00
|
|
|
class Markdowner
|
2012-09-17 20:24:29 +02:00
|
|
|
def self.to_html(text, allow_images = false)
|
2012-07-07 00:52:32 +02:00
|
|
|
if text.blank?
|
|
|
|
return ""
|
|
|
|
else
|
2012-09-17 20:24:29 +02:00
|
|
|
html = RDiscount.new(text.to_s, *[ :smart, :autolink, :safelink,
|
|
|
|
:filter_styles, :filter_html ] + (allow_images ? [] : [ :no_image ])).
|
|
|
|
to_html
|
2012-07-07 00:52:32 +02:00
|
|
|
|
|
|
|
# change <h1> headings to just emphasis tags
|
2012-07-10 01:59:38 +02:00
|
|
|
html.gsub!(/<(\/)?h(\d)>/) {|_| "<#{$1}strong>" }
|
|
|
|
|
|
|
|
# fix links that got the trailing punctuation appended to move it outside
|
|
|
|
# the link
|
|
|
|
html.gsub!(/<a ([^>]+)([\.\!\,])">([^>]+)([\.\!\,])<\/a>/) {|_|
|
|
|
|
if $2.to_s == $4.to_s
|
|
|
|
"<a #{$1}\">#{$3}</a>#{$2}"
|
|
|
|
else
|
|
|
|
_
|
|
|
|
end
|
|
|
|
}
|
2012-07-07 00:52:32 +02:00
|
|
|
|
|
|
|
# make links have rel=nofollow
|
|
|
|
html.gsub!(/<a href/, "<a rel=\"nofollow\" href")
|
|
|
|
|
2012-09-16 22:51:25 +02:00
|
|
|
# make @username link to that user's profile
|
2012-09-10 19:40:33 +02:00
|
|
|
html.gsub!(/\B\@([\w\-]+)/) do |u|
|
2012-09-16 22:51:25 +02:00
|
|
|
if User.find_by_username(u[1 .. -1])
|
|
|
|
"<a href=\"/u/#{u[1 .. -1]}\">#{u}</a>"
|
2012-09-10 19:40:33 +02:00
|
|
|
else
|
|
|
|
u
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-07 00:52:32 +02:00
|
|
|
html
|
|
|
|
end
|
|
|
|
end
|
2012-09-16 22:51:25 +02:00
|
|
|
end
|