Class: Markdown::Parse

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

Constant Summary collapse

HTML =
"<!DOCTYPE html>\n<html>\n  <head>\n<title>Document</title>\n<style>\n{{theme}}\n</style>\n  </head>\n\n  <body>\n  {{content}}\n  </body>\n</html>\n"

Instance Method Summary collapse

Constructor Details

#initialize(parser) ⇒ Parse

parser - markdown processor

- kramdown
- redcarpet
- maruku
- rdiscount


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

def initialize(parser)
  @parser = parser
end

Instance Method Details

#to_document(content) ⇒ Object



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

def to_document(content)
  theme_file = Dir['*theme.css'].first || 'nothing'
  theme_fail = !File.exist?(theme_file)
  theme = File.read(theme_file) unless theme_fail

  html = to_html(content)
  html = HTML.gsub('{{content}}', html)
  html.gsub('{{theme}}', theme) unless theme_fail
end

#to_html(content) ⇒ Object

takes markdown and returns html (as a string)

content - markdown content



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

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