forked from trulia/hologram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_markdown_renderer.rb.example
47 lines (43 loc) · 1.33 KB
/
example_markdown_renderer.rb.example
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
42
43
44
45
46
47
class ExampleMarkdownRenderer < Redcarpet::Render::HTML
def block_code(code, language)
if language and language.include?('example')
if language.include?('js')
# first actually insert the code in the docs so that it will run and make our example work.
'<script>' + code + '</script>
<div class="codeBlock jsExample">' + Pygments.highlight(code) + '</div>'
else
'<div class="codeExample">' + '<div class="exampleOutput">' + render_html(code, language) + '</div>' + '<div class="codeBlock">' + Pygments.highlight(code, :lexer => get_lexer(language)) + '</div>' + '</div>'
end
else
'<div class="codeBlock">' + Pygments.highlight(code) + '</div>'
end
end
private
def render_html(code, language)
case language
when 'haml_example'
safe_require('haml', language)
return Haml::Engine.new(code.strip).render(template_rendering_scope, {})
else
code
end
end
def template_rendering_scope
Object.new
end
def get_lexer(language)
case language
when 'haml_example'
'haml'
else
'html'
end
end
def safe_require(templating_library, language)
begin
require templating_library
rescue LoadError
raise "#{templating_library} must be present for you to use #{language}"
end
end
end