-
Notifications
You must be signed in to change notification settings - Fork 8
/
markdown_template_handler.rb
41 lines (35 loc) · 1.53 KB
/
markdown_template_handler.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class CodeRayify < Redcarpet::Render::HTML
def block_code(code, language)
if !Gem.loaded_specs.has_key?('coderay') || language == nil || language == ""
%(<div class="CodeRay"><div class="code"><pre>#{code}</pre></div></div>)
else
CodeRay.scan(code, language).div
end
end
end
module MarkdownTemplateHandler
def self.erb
@erb ||= ActionView::Template.registered_template_handler(:erb)
end
def self.call(template, source = nil)
compiled_source = if source
erb.call(template, source)
else
erb.call(template)
end
%(Redcarpet::Markdown.new(CodeRayify.new(:filter_html => false,
:hard_wrap => true),
no_intra_emphasis: true,
fenced_code_blocks: true,
space_after_headers: true,
smartypants: true,
disable_indented_code_blocks: true,
prettify: true,
tables: true,
with_toc_data: true,
autolink: true
).render(begin;#{compiled_source};end).html_safe)
end
end
ActionView::Template.register_template_handler :md, MarkdownTemplateHandler
ActionView::Template.register_template_handler :markdown, MarkdownTemplateHandler