app/controllers/preview_controller.rb in mulberry_preview-0.0.8 vs app/controllers/preview_controller.rb in mulberry_preview-0.0.9
- old
+ new
@@ -1,19 +1,21 @@
class PreviewController < ActionController::Base
- Languages = [:ruby, :python, :java, :js, :scss, :sass, :haml, :json,
+ LANGUAGES = [:ruby, :python, :java, :js, :scss, :sass, :haml, :json,
:go, :sql, :yaml, :c, :coffee, :properties, :clojure]
def index
clazz, id = params[:type].classify.constantize, params[:id]
r = clazz.find(id)
if r
mime = r.content_type
type, ext = mime[/^[^\/]+/].to_sym, mime[/(?<=\/)(x-)?(.+)/, 2].to_sym
- if Languages.include?(ext)
+ if LANGUAGES.include?(ext)
@code = CodeRay.scan(r.content, ext).div(:line_numbers => :table, :css => :class)
render 'code', layout: 'coderay'
+ elsif :markdown == ext
+ render text: markdown(r.content), content_type: 'text/html'
elsif [:pdf, :html].include?(ext)
render_native(r.content, mime)
else
case type
when :text
@@ -53,9 +55,22 @@
head :no_content
end
end
private
+ def markdown(content)
+ renderer = Markdown::Render::HTML.new(hard_wrap: true, filter_html: true)
+ options = {
+ autolink: true,
+ no_intra_emphasis: true,
+ fenced_code_blocks: true,
+ lax_html_blocks: true,
+ strikethrough: true,
+ superscript: true
+ }
+ Redcarpet::Markdown.new(renderer, options).render(content).html_safe
+ end
+
def open_osx_file
path = params[:path]
app = 'Preview'
if /^text\/.+$/ =~ params[:mime]
app = 'TextMate'
\ No newline at end of file