From c44a52a7905b605a6537e3ff9bb4fe3afede0485 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 10 May 2026 14:34:52 -0500 Subject: refactor(cj-org-text): extract Org-safe text sanitizers from calendar-sync Phase 3 of utility-consolidation. Three sanitizers moved from calendar-sync.el into a new cj-org-text.el module so other consumers (web-clipper, AI conversation, mail-to-org capture) can compose Org content from external text without depending on calendar: - `calendar-sync--sanitize-org-body' -> `cj/org-sanitize-body-text' - `calendar-sync--sanitize-org-property-value' -> `cj/org-sanitize-property-value' - `calendar-sync--sanitize-org-heading' -> `cj/org-sanitize-heading' The helpers stay pure (string in, string out, nil-safe) and have no Org-mode dependency, so they work in batch and in tests without loading Org. Migrate calendar-sync.el to use the new public names: drop the three local defuns, add `(require \='cj-org-text)', update the six call sites in `calendar-sync--make-event-entry'. Move the existing 17-test file to `tests/test-cj-org-text-sanitize.el', rename test names to match the new helpers, add 1 nil-input test for `cj/org-sanitize-heading' that wasn't in the original file. Total: 18 Normal/Boundary tests across the three helpers. --- modules/cj-org-text.el | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 modules/cj-org-text.el (limited to 'modules/cj-org-text.el') 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 + +;;; 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 -- cgit v1.2.3