slimming down application_helper, moved markdown and aspect related methods to individual helpers
This commit is contained in:
parent
8abe28d992
commit
efd5c4f500
8 changed files with 536 additions and 501 deletions
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
class ApplicationController < ActionController::Base
|
class ApplicationController < ActionController::Base
|
||||||
clear_helpers
|
clear_helpers
|
||||||
helper :layout, :error_messages
|
helper :layout, :error_messages, :markdownify, :aspect_global
|
||||||
has_mobile_fu
|
has_mobile_fu
|
||||||
protect_from_forgery :except => :receive
|
protect_from_forgery :except => :receive
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,11 @@
|
||||||
# the COPYRIGHT file.
|
# the COPYRIGHT file.
|
||||||
|
|
||||||
module ApplicationHelper
|
module ApplicationHelper
|
||||||
@@youtube_title_cache = Hash.new("no-title")
|
def how_long_ago(obj)
|
||||||
def time_for_sort post
|
timeago(obj.created_at)
|
||||||
|
end
|
||||||
|
|
||||||
|
def time_for_sort(post)
|
||||||
post.created_at
|
post.created_at
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -13,94 +16,10 @@ module ApplicationHelper
|
||||||
content_tag(:abbr, time.to_s, options.merge(:title => time.iso8601)) if time
|
content_tag(:abbr, time.to_s, options.merge(:title => time.iso8601)) if time
|
||||||
end
|
end
|
||||||
|
|
||||||
def page_title(text=nil)
|
|
||||||
return text unless text.blank?
|
|
||||||
current_user ? current_user.name : t("application.helper.diaspora_alpha")
|
|
||||||
end
|
|
||||||
|
|
||||||
def aspects_with_post(aspects, post)
|
|
||||||
aspects.select do |aspect|
|
|
||||||
AspectVisibility.exists?(:aspect_id => aspect.id, :post_id => post.id)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def aspects_without_post(aspects, post)
|
|
||||||
aspects.reject do |aspect|
|
|
||||||
AspectVisibility.exists?(:aspect_id => aspect.id, :post_id => post.id)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def aspect_badges(aspects, opts={})
|
|
||||||
str = ''
|
|
||||||
aspects.each do |aspect|
|
|
||||||
str << aspect_badge(aspect, opts)
|
|
||||||
end
|
|
||||||
str.html_safe
|
|
||||||
end
|
|
||||||
|
|
||||||
def bookmarklet
|
def bookmarklet
|
||||||
"javascript:(function(){f='#{AppConfig[:pod_url]}bookmarklet?url='+encodeURIComponent(window.location.href)+'&title='+encodeURIComponent(document.title)+'¬es='+encodeURIComponent(''+(window.getSelection?window.getSelection():document.getSelection?document.getSelection():document.selection.createRange().text))+'&v=1&';a=function(){if(!window.open(f+'noui=1&jump=doclose','diasporav1','location=yes,links=no,scrollbars=no,toolbar=no,width=620,height=250'))location.href=f+'jump=yes'};if(/Firefox/.test(navigator.userAgent)){setTimeout(a,0)}else{a()}})()"
|
"javascript:(function(){f='#{AppConfig[:pod_url]}bookmarklet?url='+encodeURIComponent(window.location.href)+'&title='+encodeURIComponent(document.title)+'¬es='+encodeURIComponent(''+(window.getSelection?window.getSelection():document.getSelection?document.getSelection():document.selection.createRange().text))+'&v=1&';a=function(){if(!window.open(f+'noui=1&jump=doclose','diasporav1','location=yes,links=no,scrollbars=no,toolbar=no,width=620,height=250'))location.href=f+'jump=yes'};if(/Firefox/.test(navigator.userAgent)){setTimeout(a,0)}else{a()}})()"
|
||||||
end
|
end
|
||||||
|
|
||||||
def aspect_badge(aspect, opts={})
|
|
||||||
str = "<span class='aspect_badge single'>"
|
|
||||||
link = opts.delete(:link)
|
|
||||||
if !link
|
|
||||||
str << link_to(aspect.name, "#", 'data-guid' => aspect.id, :class => 'hard_aspect_link').html_safe
|
|
||||||
else
|
|
||||||
str << link_for_aspect(aspect).html_safe
|
|
||||||
end
|
|
||||||
str << "</span>"
|
|
||||||
end
|
|
||||||
|
|
||||||
def aspect_links(aspects, opts={})
|
|
||||||
str = ""
|
|
||||||
aspects.each do |aspect|
|
|
||||||
str << '<li>'
|
|
||||||
str << link_for_aspect(aspect, :params => opts, 'data-guid' => aspect.id, :class => 'hard_aspect_link').html_safe
|
|
||||||
str << '</li>'
|
|
||||||
end
|
|
||||||
str.html_safe
|
|
||||||
end
|
|
||||||
|
|
||||||
def aspect_li(aspect, opts={})
|
|
||||||
param_string = ""
|
|
||||||
if opts.size > 0
|
|
||||||
param_string << '?'
|
|
||||||
opts.each_pair do |k, v|
|
|
||||||
param_string << "#{k}=#{v}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
"<li>
|
|
||||||
<a href='/aspects/#{aspect.id}#{param_string}'>
|
|
||||||
#{aspect.name}
|
|
||||||
</a>
|
|
||||||
</li>".html_safe
|
|
||||||
end
|
|
||||||
|
|
||||||
def link_for_aspect(aspect, opts={})
|
|
||||||
opts[:params] ||= {}
|
|
||||||
params ||= {}
|
|
||||||
opts[:params] = opts[:params].merge("a_ids[]" => aspect.id, :created_at => params[:created_at])
|
|
||||||
opts[:class] ||= ""
|
|
||||||
opts[:class] << " hard_aspect_link"
|
|
||||||
opts['data-guid'] = aspect.id
|
|
||||||
|
|
||||||
link_to aspect.name, aspects_path( opts[:params] ), opts
|
|
||||||
end
|
|
||||||
|
|
||||||
def current_aspect?(aspect)
|
|
||||||
!@aspect.nil? && !@aspect.instance_of?(Symbol) && @aspect.id == aspect.id
|
|
||||||
end
|
|
||||||
|
|
||||||
def aspect_or_all_path(aspect)
|
|
||||||
if @aspect.is_a? Aspect
|
|
||||||
aspect_path @aspect
|
|
||||||
else
|
|
||||||
aspects_path
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def object_path(object, opts={})
|
def object_path(object, opts={})
|
||||||
return "" if object.nil?
|
return "" if object.nil?
|
||||||
object = object.person if object.is_a? User
|
object = object.person if object.is_a? User
|
||||||
|
|
@ -120,10 +39,6 @@ module ApplicationHelper
|
||||||
"#{class_name.pluralize}/#{class_name}"
|
"#{class_name.pluralize}/#{class_name}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def how_long_ago(obj)
|
|
||||||
timeago(obj.created_at)
|
|
||||||
end
|
|
||||||
|
|
||||||
def profile_photo(person)
|
def profile_photo(person)
|
||||||
person_image_link(person, :size => :thumb_large, :to => :photos)
|
person_image_link(person, :size => :thumb_large, :to => :photos)
|
||||||
end
|
end
|
||||||
|
|
@ -165,136 +80,6 @@ module ApplicationHelper
|
||||||
(':' + post.id.to_s).to_sym
|
(':' + post.id.to_s).to_sym
|
||||||
end
|
end
|
||||||
|
|
||||||
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+/, '<br />') if options[:newlines]
|
|
||||||
|
|
||||||
message
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
def process_links(message)
|
|
||||||
message.gsub!(/\[([^\[]+)\]\(([^ ]+) \"(([^&]|(&[^q])|(&q[^u])|(&qu[^o])|(&quo[^t])|("[^;]))+)\"\)/) do |m|
|
|
||||||
escape = "\\"
|
|
||||||
link = $1
|
|
||||||
url = $2
|
|
||||||
title = $3
|
|
||||||
url.gsub!("_", "\\_")
|
|
||||||
url.gsub!("*", "\\*")
|
|
||||||
protocol = (url =~ /^\w+:\/\//) ? '' :'http://'
|
|
||||||
res = "<a target=\"#{escape}_blank\" href=\"#{protocol}#{url}\" title=\"#{title}\">#{link}</a>"
|
|
||||||
res
|
|
||||||
end
|
|
||||||
|
|
||||||
message.gsub!(/\[([^\[]+)\]\(([^ ]+)\)/) do |m|
|
|
||||||
escape = "\\"
|
|
||||||
link = $1
|
|
||||||
url = $2
|
|
||||||
url.gsub!("_", "\\_")
|
|
||||||
url.gsub!("*", "\\*")
|
|
||||||
protocol = (url =~ /^\w+:\/\//) ? '' :'http://'
|
|
||||||
res = "<a target=\"#{escape}_blank\" href=\"#{protocol}#{url}\">#{link}</a>"
|
|
||||||
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
|
|
||||||
' <a class="video-link" data-host="youtube.com" data-video-id="' + video_id + '" data-anchor="' + anchor + '" href="'+ match_data[0].strip + '" target="_blank">Youtube: ' + title + '</a>'
|
|
||||||
end
|
|
||||||
processed_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|
|
|
||||||
captures = [$1,$2,$3]
|
|
||||||
if !captures[0].nil?
|
|
||||||
m
|
|
||||||
elsif m.match(/(youtube|vimeo)/)
|
|
||||||
m.gsub(/(\*|_)/) { |m| "\\#{$1}" } #remove markers on markdown chars to not markdown inside links
|
|
||||||
else
|
|
||||||
res = %{<a target="_blank" href="#{captures[1]}://#{captures[2]}">#{captures[2]}</a>}
|
|
||||||
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, '<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^-", "_")
|
|
||||||
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
|
|
||||||
' <a class="video-link" data-host="vimeo.com" data-video-id="' + video_id + '" href="' + match_data[0] + '" target="_blank">Vimeo: ' + title + '</a>'
|
|
||||||
end
|
|
||||||
processed_message
|
|
||||||
end
|
|
||||||
|
|
||||||
def process_emoticons(message)
|
|
||||||
map = {
|
|
||||||
"<3" => "♥",
|
|
||||||
":(" => "☹",
|
|
||||||
":-(" => "☹",
|
|
||||||
":)" => "☺",
|
|
||||||
":-)" => "☺",
|
|
||||||
"->" => "→",
|
|
||||||
"<-" => "←",
|
|
||||||
"..." => "…",
|
|
||||||
"(tm)" => "™",
|
|
||||||
"(r)" => "®",
|
|
||||||
"(c)" => "©"
|
|
||||||
}
|
|
||||||
|
|
||||||
map.each do |search, replace|
|
|
||||||
message.gsub!(search, replace)
|
|
||||||
end
|
|
||||||
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
|
||||||
|
|
|
||||||
84
app/helpers/aspect_global_helper.rb
Normal file
84
app/helpers/aspect_global_helper.rb
Normal file
|
|
@ -0,0 +1,84 @@
|
||||||
|
# Copyright (c) 2010, Diaspora Inc. This file is
|
||||||
|
# licensed under the Affero General Public License version 3 or later. See
|
||||||
|
# the COPYRIGHT file.
|
||||||
|
|
||||||
|
module AspectGlobalHelper
|
||||||
|
def aspects_with_post(aspects, post)
|
||||||
|
aspects.select do |aspect|
|
||||||
|
AspectVisibility.exists?(:aspect_id => aspect.id, :post_id => post.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def aspects_without_post(aspects, post)
|
||||||
|
aspects.reject do |aspect|
|
||||||
|
AspectVisibility.exists?(:aspect_id => aspect.id, :post_id => post.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def aspect_badges(aspects, opts={})
|
||||||
|
str = ''
|
||||||
|
aspects.each do |aspect|
|
||||||
|
str << aspect_badge(aspect, opts)
|
||||||
|
end
|
||||||
|
str.html_safe
|
||||||
|
end
|
||||||
|
|
||||||
|
def aspect_badge(aspect, opts={})
|
||||||
|
str = "<span class='aspect_badge single'>"
|
||||||
|
link = opts.delete(:link)
|
||||||
|
if !link
|
||||||
|
str << link_to(aspect.name, "#", 'data-guid' => aspect.id, :class => 'hard_aspect_link').html_safe
|
||||||
|
else
|
||||||
|
str << link_for_aspect(aspect).html_safe
|
||||||
|
end
|
||||||
|
str << "</span>"
|
||||||
|
end
|
||||||
|
|
||||||
|
def aspect_links(aspects, opts={})
|
||||||
|
str = ""
|
||||||
|
aspects.each do |aspect|
|
||||||
|
str << '<li>'
|
||||||
|
str << link_for_aspect(aspect, :params => opts, 'data-guid' => aspect.id, :class => 'hard_aspect_link').html_safe
|
||||||
|
str << '</li>'
|
||||||
|
end
|
||||||
|
str.html_safe
|
||||||
|
end
|
||||||
|
|
||||||
|
def aspect_li(aspect, opts={})
|
||||||
|
param_string = ""
|
||||||
|
if opts.size > 0
|
||||||
|
param_string << '?'
|
||||||
|
opts.each_pair do |k, v|
|
||||||
|
param_string << "#{k}=#{v}"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
"<li>
|
||||||
|
<a href='/aspects/#{aspect.id}#{param_string}'>
|
||||||
|
#{aspect.name}
|
||||||
|
</a>
|
||||||
|
</li>".html_safe
|
||||||
|
end
|
||||||
|
|
||||||
|
def link_for_aspect(aspect, opts={})
|
||||||
|
opts[:params] ||= {}
|
||||||
|
params ||= {}
|
||||||
|
opts[:params] = opts[:params].merge("a_ids[]" => aspect.id, :created_at => params[:created_at])
|
||||||
|
opts[:class] ||= ""
|
||||||
|
opts[:class] << " hard_aspect_link"
|
||||||
|
opts['data-guid'] = aspect.id
|
||||||
|
|
||||||
|
link_to aspect.name, aspects_path( opts[:params] ), opts
|
||||||
|
end
|
||||||
|
|
||||||
|
def current_aspect?(aspect)
|
||||||
|
!@aspect.nil? && !@aspect.instance_of?(Symbol) && @aspect.id == aspect.id
|
||||||
|
end
|
||||||
|
|
||||||
|
def aspect_or_all_path(aspect)
|
||||||
|
if @aspect.is_a? Aspect
|
||||||
|
aspect_path @aspect
|
||||||
|
else
|
||||||
|
aspects_path
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -12,6 +12,11 @@ module LayoutHelper
|
||||||
@show_title = show_title
|
@show_title = show_title
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def page_title(text=nil)
|
||||||
|
return text unless text.blank?
|
||||||
|
current_user ? current_user.name : t("application.helper.diaspora_alpha")
|
||||||
|
end
|
||||||
|
|
||||||
def show_title?
|
def show_title?
|
||||||
@show_title
|
@show_title
|
||||||
end
|
end
|
||||||
|
|
|
||||||
134
app/helpers/markdownify_helper.rb
Normal file
134
app/helpers/markdownify_helper.rb
Normal file
|
|
@ -0,0 +1,134 @@
|
||||||
|
# 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+/, '<br />') if options[:newlines]
|
||||||
|
|
||||||
|
message
|
||||||
|
end
|
||||||
|
|
||||||
|
def process_links(message)
|
||||||
|
message.gsub!(/\[([^\[]+)\]\(([^ ]+) \"(([^&]|(&[^q])|(&q[^u])|(&qu[^o])|(&quo[^t])|("[^;]))+)\"\)/) do |m|
|
||||||
|
escape = "\\"
|
||||||
|
link = $1
|
||||||
|
url = $2
|
||||||
|
title = $3
|
||||||
|
url.gsub!("_", "\\_")
|
||||||
|
url.gsub!("*", "\\*")
|
||||||
|
protocol = (url =~ /^\w+:\/\//) ? '' :'http://'
|
||||||
|
res = "<a target=\"#{escape}_blank\" href=\"#{protocol}#{url}\" title=\"#{title}\">#{link}</a>"
|
||||||
|
res
|
||||||
|
end
|
||||||
|
|
||||||
|
message.gsub!(/\[([^\[]+)\]\(([^ ]+)\)/) do |m|
|
||||||
|
escape = "\\"
|
||||||
|
link = $1
|
||||||
|
url = $2
|
||||||
|
url.gsub!("_", "\\_")
|
||||||
|
url.gsub!("*", "\\*")
|
||||||
|
protocol = (url =~ /^\w+:\/\//) ? '' :'http://'
|
||||||
|
res = "<a target=\"#{escape}_blank\" href=\"#{protocol}#{url}\">#{link}</a>"
|
||||||
|
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
|
||||||
|
' <a class="video-link" data-host="youtube.com" data-video-id="' + video_id + '" data-anchor="' + anchor + '" href="'+ match_data[0].strip + '" target="_blank">Youtube: ' + title + '</a>'
|
||||||
|
end
|
||||||
|
processed_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|
|
||||||
|
captures = [$1,$2,$3]
|
||||||
|
if !captures[0].nil?
|
||||||
|
m
|
||||||
|
elsif m.match(/(youtube|vimeo)/)
|
||||||
|
m.gsub(/(\*|_)/) { |m| "\\#{$1}" } #remove markers on markdown chars to not markdown inside links
|
||||||
|
else
|
||||||
|
res = %{<a target="_blank" href="#{captures[1]}://#{captures[2]}">#{captures[2]}</a>}
|
||||||
|
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, '<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^-", "_")
|
||||||
|
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
|
||||||
|
' <a class="video-link" data-host="vimeo.com" data-video-id="' + video_id + '" href="' + match_data[0] + '" target="_blank">Vimeo: ' + title + '</a>'
|
||||||
|
end
|
||||||
|
processed_message
|
||||||
|
end
|
||||||
|
|
||||||
|
def process_emoticons(message)
|
||||||
|
map = {
|
||||||
|
"<3" => "♥",
|
||||||
|
":(" => "☹",
|
||||||
|
":-(" => "☹",
|
||||||
|
":)" => "☺",
|
||||||
|
":-)" => "☺",
|
||||||
|
"->" => "→",
|
||||||
|
"<-" => "←",
|
||||||
|
"..." => "…",
|
||||||
|
"(tm)" => "™",
|
||||||
|
"(r)" => "®",
|
||||||
|
"(c)" => "©"
|
||||||
|
}
|
||||||
|
|
||||||
|
map.each do |search, replace|
|
||||||
|
message.gsub!(search, replace)
|
||||||
|
end
|
||||||
|
message
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -42,290 +42,40 @@ describe ApplicationHelper do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#markdownify" do
|
describe '#person_link' do
|
||||||
describe "autolinks" do
|
|
||||||
it "should not allow basic XSS/HTML" do
|
|
||||||
markdownify("<script>alert('XSS is evil')</script>").should == "<script>alert('XSS is evil')</script>"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should recognize basic http links (1/3)" do
|
|
||||||
proto="http"
|
|
||||||
url="bugs.joindiaspora.com/issues/332"
|
|
||||||
markdownify(proto+"://"+url).should == "<a target=\"_blank\" href=\""+proto+"://"+url+"\">"+url+"</a>"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should recognize basic http links (2/3)" do
|
|
||||||
proto="http"
|
|
||||||
url="webmail.example.com?~()!*/"
|
|
||||||
markdownify(proto+"://"+url).should == "<a target=\"_blank\" href=\""+proto+"://"+url+"\">"+url+"</a>"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should recognize basic http links (3/3)" do
|
|
||||||
proto="http"
|
|
||||||
url="127.0.0.1:3000/users/sign_in"
|
|
||||||
markdownify(proto+"://"+url).should == "<a target=\"_blank\" href=\""+proto+"://"+url+"\">"+url+"</a>"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should recognize secure https links" do
|
|
||||||
proto="https"
|
|
||||||
url="127.0.0.1:3000/users/sign_in"
|
|
||||||
markdownify(proto+"://"+url).should == "<a target=\"_blank\" href=\""+proto+"://"+url+"\">"+url+"</a>"
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
describe "video links" do
|
|
||||||
it "recognizes vimeo links" do
|
|
||||||
video_id = "17449557"
|
|
||||||
url = "http://www.vimeo.com/#{video_id}"
|
|
||||||
res = markdownify(url)
|
|
||||||
res.should =~ /data-host="vimeo.com"/
|
|
||||||
res.should =~ /data-video-id="#{video_id}"/
|
|
||||||
end
|
|
||||||
|
|
||||||
it "recognizes youtube links" do
|
|
||||||
video_id = "0x__dDWdf23"
|
|
||||||
url = "http://www.youtube.com/watch?v=" + video_id + "&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
|
|
||||||
res = markdownify(url)
|
|
||||||
res.should =~ /Youtube:/
|
|
||||||
res.should =~ /data-host="youtube.com"/
|
|
||||||
res.should =~ /data-video-id="#{video_id}"/
|
|
||||||
end
|
|
||||||
|
|
||||||
it "recognizes youtube links with hyphens" do
|
|
||||||
video_id = "ABYnqp-bxvg"
|
|
||||||
url = "http://www.youtube.com/watch?v=" + video_id + "&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
|
|
||||||
res = markdownify(url)
|
|
||||||
res.should =~ /Youtube:/
|
|
||||||
res.should =~ /data-host="youtube.com"/
|
|
||||||
res.should =~ /data-video-id="#{video_id}"/
|
|
||||||
end
|
|
||||||
|
|
||||||
it "keeps anchors" do
|
|
||||||
anchor = "#t=11m34"
|
|
||||||
video_id = "DHRoHuv3I8E"
|
|
||||||
url = "http://www.youtube.com/watch?v=" + video_id + anchor
|
|
||||||
res = markdownify(url)
|
|
||||||
res.should =~ /Youtube:/
|
|
||||||
res.should =~ /data-host="youtube.com"/
|
|
||||||
res.should =~ /data-video-id="#{video_id}"/
|
|
||||||
res.should =~ /data-anchor="#{anchor}"/
|
|
||||||
end
|
|
||||||
|
|
||||||
it "has an empty data-anchor attribute if there is no anchor" do
|
|
||||||
video_id = "DHRoHuv3I8E"
|
|
||||||
url = "http://www.youtube.com/watch?v=" + video_id
|
|
||||||
res = markdownify(url)
|
|
||||||
res.should =~ /Youtube:/
|
|
||||||
res.should =~ /data-host="youtube.com"/
|
|
||||||
res.should =~ /data-video-id="#{video_id}"/
|
|
||||||
res.should =~ /data-anchor=""/
|
|
||||||
end
|
|
||||||
|
|
||||||
it "leaves the links in the href of the #a tag" do
|
|
||||||
video_id = "ABYnqp-bxvg"
|
|
||||||
start_url ="http://www.youtube.com/watch?v=" + video_id
|
|
||||||
url = start_url + "&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
|
|
||||||
res = markdownify(url)
|
|
||||||
res.should =~ /href=[\S]+v=#{video_id}/
|
|
||||||
end
|
|
||||||
it 'does not autolink inside the link' do
|
|
||||||
video_id = "ABYnqp-bxvg"
|
|
||||||
start_url ="http://www.youtube.com/watch?v=" + video_id
|
|
||||||
url = start_url + "&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
|
|
||||||
res = markdownify(url)
|
|
||||||
res.match(/href="<a/).should be_nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
it "recognizes multiple links of different types" do
|
|
||||||
message = "http:// Hello World, this is for www.joindiaspora.com and not for http://www.google.com though their Youtube service is neat, take http://www.youtube.com/watch?v=foobar----- or www.youtube.com/watch?foo=bar&v=BARFOO-----&whatever=related It is a good idea we finally have youtube, so enjoy this video http://www.youtube.com/watch?v=rickrolld--"
|
|
||||||
res = markdownify(message)
|
|
||||||
res.should =~ /a target=\"_blank\" href=\"http:\/\/www.joindiaspora.com\"/
|
|
||||||
res.should =~ /a target=\"_blank\" href=\"http:\/\/www.google.com\"/
|
|
||||||
res.should =~ /data-video-id="foobar-----"/
|
|
||||||
res.should =~ /data-video-id="BARFOO-----"/
|
|
||||||
res.should =~ /data-video-id="rickrolld--"/
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should recognize basic ftp links" do
|
|
||||||
proto="ftp"
|
|
||||||
url="ftp.uni-kl.de/CCC/26C3/mp4/26c3-3540-en-a_hackers_utopia.mp4"
|
|
||||||
# I did not watch that one, but the title sounds nice :P
|
|
||||||
markdownify(proto+"://"+url).should == "<a target=\"_blank\" href=\""+proto+"://"+url+"\">"+url+"</a>"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should recognize www links" do
|
|
||||||
url="www.joindiaspora.com"
|
|
||||||
markdownify(url).should == "<a target=\"_blank\" href=\"http://"+url+"\">"+url+"</a>"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "emoticons" do
|
|
||||||
it "replaces <3 with ♥" do
|
|
||||||
message = "i <3 you"
|
|
||||||
markdownify(message).should == "i ♥ you"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "replaces various things with (their) HTML entities" do
|
|
||||||
message = ":) :-) :( :-( ... -> <- (tm) (r) (c)"
|
|
||||||
markdownify(message).should == "☺ ☺ ☹ ☹ … → ← ™ ® ©"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "skips doing it if you say so" do
|
|
||||||
message = ":) :-) :( :-( ... -> <-"
|
|
||||||
markdownify(message, :emoticons => false).should == ":) :-) :( :-( ... -> <-"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "weak emphasis" do
|
|
||||||
it "should be recognized (1/2)" do
|
|
||||||
message = "*some text* some text *some text* some text"
|
|
||||||
markdownify(message).should == "<em>some text</em> some text <em>some text</em> some text"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should be recognized (2/2)" do
|
|
||||||
message = "_some text_ some text _some text_ some text"
|
|
||||||
markdownify(message).should == "<em>some text</em> some text <em>some text</em> some text"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "strong emphasis" do
|
|
||||||
it "should be recognized (1/2)" do
|
|
||||||
message = "**some text** some text **some text** some text"
|
|
||||||
markdownify(message).should == "<strong>some text</strong> some text <strong>some text</strong> some text"
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should be recognized (2/2)" do
|
|
||||||
message = "__some text__ some text __some text__ some text"
|
|
||||||
markdownify(message).should == "<strong>some text</strong> some text <strong>some text</strong> some text"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "nested weak and strong emphasis" do
|
|
||||||
it "should be rendered correctly" do
|
|
||||||
message = "__this is _some_ text__"
|
|
||||||
markdownify(message).should == "<strong>this is <em>some</em> text</strong>"
|
|
||||||
message = "*this is **some** text*"
|
|
||||||
markdownify(message).should == "<em>this is <strong>some</strong> text</em>"
|
|
||||||
message = "___some text___"
|
|
||||||
markdownify(message).should == "<em><strong>some text</strong></em>"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "links" do
|
|
||||||
it "should be recognized without title attribute" do
|
|
||||||
message = "[link text](http://someurl.com) [link text](http://someurl.com)"
|
|
||||||
markdownify(message).should == '<a target="_blank" href="http://someurl.com">link text</a> <a target="_blank" href="http://someurl.com">link text</a>'
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should be recognized with title attribute" do
|
|
||||||
message = '[link text](http://someurl.com "some title") [link text](http://someurl.com "some title")'
|
|
||||||
markdownify(message).should == '<a target="_blank" href="http://someurl.com" title="some title">link text</a> <a target="_blank" href="http://someurl.com" title="some title">link text</a>'
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should have a robust link parsing" do
|
|
||||||
message = "This [*text*](http://en.wikipedia.org/wiki/Text_(literary_theory)) with many [links](google.com) tests [_http_](http://google.com/search?q=with_multiple__underscores*and**asterisks), [___FTP___](ftp://ftp.uni-kl.de/CCC/26C3/mp4/26c3-3540-en-a_hackers_utopia.mp4 \"File Transfer Protocol\"), [**any protocol**](foo://bar.example.org/yes_it*makes*no_sense)"
|
|
||||||
markdownify(message).should == 'This <a target="_blank" href="http://en.wikipedia.org/wiki/Text_(literary_theory)"><em>text</em></a> with many <a target="_blank" href="http://google.com">links</a> tests <a target="_blank" href="http://google.com/search?q=with_multiple__underscores*and**asterisks"><em>http</em></a>, <a target="_blank" href="ftp://ftp.uni-kl.de/CCC/26C3/mp4/26c3-3540-en-a_hackers_utopia.mp4" title="File Transfer Protocol"><em><strong>FTP</strong></em></a>, <a target="_blank" href="foo://bar.example.org/yes_it*makes*no_sense"><strong>any protocol</strong></a>'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "nested emphasis and links tags" do
|
|
||||||
it "should be rendered correctly" do
|
|
||||||
message = '[**some *link* text**](someurl.com "some title")'
|
|
||||||
markdownify(message).should == '<a target="_blank" href="http://someurl.com" title="some title"><strong>some <em>link</em> text</strong></a>'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should allow escaping" do
|
|
||||||
message = '*some text* \\*some text* \\**some text* _some text_ \\_some text_ \\__some text_'
|
|
||||||
markdownify(message).should == "<em>some text</em> *some text<em> **some text</em> <em>some text</em> _some text<em> __some text</em>"
|
|
||||||
end
|
|
||||||
|
|
||||||
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
|
|
||||||
message = "These\nare\nsome\nnew\nlines"
|
|
||||||
res = markdownify(message)
|
|
||||||
res.should == "These<br /\>are<br /\>some<br /\>new<br /\>lines"
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'should render newlines and basic http links correctly' do
|
|
||||||
message = "Some text, then a line break and a link\nhttp://joindiaspora.com\nsome more text"
|
|
||||||
res = markdownify(message)
|
|
||||||
res.should == 'Some text, then a line break and a link<br /><a target="_blank" href="http://joindiaspora.com">joindiaspora.com</a><br />some more text'
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe '#person_link' do
|
|
||||||
before do
|
|
||||||
@person = Factory(:person)
|
|
||||||
end
|
|
||||||
it 'includes the name of the person if they have a first name' do
|
|
||||||
person_link(@person).should include @person.profile.first_name
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'uses diaspora handle if the person has no first or last name' do
|
|
||||||
@person.profile.first_name = nil
|
|
||||||
@person.profile.last_name = nil
|
|
||||||
|
|
||||||
person_link(@person).should include @person.diaspora_handle
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'uses diaspora handle if first name and first name are rails#blank?' do
|
|
||||||
@person.profile.first_name = " "
|
|
||||||
@person.profile.last_name = " "
|
|
||||||
|
|
||||||
person_link(@person).should include @person.diaspora_handle
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should not allow basic XSS/HTML" do
|
|
||||||
@person.profile.first_name = "I'm <h1>Evil"
|
|
||||||
@person.profile.last_name = "I'm <h1>Evil"
|
|
||||||
person_link(@person).should_not include("<h1>")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
context 'performance' do
|
|
||||||
before do
|
|
||||||
@message = "HHello,Hello_, I _am a strong robot.*Hello, I am *a strong robot.Hello, I am a strong robot.Hello, I am a strong robot.Hello, I am a strong robot.Hello, I am a **strong robot.Hello, I am _a _strong *robot**.Hello*, I am a strong "
|
|
||||||
end
|
|
||||||
it 'is sub millisecond' do
|
|
||||||
Benchmark.realtime{
|
|
||||||
markdownify(@message)
|
|
||||||
}.should < 0.001
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "#page_title" do
|
|
||||||
before do
|
before do
|
||||||
def current_user
|
@person = Factory(:person)
|
||||||
@current_user
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
context "passed blank text" do
|
it 'includes the name of the person if they have a first name' do
|
||||||
it "returns current_user.name if logged in" do
|
person_link(@person).should include @person.profile.first_name
|
||||||
@current_user = @user
|
|
||||||
page_title.should == @user.name
|
|
||||||
end
|
|
||||||
|
|
||||||
it "returns default title if not logged in" do
|
|
||||||
@current_user = nil
|
|
||||||
page_title.should == I18n.t("application.helper.diaspora_alpha")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
context "passed text" do
|
it 'uses diaspora handle if the person has no first or last name' do
|
||||||
it "returns the text" do
|
@person.profile.first_name = nil
|
||||||
text = "This is the title"
|
@person.profile.last_name = nil
|
||||||
page_title(text).should == text
|
|
||||||
end
|
person_link(@person).should include @person.diaspora_handle
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'uses diaspora handle if first name and first name are rails#blank?' do
|
||||||
|
@person.profile.first_name = " "
|
||||||
|
@person.profile.last_name = " "
|
||||||
|
|
||||||
|
person_link(@person).should include @person.diaspora_handle
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should not allow basic XSS/HTML" do
|
||||||
|
@person.profile.first_name = "I'm <h1>Evil"
|
||||||
|
@person.profile.last_name = "I'm <h1>Evil"
|
||||||
|
person_link(@person).should_not include("<h1>")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
describe "#time_for_sort" do
|
||||||
|
it "returns created_at" do
|
||||||
|
post = @user.post(:status_message, :text => "hello", :public => true, :to => 'all')
|
||||||
|
time_for_sort(post).should == post.created_at
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
38
spec/helpers/layout_helper_spec.rb
Normal file
38
spec/helpers/layout_helper_spec.rb
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
# Copyright (c) 2010, Diaspora Inc. This file is
|
||||||
|
# licensed under the Affero General Public License version 3 or later. See
|
||||||
|
# the COPYRIGHT file.
|
||||||
|
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe LayoutHelper do
|
||||||
|
before do
|
||||||
|
@user = alice
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#page_title" do
|
||||||
|
before do
|
||||||
|
def current_user
|
||||||
|
@current_user
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "passed blank text" do
|
||||||
|
it "returns current_user.name if logged in" do
|
||||||
|
@current_user = @user
|
||||||
|
page_title.should == @user.name
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns default title if not logged in" do
|
||||||
|
@current_user = nil
|
||||||
|
page_title.should == I18n.t("application.helper.diaspora_alpha")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "passed text" do
|
||||||
|
it "returns the text" do
|
||||||
|
text = "This is the title"
|
||||||
|
page_title(text).should == text
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
239
spec/helpers/markdownify_helper_spec.rb
Normal file
239
spec/helpers/markdownify_helper_spec.rb
Normal file
|
|
@ -0,0 +1,239 @@
|
||||||
|
# Copyright (c) 2010, Diaspora Inc. This file is
|
||||||
|
# licensed under the Affero General Public License version 3 or later. See
|
||||||
|
# the COPYRIGHT file.
|
||||||
|
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe MarkdownifyHelper do
|
||||||
|
describe "#markdownify" do
|
||||||
|
describe "autolinks" do
|
||||||
|
it "should not allow basic XSS/HTML" do
|
||||||
|
markdownify("<script>alert('XSS is evil')</script>").should == "<script>alert('XSS is evil')</script>"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should recognize basic http links (1/3)" do
|
||||||
|
proto="http"
|
||||||
|
url="bugs.joindiaspora.com/issues/332"
|
||||||
|
markdownify(proto+"://"+url).should == "<a target=\"_blank\" href=\""+proto+"://"+url+"\">"+url+"</a>"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should recognize basic http links (2/3)" do
|
||||||
|
proto="http"
|
||||||
|
url="webmail.example.com?~()!*/"
|
||||||
|
markdownify(proto+"://"+url).should == "<a target=\"_blank\" href=\""+proto+"://"+url+"\">"+url+"</a>"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should recognize basic http links (3/3)" do
|
||||||
|
proto="http"
|
||||||
|
url="127.0.0.1:3000/users/sign_in"
|
||||||
|
markdownify(proto+"://"+url).should == "<a target=\"_blank\" href=\""+proto+"://"+url+"\">"+url+"</a>"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should recognize secure https links" do
|
||||||
|
proto="https"
|
||||||
|
url="127.0.0.1:3000/users/sign_in"
|
||||||
|
markdownify(proto+"://"+url).should == "<a target=\"_blank\" href=\""+proto+"://"+url+"\">"+url+"</a>"
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "video links" do
|
||||||
|
it "recognizes vimeo links" do
|
||||||
|
video_id = "17449557"
|
||||||
|
url = "http://www.vimeo.com/#{video_id}"
|
||||||
|
res = markdownify(url)
|
||||||
|
res.should =~ /data-host="vimeo.com"/
|
||||||
|
res.should =~ /data-video-id="#{video_id}"/
|
||||||
|
end
|
||||||
|
|
||||||
|
it "recognizes youtube links" do
|
||||||
|
video_id = "0x__dDWdf23"
|
||||||
|
url = "http://www.youtube.com/watch?v=" + video_id + "&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
|
||||||
|
res = markdownify(url)
|
||||||
|
res.should =~ /Youtube:/
|
||||||
|
res.should =~ /data-host="youtube.com"/
|
||||||
|
res.should =~ /data-video-id="#{video_id}"/
|
||||||
|
end
|
||||||
|
|
||||||
|
it "recognizes youtube links with hyphens" do
|
||||||
|
video_id = "ABYnqp-bxvg"
|
||||||
|
url = "http://www.youtube.com/watch?v=" + video_id + "&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
|
||||||
|
res = markdownify(url)
|
||||||
|
res.should =~ /Youtube:/
|
||||||
|
res.should =~ /data-host="youtube.com"/
|
||||||
|
res.should =~ /data-video-id="#{video_id}"/
|
||||||
|
end
|
||||||
|
|
||||||
|
it "keeps anchors" do
|
||||||
|
anchor = "#t=11m34"
|
||||||
|
video_id = "DHRoHuv3I8E"
|
||||||
|
url = "http://www.youtube.com/watch?v=" + video_id + anchor
|
||||||
|
res = markdownify(url)
|
||||||
|
res.should =~ /Youtube:/
|
||||||
|
res.should =~ /data-host="youtube.com"/
|
||||||
|
res.should =~ /data-video-id="#{video_id}"/
|
||||||
|
res.should =~ /data-anchor="#{anchor}"/
|
||||||
|
end
|
||||||
|
|
||||||
|
it "has an empty data-anchor attribute if there is no anchor" do
|
||||||
|
video_id = "DHRoHuv3I8E"
|
||||||
|
url = "http://www.youtube.com/watch?v=" + video_id
|
||||||
|
res = markdownify(url)
|
||||||
|
res.should =~ /Youtube:/
|
||||||
|
res.should =~ /data-host="youtube.com"/
|
||||||
|
res.should =~ /data-video-id="#{video_id}"/
|
||||||
|
res.should =~ /data-anchor=""/
|
||||||
|
end
|
||||||
|
|
||||||
|
it "leaves the links in the href of the #a tag" do
|
||||||
|
video_id = "ABYnqp-bxvg"
|
||||||
|
start_url ="http://www.youtube.com/watch?v=" + video_id
|
||||||
|
url = start_url + "&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
|
||||||
|
res = markdownify(url)
|
||||||
|
res.should =~ /href=[\S]+v=#{video_id}/
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'does not autolink inside the link' do
|
||||||
|
video_id = "ABYnqp-bxvg"
|
||||||
|
start_url ="http://www.youtube.com/watch?v=" + video_id
|
||||||
|
url = start_url + "&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
|
||||||
|
res = markdownify(url)
|
||||||
|
res.match(/href="<a/).should be_nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "recognizes multiple links of different types" do
|
||||||
|
message = "http:// Hello World, this is for www.joindiaspora.com and not for http://www.google.com though their Youtube service is neat, take http://www.youtube.com/watch?v=foobar----- or www.youtube.com/watch?foo=bar&v=BARFOO-----&whatever=related It is a good idea we finally have youtube, so enjoy this video http://www.youtube.com/watch?v=rickrolld--"
|
||||||
|
res = markdownify(message)
|
||||||
|
res.should =~ /a target=\"_blank\" href=\"http:\/\/www.joindiaspora.com\"/
|
||||||
|
res.should =~ /a target=\"_blank\" href=\"http:\/\/www.google.com\"/
|
||||||
|
res.should =~ /data-video-id="foobar-----"/
|
||||||
|
res.should =~ /data-video-id="BARFOO-----"/
|
||||||
|
res.should =~ /data-video-id="rickrolld--"/
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should recognize basic ftp links" do
|
||||||
|
proto="ftp"
|
||||||
|
url="ftp.uni-kl.de/CCC/26C3/mp4/26c3-3540-en-a_hackers_utopia.mp4"
|
||||||
|
# I did not watch that one, but the title sounds nice :P
|
||||||
|
markdownify(proto+"://"+url).should == "<a target=\"_blank\" href=\""+proto+"://"+url+"\">"+url+"</a>"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should recognize www links" do
|
||||||
|
url="www.joindiaspora.com"
|
||||||
|
markdownify(url).should == "<a target=\"_blank\" href=\"http://"+url+"\">"+url+"</a>"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "emoticons" do
|
||||||
|
it "replaces <3 with ♥" do
|
||||||
|
message = "i <3 you"
|
||||||
|
markdownify(message).should == "i ♥ you"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "replaces various things with (their) HTML entities" do
|
||||||
|
message = ":) :-) :( :-( ... -> <- (tm) (r) (c)"
|
||||||
|
markdownify(message).should == "☺ ☺ ☹ ☹ … → ← ™ ® ©"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "skips doing it if you say so" do
|
||||||
|
message = ":) :-) :( :-( ... -> <-"
|
||||||
|
markdownify(message, :emoticons => false).should == ":) :-) :( :-( ... -> <-"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "weak emphasis" do
|
||||||
|
it "should be recognized (1/2)" do
|
||||||
|
message = "*some text* some text *some text* some text"
|
||||||
|
markdownify(message).should == "<em>some text</em> some text <em>some text</em> some text"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should be recognized (2/2)" do
|
||||||
|
message = "_some text_ some text _some text_ some text"
|
||||||
|
markdownify(message).should == "<em>some text</em> some text <em>some text</em> some text"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "strong emphasis" do
|
||||||
|
it "should be recognized (1/2)" do
|
||||||
|
message = "**some text** some text **some text** some text"
|
||||||
|
markdownify(message).should == "<strong>some text</strong> some text <strong>some text</strong> some text"
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should be recognized (2/2)" do
|
||||||
|
message = "__some text__ some text __some text__ some text"
|
||||||
|
markdownify(message).should == "<strong>some text</strong> some text <strong>some text</strong> some text"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "nested weak and strong emphasis" do
|
||||||
|
it "should be rendered correctly" do
|
||||||
|
message = "__this is _some_ text__"
|
||||||
|
markdownify(message).should == "<strong>this is <em>some</em> text</strong>"
|
||||||
|
message = "*this is **some** text*"
|
||||||
|
markdownify(message).should == "<em>this is <strong>some</strong> text</em>"
|
||||||
|
message = "___some text___"
|
||||||
|
markdownify(message).should == "<em><strong>some text</strong></em>"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "links" do
|
||||||
|
it "should be recognized without title attribute" do
|
||||||
|
message = "[link text](http://someurl.com) [link text](http://someurl.com)"
|
||||||
|
markdownify(message).should == '<a target="_blank" href="http://someurl.com">link text</a> <a target="_blank" href="http://someurl.com">link text</a>'
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should be recognized with title attribute" do
|
||||||
|
message = '[link text](http://someurl.com "some title") [link text](http://someurl.com "some title")'
|
||||||
|
markdownify(message).should == '<a target="_blank" href="http://someurl.com" title="some title">link text</a> <a target="_blank" href="http://someurl.com" title="some title">link text</a>'
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should have a robust link parsing" do
|
||||||
|
message = "This [*text*](http://en.wikipedia.org/wiki/Text_(literary_theory)) with many [links](google.com) tests [_http_](http://google.com/search?q=with_multiple__underscores*and**asterisks), [___FTP___](ftp://ftp.uni-kl.de/CCC/26C3/mp4/26c3-3540-en-a_hackers_utopia.mp4 \"File Transfer Protocol\"), [**any protocol**](foo://bar.example.org/yes_it*makes*no_sense)"
|
||||||
|
markdownify(message).should == 'This <a target="_blank" href="http://en.wikipedia.org/wiki/Text_(literary_theory)"><em>text</em></a> with many <a target="_blank" href="http://google.com">links</a> tests <a target="_blank" href="http://google.com/search?q=with_multiple__underscores*and**asterisks"><em>http</em></a>, <a target="_blank" href="ftp://ftp.uni-kl.de/CCC/26C3/mp4/26c3-3540-en-a_hackers_utopia.mp4" title="File Transfer Protocol"><em><strong>FTP</strong></em></a>, <a target="_blank" href="foo://bar.example.org/yes_it*makes*no_sense"><strong>any protocol</strong></a>'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "nested emphasis and links tags" do
|
||||||
|
it "should be rendered correctly" do
|
||||||
|
message = '[**some *link* text**](someurl.com "some title")'
|
||||||
|
markdownify(message).should == '<a target="_blank" href="http://someurl.com" title="some title"><strong>some <em>link</em> text</strong></a>'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should allow escaping" do
|
||||||
|
message = '*some text* \\*some text* \\**some text* _some text_ \\_some text_ \\__some text_'
|
||||||
|
markdownify(message).should == "<em>some text</em> *some text<em> **some text</em> <em>some text</em> _some text<em> __some text</em>"
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
message = "These\nare\nsome\nnew\nlines"
|
||||||
|
res = markdownify(message)
|
||||||
|
res.should == "These<br /\>are<br /\>some<br /\>new<br /\>lines"
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should render newlines and basic http links correctly' do
|
||||||
|
message = "Some text, then a line break and a link\nhttp://joindiaspora.com\nsome more text"
|
||||||
|
res = markdownify(message)
|
||||||
|
res.should == 'Some text, then a line break and a link<br /><a target="_blank" href="http://joindiaspora.com">joindiaspora.com</a><br />some more text'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'performance' do
|
||||||
|
before do
|
||||||
|
@message = "HHello,Hello_, I _am a strong robot.*Hello, I am *a strong robot.Hello, I am a strong robot.Hello, I am a strong robot.Hello, I am a strong robot.Hello, I am a **strong robot.Hello, I am _a _strong *robot**.Hello*, I am a strong "
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'is sub millisecond' do
|
||||||
|
Benchmark.realtime{
|
||||||
|
markdownify(@message)
|
||||||
|
}.should < 0.001
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in a new issue