Refactored the markdownify method.

This commit is contained in:
Dan Hansen & Sarah Mei 2010-12-15 01:00:20 -05:00
parent 1d1faea9e7
commit c4a7745c88
2 changed files with 115 additions and 125 deletions

View file

@ -24,11 +24,13 @@ module ApplicationHelper
post.aspect_ids.include?(a.id) post.aspect_ids.include?(a.id)
end end
end end
def aspects_without_post aspects, post def aspects_without_post aspects, post
aspects.reject do |a| aspects.reject do |a|
post.aspect_ids.include?(a.id) post.aspect_ids.include?(a.id)
end end
end end
def aspect_badge aspects def aspect_badge aspects
str = '' str = ''
if aspects.count > 1 if aspects.count > 1
@ -39,6 +41,7 @@ module ApplicationHelper
end end
str.html_safe str.html_safe
end end
def aspect_links aspects, opts={} def aspect_links aspects, opts={}
str = "" str = ""
aspects.each do |a| aspects.each do |a|
@ -46,6 +49,7 @@ module ApplicationHelper
end end
str.html_safe str.html_safe
end end
def aspect_li aspect, opts= {} def aspect_li aspect, opts= {}
param_string = "" param_string = ""
if opts.size > 0 if opts.size > 0
@ -165,47 +169,62 @@ module ApplicationHelper
def markdownify(message, options = {}) def markdownify(message, options = {})
message = h(message).html_safe message = h(message).html_safe
[:autolinks, :youtube, :emphasis, :links, :newlines].each do |k| if !options.has_key?(:newlines)
if !options.has_key?(k) options[:newlines] = true
options[k] = true
end
end end
if options[:links] message = process_links(message)
message = process_youtube(message)
message = process_autolinks(message)
message = process_emphasis(message)
message = process_youtube_again(message, options[:youtube_maps])
if options[:newlines]
message.gsub!(/\n+/, '<br />')
end
return message
end
def process_links(message)
message.gsub!(/\[([^\[]+)\]\(([^ ]+) \&quot;(([^&]|(&[^q])|(&q[^u])|(&qu[^o])|(&quo[^t])|(&quot[^;]))+)\&quot;\)/) do |m| message.gsub!(/\[([^\[]+)\]\(([^ ]+) \&quot;(([^&]|(&[^q])|(&q[^u])|(&qu[^o])|(&quo[^t])|(&quot[^;]))+)\&quot;\)/) do |m|
escape = (options[:emphasis]) ? "\\" : "" escape = "\\"
res = "<a target=\"#{escape}_blank\" href=\"#{$2}\" title=\"#{$3}\">#{$1}</a>" res = "<a target=\"#{escape}_blank\" href=\"#{$2}\" title=\"#{$3}\">#{$1}</a>"
res res
end end
message.gsub!(/\[([^\[]+)\]\(([^ ]+)\)/) do |m| message.gsub!(/\[([^\[]+)\]\(([^ ]+)\)/) do |m|
escape = (options[:emphasis]) ? "\\" : "" escape = "\\"
res = "<a target=\"#{escape}_blank\" href=\"#{$2}\">#{$1}</a>" res = "<a target=\"#{escape}_blank\" href=\"#{$2}\">#{$1}</a>"
res res
end end
return message
end end
if options[:youtube] def process_youtube(message)
message.gsub!(/( |^)(http:\/\/)?www\.youtube\.com\/watch[^ ]*v=([A-Za-z0-9_]+)(&[^ ]*|)/) do |m| message.gsub!(/( |^)(http:\/\/)?www\.youtube\.com\/watch[^ ]*v=([A-Za-z0-9_]+)(&[^ ]*|)/) do |m|
res = "#{$1}youtube.com::#{$3}" res = "#{$1}youtube.com::#{$3}"
res.gsub!(/(\*|_)/) { |m| "\\#{$1}" } if options[:emphasis] res.gsub!(/(\*|_)/) { |m| "\\#{$1}" }
res res
end end
return message
end end
if options[:autolinks] def process_autolinks(message)
message.gsub!(/( |^)(www\.[^\s]+\.[^\s])/, '\1http://\2') message.gsub!(/( |^)(www\.[^\s]+\.[^\s])/, '\1http://\2')
message.gsub!(/(<a target="\\?_blank" href=")?(https|http|ftp):\/\/([^\s]+)/) do |m| message.gsub!(/(<a target="\\?_blank" href=")?(https|http|ftp):\/\/([^\s]+)/) do |m|
if !$1.nil? if !$1.nil?
m m
else else
res = %{<a target="_blank" href="#{$2}://#{$3}">#{$3}</a>} res = %{<a target="_blank" href="#{$2}://#{$3}">#{$3}</a>}
res.gsub!(/(\*|_)/) { |m| "\\#{$1}" } if options[:emphasis] res.gsub!(/(\*|_)/) { |m| "\\#{$1}" }
res res
end end
end end
return message
end end
if options[:emphasis] def process_emphasis(message)
message.gsub!("\\**", "-^doublestar^-") message.gsub!("\\**", "-^doublestar^-")
message.gsub!("\\__", "-^doublescore^-") message.gsub!("\\__", "-^doublescore^-")
message.gsub!("\\*", "-^star^-") message.gsub!("\\*", "-^star^-")
@ -217,24 +236,19 @@ module ApplicationHelper
message.gsub!("-^doublescore^-", "__") message.gsub!("-^doublescore^-", "__")
message.gsub!("-^star^-", "*") message.gsub!("-^star^-", "*")
message.gsub!("-^score^-", "_") message.gsub!("-^score^-", "_")
return message
end end
if options[:youtube] def process_youtube_again(message, youtube_maps)
while youtube = message.match(/youtube\.com::([A-Za-z0-9_\\\-]+)/) while youtube = message.match(/youtube\.com::([A-Za-z0-9_\\\-]+)/)
video_id = youtube[1] video_id = youtube[1]
if options[:youtube_maps] && options[:youtube_maps][video_id] if youtube_maps && youtube_maps[video_id]
title = options[:youtube_maps][video_id] title = youtube_maps[video_id]
else else
title = I18n.t 'application.helper.youtube_title.unknown' title = I18n.t 'application.helper.youtube_title.unknown'
end end
message.gsub!('youtube.com::'+video_id, '<a class="video-link" data-host="youtube.com" data-video-id="' + video_id + '" href="#video">Youtube: ' + title + '</a>') message.gsub!('youtube.com::'+video_id, '<a class="video-link" data-host="youtube.com" data-video-id="' + video_id + '" href="#video">Youtube: ' + title + '</a>')
end end
end
if options[:newlines]
message.gsub!(/\n+/, '<br />')
end
return message return message
end end

View file

@ -72,24 +72,22 @@ describe ApplicationHelper do
markdownify(proto+"://"+url).should == "<a target=\"_blank\" href=\""+proto+"://"+url+"\">"+url+"</a>" markdownify(proto+"://"+url).should == "<a target=\"_blank\" href=\""+proto+"://"+url+"\">"+url+"</a>"
end end
describe "video links" do
it "recognizes youtube links" do it "recognizes youtube links" do
proto="http" video_id = "0x__dDWdf23"
videoid = "0x__dDWdf23" url = "http://www.youtube.com/watch?v=" + video_id + "&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
url="www.youtube.com/watch?v="+videoid+"&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1" res = markdownify(url)
title = "UP & down & UP & down &amp;"
res = markdownify(proto+'://'+url)
res.should =~ /data-host="youtube.com"/ res.should =~ /data-host="youtube.com"/
res.should =~ /data-video-id="#{videoid}"/ res.should =~ /data-video-id="#{video_id}"/
end end
it "recognizes youtube links with hyphens" do it "recognizes youtube links with hyphens" do
proto="http" video_id = "ABYnqp-bxvg"
videoid = "ABYnqp-bxvg" url = "http://www.youtube.com/watch?v=" + video_id + "&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
url="www.youtube.com/watch?v="+videoid+"&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1" res = markdownify(url)
title = "UP & down & UP & down &amp;"
res = markdownify(proto+'://'+url)
res.should =~ /data-host="youtube.com"/ res.should =~ /data-host="youtube.com"/
res.should =~ /data-video-id="#{videoid}"/ res.should =~ /data-video-id="#{video_id}"/
end
end end
it "recognizes multiple links of different types" do it "recognizes multiple links of different types" do
@ -174,35 +172,13 @@ describe ApplicationHelper do
markdownify(message).should == "<em>some text</em> *some text<em> **some text</em> <em>some text</em> _some text<em> __some text</em>" markdownify(message).should == "<em>some text</em> *some text<em> **some text</em> <em>some text</em> _some text<em> __some text</em>"
end end
describe "options" do
before do
@message = "http://url.com www.url.com www.youtube.com/watch?foo=bar&v=BARFOO&whatever=related *emphasis* __emphasis__ [link](www.url.com) [link](url.com \"title\")"
end
it "can render only autolinks" do
res = markdownify(@message, :youtube => false, :emphasis => false, :links => false)
res.should == "<a target=\"_blank\" href=\"http://url.com\">url.com</a> <a target=\"_blank\" href=\"http://www.url.com\">www.url.com</a> <a target=\"_blank\" href=\"http://www.youtube.com/watch?foo=bar&amp;v=BARFOO&amp;whatever=related\">www.youtube.com/watch?foo=bar&amp;v=BARFOO&amp;whatever=related</a> *emphasis* __emphasis__ [link](www.url.com) [link](url.com &quot;title&quot;)"
end
it "can render only youtube" do
res = markdownify(@message, :autolinks => false, :emphasis => false, :links => false)
res.should_not =~ /a href="http:\/\/url.com"/
res.should_not =~ /a href="http:\/\/www.url.com"/
res.should_not =~ /<strong>emphasis<\/strong>/
end
it "can render only emphasis tags" do
res = markdownify(@message, :autolinks => false, :youtube => false, :links => false)
res.should == "http://url.com www.url.com www.youtube.com/watch?foo=bar&amp;v=BARFOO&amp;whatever=related <em>emphasis</em> <strong>emphasis</strong> [link](www.url.com) [link](url.com &quot;title&quot;)"
end
it "can render only links tags" do
res = markdownify(@message, :autolinks => false, :youtube => false, :emphasis => false)
res.should == "http://url.com www.url.com www.youtube.com/watch?foo=bar&amp;v=BARFOO&amp;whatever=related *emphasis* __emphasis__ <a target=\"_blank\" href=\"www.url.com\">link</a> <a target=\"_blank\" href=\"url.com\" title=\"title\">link</a>"
end
end
describe "newlines" do describe "newlines" do
it 'skips inserting newlines if you pass the newlines option' do
message = "These\nare\n\some\nnew\lines"
res = markdownify(message, :newlines => false)
res.should == message
end
it 'generates breaklines' do it 'generates breaklines' do
message = "These\nare\nsome\nnew\nlines" message = "These\nare\nsome\nnew\nlines"
res = markdownify(message) res = markdownify(message)