(<)
08/05/2023

Importing HTML (orgmode-like) document with inline images to orgmode

You could use eww and copy the region of interest into an Orgmode file. (Option: If you have org-eww you could use its specialized copy.)

Then mark the copy and call the following function org-inline-images-to-images-in-region.

It stores copies of the inline-images as attachments and places links to them in the Org file.

(require 'org)
(require 'org-attach)

(defun next-property-display-data (start limit)
  (let ((pos (next-property-change start nil limit)))
    (while
	(and (< pos limit)
	     (let ((display-prop (plist-get (text-properties-at pos) 'display)))
	       (or (not display-prop)
		   (not (plist-get (cdr display-prop) :data)))))
      (setq pos (next-property-change pos nil limit)))
    pos))

(defun org-attach-inline-image-with-hashed-name (text-properties)
  (let ((display-data (cdr (plist-get text-properties 'display))))
    (when display-data
      (let* ((data (plist-get display-data :data)))
	(when data
	  (let* ((basename (sha1 data))
		 (org-attach-filename (concat (org-attach-dir t) "/" basename)))
	    (unless (file-exists-p org-attach-filename)
	      (with-temp-file org-attach-filename
		(setq buffer-file-coding-system 'binary)
		(set-buffer-multibyte nil)
		(insert data)))
	    (org-attach-sync)
	    org-attach-filename))))))

(defun org-inline-images-to-images-in-region (beg end)
  "Save the data of inline-images as attachment and insert a link to the attachment."
  (interactive "r")
  ;; I guess this should be refined.  todo: check out the
  ;; text-properties world!
  (save-excursion
    (goto-char beg)
    (when (and (plist-get (text-properties-at (point)) 'display)
	   (plist-get (cdr (plist-get (text-properties-at (point)) 'display)) 'data))
      (let ((name (org-attach-inline-image-with-hashed-name
		   (text-properties-at (point)))))
	(goto-char (next-property-change (point) nil end))
	(insert "[[" name "]]" )))
    (let ((pos (next-property-display-data (point) end)))
      (while (< pos end)
	(goto-char pos)
	(let ((name (org-attach-inline-image-with-hashed-name
		     (text-properties-at (point)))))
	  (goto-char (next-property-change (point) nil end))
	  (insert "[[" name "]]" ))
	(setq pos (next-property-display-data (point) end))))))

https://emacs.stackexchange.com/questions/34079/importing-html-orgmode-like-document-with-inline-images-to-orgmode

Categorie: :org-mode :emacs