Skip to content

Instantly share code, notes, and snippets.

@kusor
Created December 1, 2010 07:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kusor/723126 to your computer and use it in GitHub Desktop.
Save kusor/723126 to your computer and use it in GitHub Desktop.
Little plugin to open Markdown preview in browser.
" markdown.vim
" Markdown preview using RDiscount ruby gem.
" Put this file into .vim/ftplugin
command! -nargs=0 MarkdownPreview call MarkdownRenderBufferToPreview()
noremap <buffer> <Leader>rp :MarkdownPreview<CR>
setlocal ignorecase
setlocal wrap
setlocal lbr
function! MarkdownRender(lines)
if (system('which ruby') == "")
throw "Could not find ruby!"
end
let text = join(a:lines, "\n")
let html = system("ruby -e \"def e(msg); puts msg; exit 1; end; begin; require 'rubygems'; rescue LoadError; e('rubygems not found'); end; begin; require 'rdiscount'; rescue LoadError; e('RDiscount gem not installed. Run this from the terminal: sudo gem install rdiscount'); end; puts(RDiscount.new(\\$stdin.read).to_html)\"", text)
return html
endfunction
function! MarkdownRenderFile(lines, filename)
let html = MarkdownRender(getbufline(bufname("%"), 1, '$'))
let html = "<html><head><title>" . bufname("%") . "</title><body>\n" . html . "\n</body></html>"
return writefile(split(html, "\n"), a:filename)
endfunction
function! MarkdownRenderBufferToPreview()
let filename = "/tmp/markdown-preview.html"
call MarkdownRenderFile(getbufline(bufname("%"), 1, '$'), filename)
" Modify this line to make it compatible on other platforms
call system("open -a Safari ". filename)
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment