Added markdown support for status messages
This commit is contained in:
parent
41d839ecf4
commit
aab74cf1ff
6 changed files with 237 additions and 111 deletions
|
|
@ -3,6 +3,8 @@
|
|||
# the COPYRIGHT file.
|
||||
|
||||
module ApplicationHelper
|
||||
@@youtube_title_cache = Hash.new("no-title")
|
||||
|
||||
def current_aspect?(aspect)
|
||||
!@aspect.is_a?(Symbol) && @aspect.id == aspect.id
|
||||
end
|
||||
|
|
@ -85,4 +87,86 @@ module ApplicationHelper
|
|||
|
||||
"#{photos_path}?person_id=#{person_id}"
|
||||
end
|
||||
|
||||
def markdownify(message, options = {})
|
||||
message = h(message).html_safe
|
||||
|
||||
[:autolinks, :youtube, :emphasis, :links].each do |k|
|
||||
if !options.has_key?(k)
|
||||
options[k] = true
|
||||
end
|
||||
end
|
||||
|
||||
if options[:links]
|
||||
message.gsub!(/\[([^\[]+)\]\(([^ ]+) \"(([^&]|(&[^q])|(&q[^u])|(&qu[^o])|(&quo[^t])|("[^;]))+)\"\)/) do |m|
|
||||
escape = (options[:emphasis]) ? "\\" : ""
|
||||
res = "<a target=\"#{escape}_blank\" href=\"#{$2}\" title=\"#{$3}\">#{$1}</a>"
|
||||
res
|
||||
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\.[^ ]+\.[^ ])/, '\1http://\2')
|
||||
message.gsub!(/(<a target="\\?_blank" href=")?(https|http|ftp):\/\/([^ ]+)/) 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!(/([^\\]|^)\*\*(([^*]|([^*]\*[^*]))*[^*\\])\*\*/, '\1<strong>\2</strong>')
|
||||
message.gsub!(/([^\\]|^)__(([^_]|([^_]_[^_]))*[^_\\])__/, '\1<strong>\2</strong>')
|
||||
message.gsub!(/([^\\]|^)\*([^*]*[^\\])\*/, '\1<em>\2</em>')
|
||||
message.gsub!(/([^\\]|^)_([^_]*[^\\])_/, '\1<em>\2</em>')
|
||||
message.gsub!(/([^\\]|^)\*/, '\1')
|
||||
message.gsub!(/([^\\]|^)_/, '\1')
|
||||
message.gsub!("\\*", "*")
|
||||
message.gsub!("\\_", "_")
|
||||
end
|
||||
|
||||
if options[:youtube]
|
||||
while youtube = message.match(/youtube\.com::([A-Za-z0-9_\\]+)/)
|
||||
videoid = youtube[1]
|
||||
message.gsub!('youtube.com::'+videoid, '<a onclick="openVideo(\'youtube.com\', \'' + videoid + '\', this)" href="#video">Youtube: ' + youtube_title(videoid) + '</a>')
|
||||
end
|
||||
end
|
||||
|
||||
return message
|
||||
end
|
||||
|
||||
def youtube_title(id)
|
||||
unless @@youtube_title_cache[id] == 'no-title'
|
||||
return @@youtube_title_cache[id]
|
||||
end
|
||||
|
||||
ret = 'Unknown Video Title' #TODO add translation
|
||||
http = Net::HTTP.new('gdata.youtube.com', 80)
|
||||
path = '/feeds/api/videos/'+id+'?v=2'
|
||||
resp, data = http.get(path, nil)
|
||||
title = data.match(/<title>(.*)<\/title>/)
|
||||
unless title.nil?
|
||||
ret = title.to_s[7..-9]
|
||||
end
|
||||
|
||||
@@youtube_title_cache[id] = ret;
|
||||
return ret
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -3,8 +3,6 @@
|
|||
# the COPYRIGHT file.
|
||||
|
||||
module StatusMessagesHelper
|
||||
@@youtube_title_cache = Hash.new("no-title")
|
||||
|
||||
def my_latest_message
|
||||
unless @latest_status_message.nil?
|
||||
return @latest_status_message.message
|
||||
|
|
@ -12,39 +10,4 @@ module StatusMessagesHelper
|
|||
return I18n.t('status_messages.helper.no_message_to_display')
|
||||
end
|
||||
end
|
||||
|
||||
def make_links(message)
|
||||
# If there should be some kind of bb-style markup, email/diaspora highlighting, it could go here.
|
||||
|
||||
# next line is important due to XSS! (h is rail's make_html_safe-function)
|
||||
message = h(message).html_safe
|
||||
message.gsub!(/( |^)(www\.[^ ]+\.[^ ])/, '\1http://\2')
|
||||
message.gsub!(/( |^)http:\/\/www\.youtube\.com\/watch[^ ]*v=([A-Za-z0-9_]+)(&[^ ]*|)/, '\1youtube.com::\2')
|
||||
message.gsub!(/(https|http|ftp):\/\/([^ ]+)/, '<a target="_blank" href="\1://\2">\2</a>')
|
||||
|
||||
while youtube = message.match(/youtube\.com::([A-Za-z0-9_]+)/)
|
||||
videoid = youtube[1]
|
||||
message.gsub!('youtube.com::'+videoid, '<a onclick="openVideo(\'youtube.com\', \'' + videoid + '\', this)" href="#video">Youtube: ' + youtube_title(videoid) + '</a>')
|
||||
end
|
||||
return message
|
||||
end
|
||||
|
||||
def youtube_title(id)
|
||||
unless @@youtube_title_cache[id] == 'no-title'
|
||||
return @@youtube_title_cache[id]
|
||||
end
|
||||
|
||||
ret = 'Unknown Video Title' #TODO add translation
|
||||
http = Net::HTTP.new('gdata.youtube.com', 80)
|
||||
path = '/feeds/api/videos/'+id+'?v=2'
|
||||
resp, data = http.get(path, nil)
|
||||
title = data.match(/<title>(.*)<\/title>/)
|
||||
unless title.nil?
|
||||
ret = title.to_s[7..-9]
|
||||
end
|
||||
|
||||
@@youtube_title_cache[id] = ret;
|
||||
return ret
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@
|
|||
= render "shared/reshare", :post => post, :current_user => current_user
|
||||
= link_to t('.delete'), status_message_path(post), :confirm => t('.are_you_sure'), :method => :delete, :remote => true, :class => "delete"
|
||||
|
||||
= make_links(post.message)
|
||||
= markdownify(post.message)
|
||||
|
||||
.info
|
||||
%span.time= link_to(how_long_ago(post), object_path(post))
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
.span-14.append-1.last
|
||||
%h1.show_text
|
||||
= make_links(@status_message.message)
|
||||
= markdownify(@status_message.message)
|
||||
|
||||
= how_long_ago(@status_message)
|
||||
|
||||
|
|
|
|||
|
|
@ -18,4 +18,155 @@ describe ApplicationHelper do
|
|||
person_url(@user).should == "/users/#{@user.id}"
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
it "should recognize youtube links" do
|
||||
proto="http"
|
||||
videoid = "0x__dDWdf23"
|
||||
url="www.youtube.com/watch?v="+videoid+"&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
|
||||
title = "UP & down & UP & down &"
|
||||
mock_http = mock("http")
|
||||
Net::HTTP.stub!(:new).with('gdata.youtube.com', 80).and_return(mock_http)
|
||||
mock_http.should_receive(:get).with('/feeds/api/videos/'+videoid+'?v=2', nil).and_return([nil, 'Foobar <title>'+title+'</title> hallo welt <asd><dasdd><a>dsd</a>'])
|
||||
res = markdownify(proto+'://'+url)
|
||||
res.should == "<a onclick=\"openVideo('youtube.com', '"+videoid+"', this)\" href=\"#video\">Youtube: "+title+"</a>"
|
||||
end
|
||||
|
||||
it "should recognize a bunch of different links" 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"
|
||||
mock_http = mock("http")
|
||||
Net::HTTP.stub!(:new).with('gdata.youtube.com', 80).and_return(mock_http)
|
||||
mock_http.should_receive(:get).with('/feeds/api/videos/foobar?v=2', nil).and_return([nil, 'Foobar <title>F 007 - the bar is not enough</title> hallo welt <asd><dasdd><a>dsd</a>'])
|
||||
mock_http.should_receive(:get).with('/feeds/api/videos/BARFOO?v=2', nil).and_return([nil, 'Foobar <title>BAR is the new FOO</title> hallo welt <asd><dasdd><a>dsd</a>'])
|
||||
mock_http.should_receive(:get).with('/feeds/api/videos/rickrolld?v=2', nil).and_return([nil, 'Foobar <title>Never gonne give you up</title> hallo welt <asd><dasdd><a>dsd</a>'])
|
||||
res = markdownify(message)
|
||||
res.should == "http:// Hello World, this is for <a target=\"_blank\" href=\"http://www.joindiaspora.com\">www.joindiaspora.com</a> and not for <a target=\"_blank\" href=\"http://www.google.com\">www.google.com</a> though their Youtube service is neat, take <a onclick=\"openVideo('youtube.com', 'foobar', this)\" href=\"#video\">Youtube: F 007 - the bar is not enough</a> or <a onclick=\"openVideo('youtube.com', 'BARFOO', this)\" href=\"#video\">Youtube: BAR is the new FOO</a> It is a good idea we finally have youtube, so enjoy this video <a onclick=\"openVideo('youtube.com', 'rickrolld', this)\" href=\"#video\">Youtube: Never gonne give you up</a>"
|
||||
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 "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
|
||||
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="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> *</em>some text <em>some text</em> _some text<em> _</em>some text"
|
||||
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 "should allow to 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 "should allow to render only youtube autolinks" do
|
||||
res = markdownify(@message, :autolinks => false, :emphasis => false, :links => false)
|
||||
res.should == "http://url.com www.url.com <a onclick=\"openVideo('youtube.com', 'BARFOO', this)\" href=\"#video\">Youtube: BAR is the new FOO</a> *emphasis* __emphasis__ [link](www.url.com) [link](url.com "title")"
|
||||
end
|
||||
|
||||
it "should allow to 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 "should allo to 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
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,72 +0,0 @@
|
|||
# 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 StatusMessagesHelper do
|
||||
it "should not allow basic XSS/HTML" do
|
||||
make_links("<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"
|
||||
make_links(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?~()!*/"
|
||||
make_links(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"
|
||||
make_links(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"
|
||||
make_links(proto+"://"+url).should == "<a target=\"_blank\" href=\""+proto+"://"+url+"\">"+url+"</a>"
|
||||
end
|
||||
|
||||
it "should recognize youtube links" do
|
||||
proto="http"
|
||||
videoid = "0x__dDWdf23"
|
||||
url="www.youtube.com/watch?v="+videoid+"&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1"
|
||||
title = "UP & down & UP & down &"
|
||||
mock_http = mock("http")
|
||||
Net::HTTP.stub!(:new).with('gdata.youtube.com', 80).and_return(mock_http)
|
||||
mock_http.should_receive(:get).with('/feeds/api/videos/'+videoid+'?v=2', nil).and_return([nil, 'Foobar <title>'+title+'</title> hallo welt <asd><dasdd><a>dsd</a>'])
|
||||
res = make_links(proto+'://'+url)
|
||||
res.should == "<a onclick=\"openVideo('youtube.com', '"+videoid+"', this)\" href=\"#video\">Youtube: "+title+"</a>"
|
||||
end
|
||||
|
||||
it "should recognize a bunch of different links" 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"
|
||||
mock_http = mock("http")
|
||||
Net::HTTP.stub!(:new).with('gdata.youtube.com', 80).and_return(mock_http)
|
||||
mock_http.should_receive(:get).with('/feeds/api/videos/foobar?v=2', nil).and_return([nil, 'Foobar <title>F 007 - the bar is not enough</title> hallo welt <asd><dasdd><a>dsd</a>'])
|
||||
mock_http.should_receive(:get).with('/feeds/api/videos/BARFOO?v=2', nil).and_return([nil, 'Foobar <title>BAR is the new FOO</title> hallo welt <asd><dasdd><a>dsd</a>'])
|
||||
mock_http.should_receive(:get).with('/feeds/api/videos/rickrolld?v=2', nil).and_return([nil, 'Foobar <title>Never gonne give you up</title> hallo welt <asd><dasdd><a>dsd</a>'])
|
||||
res = make_links(message)
|
||||
res.should == "http:// Hello World, this is for <a target=\"_blank\" href=\"http://www.joindiaspora.com\">www.joindiaspora.com</a> and not for <a target=\"_blank\" href=\"http://www.google.com\">www.google.com</a> though their Youtube service is neat, take <a onclick=\"openVideo('youtube.com', 'foobar', this)\" href=\"#video\">Youtube: F 007 - the bar is not enough</a> or <a onclick=\"openVideo('youtube.com', 'BARFOO', this)\" href=\"#video\">Youtube: BAR is the new FOO</a> It is a good idea we finally have youtube, so enjoy this video <a onclick=\"openVideo('youtube.com', 'rickrolld', this)\" href=\"#video\">Youtube: Never gonne give you up</a>"
|
||||
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
|
||||
make_links(proto+"://"+url).should == "<a target=\"_blank\" href=\""+proto+"://"+url+"\">"+url+"</a>"
|
||||
end
|
||||
|
||||
it "should recognize www links" do
|
||||
url="www.joindiaspora.com"
|
||||
make_links(url).should == "<a target=\"_blank\" href=\"http://"+url+"\">"+url+"</a>"
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
Loading…
Reference in a new issue