diff options
Diffstat (limited to 'modules')
| -rw-r--r-- | modules/calendar-sync.el | 38 | ||||
| -rw-r--r-- | modules/cj-org-text.el | 58 |
2 files changed, 65 insertions, 31 deletions
diff --git a/modules/calendar-sync.el b/modules/calendar-sync.el index f87d0192..890dd9df 100644 --- a/modules/calendar-sync.el +++ b/modules/calendar-sync.el @@ -71,6 +71,7 @@ (require 'cl-lib) (require 'subr-x) +(require 'cj-org-text) (defun calendar-sync--log-silently (format-string &rest args) "Log FORMAT-STRING with ARGS without requiring the full config." @@ -294,31 +295,6 @@ Returns nil for nil input. Returns empty string for whitespace-only input." (when text (string-trim (calendar-sync--strip-html (calendar-sync--unescape-ics-text text))))) -(defun calendar-sync--sanitize-org-body (text) - "Sanitize TEXT for safe inclusion as org body content. -Replaces leading asterisks with dashes to prevent lines from being -parsed as org headings. Handles multiple levels (e.g. ** becomes --)." - (when text - (replace-regexp-in-string - "^\\(\\*+\\) " - (lambda (match) - (concat (make-string (length (match-string 1 match)) ?-) " ")) - text))) - -(defun calendar-sync--sanitize-org-property-value (text) - "Sanitize TEXT for safe inclusion as a single Org property value." - (when text - (string-trim - (replace-regexp-in-string - "[[:space:]\n\r]+" - " " - text)))) - -(defun calendar-sync--sanitize-org-heading (text) - "Sanitize TEXT for safe inclusion as a single Org heading title." - (calendar-sync--sanitize-org-property-value - (calendar-sync--sanitize-org-body text))) - ;;; Date Utilities (defun calendar-sync--add-months (date months) @@ -1091,7 +1067,7 @@ Cleans text fields (description, location, summary) via `calendar-sync--clean-te "Convert parsed EVENT plist to org entry string. Produces property drawer with LOCATION, ORGANIZER, STATUS, URL when present. Description appears as body text after the drawer." - (let* ((summary (calendar-sync--sanitize-org-heading + (let* ((summary (cj/org-sanitize-heading (or (plist-get event :summary) "(No Title)"))) (description (plist-get event :description)) (location (plist-get event :location)) @@ -1106,22 +1082,22 @@ Description appears as body text after the drawer." ;; Collect non-nil properties (when (and location (not (string-empty-p location))) (push (format ":LOCATION: %s" - (calendar-sync--sanitize-org-property-value location)) + (cj/org-sanitize-property-value location)) props)) (when organizer (let ((org-name (or (plist-get organizer :cn) (plist-get organizer :email)))) (when org-name (push (format ":ORGANIZER: %s" - (calendar-sync--sanitize-org-property-value org-name)) + (cj/org-sanitize-property-value org-name)) props)))) (when (and status (not (string-empty-p status))) (push (format ":STATUS: %s" - (calendar-sync--sanitize-org-property-value status)) + (cj/org-sanitize-property-value status)) props)) (when (and url (not (string-empty-p url))) (push (format ":URL: %s" - (calendar-sync--sanitize-org-property-value url)) + (cj/org-sanitize-property-value url)) props)) (setq props (nreverse props)) ;; Build output @@ -1134,7 +1110,7 @@ Description appears as body text after the drawer." (push ":END:" parts)) ;; Add description as body text (sanitized to prevent org heading conflicts) (when (and description (not (string-empty-p description))) - (push (calendar-sync--sanitize-org-body description) parts)) + (push (cj/org-sanitize-body-text description) parts)) (string-join (nreverse parts) "\n")))) (defun calendar-sync--event-start-time (event) diff --git a/modules/cj-org-text.el b/modules/cj-org-text.el new file mode 100644 index 00000000..69224573 --- /dev/null +++ b/modules/cj-org-text.el @@ -0,0 +1,58 @@ +;;; cj-org-text.el --- Pure helpers for sanitizing external text into Org -*- lexical-binding: t; -*- + +;; Author: Craig Jennings <c@cjennings.net> + +;;; Commentary: + +;; Pure string helpers for safely composing Org-mode content from +;; external text (calendar event bodies, web-clipped HTML, mail +;; subject lines, AI conversation transcripts, etc.). +;; +;; The shared concern is that text from outside sources can contain +;; characters that disturb Org structure if pasted verbatim: +;; +;; - leading `*' creates an unintended heading, +;; - newlines inside a property value spawn extra drawer lines, +;; - newlines inside a heading split it into two outline entries. +;; +;; These helpers neutralize each pattern with predictable, testable +;; replacements. They are pure (string in, string out, nil-safe) and +;; have no Org-mode dependency, so they remain useful in batch and in +;; tests without loading Org. + +;;; Code: + +(defun cj/org-sanitize-body-text (text) + "Sanitize TEXT for safe inclusion as Org body content. +Replaces leading asterisks with dashes so external lines aren't +parsed as Org headings. Handles multiple levels (`**' becomes `--'). +Returns nil for nil input." + (when text + (replace-regexp-in-string + "^\\(\\*+\\) " + (lambda (match) + (concat (make-string (length (match-string 1 match)) ?-) " ")) + text))) + +(defun cj/org-sanitize-property-value (text) + "Sanitize TEXT for safe inclusion as a single Org property value. +Collapses whitespace and newlines into single spaces and trims, so the +result fits on one line of an Org property drawer. Returns nil for +nil input." + (when text + (string-trim + (replace-regexp-in-string + "[[:space:]\n\r]+" + " " + text)))) + +(defun cj/org-sanitize-heading (text) + "Sanitize TEXT for safe inclusion as a single Org heading title. +Composes `cj/org-sanitize-body-text' (neutralizes leading stars) and +`cj/org-sanitize-property-value' (flattens to a single line). Returns +nil for nil input." + (cj/org-sanitize-property-value + (cj/org-sanitize-body-text text))) + +(provide 'cj-org-text) +;;; cj-org-text.el ends here |
