From 112088ace374a260e535282b14962308cc931fae Mon Sep 17 00:00:00 2001 From: "livefromthemoon@gmail.com" Date: Sat, 30 Oct 2010 17:21:16 +0200 Subject: [PATCH 1/3] add support for markdown emphasis in status messages --- app/helpers/status_messages_helper.rb | 34 +++++++++++++++--- spec/helpers/status_messages_helper_spec.rb | 40 +++++++++++++++++++++ 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/app/helpers/status_messages_helper.rb b/app/helpers/status_messages_helper.rb index 2e010715a..39ba258d3 100644 --- a/app/helpers/status_messages_helper.rb +++ b/app/helpers/status_messages_helper.rb @@ -18,14 +18,38 @@ module StatusMessagesHelper # 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):\/\/([^ ]+)/, '\2') - - while youtube = message.match(/youtube\.com::([A-Za-z0-9_]+)/) + + message.gsub!(/( |^)(www\.[^ ]+\.[^ ])/) do |m| + res = "#{$1}http://#{$2}" + res.gsub!(/^(\*|_)$/) { |m| "\\#{$1}" } + res + end + message.gsub!(/( |^)http:\/\/www\.youtube\.com\/watch[^ ]*v=([A-Za-z0-9_]+)(&[^ ]*|)/) do |m| + res = "#{$1}youtube.com::#{$2}" + res.gsub!(/(\*|_)/) { |m| "\\#{$1}" } + res + end + message.gsub!(/(https|http|ftp):\/\/([^ ]+)/) do |m| + res = %{#{$2}} + res.gsub!(/(\*|_)/) { |m| "\\#{$1}" } + res + end + + # markdown + message.gsub!(/([^\\]|^)\*\*(([^*]|([^*]\*[^*]))*[^\\])\*\*/, '\1\2') + message.gsub!(/([^\\]|^)__(([^_]|([^_]_[^_]))*[^\\])__/, '\1\2') + message.gsub!(/([^\\]|^)\*([^*]*[^\\])\*/, '\1\2') + message.gsub!(/([^\\]|^)_([^_]*[^\\])_/, '\1\2') + message.gsub!(/([^\\]|^)\*/, '\1') + message.gsub!(/([^\\]|^)_/, '\1') + message.gsub!("\\*", "*") + message.gsub!("\\_", "_") + + while youtube = message.match(/youtube\.com::([A-Za-z0-9_\\]+)/) videoid = youtube[1] message.gsub!('youtube.com::'+videoid, 'Youtube: ' + youtube_title(videoid) + '') end + return message end diff --git a/spec/helpers/status_messages_helper_spec.rb b/spec/helpers/status_messages_helper_spec.rb index 0aee3826a..9f89a1eb5 100644 --- a/spec/helpers/status_messages_helper_spec.rb +++ b/spec/helpers/status_messages_helper_spec.rb @@ -68,5 +68,45 @@ describe StatusMessagesHelper do make_links(url).should == ""+url+"" end + describe "markdown" do + describe "weak emphasis" do + it "should be recognized (1/2)" do + message = "*some text* some text *some text* some text" + make_links(message).should == "some text some text some text some text" + end + + it "should be recognized (2/2)" do + message = "_some text_ some text _some text_ some text" + make_links(message).should == "some text some text some text some text" + end + end + + describe "strong emphasis" do + it "should be recognized (1/2)" do + message = "**some text** some text **some text** some text" + make_links(message).should == "some text some text some text some text" + end + + it "should be recognized (2/2)" do + message = "__some text__ some text __some text__ some text" + make_links(message).should == "some text some text some text some text" + end + end + + describe "imbricated weak and strong emphasis" do + it "should be rendered correctly" do + message = "__this is _some_ text__" + make_links(message).should == "this is some text" + message = "*this is **some** text*" + make_links(message).should == "this is some text" + end + end + + it "should allow escaping" do + message = '*some text* \\*some text* \\**some text* _some text_ \\_some text_ \\__some text_' + make_links(message).should == "some text *some text *some text some text _some text _some text" + end + end + end From ebf9dd464f47593013e74a2363c2db7a5d3d0696 Mon Sep 17 00:00:00 2001 From: "livefromthemoon@gmail.com" Date: Sat, 30 Oct 2010 18:02:43 +0200 Subject: [PATCH 2/3] Better support for imbricated markdown emphasis --- app/helpers/status_messages_helper.rb | 4 ++-- spec/helpers/status_messages_helper_spec.rb | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/helpers/status_messages_helper.rb b/app/helpers/status_messages_helper.rb index 39ba258d3..80a20d00b 100644 --- a/app/helpers/status_messages_helper.rb +++ b/app/helpers/status_messages_helper.rb @@ -36,8 +36,8 @@ module StatusMessagesHelper end # markdown - message.gsub!(/([^\\]|^)\*\*(([^*]|([^*]\*[^*]))*[^\\])\*\*/, '\1\2') - message.gsub!(/([^\\]|^)__(([^_]|([^_]_[^_]))*[^\\])__/, '\1\2') + message.gsub!(/([^\\]|^)\*\*(([^*]|([^*]\*[^*]))*[^*\\])\*\*/, '\1\2') + message.gsub!(/([^\\]|^)__(([^_]|([^_]_[^_]))*[^_\\])__/, '\1\2') message.gsub!(/([^\\]|^)\*([^*]*[^\\])\*/, '\1\2') message.gsub!(/([^\\]|^)_([^_]*[^\\])_/, '\1\2') message.gsub!(/([^\\]|^)\*/, '\1') diff --git a/spec/helpers/status_messages_helper_spec.rb b/spec/helpers/status_messages_helper_spec.rb index 9f89a1eb5..0f699781b 100644 --- a/spec/helpers/status_messages_helper_spec.rb +++ b/spec/helpers/status_messages_helper_spec.rb @@ -99,6 +99,8 @@ describe StatusMessagesHelper do make_links(message).should == "this is some text" message = "*this is **some** text*" make_links(message).should == "this is some text" + message = "___some text___" + make_links(message).should == "some text" end end From bcb17aae166f0cdf7380cbe4c18cecf000ace594 Mon Sep 17 00:00:00 2001 From: "livefromthemoon@gmail.com" Date: Sat, 30 Oct 2010 19:24:46 +0200 Subject: [PATCH 3/3] Add support for markdown links --- app/helpers/status_messages_helper.rb | 16 +++++++++++----- spec/helpers/status_messages_helper_spec.rb | 12 ++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/app/helpers/status_messages_helper.rb b/app/helpers/status_messages_helper.rb index 80a20d00b..b999f1a49 100644 --- a/app/helpers/status_messages_helper.rb +++ b/app/helpers/status_messages_helper.rb @@ -19,6 +19,9 @@ module StatusMessagesHelper # next line is important due to XSS! (h is rail's make_html_safe-function) message = h(message).html_safe + message.gsub!(/\[([^\[]+)\]\(([^ ]+) \"(([^&]|(&[^q])|(&q[^u])|(&qu[^o])|(&quo[^t])|("[^;]))+)\"\)/, '\1') + message.gsub!(/\[([^\[]+)\]\(([^ ]+)\)/, '\1') + message.gsub!(/( |^)(www\.[^ ]+\.[^ ])/) do |m| res = "#{$1}http://#{$2}" res.gsub!(/^(\*|_)$/) { |m| "\\#{$1}" } @@ -29,13 +32,16 @@ module StatusMessagesHelper res.gsub!(/(\*|_)/) { |m| "\\#{$1}" } res end - message.gsub!(/(https|http|ftp):\/\/([^ ]+)/) do |m| - res = %{#{$2}} - res.gsub!(/(\*|_)/) { |m| "\\#{$1}" } - res + message.gsub!(/(#{$3}} + res.gsub!(/(\*|_)/) { |m| "\\#{$1}" } + res + end end - # markdown message.gsub!(/([^\\]|^)\*\*(([^*]|([^*]\*[^*]))*[^*\\])\*\*/, '\1\2') message.gsub!(/([^\\]|^)__(([^_]|([^_]_[^_]))*[^_\\])__/, '\1\2') message.gsub!(/([^\\]|^)\*([^*]*[^\\])\*/, '\1\2') diff --git a/spec/helpers/status_messages_helper_spec.rb b/spec/helpers/status_messages_helper_spec.rb index 0f699781b..61ae88be0 100644 --- a/spec/helpers/status_messages_helper_spec.rb +++ b/spec/helpers/status_messages_helper_spec.rb @@ -104,6 +104,18 @@ describe StatusMessagesHelper do end end + describe "links" do + it "should be recognized without title attribute" do + message = "[link text](http://someurl.com) [link text](http://someurl.com)" + make_links(message).should == 'link text link text' + end + + it "should be recognized with title attribute" do + message = '[link text](http://someurl.com "some title") [link text](http://someurl.com "some title")' + make_links(message).should == 'link text link text' + end + end + it "should allow escaping" do message = '*some text* \\*some text* \\**some text* _some text_ \\_some text_ \\__some text_' make_links(message).should == "some text *some text *some text some text _some text _some text"