From 6e248c2d5ca7d136d309ef28f7d1e7ece4d44ee9 Mon Sep 17 00:00:00 2001 From: Brian Wisti Date: Tue, 16 Aug 2011 12:28:09 -0700 Subject: [PATCH] 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