diff --git a/doc/dokuvimki.txt b/doc/dokuvimki.txt index a484a55..e4f8cb6 100644 --- a/doc/dokuvimki.txt +++ b/doc/dokuvimki.txt @@ -83,6 +83,10 @@ The following variables are optional and have a default: g:DokuVimKi_HTTP_BASIC_AUTH Use HTTP basic auth (default off). Set this to anything but the empty string to enable. +g:DokuVimKi_IMG_SUB_NS The default namespace where an image should be + uploaded when pasting from the clipboard (default + images). + g:DokuVimKi_INDEX_WINWIDTH The width of the index window (default 30). g:DokuVimKi_DEFAULT_SUM Default summary used if no summary is given on @@ -159,6 +163,12 @@ COMMANDS *dokuvimki-commands* :DWupload Allows to upload a file in the current namespace. :DWupload! +:DWpasteimage Upload an image from the clipboard to the remote + wiki and paste the media link into the buffer. + +:DWpasteimageAfer The same as DWpasteimage but paste the link after + the cursor. + :DWquit Quits the current session and quits vim. This will :DWquit! fail if there are unsaved changes. diff --git a/plugin/dokuvimki.py b/plugin/dokuvimki.py index d07ba19..0bcc577 100644 --- a/plugin/dokuvimki.py +++ b/plugin/dokuvimki.py @@ -9,6 +9,10 @@ import time import subprocess +from tempfile import TemporaryDirectory + +from os import path + __version__ = '2021.4.4' __author__ = 'Michael Klier ' __license__ = 'MIT' @@ -21,6 +25,13 @@ print('DokuVimKi Error: The dokuwikixmlrpc python module is missing!', file=sys.stderr) has_dokuwikixmlrpc = False +try: + from PIL import ImageGrab + has_pil = True +except ImportError: + has_pil = False + + vim_version = int(vim.eval('v:version')) if sys.version_info < (3,): @@ -64,6 +75,8 @@ def __init__(self): vim.command("command! -nargs=0 -bang DWclose exec('Py dokuvimki.close(\"\")')") vim.command("command! -nargs=0 DWdiffclose exec('Py dokuvimki.diff_close()')") vim.command("command! -complete=file -bang -nargs=1 DWupload exec('Py dokuvimki.upload(,\"\")')") + vim.command("command! -nargs=0 DWpasteimage exec('Py dokuvimki.paste_image(0)')") + vim.command("command! -nargs=0 DWpasteimageAfter exec('Py dokuvimki.paste_image(1)')") vim.command("command! -nargs=0 DWhelp exec('Py dokuvimki.help()')") vim.command("command! -nargs=0 -bang DWquit exec('Py dokuvimki.quit(\"\")')") @@ -84,6 +97,8 @@ def __init__(self): self.default_sum = vim.eval('g:DokuVimKi_DEFAULT_SUM') + self.img_sub_ns = vim.eval("g:DokuVimKi_IMG_SUB_NS") + self.index_winwith = vim.eval('g:DokuVimKi_INDEX_WINWIDTH') self.index(self.cur_ns, True) @@ -321,6 +336,48 @@ def upload(self, file, overwrite=False): else: print('%s is not a file' % path, file=sys.stderr) + def paste_image(self, after): + """ + Uploads an image from the clipboard to the remote wiki + and paste the media link into the buffer. + """ + if not has_pil: + print('DokuVimKi Error: The PIL python module is missing!', file=sys.stderr) + return + + img = ImageGrab.grabclipboard() + if img is None: + return + + with TemporaryDirectory() as tmpdir: + img_name = vim.exec_lua("return vim.fn.input('File Name? ', '')") + img_name = path.basename(img_name) + if img_name == "": + timestamp = int(time.time()) + img_name = f"image_{timestamp}" + + img_path = path.abspath(path.join(tmpdir, f"{img_name}.png")) + img.save(img_path, "PNG") + + img_ns = self.cur_ns + if self.img_sub_ns: + img_ns = f"{img_ns}{self.img_sub_ns}:" + + old_ns = self.cur_ns + self.cur_ns = img_ns + self.upload(img_path, True) + self.cur_ns = old_ns + + img_url = f"{img_ns}{img_name}.png" + pattern = "{{" + img_url + "}}" + if vim.eval("mode()") in ["v", "V"]: + vim.command(f"normal! c{pattern}") + else: + if after: + vim.command(f"normal! a{pattern}") + else: + vim.command(f"normal! i{pattern}") + def cd(self, query=''): """ Changes into the given namespace.