Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMaxwell Salzberg <maxwell@joindiaspora.com>2011-08-28 01:12:35 +0400
committerMaxwell Salzberg <maxwell@joindiaspora.com>2011-08-28 01:13:15 +0400
commitc66b7d584df49dd27e974e610e46a2576447ba94 (patch)
tree69e665d960be19d6943c3c211f1f7252a16ac95f /lib
parentcc1add81b043a206edf917fe0031d1d4e934ee7a (diff)
parenta442e7c8ae78ca335d746025fcb94ccdb718496c (diff)
Merge branch '1305-full-markdown' of https://github.com/brianwisti/diaspora into brianwisti-1305-full-markdown
Conflicts: Gemfile.lock
Diffstat (limited to 'lib')
-rw-r--r--lib/diaspora/markdownify.rb177
1 files changed, 177 insertions, 0 deletions
diff --git a/lib/diaspora/markdownify.rb b/lib/diaspora/markdownify.rb
new file mode 100644
index 000000000..6e38faadd
--- /dev/null
+++ b/lib/diaspora/markdownify.rb
@@ -0,0 +1,177 @@
+require 'erb'
+
+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 = 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)
+ if link =~ YoutubeTitles::YOUTUBE_ID_REGEX
+ video_id = $1
+ anchor = $2 || ''
+
+ if @youtube_maps[video_id]
+ title = ERB::Util.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
+ return
+ end
+
+ def block_code(text, language)
+ "<pre><code>\n#{text}</code></pre>"
+ 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
+
+ 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)
+ 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
+
+ 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