From 5a62a773a095b5ec2b40d51567f70b95c2cbab34 Mon Sep 17 00:00:00 2001 From: jao Date: Fri, 29 Mar 2024 05:32:43 +0000 Subject: jao-eww-to-org --- custom/jao-custom-eww.el | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/custom/jao-custom-eww.el b/custom/jao-custom-eww.el index 507069f..0b444a4 100644 --- a/custom/jao-custom-eww.el +++ b/custom/jao-custom-eww.el @@ -114,6 +114,48 @@ (use-package jao-eww-session :custom ((jao-eww-session-file "~/.emacs.d/cache/eww-session.eld"))) +;;; eww to org +(defun jao-eww-to-org (&optional dest) + (interactive) + (unless (org-region-active-p) + (let ((shr-width 80)) (eww-readable))) + (let* ((start (if (org-region-active-p) (region-beginning) (point-min))) + (end (if (org-region-active-p) (region-end) (point-max))) + (buff (or dest (generate-new-buffer "*eww-to-org*"))) + (link (eww-current-url)) + (title (or (plist-get eww-data :title) ""))) + (with-current-buffer buff + (insert "#+title: " title "\n#+link: " link "\n\n") + (org-mode)) + (save-excursion + (goto-char start) + (while (< (point) end) + (let* ((p (point)) + (props (text-properties-at p)) + (k (seq-find (lambda (x) (plist-get props x)) + '(shr-url image-url outline-level face))) + (prop (and k (list k (plist-get props k)))) + (next (if prop + (next-single-property-change p (car prop) nil end) + (next-property-change p nil end))) + (txt (buffer-substring (point) next)) + (txt (replace-regexp-in-string "\\*" "ยท" txt))) + (with-current-buffer buff + (insert + (pcase prop + ((and (or `(shr-url ,url) `(image-url ,url)) + (guard (string-match-p "^http" url))) + (let ((tt (replace-regexp-in-string "\n\\([^$]\\)" " \\1" txt))) + (org-link-make-string url tt))) + (`(outline-level ,n) + (concat (make-string (- (* 2 n) 1) ?*) " " txt "\n")) + ('(face italic) (format "/%s/ " (string-trim txt))) + ('(face bold) (format "*%s* " (string-trim txt))) + (t txt)))) + (goto-char next)))) + (pop-to-buffer buff) + (goto-char (point-min)))) + ;;; package (use-package shr :custom ((shr-width nil) @@ -157,7 +199,7 @@ ("T" . jao-mastodon-toot-url) ("u" . jao-eww-reopen) ("U" . jao-eww-reopen-new) - ("w" . org-eww-copy-for-org-mode) + ("w" . jao-eww-to-org) ("q" . jao-eww-close) ("x" . jao-rss-subscribe) ("y" . jao-eww-copy-link) -- cgit v1.2.3