From 2303686e310a5d1d23255ed82d10b63fa5c3ecc7 Mon Sep 17 00:00:00 2001 From: Brian Wisti Date: Tue, 16 Aug 2011 10:28:00 -0700 Subject: [PATCH 01/10] basic support for Redcarpet Markdown --- Gemfile | 1 + Gemfile.lock | 7 + app/helpers/markdownify_helper.rb | 137 ++---------------- features/follows_tags.feature | 2 +- lib/diaspora/markdownify.rb | 169 +++++++++++++++++++++++ public/stylesheets/sass/application.sass | 3 + spec/helpers/markdownify_helper_spec.rb | 90 ++++++++---- 7 files changed, 257 insertions(+), 152 deletions(-) create mode 100644 lib/diaspora/markdownify.rb diff --git a/Gemfile b/Gemfile index 3a8199dd7..371bd9fa3 100644 --- a/Gemfile +++ b/Gemfile @@ -50,6 +50,7 @@ gem 'json', '1.4.6' gem 'http_accept_language', :git => 'git://github.com/iain/http_accept_language.git', :ref => '0b78aa7849fc90cf9e12' gem 'thin', '1.2.11', :require => false +gem 'redcarpet', :git => 'git://github.com/tanoku/redcarpet' #Websocket gem 'em-websocket', :git => 'git://github.com/igrigorik/em-websocket', :ref => 'e278f5a1c4db60be7485' diff --git a/Gemfile.lock b/Gemfile.lock index 15d2b5f03..119db8033 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -56,6 +56,12 @@ GIT addressable (>= 2.1.1) eventmachine (>= 0.12.9) +GIT + remote: git://github.com/tanoku/redcarpet + revision: 6b43f042305eb63620e3771a977f45e24ea7b576 + specs: + redcarpet (2.0.0b3) + GIT remote: git://github.com/zhitomirskiyi/ruby-jwt.git revision: fa7f46b5ac3653e30cf60abc78de9ffb3319dc0c @@ -500,6 +506,7 @@ DEPENDENCIES rails (= 3.0.9) rails-i18n rcov + redcarpet! resque (= 1.10.0) resque-ensure-connected rest-client (= 1.6.1) diff --git a/app/helpers/markdownify_helper.rb b/app/helpers/markdownify_helper.rb index edf3c7f74..ef38d352d 100644 --- a/app/helpers/markdownify_helper.rb +++ b/app/helpers/markdownify_helper.rb @@ -2,134 +2,27 @@ # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. +require 'lib/diaspora/markdownify' + module MarkdownifyHelper - def markdownify(message, options={}) - message = h(message).to_str + def markdownify(message, render_options={}) + markdown_options = { + :autolink => true, + } - options[:newlines] = true if !options.has_key?(:newlines) - options[:specialchars] = true if !options.has_key?(:specialchars) + render_options[:filter_html] = true - 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_specialchars(message) if options[:specialchars] - message = process_newlines(message) if options[:newlines] - - message.html_safe + renderer = Diaspora::Markdownify::HTML.new(render_options) + markdown = Redcarpet::Markdown.new(renderer, markdown_options) + message = markdown.render(message) + return message.html_safe end def process_newlines(message) - message.gsub!(/\n+/, '
') - message - end - - def process_links(message) - message.gsub!(/\[\s*([^\[]+?)\s*\]\(\s*([^ ]+\s*) \"(([^&]|(&[^q])|(&q[^u])|(&qu[^o])|(&quo[^t])|("[^;]))+)\"\s*\)/) do |m| - escape = "\\" - link = $1 - url = $2 - title = $3 - url.gsub!("_", "\\_") - url.gsub!("*", "\\*") - protocol = (url =~ /^\w+:\/\//) ? '' :'http://' - res = "#{link}" - res + # in very clear cases, let newlines become
tags + # Grabbed from Github flavored Markdown + message.gsub(/^[\w\<][^\n]*\n+/) do |x| + x =~ /\n{2}/ ? x : (x.strip!; x << " \n") end - - message.gsub!(/\[\s*([^\[]+?)\s*\]\(\s*([^ ]+)\s*\)/) do |m| - escape = "\\" - link = $1 - url = $2 - url.gsub!("_", "\\_") - url.gsub!("*", "\\*") - protocol = (url =~ /^\w+:\/\//) ? '' :'http://' - res = "#{link}" - 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 - ' Youtube: ' + title + '' - end - processed_message - end - - def process_autolinks(message) - message.gsub!(/( |^)(www\.[^\s]+\.[^\s])/, '\1http://\2') - message.gsub!(/(#{captures[2]}} - 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, '\2') - message.gsub!(/(\*\*|__)(.+?)\1/m, '\2') - message.gsub!(/(\*|_)(.+?)\1/m, '\2') - 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 - ' Vimeo: ' + title + '' - end - processed_message - end - - def process_specialchars(message) - map = [ - ["<3", "♥"], - ["<->", "↔"], - ["->", "→"], - ["<-", "←"], - ["...", "…"], - ["(tm)", "™"], - ["(r)", "®"], - ["(c)", "©"] - ] - - map.each do |mapping| - message.gsub!(mapping[0], mapping[1]) - end - message end end diff --git a/features/follows_tags.feature b/features/follows_tags.feature index d6b0afda1..b74fb8fb1 100644 --- a/features/follows_tags.feature +++ b/features/follows_tags.feature @@ -25,7 +25,7 @@ Feature: posting Scenario: see a tag that I am following When I go to the home page And I follow "#boss" - Then I should see "I am da #boss" + Then I should see "I am da #boss" within "body" Scenario: can stop following a particular tag When I hover over the ".button.tag_following" diff --git a/lib/diaspora/markdownify.rb b/lib/diaspora/markdownify.rb new file mode 100644 index 000000000..734e34f01 --- /dev/null +++ b/lib/diaspora/markdownify.rb @@ -0,0 +1,169 @@ +require 'uri' + +module Diaspora + module Markdownify + class HTML < Redcarpet::Render::HTML + attr_accessor :newlines, :specialchars, :youtube_maps, :vimeo_maps + + def initialize(options={}) + super + + @expand_tags = options.fetch(:expand_tabs, true) + @newlines = options.fetch(:newlines, true) + @specialchars = options.fetch(:specialchars, true) + @youtube_maps = options.fetch(:youtube_maps, {}) + @vimeo_maps = options.fetch(:vimeo_maps, {}) + end + + def autolink(link, type) + autolink_youtube(link) || autolink_vimeo(link) || autolink_simple(link) + end + + def autolink_simple(link) + + # If there isn't *some* protocol, assume http + if link !~ %r{^\w+://} + link = "http://#{link}" + end + + content = link.gsub(%r{^\w+://}, '') + + %Q{#{content}} + end + + def autolink_vimeo(link) + regex = %r{https?://(?:w{3}\.)?vimeo.com/(\d{6,})/?} + if link =~ regex + video_id = $1 + if @vimeo_maps[video_id] + title = h(CGI::unescape(@vimeo_maps[video_id])) + else + title = I18n.t 'application.helper.video_title.unknown' + end + return ' Vimeo: ' + title + '' + end + end + + def autolink_youtube(link) + if link =~ YoutubeTitles::YOUTUBE_ID_REGEX + video_id = $1 + anchor = $2 || '' + + if @youtube_maps[video_id] + title = h(CGI::unescape(@youtube_maps[video_id])) + else + title = I18n.t 'application.helper.video_title.unknown' + end + return ' Youtube: ' + title + '' + end + end + + def double_emphasis(text) + "#{text}" + end + + def linebreak() + "
" + end + + def link(link, title, content) + return autolink(link, 'url') if link == content + + if link =~ Regexp.new(Regexp.escape(content)) + return autolink(link, 'url') + end + + if link !~ %r{^\w+://} + link = "http://#{link}" + end + + tag = if title and content + %Q{#{content}} + elsif content + %Q{#{content}} + else + autolink(link, 'url') + end + return tag + end + + def paragraph(text) + if @expand_tags + text = Diaspora::Taggable.format_tags(text, :no_escape => true) + end + + return "

#{text}

" + end + + + def preprocess(full_document) + if @specialchars + full_document = specialchars(full_document) + end + + our_unsafe_chars = '()' + full_document = full_document.gsub(%r{ + \[ \s*? ( [^ \] ]+ ) \s*? \] + (?: + \( \s*? (\S+) \s*? (?: "([^"]+)" )? \) \s*? + ) + }xm) do |m| + content = $1 + link = URI.escape($2, our_unsafe_chars) + title = $3 + + title_chunk = if title + %W{" #{title}"} + else + '' + end + %Q{[#{content}](#{link}#{title_chunk})} + end + + if @newlines + # in very clear cases, let newlines become
tags + # Grabbed from Github flavored Markdown + full_document = full_document.gsub(/^[\w\<][^\n]*\n+/) do |x| + x =~ /\n{2}/ ? x : (x.strip!; x << " \n") + end + end + + return full_document + end + + def single_emphasis(text) + "#{text}" + end + + def specialchars(text) + if @specialchars + map = [ + ["<3", "♥"], + ["<->", "↔"], + ["->", "→"], + ["<-", "←"], + ["\.\.\.", "…"], + ["(tm)", "™"], + ["(r)", "®"], + ["(c)", "©"] + ] + end + + map.each do |mapping| + text = text.gsub(mapping[0], mapping[1]) + end + + return text + end + + + def triple_emphasis(text) + single_emphasis(double_emphasis(text)) + end + + end + end +end diff --git a/public/stylesheets/sass/application.sass b/public/stylesheets/sass/application.sass index 1626b2486..70e4d9602 100644 --- a/public/stylesheets/sass/application.sass +++ b/public/stylesheets/sass/application.sass @@ -38,6 +38,9 @@ h1 ul > li :list-style none +.content ul li + :list-style disc + form :position relative input, diff --git a/spec/helpers/markdownify_helper_spec.rb b/spec/helpers/markdownify_helper_spec.rb index 1e036aa8d..e547ff4df 100644 --- a/spec/helpers/markdownify_helper_spec.rb +++ b/spec/helpers/markdownify_helper_spec.rb @@ -8,31 +8,35 @@ describe MarkdownifyHelper do describe "#markdownify" do describe "autolinks" do it "should not allow basic XSS/HTML" do - markdownify("").should == "<script>alert('XSS is evil')</script>" + markdownify("").should == "

alert('XSS is evil')

" end it "should recognize basic http links (1/3)" do proto="http" url="bugs.joindiaspora.com/issues/332" - markdownify(proto+"://"+url).should == ""+url+"" + full_url = "#{proto}://#{url}" + markdownify(full_url).should == %Q{

#{url}

} end it "should recognize basic http links (2/3)" do proto="http" url="webmail.example.com?~()!*/" - markdownify(proto+"://"+url).should == ""+url+"" + full_url = "#{proto}://#{url}" + markdownify(full_url).should == %Q{

#{url}

} 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 == ""+url+"" + full_url = "#{proto}://#{url}" + markdownify(full_url).should == %Q{

#{url}

} end it "should recognize secure https links" do proto="https" url="127.0.0.1:3000/users/sign_in" - markdownify(proto+"://"+url).should == ""+url+"" + full_url = "#{proto}://#{url}" + markdownify(full_url).should == %Q{

#{url}

} end it "doesn't double parse video links" do @@ -40,8 +44,8 @@ describe MarkdownifyHelper do http://www.youtube.com/watch?v=0x__dDWdf23&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1 http://youtu.be/x_CzD0GBD-4" res = markdownify(message) - res.should =~ /href.+href.+href/ - res.should_not =~ /href.+href.+href.+href/ + res.should =~ /href.+href.+href/m + res.should_not =~ /href.+href.+href.+href/m end describe "video links" do @@ -69,6 +73,12 @@ describe MarkdownifyHelper do res.should =~ /Youtube:/ res.should =~ /data-host="youtube.com"/ res.should =~ /data-video-id="#{video_id}"/ + + url = "www.youtube.com/watch?foo=bar&v=BARFOO-----&whatever=related" + res = markdownify(url) + res.should =~ /Youtube:/ + res.should =~ /data-host="youtube.com"/ + res.should =~ /data-video-id="BARFOO-----"/ end it "recognizes youtu.be links" do @@ -141,113 +151,135 @@ describe MarkdownifyHelper 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 == ""+url+"" + markdownify(proto+"://"+url).should == "

"+url+"

" end it "should recognize www links" do url="www.joindiaspora.com" - markdownify(url).should == ""+url+"" + markdownify(url).should == %Q{

#{url}

} end end describe "specialchars" do it "replaces <3 with ♥" do message = "i <3 you" - markdownify(message).should == "i ♥ you" + markdownify(message).should == "

i ♥ you

" end it "replaces various things with (their) HTML entities" do message = "... <-> -> <- (tm) (r) (c)" - markdownify(message).should == "… ↔ → ← ™ ® ©" + markdownify(message).should == "

… ↔ → ← ™ ® ©

" end it "skips doing it if you say so" do message = "... -> <-" - markdownify(message, :specialchars => false).should == "... -> <-" + markdownify(message, :specialchars => 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 == "some text some text some text some text" + markdownify(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" - markdownify(message).should == "some text some text some text some text" + markdownify(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" - markdownify(message).should == "some text some text some text some text" + markdownify(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" - markdownify(message).should == "some text some text some text some text" + markdownify(message).should == "

some text some text some text 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 == "this is some text" + markdownify(message).should == "

this is some text

" message = "*this is **some** text*" - markdownify(message).should == "this is some text" + markdownify(message).should == "

this is some text

" message = "___some text___" - markdownify(message).should == "some text" + markdownify(message).should == "

some text

" 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 == 'link text link text' + markdownify(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")' - markdownify(message).should == 'link text link text' + markdownify(message).should == '

link text link text

' end it "should have a robust link parsing" do + message = "[wikipedia](http://en.wikipedia.org/wiki/Text_(literary_theory))" + link = markdownify(message) + link.should =~ %r{href="http://en.wikipedia.org/wiki/Text_%28literary_theory%29"} + + message = "[ links]( google.com)" + markdownify(message).should == %Q{

links

} + + message = "[_http_](http://google.com/search?q=with_multiple__underscores*and**asterisks )" + markdownify(message).should == %Q{

http

} + message = %{[___FTP___]( ftp://ftp.uni-kl.de/CCC/26C3/mp4/26c3-3540-en-a_hackers_utopia.mp4 \"File Transfer Protocol\")} + markdownify(message).should == %{

FTP

} + + message = %{[**any protocol**](foo://bar.example.org/yes_it*makes*no_sense)} + markdownify(message).should == %{

any protocol

} + 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 text with many links tests http, FTP, any protocol' + markdownify(message).should == '

This text with many links tests http, FTP, any protocol

' + end + + it "should leave #tag links intact" do + message = %{#tagged} + markdownify(message).should == "

#{message}

" + message = %{alice - 1 - #tagged} + markdownify(message).should == "

#{message}

" 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 == 'some link text' + markdownify(message).should == '

some link text

' end end it "should allow escaping" do - message = '*some text* \\*some text* \\**some text* _some text_ \\_some text_ \\__some text_' - markdownify(message).should == "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 == "

some text *some text* *some text some text _some text_ _some text

" 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 + res.should == "

#{message}

" end it 'generates breaklines' do message = "These\nare\nsome\nnew\nlines" res = markdownify(message) - res.should == "These
are
some
new
lines" + res.should == "

These
are
some
new
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
joindiaspora.com
some more text' + res.should == '

Some text, then a line break and a link
joindiaspora.com
some more text

' end end @@ -263,4 +295,4 @@ describe MarkdownifyHelper do end end end -end \ No newline at end of file +end From 6e248c2d5ca7d136d309ef28f7d1e7ece4d44ee9 Mon Sep 17 00:00:00 2001 From: Brian Wisti Date: Tue, 16 Aug 2011 12:28:09 -0700 Subject: [PATCH 02/10] Improve code block support --- app/helpers/markdownify_helper.rb | 4 ++- .../status_messages/_status_message.haml | 2 +- lib/diaspora/markdownify.rb | 30 ++++++++++++------- spec/helpers/markdownify_helper_spec.rb | 7 +++++ 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/app/helpers/markdownify_helper.rb b/app/helpers/markdownify_helper.rb index ef38d352d..19f6aa850 100644 --- a/app/helpers/markdownify_helper.rb +++ b/app/helpers/markdownify_helper.rb @@ -7,7 +7,9 @@ require 'lib/diaspora/markdownify' module MarkdownifyHelper def markdownify(message, render_options={}) markdown_options = { - :autolink => true, + :autolink => true, + :fenced_code_blocks => true, + :space_after_headers => true } render_options[:filter_html] = true diff --git a/app/views/status_messages/_status_message.haml b/app/views/status_messages/_status_message.haml index 133cde0b5..c3be5bad9 100644 --- a/app/views/status_messages/_status_message.haml +++ b/app/views/status_messages/_status_message.haml @@ -16,4 +16,4 @@ = link_to (image_tag photo.url(:thumb_small), :class => 'stream-photo thumb_small', 'data-small-photo' => photo.url(:thumb_medium), 'data-full-photo' => photo.url), photo_path(photo), :class => 'stream-photo-link' %p{:class => direction_for(post.text)} - = markdownify(post.text, :youtube_maps => post[:youtube_titles]) + != markdownify(post.text, :youtube_maps => post[:youtube_titles]) diff --git a/lib/diaspora/markdownify.rb b/lib/diaspora/markdownify.rb index 734e34f01..49c7665f1 100644 --- a/lib/diaspora/markdownify.rb +++ b/lib/diaspora/markdownify.rb @@ -1,3 +1,4 @@ +require 'erb' require 'uri' module Diaspora @@ -36,13 +37,14 @@ module Diaspora if link =~ regex video_id = $1 if @vimeo_maps[video_id] - title = h(CGI::unescape(@vimeo_maps[video_id])) + title = ERB::Util.h(CGI::unescape(@vimeo_maps[video_id])) else title = I18n.t 'application.helper.video_title.unknown' end return ' Vimeo: ' + title + '' end + return end def autolink_youtube(link) @@ -51,7 +53,7 @@ module Diaspora anchor = $2 || '' if @youtube_maps[video_id] - title = h(CGI::unescape(@youtube_maps[video_id])) + title = ERB::Util.h(CGI::unescape(@youtube_maps[video_id])) else title = I18n.t 'application.helper.video_title.unknown' end @@ -59,6 +61,11 @@ module Diaspora video_id + '" data-anchor="' + anchor + '" href="'+ link + '" target="_blank">Youtube: ' + title + '' end + return + end + + def block_code(text, language) + "
\n#{text}
" end def double_emphasis(text) @@ -95,10 +102,19 @@ module Diaspora text = Diaspora::Taggable.format_tags(text, :no_escape => true) end + if @newlines + br = linebreak + + # in very clear cases, let newlines become
tags + # Grabbed from Github flavored Markdown + text = text.gsub(/^[\w\<][^\n]*\n+/) do |x| + x =~ /\n{2}/ ? x : (x = x.strip; x << br) + end + end + return "

#{text}

" end - def preprocess(full_document) if @specialchars full_document = specialchars(full_document) @@ -123,17 +139,11 @@ module Diaspora %Q{[#{content}](#{link}#{title_chunk})} end - if @newlines - # in very clear cases, let newlines become
tags - # Grabbed from Github flavored Markdown - full_document = full_document.gsub(/^[\w\<][^\n]*\n+/) do |x| - x =~ /\n{2}/ ? x : (x.strip!; x << " \n") - end - end return full_document end + def single_emphasis(text) "#{text}" end diff --git a/spec/helpers/markdownify_helper_spec.rb b/spec/helpers/markdownify_helper_spec.rb index e547ff4df..1349b0033 100644 --- a/spec/helpers/markdownify_helper_spec.rb +++ b/spec/helpers/markdownify_helper_spec.rb @@ -39,6 +39,13 @@ describe MarkdownifyHelper do markdownify(full_url).should == %Q{

#{url}

} end + it "doesn't muck up code text" do + message = %{`puts "Hello"`} + markdownify(message).should =~ %r{

puts "Hello"

} + message = %Q{~~~\nA\nB\n~~~\n} + markdownify(message).should =~ %r{
\nA\nB\n
} + end + it "doesn't double parse video links" do message = "http://www.vimeo.com/17449557 http://www.youtube.com/watch?v=0x__dDWdf23&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1 From ddf912d3ac751fffbf9af2f4a94463fa189e6794 Mon Sep 17 00:00:00 2001 From: Brian Wisti Date: Tue, 16 Aug 2011 19:30:19 -0700 Subject: [PATCH 03/10] Remove unnecessary invite seeds --- db/seeds.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/db/seeds.rb b/db/seeds.rb index 53e29fd9a..1800ec806 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -15,9 +15,9 @@ require 'factory_girl_rails' require File.join(File.dirname(__FILE__), "..", "spec", "helper_methods") include HelperMethods -alice = Factory(:user_with_aspect, :username => "alice", :password => 'evankorth', :invites => 10) -bob = Factory(:user_with_aspect, :username => "bob", :password => 'evankorth', :invites => 10) -eve = Factory(:user_with_aspect, :username => "eve", :password => 'evankorth', :invites => 10) +alice = Factory(:user_with_aspect, :username => "alice", :password => 'evankorth') +bob = Factory(:user_with_aspect, :username => "bob", :password => 'evankorth') +eve = Factory(:user_with_aspect, :username => "eve", :password => 'evankorth') print "Creating seeded users... " alice.person.profile.update_attributes(:first_name => "Alice", :last_name => "Smith", From 6b34a2611da25c7af1c2bcd41c3f8cb326df01a7 Mon Sep 17 00:00:00 2001 From: Brian Wisti Date: Tue, 16 Aug 2011 21:11:34 -0700 Subject: [PATCH 04/10] Turn on all the redcarpet options --- app/helpers/markdownify_helper.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/helpers/markdownify_helper.rb b/app/helpers/markdownify_helper.rb index 19f6aa850..1f4b1b303 100644 --- a/app/helpers/markdownify_helper.rb +++ b/app/helpers/markdownify_helper.rb @@ -9,7 +9,10 @@ module MarkdownifyHelper markdown_options = { :autolink => true, :fenced_code_blocks => true, - :space_after_headers => true + :space_after_headers => true, + :strikethrough => true, + :superscript => true, + :tables => true } render_options[:filter_html] = true From 03127cd592e86839a9a0f2acbb561976b7b18b74 Mon Sep 17 00:00:00 2001 From: Brian Wisti Date: Tue, 16 Aug 2011 10:28:00 -0700 Subject: [PATCH 05/10] basic support for Redcarpet Markdown --- Gemfile | 1 + Gemfile.lock | 7 + app/helpers/markdownify_helper.rb | 137 ++---------------- features/follows_tags.feature | 2 +- lib/diaspora/markdownify.rb | 169 +++++++++++++++++++++++ public/stylesheets/sass/application.sass | 3 + spec/helpers/markdownify_helper_spec.rb | 90 ++++++++---- 7 files changed, 257 insertions(+), 152 deletions(-) create mode 100644 lib/diaspora/markdownify.rb diff --git a/Gemfile b/Gemfile index 3a8199dd7..371bd9fa3 100644 --- a/Gemfile +++ b/Gemfile @@ -50,6 +50,7 @@ gem 'json', '1.4.6' gem 'http_accept_language', :git => 'git://github.com/iain/http_accept_language.git', :ref => '0b78aa7849fc90cf9e12' gem 'thin', '1.2.11', :require => false +gem 'redcarpet', :git => 'git://github.com/tanoku/redcarpet' #Websocket gem 'em-websocket', :git => 'git://github.com/igrigorik/em-websocket', :ref => 'e278f5a1c4db60be7485' diff --git a/Gemfile.lock b/Gemfile.lock index 15d2b5f03..119db8033 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -56,6 +56,12 @@ GIT addressable (>= 2.1.1) eventmachine (>= 0.12.9) +GIT + remote: git://github.com/tanoku/redcarpet + revision: 6b43f042305eb63620e3771a977f45e24ea7b576 + specs: + redcarpet (2.0.0b3) + GIT remote: git://github.com/zhitomirskiyi/ruby-jwt.git revision: fa7f46b5ac3653e30cf60abc78de9ffb3319dc0c @@ -500,6 +506,7 @@ DEPENDENCIES rails (= 3.0.9) rails-i18n rcov + redcarpet! resque (= 1.10.0) resque-ensure-connected rest-client (= 1.6.1) diff --git a/app/helpers/markdownify_helper.rb b/app/helpers/markdownify_helper.rb index edf3c7f74..ef38d352d 100644 --- a/app/helpers/markdownify_helper.rb +++ b/app/helpers/markdownify_helper.rb @@ -2,134 +2,27 @@ # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. +require 'lib/diaspora/markdownify' + module MarkdownifyHelper - def markdownify(message, options={}) - message = h(message).to_str + def markdownify(message, render_options={}) + markdown_options = { + :autolink => true, + } - options[:newlines] = true if !options.has_key?(:newlines) - options[:specialchars] = true if !options.has_key?(:specialchars) + render_options[:filter_html] = true - 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_specialchars(message) if options[:specialchars] - message = process_newlines(message) if options[:newlines] - - message.html_safe + renderer = Diaspora::Markdownify::HTML.new(render_options) + markdown = Redcarpet::Markdown.new(renderer, markdown_options) + message = markdown.render(message) + return message.html_safe end def process_newlines(message) - message.gsub!(/\n+/, '
') - message - end - - def process_links(message) - message.gsub!(/\[\s*([^\[]+?)\s*\]\(\s*([^ ]+\s*) \"(([^&]|(&[^q])|(&q[^u])|(&qu[^o])|(&quo[^t])|("[^;]))+)\"\s*\)/) do |m| - escape = "\\" - link = $1 - url = $2 - title = $3 - url.gsub!("_", "\\_") - url.gsub!("*", "\\*") - protocol = (url =~ /^\w+:\/\//) ? '' :'http://' - res = "#{link}" - res + # in very clear cases, let newlines become
tags + # Grabbed from Github flavored Markdown + message.gsub(/^[\w\<][^\n]*\n+/) do |x| + x =~ /\n{2}/ ? x : (x.strip!; x << " \n") end - - message.gsub!(/\[\s*([^\[]+?)\s*\]\(\s*([^ ]+)\s*\)/) do |m| - escape = "\\" - link = $1 - url = $2 - url.gsub!("_", "\\_") - url.gsub!("*", "\\*") - protocol = (url =~ /^\w+:\/\//) ? '' :'http://' - res = "#{link}" - 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 - ' Youtube: ' + title + '' - end - processed_message - end - - def process_autolinks(message) - message.gsub!(/( |^)(www\.[^\s]+\.[^\s])/, '\1http://\2') - message.gsub!(/(#{captures[2]}} - 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, '\2') - message.gsub!(/(\*\*|__)(.+?)\1/m, '\2') - message.gsub!(/(\*|_)(.+?)\1/m, '\2') - 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 - ' Vimeo: ' + title + '' - end - processed_message - end - - def process_specialchars(message) - map = [ - ["<3", "♥"], - ["<->", "↔"], - ["->", "→"], - ["<-", "←"], - ["...", "…"], - ["(tm)", "™"], - ["(r)", "®"], - ["(c)", "©"] - ] - - map.each do |mapping| - message.gsub!(mapping[0], mapping[1]) - end - message end end diff --git a/features/follows_tags.feature b/features/follows_tags.feature index d6b0afda1..b74fb8fb1 100644 --- a/features/follows_tags.feature +++ b/features/follows_tags.feature @@ -25,7 +25,7 @@ Feature: posting Scenario: see a tag that I am following When I go to the home page And I follow "#boss" - Then I should see "I am da #boss" + Then I should see "I am da #boss" within "body" Scenario: can stop following a particular tag When I hover over the ".button.tag_following" diff --git a/lib/diaspora/markdownify.rb b/lib/diaspora/markdownify.rb new file mode 100644 index 000000000..734e34f01 --- /dev/null +++ b/lib/diaspora/markdownify.rb @@ -0,0 +1,169 @@ +require 'uri' + +module Diaspora + module Markdownify + class HTML < Redcarpet::Render::HTML + attr_accessor :newlines, :specialchars, :youtube_maps, :vimeo_maps + + def initialize(options={}) + super + + @expand_tags = options.fetch(:expand_tabs, true) + @newlines = options.fetch(:newlines, true) + @specialchars = options.fetch(:specialchars, true) + @youtube_maps = options.fetch(:youtube_maps, {}) + @vimeo_maps = options.fetch(:vimeo_maps, {}) + end + + def autolink(link, type) + autolink_youtube(link) || autolink_vimeo(link) || autolink_simple(link) + end + + def autolink_simple(link) + + # If there isn't *some* protocol, assume http + if link !~ %r{^\w+://} + link = "http://#{link}" + end + + content = link.gsub(%r{^\w+://}, '') + + %Q{#{content}} + end + + def autolink_vimeo(link) + regex = %r{https?://(?:w{3}\.)?vimeo.com/(\d{6,})/?} + if link =~ regex + video_id = $1 + if @vimeo_maps[video_id] + title = h(CGI::unescape(@vimeo_maps[video_id])) + else + title = I18n.t 'application.helper.video_title.unknown' + end + return ' Vimeo: ' + title + '' + end + end + + def autolink_youtube(link) + if link =~ YoutubeTitles::YOUTUBE_ID_REGEX + video_id = $1 + anchor = $2 || '' + + if @youtube_maps[video_id] + title = h(CGI::unescape(@youtube_maps[video_id])) + else + title = I18n.t 'application.helper.video_title.unknown' + end + return ' Youtube: ' + title + '' + end + end + + def double_emphasis(text) + "#{text}" + end + + def linebreak() + "
" + end + + def link(link, title, content) + return autolink(link, 'url') if link == content + + if link =~ Regexp.new(Regexp.escape(content)) + return autolink(link, 'url') + end + + if link !~ %r{^\w+://} + link = "http://#{link}" + end + + tag = if title and content + %Q{#{content}} + elsif content + %Q{#{content}} + else + autolink(link, 'url') + end + return tag + end + + def paragraph(text) + if @expand_tags + text = Diaspora::Taggable.format_tags(text, :no_escape => true) + end + + return "

#{text}

" + end + + + def preprocess(full_document) + if @specialchars + full_document = specialchars(full_document) + end + + our_unsafe_chars = '()' + full_document = full_document.gsub(%r{ + \[ \s*? ( [^ \] ]+ ) \s*? \] + (?: + \( \s*? (\S+) \s*? (?: "([^"]+)" )? \) \s*? + ) + }xm) do |m| + content = $1 + link = URI.escape($2, our_unsafe_chars) + title = $3 + + title_chunk = if title + %W{" #{title}"} + else + '' + end + %Q{[#{content}](#{link}#{title_chunk})} + end + + if @newlines + # in very clear cases, let newlines become
tags + # Grabbed from Github flavored Markdown + full_document = full_document.gsub(/^[\w\<][^\n]*\n+/) do |x| + x =~ /\n{2}/ ? x : (x.strip!; x << " \n") + end + end + + return full_document + end + + def single_emphasis(text) + "#{text}" + end + + def specialchars(text) + if @specialchars + map = [ + ["<3", "♥"], + ["<->", "↔"], + ["->", "→"], + ["<-", "←"], + ["\.\.\.", "…"], + ["(tm)", "™"], + ["(r)", "®"], + ["(c)", "©"] + ] + end + + map.each do |mapping| + text = text.gsub(mapping[0], mapping[1]) + end + + return text + end + + + def triple_emphasis(text) + single_emphasis(double_emphasis(text)) + end + + end + end +end diff --git a/public/stylesheets/sass/application.sass b/public/stylesheets/sass/application.sass index f006caffe..e2038514b 100644 --- a/public/stylesheets/sass/application.sass +++ b/public/stylesheets/sass/application.sass @@ -38,6 +38,9 @@ h1 ul > li :list-style none +.content ul li + :list-style disc + form :position relative input, diff --git a/spec/helpers/markdownify_helper_spec.rb b/spec/helpers/markdownify_helper_spec.rb index 1e036aa8d..e547ff4df 100644 --- a/spec/helpers/markdownify_helper_spec.rb +++ b/spec/helpers/markdownify_helper_spec.rb @@ -8,31 +8,35 @@ describe MarkdownifyHelper do describe "#markdownify" do describe "autolinks" do it "should not allow basic XSS/HTML" do - markdownify("").should == "<script>alert('XSS is evil')</script>" + markdownify("").should == "

alert('XSS is evil')

" end it "should recognize basic http links (1/3)" do proto="http" url="bugs.joindiaspora.com/issues/332" - markdownify(proto+"://"+url).should == ""+url+"" + full_url = "#{proto}://#{url}" + markdownify(full_url).should == %Q{

#{url}

} end it "should recognize basic http links (2/3)" do proto="http" url="webmail.example.com?~()!*/" - markdownify(proto+"://"+url).should == ""+url+"" + full_url = "#{proto}://#{url}" + markdownify(full_url).should == %Q{

#{url}

} 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 == ""+url+"" + full_url = "#{proto}://#{url}" + markdownify(full_url).should == %Q{

#{url}

} end it "should recognize secure https links" do proto="https" url="127.0.0.1:3000/users/sign_in" - markdownify(proto+"://"+url).should == ""+url+"" + full_url = "#{proto}://#{url}" + markdownify(full_url).should == %Q{

#{url}

} end it "doesn't double parse video links" do @@ -40,8 +44,8 @@ describe MarkdownifyHelper do http://www.youtube.com/watch?v=0x__dDWdf23&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1 http://youtu.be/x_CzD0GBD-4" res = markdownify(message) - res.should =~ /href.+href.+href/ - res.should_not =~ /href.+href.+href.+href/ + res.should =~ /href.+href.+href/m + res.should_not =~ /href.+href.+href.+href/m end describe "video links" do @@ -69,6 +73,12 @@ describe MarkdownifyHelper do res.should =~ /Youtube:/ res.should =~ /data-host="youtube.com"/ res.should =~ /data-video-id="#{video_id}"/ + + url = "www.youtube.com/watch?foo=bar&v=BARFOO-----&whatever=related" + res = markdownify(url) + res.should =~ /Youtube:/ + res.should =~ /data-host="youtube.com"/ + res.should =~ /data-video-id="BARFOO-----"/ end it "recognizes youtu.be links" do @@ -141,113 +151,135 @@ describe MarkdownifyHelper 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 == ""+url+"" + markdownify(proto+"://"+url).should == "

"+url+"

" end it "should recognize www links" do url="www.joindiaspora.com" - markdownify(url).should == ""+url+"" + markdownify(url).should == %Q{

#{url}

} end end describe "specialchars" do it "replaces <3 with ♥" do message = "i <3 you" - markdownify(message).should == "i ♥ you" + markdownify(message).should == "

i ♥ you

" end it "replaces various things with (their) HTML entities" do message = "... <-> -> <- (tm) (r) (c)" - markdownify(message).should == "… ↔ → ← ™ ® ©" + markdownify(message).should == "

… ↔ → ← ™ ® ©

" end it "skips doing it if you say so" do message = "... -> <-" - markdownify(message, :specialchars => false).should == "... -> <-" + markdownify(message, :specialchars => 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 == "some text some text some text some text" + markdownify(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" - markdownify(message).should == "some text some text some text some text" + markdownify(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" - markdownify(message).should == "some text some text some text some text" + markdownify(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" - markdownify(message).should == "some text some text some text some text" + markdownify(message).should == "

some text some text some text 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 == "this is some text" + markdownify(message).should == "

this is some text

" message = "*this is **some** text*" - markdownify(message).should == "this is some text" + markdownify(message).should == "

this is some text

" message = "___some text___" - markdownify(message).should == "some text" + markdownify(message).should == "

some text

" 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 == 'link text link text' + markdownify(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")' - markdownify(message).should == 'link text link text' + markdownify(message).should == '

link text link text

' end it "should have a robust link parsing" do + message = "[wikipedia](http://en.wikipedia.org/wiki/Text_(literary_theory))" + link = markdownify(message) + link.should =~ %r{href="http://en.wikipedia.org/wiki/Text_%28literary_theory%29"} + + message = "[ links]( google.com)" + markdownify(message).should == %Q{

links

} + + message = "[_http_](http://google.com/search?q=with_multiple__underscores*and**asterisks )" + markdownify(message).should == %Q{

http

} + message = %{[___FTP___]( ftp://ftp.uni-kl.de/CCC/26C3/mp4/26c3-3540-en-a_hackers_utopia.mp4 \"File Transfer Protocol\")} + markdownify(message).should == %{

FTP

} + + message = %{[**any protocol**](foo://bar.example.org/yes_it*makes*no_sense)} + markdownify(message).should == %{

any protocol

} + 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 text with many links tests http, FTP, any protocol' + markdownify(message).should == '

This text with many links tests http, FTP, any protocol

' + end + + it "should leave #tag links intact" do + message = %{#tagged} + markdownify(message).should == "

#{message}

" + message = %{alice - 1 - #tagged} + markdownify(message).should == "

#{message}

" 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 == 'some link text' + markdownify(message).should == '

some link text

' end end it "should allow escaping" do - message = '*some text* \\*some text* \\**some text* _some text_ \\_some text_ \\__some text_' - markdownify(message).should == "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 == "

some text *some text* *some text some text _some text_ _some text

" 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 + res.should == "

#{message}

" end it 'generates breaklines' do message = "These\nare\nsome\nnew\nlines" res = markdownify(message) - res.should == "These
are
some
new
lines" + res.should == "

These
are
some
new
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
joindiaspora.com
some more text' + res.should == '

Some text, then a line break and a link
joindiaspora.com
some more text

' end end @@ -263,4 +295,4 @@ describe MarkdownifyHelper do end end end -end \ No newline at end of file +end From b87c7bb4890647a38646b38d9ed38030b57d50fc Mon Sep 17 00:00:00 2001 From: Brian Wisti Date: Tue, 16 Aug 2011 12:28:09 -0700 Subject: [PATCH 06/10] Improve code block support --- app/helpers/markdownify_helper.rb | 4 ++- .../status_messages/_status_message.haml | 2 +- lib/diaspora/markdownify.rb | 30 ++++++++++++------- spec/helpers/markdownify_helper_spec.rb | 7 +++++ 4 files changed, 31 insertions(+), 12 deletions(-) diff --git a/app/helpers/markdownify_helper.rb b/app/helpers/markdownify_helper.rb index ef38d352d..19f6aa850 100644 --- a/app/helpers/markdownify_helper.rb +++ b/app/helpers/markdownify_helper.rb @@ -7,7 +7,9 @@ require 'lib/diaspora/markdownify' module MarkdownifyHelper def markdownify(message, render_options={}) markdown_options = { - :autolink => true, + :autolink => true, + :fenced_code_blocks => true, + :space_after_headers => true } render_options[:filter_html] = true diff --git a/app/views/status_messages/_status_message.haml b/app/views/status_messages/_status_message.haml index 133cde0b5..c3be5bad9 100644 --- a/app/views/status_messages/_status_message.haml +++ b/app/views/status_messages/_status_message.haml @@ -16,4 +16,4 @@ = link_to (image_tag photo.url(:thumb_small), :class => 'stream-photo thumb_small', 'data-small-photo' => photo.url(:thumb_medium), 'data-full-photo' => photo.url), photo_path(photo), :class => 'stream-photo-link' %p{:class => direction_for(post.text)} - = markdownify(post.text, :youtube_maps => post[:youtube_titles]) + != markdownify(post.text, :youtube_maps => post[:youtube_titles]) diff --git a/lib/diaspora/markdownify.rb b/lib/diaspora/markdownify.rb index 734e34f01..49c7665f1 100644 --- a/lib/diaspora/markdownify.rb +++ b/lib/diaspora/markdownify.rb @@ -1,3 +1,4 @@ +require 'erb' require 'uri' module Diaspora @@ -36,13 +37,14 @@ module Diaspora if link =~ regex video_id = $1 if @vimeo_maps[video_id] - title = h(CGI::unescape(@vimeo_maps[video_id])) + title = ERB::Util.h(CGI::unescape(@vimeo_maps[video_id])) else title = I18n.t 'application.helper.video_title.unknown' end return ' Vimeo: ' + title + '' end + return end def autolink_youtube(link) @@ -51,7 +53,7 @@ module Diaspora anchor = $2 || '' if @youtube_maps[video_id] - title = h(CGI::unescape(@youtube_maps[video_id])) + title = ERB::Util.h(CGI::unescape(@youtube_maps[video_id])) else title = I18n.t 'application.helper.video_title.unknown' end @@ -59,6 +61,11 @@ module Diaspora video_id + '" data-anchor="' + anchor + '" href="'+ link + '" target="_blank">Youtube: ' + title + '' end + return + end + + def block_code(text, language) + "
\n#{text}
" end def double_emphasis(text) @@ -95,10 +102,19 @@ module Diaspora text = Diaspora::Taggable.format_tags(text, :no_escape => true) end + if @newlines + br = linebreak + + # in very clear cases, let newlines become
tags + # Grabbed from Github flavored Markdown + text = text.gsub(/^[\w\<][^\n]*\n+/) do |x| + x =~ /\n{2}/ ? x : (x = x.strip; x << br) + end + end + return "

#{text}

" end - def preprocess(full_document) if @specialchars full_document = specialchars(full_document) @@ -123,17 +139,11 @@ module Diaspora %Q{[#{content}](#{link}#{title_chunk})} end - if @newlines - # in very clear cases, let newlines become
tags - # Grabbed from Github flavored Markdown - full_document = full_document.gsub(/^[\w\<][^\n]*\n+/) do |x| - x =~ /\n{2}/ ? x : (x.strip!; x << " \n") - end - end return full_document end + def single_emphasis(text) "#{text}" end diff --git a/spec/helpers/markdownify_helper_spec.rb b/spec/helpers/markdownify_helper_spec.rb index e547ff4df..1349b0033 100644 --- a/spec/helpers/markdownify_helper_spec.rb +++ b/spec/helpers/markdownify_helper_spec.rb @@ -39,6 +39,13 @@ describe MarkdownifyHelper do markdownify(full_url).should == %Q{

#{url}

} end + it "doesn't muck up code text" do + message = %{`puts "Hello"`} + markdownify(message).should =~ %r{

puts "Hello"

} + message = %Q{~~~\nA\nB\n~~~\n} + markdownify(message).should =~ %r{
\nA\nB\n
} + end + it "doesn't double parse video links" do message = "http://www.vimeo.com/17449557 http://www.youtube.com/watch?v=0x__dDWdf23&a=GxdCwVVULXdvEBKmx_f5ywvZ0zZHHHDU&list=ML&playnext=1 From 736b179888ac90bd1007ca64bed194651f861348 Mon Sep 17 00:00:00 2001 From: Brian Wisti Date: Tue, 16 Aug 2011 21:11:34 -0700 Subject: [PATCH 07/10] Turn on all the redcarpet options --- app/helpers/markdownify_helper.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/helpers/markdownify_helper.rb b/app/helpers/markdownify_helper.rb index 19f6aa850..1f4b1b303 100644 --- a/app/helpers/markdownify_helper.rb +++ b/app/helpers/markdownify_helper.rb @@ -9,7 +9,10 @@ module MarkdownifyHelper markdown_options = { :autolink => true, :fenced_code_blocks => true, - :space_after_headers => true + :space_after_headers => true, + :strikethrough => true, + :superscript => true, + :tables => true } render_options[:filter_html] = true From 805a9eb921a3402032e1300aa5d52a1185fb9b05 Mon Sep 17 00:00:00 2001 From: Brian Wisti Date: Tue, 16 Aug 2011 21:59:13 -0700 Subject: [PATCH 08/10] resolve my own merge conflicts. --- lib/diaspora/markdownify.rb | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/diaspora/markdownify.rb b/lib/diaspora/markdownify.rb index 49c7665f1..826fa4cb5 100644 --- a/lib/diaspora/markdownify.rb +++ b/lib/diaspora/markdownify.rb @@ -1,5 +1,4 @@ require 'erb' -require 'uri' module Diaspora module Markdownify @@ -139,11 +138,9 @@ module Diaspora %Q{[#{content}](#{link}#{title_chunk})} end - return full_document end - def single_emphasis(text) "#{text}" end From 888ad0a31c30ae139fca64d50e88100af049c025 Mon Sep 17 00:00:00 2001 From: Brian Wisti Date: Tue, 16 Aug 2011 22:05:52 -0700 Subject: [PATCH 09/10] merging for 1305 --- lib/diaspora/markdownify.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/diaspora/markdownify.rb b/lib/diaspora/markdownify.rb index 826fa4cb5..6e38faadd 100644 --- a/lib/diaspora/markdownify.rb +++ b/lib/diaspora/markdownify.rb @@ -141,6 +141,7 @@ module Diaspora return full_document end + def single_emphasis(text) "#{text}" end From a442e7c8ae78ca335d746025fcb94ccdb718496c Mon Sep 17 00:00:00 2001 From: Brian Wisti Date: Tue, 16 Aug 2011 23:01:08 -0700 Subject: [PATCH 10/10] oops. left a merge conflict in --- lib/diaspora/markdownify.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/diaspora/markdownify.rb b/lib/diaspora/markdownify.rb index 19a1067eb..6e38faadd 100644 --- a/lib/diaspora/markdownify.rb +++ b/lib/diaspora/markdownify.rb @@ -138,10 +138,6 @@ module Diaspora %Q{[#{content}](#{link}#{title_chunk})} end -<<<<<<< HEAD -======= - ->>>>>>> 6b34a2611da25c7af1c2bcd41c3f8cb326df01a7 return full_document end