mirror of
https://github.com/mattn/emmet-vim.git
synced 2025-12-07 03:04:27 +08:00
Implement image encode
This commit is contained in:
@@ -811,9 +811,9 @@ function! emmet#imageSize() abort
|
|||||||
return ''
|
return ''
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! emmet#encodeImage() abort
|
function! emmet#imageEncode() abort
|
||||||
let type = emmet#getFileType()
|
let type = emmet#getFileType()
|
||||||
return emmet#lang#{emmet#lang#type(type)}#encodeImage()
|
return emmet#lang#{emmet#lang#type(type)}#imageEncode()
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! emmet#toggleComment() abort
|
function! emmet#toggleComment() abort
|
||||||
|
|||||||
@@ -692,7 +692,7 @@ function! emmet#lang#html#imageSize() abort
|
|||||||
call emmet#util#setContent(img_region, html)
|
call emmet#util#setContent(img_region, html)
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! emmet#lang#html#encodeImage() abort
|
function! emmet#lang#html#imageEncode() abort
|
||||||
let img_region = emmet#util#searchRegion('<img\s', '>')
|
let img_region = emmet#util#searchRegion('<img\s', '>')
|
||||||
if !emmet#util#regionIsValid(img_region) || !emmet#util#cursorInRegion(img_region)
|
if !emmet#util#regionIsValid(img_region) || !emmet#util#cursorInRegion(img_region)
|
||||||
return
|
return
|
||||||
@@ -706,17 +706,16 @@ function! emmet#lang#html#encodeImage() abort
|
|||||||
return
|
return
|
||||||
endif
|
endif
|
||||||
let fn = current.attr.src
|
let fn = current.attr.src
|
||||||
if fn !~# '^\(/\|http\)'
|
if fn =~# '^\s*$'
|
||||||
|
return
|
||||||
|
elseif fn !~# '^\(/\|http\)'
|
||||||
let fn = simplify(expand('%:h') . '/' . fn)
|
let fn = simplify(expand('%:h') . '/' . fn)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
let [width, height] = emmet#util#getImageSize(fn)
|
let encoded = emmet#util#imageEncodeDecode(fn, 0)
|
||||||
if width == -1 && height == -1
|
let current.attr.src = encoded
|
||||||
return
|
let html = substitute(emmet#toString(current, 'html', 1), '\n', '', '')
|
||||||
endif
|
let html = substitute(html, '\${cursor}', '', '')
|
||||||
let current.attr.width = width
|
|
||||||
let current.attr.height = height
|
|
||||||
let html = emmet#toString(current, 'html', 1)
|
|
||||||
call emmet#util#setContent(img_region, html)
|
call emmet#util#setContent(img_region, html)
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
|||||||
@@ -233,12 +233,12 @@ function! emmet#util#getImageSize(fn) abort
|
|||||||
else
|
else
|
||||||
if fn !~# '^\w\+://'
|
if fn !~# '^\w\+://'
|
||||||
let path = fnamemodify(expand('%'), ':p:gs?\\?/?')
|
let path = fnamemodify(expand('%'), ':p:gs?\\?/?')
|
||||||
if has('win32') || has('win64') |
|
if has('win32') || has('win64') |
|
||||||
let path = tolower(path)
|
let path = tolower(path)
|
||||||
endif
|
endif
|
||||||
for k in keys(g:emmet_docroot)
|
for k in keys(g:emmet_docroot)
|
||||||
let root = fnamemodify(k, ':p:gs?\\?/?')
|
let root = fnamemodify(k, ':p:gs?\\?/?')
|
||||||
if has('win32') || has('win64') |
|
if has('win32') || has('win64') |
|
||||||
let root = tolower(root)
|
let root = tolower(root)
|
||||||
endif
|
endif
|
||||||
if stridx(path, root) == 0
|
if stridx(path, root) == 0
|
||||||
@@ -298,6 +298,67 @@ function! emmet#util#isImageMagickInstalled() abort
|
|||||||
return executable('identify')
|
return executable('identify')
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
|
function! s:b64encode(bytes, table, pad)
|
||||||
|
let b64 = []
|
||||||
|
for i in range(0, len(a:bytes) - 1, 3)
|
||||||
|
let n = a:bytes[i] * 0x10000
|
||||||
|
\ + get(a:bytes, i + 1, 0) * 0x100
|
||||||
|
\ + get(a:bytes, i + 2, 0)
|
||||||
|
call add(b64, a:table[n / 0x40000])
|
||||||
|
call add(b64, a:table[n / 0x1000 % 0x40])
|
||||||
|
call add(b64, a:table[n / 0x40 % 0x40])
|
||||||
|
call add(b64, a:table[n % 0x40])
|
||||||
|
endfor
|
||||||
|
if len(a:bytes) % 3 == 2
|
||||||
|
let b64[-1] = a:pad
|
||||||
|
elseif len(a:bytes) % 3 == 1
|
||||||
|
let b64[-1] = a:pad
|
||||||
|
let b64[-2] = a:pad
|
||||||
|
endif
|
||||||
|
return b64
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
function! emmet#util#imageEncodeDecode(fn, flag) abort
|
||||||
|
let fn = a:fn
|
||||||
|
|
||||||
|
if filereadable(fn)
|
||||||
|
let hex = substitute(system('xxd -p "'.fn.'"'), '\n', '', 'g')
|
||||||
|
else
|
||||||
|
if fn !~# '^\w\+://'
|
||||||
|
let path = fnamemodify(expand('%'), ':p:gs?\\?/?')
|
||||||
|
if has('win32') || has('win64') |
|
||||||
|
let path = tolower(path)
|
||||||
|
endif
|
||||||
|
for k in keys(g:emmet_docroot)
|
||||||
|
let root = fnamemodify(k, ':p:gs?\\?/?')
|
||||||
|
if has('win32') || has('win64') |
|
||||||
|
let root = tolower(root)
|
||||||
|
endif
|
||||||
|
if stridx(path, root) == 0
|
||||||
|
let v = g:emmet_docroot[k]
|
||||||
|
let fn = (len(v) == 0 ? k : v) . fn
|
||||||
|
break
|
||||||
|
endif
|
||||||
|
endfor
|
||||||
|
endif
|
||||||
|
let hex = substitute(system(g:emmet_curl_command.' "'.fn.'" | xxd -p'), '\n', '', 'g')
|
||||||
|
endif
|
||||||
|
|
||||||
|
let bin = map(split(hex, '..\zs'), 'eval("0x" . v:val)')
|
||||||
|
let table = split('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/', '\zs')
|
||||||
|
let ret = 'data:image/'
|
||||||
|
if hex =~# '^89504e470d0a1a0a'
|
||||||
|
let ret .= 'png'
|
||||||
|
elseif hex =~# '^ffd8'
|
||||||
|
let ret .= 'jpeg'
|
||||||
|
elseif hex =~# '^47494638'
|
||||||
|
let ret .= 'gif'
|
||||||
|
else
|
||||||
|
let ret .= 'unknown'
|
||||||
|
endif
|
||||||
|
return ret . ';base64,' . join(s:b64encode(bin, table, '='), '')
|
||||||
|
endfunction
|
||||||
|
|
||||||
function! emmet#util#unique(arr) abort
|
function! emmet#util#unique(arr) abort
|
||||||
let m = {}
|
let m = {}
|
||||||
let r = []
|
let r = []
|
||||||
|
|||||||
@@ -120,6 +120,8 @@ function! s:install_plugin(mode, buffer)
|
|||||||
\ {'mode': 'n', 'var': '', 'key': '', 'plug': 'emmet-move-prev-item', 'func': ':call emmet#moveNextPrevItem(1)<cr>'},
|
\ {'mode': 'n', 'var': '', 'key': '', 'plug': 'emmet-move-prev-item', 'func': ':call emmet#moveNextPrevItem(1)<cr>'},
|
||||||
\ {'mode': 'i', 'var': 'user_emmet_imagesize_key', 'key': 'i', 'plug': 'emmet-image-size', 'func': '<c-r>=emmet#util#closePopup()<cr><c-r>=emmet#imageSize()<cr>'},
|
\ {'mode': 'i', 'var': 'user_emmet_imagesize_key', 'key': 'i', 'plug': 'emmet-image-size', 'func': '<c-r>=emmet#util#closePopup()<cr><c-r>=emmet#imageSize()<cr>'},
|
||||||
\ {'mode': 'n', 'var': 'user_emmet_imagesize_key', 'key': 'i', 'plug': 'emmet-image-size', 'func': ':call emmet#imageSize()<cr>'},
|
\ {'mode': 'n', 'var': 'user_emmet_imagesize_key', 'key': 'i', 'plug': 'emmet-image-size', 'func': ':call emmet#imageSize()<cr>'},
|
||||||
|
\ {'mode': 'i', 'var': 'user_emmet_imageencode_key', 'key': 'I', 'plug': 'emmet-image-encode', 'func': '<c-r>=emmet#util#closePopup()<cr><c-r>=emmet#imageEncode()<cr>'},
|
||||||
|
\ {'mode': 'n', 'var': 'user_emmet_imageencode_key', 'key': 'I', 'plug': 'emmet-image-encode', 'func': ':call emmet#imageEncode()<cr>'},
|
||||||
\ {'mode': 'i', 'var': 'user_emmet_togglecomment_key', 'key': '/', 'plug': 'emmet-toggle-comment', 'func': '<c-r>=emmet#util#closePopup()<cr><c-r>=emmet#toggleComment()<cr>'},
|
\ {'mode': 'i', 'var': 'user_emmet_togglecomment_key', 'key': '/', 'plug': 'emmet-toggle-comment', 'func': '<c-r>=emmet#util#closePopup()<cr><c-r>=emmet#toggleComment()<cr>'},
|
||||||
\ {'mode': 'n', 'var': 'user_emmet_togglecomment_key', 'key': '/', 'plug': 'emmet-toggle-comment', 'func': ':call emmet#toggleComment()<cr>'},
|
\ {'mode': 'n', 'var': 'user_emmet_togglecomment_key', 'key': '/', 'plug': 'emmet-toggle-comment', 'func': ':call emmet#toggleComment()<cr>'},
|
||||||
\ {'mode': 'i', 'var': 'user_emmet_splitjointag_key', 'key': 'j', 'plug': 'emmet-split-join-tag', 'func': '<esc>:call emmet#splitJoinTag()<cr>'},
|
\ {'mode': 'i', 'var': 'user_emmet_splitjointag_key', 'key': 'j', 'plug': 'emmet-split-join-tag', 'func': '<esc>:call emmet#splitJoinTag()<cr>'},
|
||||||
|
|||||||
Reference in New Issue
Block a user