├── README.md └── clipboard2org.el /README.md: -------------------------------------------------------------------------------- 1 | # clipboard2org 2 | Paste html, or paste images or paste lists of files to an org file. 3 | 4 | This package converts HTML copied in the clipboard to ORG by using Pandoc 5 | And it automatically save images in png or jpeg from the clipboard to a 6 | sub-directory ./img/ by generating a random name, and then inserts it in the 7 | org file. 8 | 9 | 10 | ## Usage 11 | Add it to path, run (require 'clipboard2org). 12 | Bind clipboard2org-paste to some key, suggested: 13 | (define-key org-mode-map (kbd "C-S-y ") 'clipboard2org-paste) 14 | -------------------------------------------------------------------------------- /clipboard2org.el: -------------------------------------------------------------------------------- 1 | ;;; clipboard2org.el --- paste html or images to an org file 2 | 3 | ;;; Commentary: 4 | 5 | ;; This package converts HTML copied in the clipboard to ORG by using Pandoc 6 | ;; And it automatically save images in png or jpeg from the clipboard to a 7 | ;; sub-directory ./img/ by generating a random name, and then inserts it in the 8 | ;; org file. 9 | ;; 10 | ;; Inspired by https://emacs.stackexchange.com/questions/12121/org-mode-parsing-rich-html-directly-when-pasting 11 | 12 | ;;; License: 13 | 14 | ;; This program is free software; you can redistribute it and/or modify 15 | ;; it under the terms of the GNU General Public License as published by 16 | ;; the Free Software Foundation, either version 3 of the License, or 17 | ;; (at your option) any later version. 18 | 19 | ;; This program is distributed in the hope that it will be useful, 20 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 21 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 | ;; GNU General Public License for more details. 23 | 24 | ;; You should have received a copy of the GNU General Public License 25 | ;; along with this program. If not, see . 26 | 27 | (require 'org) 28 | 29 | ;;; Code: 30 | (defun clipboard2org-paste() 31 | "Paste HTML as org by using pandoc, or insert an image from the clipboard. 32 | It inserts the image by first saving it with the unixtime name in a ./img/ sub-directory" 33 | (interactive) 34 | (let* ((data-file (gui-backend-get-selection 'CLIPBOARD 'text/uri-list)) 35 | (data-html (or (gui-backend-get-selection 'PRIMARY 'text/html) (gui-backend-get-selection 'CLIPBOARD 'text/html))) 36 | (data-png (or (gui-backend-get-selection 'PRIMARY 'image/png) (gui-backend-get-selection 'CLIPBOARD 'image/png))) 37 | (data-jpg (or (gui-backend-get-selection 'PRIMARY 'image/jpeg) (gui-backend-get-selection 'CLIPBOARD 'image/jpeg))) 38 | (text-raw (gui-get-selection))) 39 | (cond 40 | (data-file (clipboard2org--file data-file)) 41 | (data-jpg (clipboard2org--image data-jpg ".jpg")) 42 | (data-png (clipboard2org--image data-png ".png")) 43 | (data-html (clipboard2org--html data-html)) 44 | (text-raw (yank))))) 45 | 46 | 47 | (defun clipboard2org--file(file-url) 48 | "Inserts a list of files. Useful if you copied files from your file explorer 49 | and want to insert links to them into your org file" 50 | (let* ((decoded-file-url (decode-coding-string file-url 'raw-text t nil)) 51 | (decoded-file-url (substring decoded-file-url 0 -1)) 52 | (file-list (split-string decoded-file-url))) 53 | (dolist (file-url file-list) 54 | (let* ((file-url (replace-regexp-in-string "%20" " " file-url)) 55 | (file-name (file-name-nondirectory file-url))) 56 | (insert (concat "[["file-url"]["file-name "]]\n")))))) 57 | 58 | (defun clipboard2org--html(html-data) 59 | "Insert html data into the buffer. 60 | HTML-DATA: html data from the clipboard" 61 | (let* ((decoded-html (decode-coding-string html-data 'unix)) 62 | (text-html (shell-command-to-string (concat "echo " (shell-quote-argument decoded-html) "|timeout 2 pandoc --wrap=preserve -f html-native_divs-native_spans -t org")))) 63 | (insert text-html))) 64 | 65 | 66 | (defun clipboard2org--image(image-data extension) 67 | "Insert image into the buffer. 68 | IMAGE-DATA: Raw image-data from the clipboard 69 | EXTENSION: the image extensions, for example png, jpg. Additional support for others is trivial." 70 | (let* ((image-directory "./img/") 71 | (temp-file-name 72 | (let ((coding-system-for-write 'raw-text) 73 | (buffer-file-coding-system 'raw-text)) 74 | (make-directory image-directory t) 75 | (make-temp-file "img" nil extension image-data))) 76 | (file-name (replace-regexp-in-string "\\." "" (format "%s" (float-time)))) 77 | (new-name (concat image-directory file-name extension))) 78 | (rename-file temp-file-name new-name) 79 | (insert "#+ATTR_ORG: :width 300\n") 80 | (insert (concat "#+CAPTION: " "\n")) 81 | (insert (concat "[[file:" new-name "][file:" new-name "]]")) 82 | (org-display-inline-images))) 83 | 84 | (provide 'clipboard2org) 85 | ;;; clipboard2org.el ends here 86 | --------------------------------------------------------------------------------