Permalink
Browse files

basic support for Redcarpet Markdown

  • Loading branch information...
1 parent 97ab077 commit 03127cd592e86839a9a0f2acbb561976b7b18b74 @brianwisti brianwisti committed Aug 16, 2011
View
@@ -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'
View
@@ -57,6 +57,12 @@ GIT
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
specs:
@@ -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)
@@ -2,134 +2,27 @@
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
-module MarkdownifyHelper
- def markdownify(message, options={})
- message = h(message).to_str
+require 'lib/diaspora/markdownify'
- options[:newlines] = true if !options.has_key?(:newlines)
- options[:specialchars] = true if !options.has_key?(:specialchars)
+module MarkdownifyHelper
+ def markdownify(message, render_options={})
+ markdown_options = {
+ :autolink => 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]
+ render_options[:filter_html] = true
- 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+/, '<br />')
- message
- end
-
- def process_links(message)
- message.gsub!(/\[\s*([^\[]+?)\s*\]\(\s*([^ ]+\s*) \&quot;(([^&]|(&[^q])|(&q[^u])|(&qu[^o])|(&quo[^t])|(&quot[^;]))+)\&quot;\s*\)/) do |m|
- escape = "\\"
- link = $1
- url = $2
- title = $3
- url.gsub!("_", "\\_")
- url.gsub!("*", "\\*")
- protocol = (url =~ /^\w+:\/\//) ? '' :'http://'
- res = "<a target=\"#{escape}_blank\" href=\"#{protocol}#{url}\" title=\"#{title}\">#{link}</a>"
- res
- end
-
- message.gsub!(/\[\s*([^\[]+?)\s*\]\(\s*([^ ]+)\s*\)/) do |m|
- escape = "\\"
- link = $1
- url = $2
- url.gsub!("_", "\\_")
- url.gsub!("*", "\\*")
- protocol = (url =~ /^\w+:\/\//) ? '' :'http://'
- res = "<a target=\"#{escape}_blank\" href=\"#{protocol}#{url}\">#{link}</a>"
- 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
- ' <a class="video-link" data-host="youtube.com" data-video-id="' + video_id + '" data-anchor="' + anchor + '" href="'+ match_data[0].strip + '" target="_blank">Youtube: ' + title + '</a>'
- end
- processed_message
- end
-
- def process_autolinks(message)
- message.gsub!(/( |^)(www\.[^\s]+\.[^\s])/, '\1http://\2')
- message.gsub!(/(<a target="\\?_blank" href=")?(https|http|ftp):\/\/([^\s]+)/) do |m|
- captures = [$1,$2,$3]
- if !captures[0].nil?
- m
- elsif m.match(/(youtu.?be|vimeo)/)
- m.gsub(/(\*|_)/) { |m| "\\#{$1}" } #remove markers on markdown chars to not markdown inside links
- else
- res = %{<a target="_blank" href="#{captures[1]}://#{captures[2]}">#{captures[2]}</a>}
- 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, '<em><strong>\2</strong></em>')
- message.gsub!(/(\*\*|__)(.+?)\1/m, '<strong>\2</strong>')
- message.gsub!(/(\*|_)(.+?)\1/m, '<em>\2</em>')
- 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
- ' <a class="video-link" data-host="vimeo.com" data-video-id="' + video_id + '" href="' + match_data[0] + '" target="_blank">Vimeo: ' + title + '</a>'
- end
- processed_message
- end
-
- def process_specialchars(message)
- map = [
- ["&lt;3", "&hearts;"],
- ["&lt;-&gt;", "&#8596;"],
- ["-&gt;", "&rarr;"],
- ["&lt;-", "&larr;"],
- ["...", "&hellip;"],
- ["(tm)", "&trade;"],
- ["(r)", "&reg;"],
- ["(c)", "&copy;"]
- ]
-
- map.each do |mapping|
- message.gsub!(mapping[0], mapping[1])
+ # in very clear cases, let newlines become <br /> tags
+ # Grabbed from Github flavored Markdown
+ message.gsub(/^[\w\<][^\n]*\n+/) do |x|
+ x =~ /\n{2}/ ? x : (x.strip!; x << " \n")
end
- message
end
end
@@ -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"
@@ -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{<a target="_blank" href="#{link}">#{content}</a>}
+ 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 ' <a class="video-link" data-host="vimeo.com" data-video-id="' +
+ video_id + '" href="' + link + '" target="_blank">Vimeo: ' + title + '</a>'
+ 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 ' <a class="video-link" data-host="youtube.com" data-video-id="' +
+ video_id + '" data-anchor="' + anchor +
+ '" href="'+ link + '" target="_blank">Youtube: ' + title + '</a>'
+ end
+ end
+
+ def double_emphasis(text)
+ "<strong>#{text}</strong>"
+ end
+
+ def linebreak()
+ "<br />"
+ 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{<a target="_blank" href="#{link}" title="#{title}">#{content}</a>}
+ elsif content
+ %Q{<a target="_blank" href="#{link}">#{content}</a>}
+ 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 "<p>#{text}</p>"
+ 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 <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
+
+ def specialchars(text)
+ if @specialchars
+ map = [
+ ["<3", "&hearts;"],
+ ["<->", "&#8596;"],
+ ["->", "&rarr;"],
+ ["<-", "&larr;"],
+ ["\.\.\.", "&hellip;"],
+ ["(tm)", "&trade;"],
+ ["(r)", "&reg;"],
+ ["(c)", "&copy;"]
+ ]
+ 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
@@ -38,6 +38,9 @@ h1
ul > li
:list-style none
+.content ul li
+ :list-style disc
+
form
:position relative
input,
Oops, something went wrong.

0 comments on commit 03127cd

Please sign in to comment.