Skip to content

Instantly share code, notes, and snippets.

@hail2u
Created August 7, 2014 12:53
Show Gist options
  • Save hail2u/231ae6d3c7e87acccba6 to your computer and use it in GitHub Desktop.
Save hail2u/231ae6d3c7e87acccba6 to your computer and use it in GitHub Desktop.
カーソル下のパスをData URIに変換するVimスクリプト。通常はbase64だが、SVGだけSVGOを使ってURLエンコードする。
#!/usr/bin/env node
var fs = require('fs');
var mime = require('mime');
var file = process.argv.slice(2)[0];
fs.readFile(file, function (error, data) {
if (error) {
throw error;
}
var datauri = 'data:' + mime.lookup(file) + ';base64,' + new Buffer(data).toString('base64');
process.stdout.write(datauri);
});
" Convert a file under a cursor to Data URI
function! s:ConvertCFileToDataURI()
let cfile = expand('<cfile>')
let command = ''
if matchend(cfile, '\.svg') == strlen(cfile)
let command = 'svgo --datauri=enc -o - -i '
else
let command = 'datauri '
endif
let @d = substitute(getline('.'), cfile, system(command . cfile), '')
normal! V"dp
endfunction
command! DataURI call <SID>ConvertCFileToDataURI()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment