Class: Markdown::Parse

Inherits:
Object
  • Object
show all
Defined in:
lib/markdown_render/parse.rb

Constant Summary collapse

HTML =
<<-TEXT
<!DOCTYPE html>
<html>
  <head>
<title>Document</title>
<style>
{{theme}}
</style>
  </head>

  <body>
  {{content}}
  </body>
</html>
TEXT

Instance Method Summary collapse

Constructor Details

#initialize(parser, theme = '') ⇒ Parse

parser - markdown processor

- kramdown
- redcarpet
- maruku
- rdiscount


9
10
11
12
# File 'lib/markdown_render/parse.rb', line 9

def initialize(parser, theme = '')
  @parser = parser
  @theme = theme
end

Instance Method Details

#to_document(content) ⇒ Object



61
62
63
64
65
# File 'lib/markdown_render/parse.rb', line 61

def to_document(content)
  html = to_html(content)
  html = HTML.gsub('{{content}}', html)
  html.gsub('{{theme}}', @theme)
end

#to_html(content) ⇒ Object

takes markdown and returns html (as a string)

content - markdown content



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
# File 'lib/markdown_render/parse.rb', line 17

def to_html(content)
  case @parser
  when :kramdown, 'kramdown'
    require 'kramdown'
    Kramdown::Document.new(content).to_html
  when :redcarpet, 'redcarpet'
    require 'redcarpet'
    markdown = Redcarpet::Markdown.new(
      Redcarpet::Render::HTML,
      smart: true,
      no_intra_emphasis: true,
      fenced_code_blocks: true,
      autolink: true,
      tables: true,
      with_toc_data: true
    )

    # add smartypants support
    Redcarpet::Render::SmartyPants.render markdown.render(content)
  when :rdiscount, 'rdiscount'
    require 'rdiscount'
    RDiscount.new(content).to_html
  when :gfm, :github, :github_markdown, 'gfm', 'github_markdown'
    require 'github/markdown'
    GitHub::Markdown.render(content)
  end
end