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

Percent encode paths #21

Merged
merged 3 commits into from
Sep 16, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions autoload/vivify.vim
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ let s:viv_url = 'http://localhost:' . ($VIV_PORT == '' ? '31622' : $VIV_PORT)

" Note: nvim's jobstart isn't exactly a drop-in replacement for vim's job_start
" See here: https://stackoverflow.com/questions/74999614/difference-between-vims-job-start-function-and-neovims-jobstart-functi
if has("nvim")
let s:job_start = function("jobstart")
if has('nvim')
let s:job_start = function('jobstart')

" job is job_id as returned from jobstart()
function! s:stdin_send_and_close(job, data)
Expand All @@ -18,16 +18,30 @@ else
call ch_close_in(l:channel)
endfunction

let s:job_start = function("job_start")
let s:job_start = function('job_start')
endif

function! s:percent_encode(path)
if has('python3')
" With python3 support, do proper percent encoding of url
" Vim needs to be compiled with +python
tuurep marked this conversation as resolved.
Show resolved Hide resolved
" Neovim needs pynvim installed
py3 from urllib.parse import quote
return py3eval('quote("' . a:path . '")')
else
" If no python3 support, still handle the biggest problem
" which is spaces in filenames
return substitute(a:path, ' ', '%20', 'g')
endif
endfunction

function! s:post(data)
let l:job = s:job_start([
\ 'curl',
\ '-X', 'POST',
\ '-H', 'Content-type: application/json',
\ '--data', '@-',
\ s:viv_url . '/viewer' . expand('%:p')
\ s:viv_url . '/viewer' . s:percent_encode(expand('%:p'))
\])
call s:stdin_send_and_close(l:job, json_encode(a:data))
endfunction
Expand All @@ -44,6 +58,6 @@ function! vivify#open()
" Note: nvim's jobstart doesn't use these opt keys
call s:job_start(
\ ['viv', expand('%:p')->substitute(':', '\\:', 'g') . ':' . getpos('.')[1]],
\ {"in_io": "null", "out_io": "null", "err_io": "null"}
\ {'in_io': 'null', 'out_io': 'null', 'err_io': 'null'}
\)
endfunction