diff options
Diffstat (limited to 'modules/org-webclipper.el')
| -rw-r--r-- | modules/org-webclipper.el | 80 |
1 files changed, 38 insertions, 42 deletions
diff --git a/modules/org-webclipper.el b/modules/org-webclipper.el index e8f2cf23..7b024e43 100644 --- a/modules/org-webclipper.el +++ b/modules/org-webclipper.el @@ -11,6 +11,7 @@ ;; - Automatic conversion to Org format using eww-readable and Pandoc ;; - One-click capture from any web page ;; - Preserves page structure and formatting +;; - Smart heading adjustment (removes page title, demotes remaining headings) ;; ;; Setup: ;; 1. Ensure this file is loaded in your Emacs configuration @@ -30,6 +31,11 @@ ;; The clipped content will be added to the file specified by `webclipped-file` ;; under the "Webclipped Inbox" heading with proper formatting and metadata. ;; +;; Architecture: +;; - cj/--process-webclip-content: Pure function for content processing +;; - cj/org-protocol-webclip-handler: Handles URL fetching and capture +;; - cj/org-webclipper-EWW: Direct capture from EWW/W3M buffers +;; ;; Requirements: ;; - org-web-tools package ;; - Pandoc installed on your system @@ -37,23 +43,6 @@ ;;; Code: -;; Declare functions and variables to avoid warnings -(declare-function org-protocol-protocol-alist "org-protocol") -(declare-function org-capture "org-capture") -(declare-function org-capture-get "org-capture") -(declare-function org-web-tools--url-as-readable-org "org-web-tools") -(declare-function org-w3m-copy-for-org-mode "org-w3m") -(declare-function org-eww-copy-for-org-mode "org-eww") -(declare-function org-at-heading-p "org") -(declare-function org-heading-components "org") -(declare-function org-copy-subtree "org") -(declare-function org-cut-subtree "org") -(declare-function org-id-new "org-id") -(declare-function org-roam-db-sync "org-roam") -(defvar org-capture-templates) -(defvar org-protocol-protocol-alist) -(defvar org-roam-directory) -(defvar webclipped-file) ;; Variables for storing org-protocol data (defvar cj/webclip-current-url nil @@ -66,6 +55,9 @@ (defvar cj/webclipper-initialized nil "Track if webclipper has been initialized.") +(use-package org-web-tools + :defer t) + ;; Lazy initialization function (defun cj/webclipper-ensure-initialized () "Ensure webclipper is initialized when first used." @@ -73,6 +65,7 @@ ;; Load required packages now (require 'org-protocol) (require 'org-capture) + (require 'org-web-tools) (require 'user-constants) ;; for webclipped-file ;; Register the org-protocol handler @@ -102,7 +95,28 @@ (setq cj/webclipper-initialized t))) -;;;###autoload +(defun cj/--process-webclip-content (org-content) + "Process webclip ORG-CONTENT by removing first heading and demoting others. +ORG-CONTENT is the raw org-mode text from the web page conversion. +Returns the processed content as a string with: +- First top-level heading removed +- Initial blank lines removed +- All remaining headings demoted by one level" + (with-temp-buffer + (insert org-content) + (goto-char (point-min)) + ;; Skip the first heading line (we'll use our template's heading) + (when (looking-at "^\\* .*\n") + (delete-region (match-beginning 0) (match-end 0))) + ;; Remove any initial blank lines + (while (looking-at "^[ \t]*\n") + (delete-char 1)) + ;; Demote all remaining headings by one level + ;; since our template already provides the top-level heading + (while (re-search-forward "^\\(\\*+\\) " nil t) + (replace-match (concat (match-string 1) "* ") t t)) + (buffer-string))) + (defun cj/org-protocol-webclip (info) "Process org-protocol webclip requests. INFO is a plist containing :url and :title from the org-protocol call." @@ -135,22 +149,7 @@ It fetches the page content and converts it to Org format." (error "No URL provided for clipping") (condition-case err (let* ((org-content (org-web-tools--url-as-readable-org url)) - ;; Process the content to adjust heading levels - (processed-content - (with-temp-buffer - (insert org-content) - (goto-char (point-min)) - ;; Skip the first heading line (we'll use our template's heading) - (when (looking-at "^\\* .*\n") - (delete-region (match-beginning 0) (match-end 0))) - ;; Remove any initial blank lines - (while (looking-at "^[ \t]*\n") - (delete-char 1)) - ;; Demote all remaining headings by one level - ;; since our template already provides the top-level heading - (while (re-search-forward "^\\(\\*+\\) " nil t) - (replace-match (concat (match-string 1) "* ") t t)) - (buffer-string)))) + (processed-content (cj/--process-webclip-content org-content))) ;; Show success message with the title (require 'user-constants) ;; Ensure webclipped-file is available (message "'%s' added to %s" title webclipped-file) @@ -162,7 +161,7 @@ It fetches the page content and converts it to Org format." ;; ---------------------------- Org Webpage Clipper ---------------------------- -;;;###autoload + (defun cj/org-webclipper-EWW () "Capture the current web page for later viewing in an Org file. Return the yanked content as a string so templates can insert it." @@ -182,13 +181,11 @@ Return the yanked content as a string so templates can insert it." ;; extract the webpage content from the kill ring (car kill-ring))) - ;; ----------------------------- Webclipper Keymap ----------------------------- ;; keymaps shouldn't be required for webclipper -;; TASK Move org-branch to roam functionality under org-roam ;; Setup keymaps -;; ;;;###autoload +;; ;; (defun cj/webclipper-setup-keymaps () ;; "Setup webclipper keymaps." ;; (define-prefix-command 'cj/webclipper-map nil @@ -201,7 +198,6 @@ Return the yanked content as a string so templates can insert it." ;; (cj/webclipper-setup-keymaps)) ;; Register protocol handler early for external calls -;;;###autoload (with-eval-after-load 'org-protocol (unless (assoc "webclip" org-protocol-protocol-alist) (add-to-list 'org-protocol-protocol-alist @@ -210,9 +206,9 @@ Return the yanked content as a string so templates can insert it." :function cj/org-protocol-webclip :kill-client t)))) -(with-eval-after-load 'cj/custom-keymap - (require 'org-webclipper) - (cj/webclipper-setup-keymaps)) +;; (with-eval-after-load 'cj/custom-keymap +;; (require 'org-webclipper) +;; (cj/webclipper-setup-keymaps)) (provide 'org-webclipper) ;;; org-webclipper.el ends here |
