Improve code block support

This commit is contained in:
Brian Wisti 2011-08-16 12:28:09 -07:00
parent 2303686e31
commit 6e248c2d5c
4 changed files with 31 additions and 12 deletions

View file

@ -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

View file

@ -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])

View file

@ -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 ' <a class="video-link" data-host="vimeo.com" data-video-id="' +
video_id + '" href="' + link + '" target="_blank">Vimeo: ' + title + '</a>'
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 + '</a>'
end
return
end
def block_code(text, language)
"<pre><code>\n#{text}</code></pre>"
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 <br /> 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 "<p>#{text}</p>"
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 <br /> 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)
"<em>#{text}</em>"
end

View file

@ -39,6 +39,13 @@ describe MarkdownifyHelper do
markdownify(full_url).should == %Q{<p><a target="_blank" href="#{full_url}">#{url}</a></p>}
end
it "doesn't muck up code text" do
message = %{`puts "Hello"`}
markdownify(message).should =~ %r{<p><code>puts &quot;Hello&quot;</code></p>}
message = %Q{~~~\nA\nB\n~~~\n}
markdownify(message).should =~ %r{<pre><code>\nA\nB\n</code></pre>}
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