Implement image encode

This commit is contained in:
Yasuhiro Matsumoto
2019-06-18 15:44:00 +09:00
parent d02023cd7a
commit 7b08b89d7d
4 changed files with 75 additions and 13 deletions

View File

@@ -811,9 +811,9 @@ function! emmet#imageSize() abort
return ''
endfunction
function! emmet#encodeImage() abort
function! emmet#imageEncode() abort
let type = emmet#getFileType()
return emmet#lang#{emmet#lang#type(type)}#encodeImage()
return emmet#lang#{emmet#lang#type(type)}#imageEncode()
endfunction
function! emmet#toggleComment() abort

View File

@@ -692,7 +692,7 @@ function! emmet#lang#html#imageSize() abort
call emmet#util#setContent(img_region, html)
endfunction
function! emmet#lang#html#encodeImage() abort
function! emmet#lang#html#imageEncode() abort
let img_region = emmet#util#searchRegion('<img\s', '>')
if !emmet#util#regionIsValid(img_region) || !emmet#util#cursorInRegion(img_region)
return
@@ -706,17 +706,16 @@ function! emmet#lang#html#encodeImage() abort
return
endif
let fn = current.attr.src
if fn !~# '^\(/\|http\)'
if fn =~# '^\s*$'
return
elseif fn !~# '^\(/\|http\)'
let fn = simplify(expand('%:h') . '/' . fn)
endif
let [width, height] = emmet#util#getImageSize(fn)
if width == -1 && height == -1
return
endif
let current.attr.width = width
let current.attr.height = height
let html = emmet#toString(current, 'html', 1)
let encoded = emmet#util#imageEncodeDecode(fn, 0)
let current.attr.src = encoded
let html = substitute(emmet#toString(current, 'html', 1), '\n', '', '')
let html = substitute(html, '\${cursor}', '', '')
call emmet#util#setContent(img_region, html)
endfunction

View File

@@ -233,12 +233,12 @@ function! emmet#util#getImageSize(fn) abort
else
if fn !~# '^\w\+://'
let path = fnamemodify(expand('%'), ':p:gs?\\?/?')
if has('win32') || has('win64') |
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') |
if has('win32') || has('win64') |
let root = tolower(root)
endif
if stridx(path, root) == 0
@@ -298,6 +298,67 @@ function! emmet#util#isImageMagickInstalled() abort
return executable('identify')
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
let m = {}
let r = []

View File

@@ -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': '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': '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': '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>'},