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

Paste Image feature #28

Merged
merged 3 commits into from
Mar 16, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions doc/dokuvimki.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -159,6 +163,12 @@ COMMANDS *dokuvimki-commands*
:DWupload <file> Allows to upload a file in the current namespace.
:DWupload! <file>

: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.

Expand Down
57 changes: 57 additions & 0 deletions plugin/dokuvimki.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import time
import subprocess

from tempfile import TemporaryDirectory

from os import path

__version__ = '2021.4.4'
__author__ = 'Michael Klier <[email protected]>'
__license__ = 'MIT'
Expand All @@ -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,):
Expand Down Expand Up @@ -64,6 +75,8 @@ def __init__(self):
vim.command("command! -nargs=0 -bang DWclose exec('Py dokuvimki.close(\"<bang>\")')")
vim.command("command! -nargs=0 DWdiffclose exec('Py dokuvimki.diff_close()')")
vim.command("command! -complete=file -bang -nargs=1 DWupload exec('Py dokuvimki.upload(<f-args>,\"<bang>\")')")
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(\"<bang>\")')")

Expand All @@ -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)

Expand Down Expand Up @@ -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.
Expand Down