Module: Mdopen
- Defined in:
- lib/mdopen.rb,
lib/mdopen/version.rb
Constant Summary collapse
- VERSION =
"0.1.5"
Class Method Summary collapse
- .erb_render(content, tmp_file) ⇒ Object
- .md2html(md_file) ⇒ Object
- .new_tmpfile ⇒ Object
- .open_cmd ⇒ Object
- .preview(md_file) ⇒ Object
- .tmp_filename ⇒ Object
Class Method Details
.erb_render(content, tmp_file) ⇒ Object
17 18 19 20 21 22 23 |
# File 'lib/mdopen.rb', line 17 def erb_render(content, tmp_file) template_path = File.join(__dir__, 'templates/github.html.erb') template = Tilt::ERBTemplate.new(template_path) output = template.render(self, content: content) tmp_file.write(output) tmp_file.close end |
.md2html(md_file) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/mdopen.rb', line 25 def md2html(md_file) parser = Redcarpet::Markdown.new( Redcarpet::Render::HTML.new, fenced_code_blocks: true, tables: true, autolink: true, space_after_headers: true, highlight: true, quote: true, strikethrough: true, superscript: true, no_intra_emphasis: true) parser.render(md_file) end |
.new_tmpfile ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/mdopen.rb', line 50 def new_tmpfile filename = tmp_filename tmp_file = Tempfile.new([filename, '.html']) # prevent tempfile from deleting by GC ObjectSpace.undefine_finalizer(tmp_file) tmp_file end |
.open_cmd ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/mdopen.rb', line 40 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 15 |
# File 'lib/mdopen.rb', line 9 def preview(md_file) content = md2html(md_file) tmp_file = new_tmpfile html_file_path = tmp_file.path erb_render(content, tmp_file) system "#{open_cmd} file://#{html_file_path}" end |
.tmp_filename ⇒ Object
58 59 60 |
# File 'lib/mdopen.rb', line 58 def tmp_filename [('a'..'z'), ('A'..'Z'), (0..9)].map(&:to_a).flatten.sample(12).join end |