Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support :make variant/option that renders to a temporary file and opens a webbrowser to display it? #5

Closed
anntzer opened this issue Nov 4, 2021 · 6 comments
Labels
enhancement New feature or request

Comments

@anntzer
Copy link

anntzer commented Nov 4, 2021

Your :make currently writes the html output next to the rst file. I have personally found it useful to write instead to a temporary directory, and immediately display the result in a webbrowser. See marshallward/vim-restructuredtext#62 for the implementation I currently use, although variants are certainly possible. Perhaps this behavior can be configured with a variable?

@habamax
Copy link
Owner

habamax commented Nov 4, 2021

Probably. I for now use :make and a simple mapping that opens generated html in a browser.

@anntzer
Copy link
Author

anntzer commented Nov 4, 2021

If you can suggest a mapping implementation that would be nice too.

@habamax
Copy link
Owner

habamax commented Nov 4, 2021

Something similar is currently in my after/ftplugin/rst.vim

" Open filename in an OS
func! OSopen(url) abort
    let url = a:url
    if has("win32") || has("win32unix")
        let cmd = ':silent !start'
    elseif executable('xdg-open')
        let cmd = ":silent !xdg-open"
    elseif executable('open')
        let cmd = ":silent !open"
    else
        echohl Error
        echomsg "Can't find proper opener for an URL!"
        echohl None
        return
    endif

    try
        echom cmd . ' "' . url . '"'
        exe cmd . ' "' . url . '"'
    catch
        echohl Error
        echomsg v:exception
        echohl None
    finally
        redraw!
    endtry
endfunc


command -buffer RSTViewHtml :call OSopen(expand("%:p:r").'.html')
nnoremap <buffer> goh :RSTViewHtml<CR>

@anntzer
Copy link
Author

anntzer commented Nov 4, 2021

Ah, thanks, I'll play with that. (For the OS-dependent side, perhaps you can use python -mwebbrowser instead -- if rst2html5 works, you probably have a python in your path.)

@habamax habamax added the enhancement New feature or request label Nov 4, 2021
@habamax
Copy link
Owner

habamax commented Nov 7, 2021

I wish there was webbrowser extension, like Asciidoctor.js Live Preview that would render plain rst file as html.

image

@habamax
Copy link
Owner

habamax commented Nov 11, 2021

I was thinking about it the other day and decided not to add it.

One can add following snippet into their ~/.vim/after/ftplugin/rst.vim to get desired/tailored behavior:

  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()

@habamax habamax closed this as completed Nov 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants