Module: Mdopen

Defined in:
lib/mdopen.rb,
lib/mdopen/version.rb,
lib/mdopen/erb_template.rb

Defined Under Namespace

Classes: ErbTemplate

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.erb_render(content, html_file_path) ⇒ Object



16
17
18
19
# File 'lib/mdopen.rb', line 16

def erb_render(content, html_file_path)
  erb_t = Mdopen::ErbTemplate.new(content, get_template)
  erb_t.save(html_file_path)
end

.get_templateObject



21
22
23
24
# File 'lib/mdopen.rb', line 21

def get_template
  template_file = File.join(__dir__, 'templates/github.html.erb')
  File.read(template_file)
end

.md2html(md_file) ⇒ Object



26
27
28
29
# File 'lib/mdopen.rb', line 26

def md2html(md_file)
  parser = Redcarpet::Markdown.new(Redcarpet::Render::HTML.new, fenced_code_blocks: true)
  parser.render(md_file)
end

.open_cmdObject



31
32
33
34
35
36
37
38
39
# File 'lib/mdopen.rb', line 31

def open_cmd
  if OS.mac?
    "open"
  elsif OS.windows?
    "cmd /c start"
  else
    "xdg-open"
  end
end

.preview(md_file) ⇒ Object



9
10
11
12
13
14
# File 'lib/mdopen.rb', line 9

def preview(md_file)
  content = md2html(md_file)
  html_file_path = tmp_path
  erb_render(content, html_file_path)
  system "#{open_cmd} file://#{html_file_path}"
end

.tmp_filenameObject



47
48
49
# File 'lib/mdopen.rb', line 47

def tmp_filename
  [('a'..'z'), ('A'..'Z'), (0..9)].map(&:to_a).flatten.sample(12).join
end

.tmp_pathObject



41
42
43
44
45
# File 'lib/mdopen.rb', line 41

def tmp_path
  filename = tmp_filename
  tmp = Tempfile.new([filename, ".html"])
  tmp.path
end