Skip to content

Preview HTML version in web browser

Maxim Kim edited this page Aug 12, 2022 · 1 revision

With :make one can generate HTML file with the same name and in the same directory as source reST file.

That is usually enough for me, I have a mapping to open said generated HTML file when needed.

But for some people preview is needed without generating the file in the same directory. The next snippet is from https://github.com/habamax/vim-rst/issues/5.

It creates a buffer local command RSTView that generates an HTML for the current reST file in temporary directory and opens it using default OS web browser, add to ~/.vim/after/ftplugin/rst.vim:

func! s:rst_view() abort
  let output = tempname() . '.html'

  call system(printf("%s %s %s %s",
        \ "rst2html5.py",
        \ " --input-encoding=utf8 --stylesheet-path=minimal.css,responsive.css",
        \ shellescape(expand("%:p")),
        \ output
        \ ))

  " Comment/Uncomment what is appropriate
  " Windows
  exe ':silent !start ' . output
  " OSX
  " exe ':silent !xdg-open ' . output
  " Linux
  " exe ':silent !open ' . output
endfunc

command! -buffer RSTView call s:rst_view()