fix poetry mode, due to a mistake I made previously. also fixed a 500 where youtube titles do the unexpected

This commit is contained in:
Maxwell Salzberg 2011-09-01 12:03:07 -07:00
parent e6d1617268
commit 48cdf7c79f
4 changed files with 36 additions and 33 deletions

View file

@ -34,7 +34,7 @@ module MarkdownifyHelper
renderer = Diaspora::Markdownify::HTML.new(render_options) renderer = Diaspora::Markdownify::HTML.new(render_options)
markdown = Redcarpet::Markdown.new(renderer, markdown_options) markdown = Redcarpet::Markdown.new(renderer, markdown_options)
message = markdown.render(message) message = markdown.render(message).html_safe
if target.respond_to?(:format_mentions) if target.respond_to?(:format_mentions)
message = target.format_mentions(message) message = target.format_mentions(message)

View file

@ -100,6 +100,9 @@ module Diaspora
end end
def paragraph(text) def paragraph(text)
#hax again... why is markdownify passing us nil?
text ||=''
if @newlines if @newlines
br = linebreak br = linebreak
@ -109,7 +112,7 @@ module Diaspora
x =~ /\n{2}/ ? x : (x = x.strip; x << br) x =~ /\n{2}/ ? x : (x = x.strip; x << br)
end end
end end
return text return "<p>#{text}</p>"
end end
def preprocess(full_document) def preprocess(full_document)

View file

@ -41,7 +41,7 @@ module Diaspora
def self.format_tags(text, opts={}) def self.format_tags(text, opts={})
return text if opts[:plain_text] return text if opts[:plain_text]
text = ERB::Util.h(text) unless opts[:no_escape] text = ERB::Util.h(text) unless opts[:no_escape]
regex = /(^|\s)#(#{VALID_TAG_BODY})/ regex = /(^|\s|>)#(#{VALID_TAG_BODY})/
form_message = text.to_str.gsub(regex) do |matched_string| form_message = text.to_str.gsub(regex) do |matched_string|
"#{$~[1]}<a href=\"/tags/#{$~[2]}\" class=\"tag\">##{$~[2]}</a>" "#{$~[1]}<a href=\"/tags/#{$~[2]}\" class=\"tag\">##{$~[2]}</a>"
end end

View file

@ -21,35 +21,35 @@ describe MarkdownifyHelper do
describe "autolinks" do describe "autolinks" do
it "should not allow basic XSS/HTML" do it "should not allow basic XSS/HTML" do
markdownify("<script>alert('XSS is evil')</script>").should == "&lt;script&gt;alert('XSS is evil')&lt;/script&gt;" markdownify("<script>alert('XSS is evil')</script>").should == "<p>&lt;script&gt;alert('XSS is evil')&lt;/script&gt;</p>"
end end
it "should recognize basic http links (1/3)" do it "should recognize basic http links (1/3)" do
proto="http" proto="http"
url="bugs.joindiaspora.com/issues/332" url="bugs.joindiaspora.com/issues/332"
full_url = "#{proto}://#{url}" full_url = "#{proto}://#{url}"
markdownify(full_url).should == %Q{<a target="_blank" href="#{full_url}">#{url}</a>} markdownify(full_url).should == %Q{<p><a target="_blank" href="#{full_url}">#{url}</a></p>}
end end
it "should recognize basic http links (2/3)" do it "should recognize basic http links (2/3)" do
proto="http" proto="http"
url="webmail.example.com?~()!*/" url="webmail.example.com?~()!*/"
full_url = "#{proto}://#{url}" full_url = "#{proto}://#{url}"
markdownify(full_url).should == %Q{<a target="_blank" href="#{full_url}">#{url}</a>} markdownify(full_url).should == %Q{<p><a target="_blank" href="#{full_url}">#{url}</a></p>}
end end
it "should recognize basic http links (3/3)" do it "should recognize basic http links (3/3)" do
proto="http" proto="http"
url="127.0.0.1:3000/users/sign_in" url="127.0.0.1:3000/users/sign_in"
full_url = "#{proto}://#{url}" full_url = "#{proto}://#{url}"
markdownify(full_url).should == %Q{<a target="_blank" href="#{full_url}">#{url}</a>} markdownify(full_url).should == %Q{<p><a target="_blank" href="#{full_url}">#{url}</a></p>}
end end
it "should recognize secure https links" do it "should recognize secure https links" do
proto="https" proto="https"
url="127.0.0.1:3000/users/sign_in" url="127.0.0.1:3000/users/sign_in"
full_url = "#{proto}://#{url}" full_url = "#{proto}://#{url}"
markdownify(full_url).should == %Q{<a target="_blank" href="#{full_url}">#{url}</a>} markdownify(full_url).should == %Q{<p><a target="_blank" href="#{full_url}">#{url}</a></p>}
end end
it "doesn't muck up code text" do it "doesn't muck up code text" do
@ -171,76 +171,76 @@ describe MarkdownifyHelper do
proto="ftp" proto="ftp"
url="ftp.uni-kl.de/CCC/26C3/mp4/26c3-3540-en-a_hackers_utopia.mp4" 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 # I did not watch that one, but the title sounds nice :P
markdownify(proto+"://"+url).should == "<a target=\"_blank\" href=\""+proto+"://"+url+"\">"+url+"</a>" markdownify(proto+"://"+url).should == "<p><a target=\"_blank\" href=\""+proto+"://"+url+"\">"+url+"</a></p>"
end end
it "should recognize www links" do it "should recognize www links" do
url="www.joindiaspora.com" url="www.joindiaspora.com"
markdownify(url).should == %Q{<a target="_blank" href="http://#{url}">#{url}</a>} markdownify(url).should == %Q{<p><a target="_blank" href="http://#{url}">#{url}</a></p>}
end end
end end
describe "specialchars" do describe "specialchars" do
it "replaces &lt;3 with &hearts;" do it "replaces &lt;3 with &hearts;" do
message = "i <3 you" message = "i <3 you"
markdownify(message).should == "i &hearts; you" markdownify(message).should == "<p>i &hearts; you</p>"
end end
it "replaces various things with (their) HTML entities" do it "replaces various things with (their) HTML entities" do
message = "... <-> -> <- (tm) (r) (c)" message = "... <-> -> <- (tm) (r) (c)"
markdownify(message).should == "&hellip; &#8596; &rarr; &larr; &trade; &reg; &copy;" markdownify(message).should == "<p>&hellip; &#8596; &rarr; &larr; &trade; &reg; &copy;</p>"
end end
it "skips doing it if you say so" do it "skips doing it if you say so" do
message = "... -> <-" message = "... -> <-"
markdownify(message, :specialchars => false).should == "... -&gt; &lt;-" markdownify(message, :specialchars => false).should == "<p>... -&gt; &lt;-</p>"
end end
end end
describe "weak emphasis" do describe "weak emphasis" do
it "should be recognized (1/2)" do it "should be recognized (1/2)" do
message = "*some text* some text *some text* some text" message = "*some text* some text *some text* some text"
markdownify(message).should == "<em>some text</em> some text <em>some text</em> some text" markdownify(message).should == "<p><em>some text</em> some text <em>some text</em> some text</p>"
end end
it "should be recognized (2/2)" do it "should be recognized (2/2)" do
message = "_some text_ some text _some text_ some text" message = "_some text_ some text _some text_ some text"
markdownify(message).should == "<em>some text</em> some text <em>some text</em> some text" markdownify(message).should == "<p><em>some text</em> some text <em>some text</em> some text</p>"
end end
end end
describe "strong emphasis" do describe "strong emphasis" do
it "should be recognized (1/2)" do it "should be recognized (1/2)" do
message = "**some text** some text **some text** some text" message = "**some text** some text **some text** some text"
markdownify(message).should == "<strong>some text</strong> some text <strong>some text</strong> some text" markdownify(message).should == "<p><strong>some text</strong> some text <strong>some text</strong> some text</p>"
end end
it "should be recognized (2/2)" do it "should be recognized (2/2)" do
message = "__some text__ some text __some text__ some text" message = "__some text__ some text __some text__ some text"
markdownify(message).should == "<strong>some text</strong> some text <strong>some text</strong> some text" markdownify(message).should == "<p><strong>some text</strong> some text <strong>some text</strong> some text</p>"
end end
end end
describe "nested weak and strong emphasis" do describe "nested weak and strong emphasis" do
it "should be rendered correctly" do it "should be rendered correctly" do
message = "__this is _some_ text__" message = "__this is _some_ text__"
markdownify(message).should == "<strong>this is <em>some</em> text</strong>" markdownify(message).should == "<p><strong>this is <em>some</em> text</strong></p>"
message = "*this is **some** text*" message = "*this is **some** text*"
markdownify(message).should == "<em>this is <strong>some</strong> text</em>" markdownify(message).should == "<p><em>this is <strong>some</strong> text</em></p>"
message = "___some text___" message = "___some text___"
markdownify(message).should == "<em><strong>some text</strong></em>" markdownify(message).should == "<p><em><strong>some text</strong></em></p>"
end end
end end
describe "links" do describe "links" do
it "should be recognized without title attribute" do it "should be recognized without title attribute" do
message = "[link text](http://someurl.com) [link text](http://someurl.com)" 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>' markdownify(message).should == '<p><a target="_blank" href="http://someurl.com">link text</a> <a target="_blank" href="http://someurl.com">link text</a></p>'
end end
it "should be recognized with title attribute" do it "should be recognized with title attribute" do
message = '[link text](http://someurl.com "some title") [link text](http://someurl.com "some title")' 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>' markdownify(message).should == '<p><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></p>'
end end
it "should have a robust link parsing" do it "should have a robust link parsing" do
@ -249,18 +249,18 @@ describe MarkdownifyHelper do
link.should =~ %r{href="http://en.wikipedia.org/wiki/Text_%28literary_theory%29"} link.should =~ %r{href="http://en.wikipedia.org/wiki/Text_%28literary_theory%29"}
message = "[ links]( google.com)" message = "[ links]( google.com)"
markdownify(message).should == %Q{<a target="_blank" href="http://google.com">links</a>} markdownify(message).should == %Q{<p><a target="_blank" href="http://google.com">links</a></p>}
message = "[_http_](http://google.com/search?q=with_multiple__underscores*and**asterisks )" message = "[_http_](http://google.com/search?q=with_multiple__underscores*and**asterisks )"
markdownify(message).should == %Q{<a target="_blank" href="http://google.com/search?q=with_multiple__underscores*and**asterisks"><em>http</em></a>} markdownify(message).should == %Q{<p><a target="_blank" href="http://google.com/search?q=with_multiple__underscores*and**asterisks"><em>http</em></a></p>}
message = %{[___FTP___]( ftp://ftp.uni-kl.de/CCC/26C3/mp4/26c3-3540-en-a_hackers_utopia.mp4 'File Transfer Protocol')} message = %{[___FTP___]( ftp://ftp.uni-kl.de/CCC/26C3/mp4/26c3-3540-en-a_hackers_utopia.mp4 'File Transfer Protocol')}
markdownify(message).should == %{<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>} markdownify(message).should == %{<p><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></p>}
message = %{[**any protocol**](foo://bar.example.org/yes_it*makes*no_sense)} message = %{[**any protocol**](foo://bar.example.org/yes_it*makes*no_sense)}
markdownify(message).should == %{<a target="_blank" href="foo://bar.example.org/yes_it*makes*no_sense"><strong>any protocol</strong></a>} markdownify(message).should == %{<p><a target="_blank" href="foo://bar.example.org/yes_it*makes*no_sense"><strong>any protocol</strong></a></p>}
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)" 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_%28literary_theory%29"><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>' markdownify(message).should == '<p>This <a target="_blank" href="http://en.wikipedia.org/wiki/Text_%28literary_theory%29"><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></p>'
end end
end end
@ -268,32 +268,32 @@ describe MarkdownifyHelper do
describe "nested emphasis and links tags" do describe "nested emphasis and links tags" do
it "should be rendered correctly" do it "should be rendered correctly" do
message = '[**some *link* text**](someurl.com "some title")' 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>' markdownify(message).should == '<p><a target="_blank" href="http://someurl.com" title="some title"><strong>some <em>link</em> text</strong></a></p>'
end end
end end
it "should allow escaping" do it "should allow escaping" do
message = '*some text* \*some text* \**some text* _some text_ \_some text_ \__some text_' 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>" markdownify(message).should == "<p><em>some text</em> *some text* *<em>some text</em> <em>some text</em> _some text_ _<em>some text</em></p>"
end end
describe "newlines" do describe "newlines" do
it 'skips inserting newlines if you pass the newlines option' do it 'skips inserting newlines if you pass the newlines option' do
message = "These\nare\n\some\nnew\lines" message = "These\nare\n\some\nnew\lines"
res = markdownify(message, :newlines => false) res = markdownify(message, :newlines => false)
res.should == "#{message}" res.should == "<p>#{message}</p>"
end 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)
res.should == "These<br /\>are<br /\>some<br /\>new<br /\>lines" res.should == "<p>These<br /\>are<br /\>some<br /\>new<br /\>lines</p>"
end end
it 'should render newlines and basic http links correctly' do 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" message = "Some text, then a line break and a link\nhttp://joindiaspora.com\nsome more text"
res = markdownify(message) 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' res.should == '<p>Some text, then a line break and a link<br /><a target="_blank" href="http://joindiaspora.com">joindiaspora.com</a><br />some more text</p>'
end end
end end
@ -331,7 +331,7 @@ describe MarkdownifyHelper do
context 'performance' do context 'performance' do
before 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 " @message = "</p>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</p>"
end end
it 'is sub millisecond' do it 'is sub millisecond' do