Refactored the markdownify method.
This commit is contained in:
parent
1d1faea9e7
commit
c4a7745c88
2 changed files with 115 additions and 125 deletions
|
|
@ -24,21 +24,24 @@ 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
|
||||||
str = "<span class='aspect_badge all'>#{I18n.t('application.helper.aspect_badge.all_aspects')}</span>"
|
str = "<span class='aspect_badge all'>#{I18n.t('application.helper.aspect_badge.all_aspects')}</span>"
|
||||||
elsif aspects.count == 1
|
elsif aspects.count == 1
|
||||||
aspect = aspects.first
|
aspect = aspects.first
|
||||||
str = "<span class='aspect_badge single'><a href=#{aspect_path(aspect)}>#{aspect.name}</a></span>"
|
str = "<span class='aspect_badge single'><a href=#{aspect_path(aspect)}>#{aspect.name}</a></span>"
|
||||||
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,15 +49,16 @@ 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
|
||||||
param_string << '?'
|
param_string << '?'
|
||||||
opts.each_pair do |k,v|
|
opts.each_pair do |k, v|
|
||||||
param_string << "#{k}=#{v}"
|
param_string << "#{k}=#{v}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
"<li>
|
"<li>
|
||||||
<a href='/aspects/#{aspect.id}#{param_string}'>
|
<a href='/aspects/#{aspect.id}#{param_string}'>
|
||||||
#{aspect.name}
|
#{aspect.name}
|
||||||
</a>
|
</a>
|
||||||
|
|
@ -98,12 +102,12 @@ module ApplicationHelper
|
||||||
|
|
||||||
def person_url(person)
|
def person_url(person)
|
||||||
case person.class.to_s
|
case person.class.to_s
|
||||||
when "User"
|
when "User"
|
||||||
user_path(person)
|
user_path(person)
|
||||||
when "Person"
|
when "Person"
|
||||||
person_path(person)
|
person_path(person)
|
||||||
else
|
else
|
||||||
I18n.t('application.helper.unknown_person')
|
I18n.t('application.helper.unknown_person')
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -124,28 +128,28 @@ module ApplicationHelper
|
||||||
end
|
end
|
||||||
|
|
||||||
def person_link(person)
|
def person_link(person)
|
||||||
"<a href='/people/#{person.id}'>
|
"<a href='/people/#{person.id}'>
|
||||||
#{person.name}
|
#{person.name}
|
||||||
</a>".html_safe
|
</a>".html_safe
|
||||||
end
|
end
|
||||||
|
|
||||||
def image_or_default(person, size=:thumb_large)
|
def image_or_default(person, size=:thumb_large)
|
||||||
image_location = person.profile.image_url(size) if person.profile
|
image_location = person.profile.image_url(size) if person.profile
|
||||||
image_location ||= person.profile.image_url(:thumb_large) if person.profile #backwards compatability for old profile pictures
|
image_location ||= person.profile.image_url(:thumb_large) if person.profile #backwards compatability for old profile pictures
|
||||||
image_location ||= "/images/user/default.png"
|
image_location ||= "/images/user/default.png"
|
||||||
image_location
|
image_location
|
||||||
end
|
end
|
||||||
|
|
||||||
def hard_link(string, path)
|
def hard_link(string, path)
|
||||||
link_to string, path, :rel => 'external'
|
link_to string, path, :rel => 'external'
|
||||||
end
|
end
|
||||||
|
|
||||||
def person_image_link(person, opts = {})
|
def person_image_link(person, opts = {})
|
||||||
return "" if person.nil?
|
return "" if person.nil?
|
||||||
if opts[:to] == :photos
|
if opts[:to] == :photos
|
||||||
link_to person_image_tag(person,opts[:size]), person_photos_path(person)
|
link_to person_image_tag(person, opts[:size]), person_photos_path(person)
|
||||||
else
|
else
|
||||||
"<a href='/people/#{person.id}'>
|
"<a href='/people/#{person.id}'>
|
||||||
#{person_image_tag(person)}
|
#{person_image_tag(person)}
|
||||||
</a>".html_safe
|
</a>".html_safe
|
||||||
end
|
end
|
||||||
|
|
@ -158,78 +162,22 @@ module ApplicationHelper
|
||||||
def person_photos_path person
|
def person_photos_path person
|
||||||
person_id = person.id if person.respond_to?(:id)
|
person_id = person.id if person.respond_to?(:id)
|
||||||
person_id ||= person
|
person_id ||= person
|
||||||
|
|
||||||
"#{photos_path}?person_id=#{person_id}"
|
"#{photos_path}?person_id=#{person_id}"
|
||||||
end
|
end
|
||||||
|
|
||||||
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.gsub!(/\[([^\[]+)\]\(([^ ]+) \"(([^&]|(&[^q])|(&q[^u])|(&qu[^o])|(&quo[^t])|("[^;]))+)\"\)/) do |m|
|
message = process_youtube(message)
|
||||||
escape = (options[:emphasis]) ? "\\" : ""
|
message = process_autolinks(message)
|
||||||
res = "<a target=\"#{escape}_blank\" href=\"#{$2}\" title=\"#{$3}\">#{$1}</a>"
|
message = process_emphasis(message)
|
||||||
res
|
message = process_youtube_again(message, options[:youtube_maps])
|
||||||
end
|
|
||||||
message.gsub!(/\[([^\[]+)\]\(([^ ]+)\)/) do |m|
|
|
||||||
escape = (options[:emphasis]) ? "\\" : ""
|
|
||||||
res = "<a target=\"#{escape}_blank\" href=\"#{$2}\">#{$1}</a>"
|
|
||||||
res
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if options[:youtube]
|
|
||||||
message.gsub!(/( |^)(http:\/\/)?www\.youtube\.com\/watch[^ ]*v=([A-Za-z0-9_]+)(&[^ ]*|)/) do |m|
|
|
||||||
res = "#{$1}youtube.com::#{$3}"
|
|
||||||
res.gsub!(/(\*|_)/) { |m| "\\#{$1}" } if options[:emphasis]
|
|
||||||
res
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if options[:autolinks]
|
|
||||||
message.gsub!(/( |^)(www\.[^\s]+\.[^\s])/, '\1http://\2')
|
|
||||||
message.gsub!(/(<a target="\\?_blank" href=")?(https|http|ftp):\/\/([^\s]+)/) do |m|
|
|
||||||
if !$1.nil?
|
|
||||||
m
|
|
||||||
else
|
|
||||||
res = %{<a target="_blank" href="#{$2}://#{$3}">#{$3}</a>}
|
|
||||||
res.gsub!(/(\*|_)/) { |m| "\\#{$1}" } if options[:emphasis]
|
|
||||||
res
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if options[:emphasis]
|
|
||||||
message.gsub!("\\**", "-^doublestar^-")
|
|
||||||
message.gsub!("\\__", "-^doublescore^-")
|
|
||||||
message.gsub!("\\*", "-^star^-")
|
|
||||||
message.gsub!("\\_", "-^score^-")
|
|
||||||
message.gsub!(/(\*\*\*|___)(.+?)\1/m, '<em><strong>\2</strong></em>')
|
|
||||||
message.gsub!(/(\*\*|__)(.+?)\1/m, '<strong>\2</strong>')
|
|
||||||
message.gsub!(/(\*|_)(.+?)\1/m, '<em>\2</em>')
|
|
||||||
message.gsub!("-^doublestar^-", "**")
|
|
||||||
message.gsub!("-^doublescore^-", "__")
|
|
||||||
message.gsub!("-^star^-", "*")
|
|
||||||
message.gsub!("-^score^-", "_")
|
|
||||||
end
|
|
||||||
|
|
||||||
if options[:youtube]
|
|
||||||
while youtube = message.match(/youtube\.com::([A-Za-z0-9_\\\-]+)/)
|
|
||||||
video_id = youtube[1]
|
|
||||||
if options[:youtube_maps] && options[:youtube_maps][video_id]
|
|
||||||
title = options[:youtube_maps][video_id]
|
|
||||||
else
|
|
||||||
title = I18n.t 'application.helper.youtube_title.unknown'
|
|
||||||
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>')
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if options[:newlines]
|
if options[:newlines]
|
||||||
message.gsub!(/\n+/, '<br />')
|
message.gsub!(/\n+/, '<br />')
|
||||||
|
|
@ -238,6 +186,72 @@ module ApplicationHelper
|
||||||
return message
|
return message
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def process_links(message)
|
||||||
|
message.gsub!(/\[([^\[]+)\]\(([^ ]+) \"(([^&]|(&[^q])|(&q[^u])|(&qu[^o])|(&quo[^t])|("[^;]))+)\"\)/) do |m|
|
||||||
|
escape = "\\"
|
||||||
|
res = "<a target=\"#{escape}_blank\" href=\"#{$2}\" title=\"#{$3}\">#{$1}</a>"
|
||||||
|
res
|
||||||
|
end
|
||||||
|
message.gsub!(/\[([^\[]+)\]\(([^ ]+)\)/) do |m|
|
||||||
|
escape = "\\"
|
||||||
|
res = "<a target=\"#{escape}_blank\" href=\"#{$2}\">#{$1}</a>"
|
||||||
|
res
|
||||||
|
end
|
||||||
|
|
||||||
|
return message
|
||||||
|
end
|
||||||
|
|
||||||
|
def process_youtube(message)
|
||||||
|
message.gsub!(/( |^)(http:\/\/)?www\.youtube\.com\/watch[^ ]*v=([A-Za-z0-9_]+)(&[^ ]*|)/) do |m|
|
||||||
|
res = "#{$1}youtube.com::#{$3}"
|
||||||
|
res.gsub!(/(\*|_)/) { |m| "\\#{$1}" }
|
||||||
|
res
|
||||||
|
end
|
||||||
|
return message
|
||||||
|
end
|
||||||
|
|
||||||
|
def process_autolinks(message)
|
||||||
|
message.gsub!(/( |^)(www\.[^\s]+\.[^\s])/, '\1http://\2')
|
||||||
|
message.gsub!(/(<a target="\\?_blank" href=")?(https|http|ftp):\/\/([^\s]+)/) do |m|
|
||||||
|
if !$1.nil?
|
||||||
|
m
|
||||||
|
else
|
||||||
|
res = %{<a target="_blank" href="#{$2}://#{$3}">#{$3}</a>}
|
||||||
|
res.gsub!(/(\*|_)/) { |m| "\\#{$1}" }
|
||||||
|
res
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return message
|
||||||
|
end
|
||||||
|
|
||||||
|
def process_emphasis(message)
|
||||||
|
message.gsub!("\\**", "-^doublestar^-")
|
||||||
|
message.gsub!("\\__", "-^doublescore^-")
|
||||||
|
message.gsub!("\\*", "-^star^-")
|
||||||
|
message.gsub!("\\_", "-^score^-")
|
||||||
|
message.gsub!(/(\*\*\*|___)(.+?)\1/m, '<em><strong>\2</strong></em>')
|
||||||
|
message.gsub!(/(\*\*|__)(.+?)\1/m, '<strong>\2</strong>')
|
||||||
|
message.gsub!(/(\*|_)(.+?)\1/m, '<em>\2</em>')
|
||||||
|
message.gsub!("-^doublestar^-", "**")
|
||||||
|
message.gsub!("-^doublescore^-", "__")
|
||||||
|
message.gsub!("-^star^-", "*")
|
||||||
|
message.gsub!("-^score^-", "_")
|
||||||
|
return message
|
||||||
|
end
|
||||||
|
|
||||||
|
def process_youtube_again(message, youtube_maps)
|
||||||
|
while youtube = message.match(/youtube\.com::([A-Za-z0-9_\\\-]+)/)
|
||||||
|
video_id = youtube[1]
|
||||||
|
if youtube_maps && youtube_maps[video_id]
|
||||||
|
title = youtube_maps[video_id]
|
||||||
|
else
|
||||||
|
title = I18n.t 'application.helper.youtube_title.unknown'
|
||||||
|
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>')
|
||||||
|
end
|
||||||
|
return message
|
||||||
|
end
|
||||||
|
|
||||||
def info_text(text)
|
def info_text(text)
|
||||||
image_tag 'icons/monotone_question.png', :class => 'what_is_this', :title => text
|
image_tag 'icons/monotone_question.png', :class => 'what_is_this', :title => text
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
|
||||||
it "recognizes youtube links" do
|
describe "video links" do
|
||||||
proto="http"
|
it "recognizes youtube links" do
|
||||||
videoid = "0x__dDWdf23"
|
video_id = "0x__dDWdf23"
|
||||||
url="www.youtube.com/watch?v="+videoid+"&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
|
url = "http://www.youtube.com/watch?v=" + video_id + "&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
|
||||||
title = "UP & down & UP & down &"
|
res = markdownify(url)
|
||||||
res = markdownify(proto+'://'+url)
|
res.should =~ /data-host="youtube.com"/
|
||||||
res.should =~ /data-host="youtube.com"/
|
res.should =~ /data-video-id="#{video_id}"/
|
||||||
res.should =~ /data-video-id="#{videoid}"/
|
end
|
||||||
end
|
|
||||||
|
it "recognizes youtube links with hyphens" do
|
||||||
it "recognizes youtube links with hyphens" do
|
video_id = "ABYnqp-bxvg"
|
||||||
proto="http"
|
url = "http://www.youtube.com/watch?v=" + video_id + "&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
|
||||||
videoid = "ABYnqp-bxvg"
|
res = markdownify(url)
|
||||||
url="www.youtube.com/watch?v="+videoid+"&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
|
res.should =~ /data-host="youtube.com"/
|
||||||
title = "UP & down & UP & down &"
|
res.should =~ /data-video-id="#{video_id}"/
|
||||||
res = markdownify(proto+'://'+url)
|
end
|
||||||
res.should =~ /data-host="youtube.com"/
|
|
||||||
res.should =~ /data-video-id="#{videoid}"/
|
|
||||||
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&v=BARFOO&whatever=related\">www.youtube.com/watch?foo=bar&v=BARFOO&whatever=related</a> *emphasis* __emphasis__ [link](www.url.com) [link](url.com "title")"
|
|
||||||
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&v=BARFOO&whatever=related <em>emphasis</em> <strong>emphasis</strong> [link](www.url.com) [link](url.com "title")"
|
|
||||||
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&v=BARFOO&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)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue