From 4c646a6bdeba1a5a55fdb78991c009e46853618b Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Sun, 18 Apr 2021 19:32:27 +0200 Subject: [PATCH] ox: Introduce "raw" pseudo objects * lisp/ox.el (org-export-raw-string): New function (org-export-data): (org-export-with-backend): React to raw objects. * testing/lisp/test-ox.el (test-org-export/raw-string): New test. A raw object is a pseudo-object (i.e., special object type that exists only during export) with the property of being exported as-is, with no processing from an export back-end. It is particularly useful to add contents to, or pre-process objects from, a parse tree. --- lisp/ox.el | 18 ++++++++++++++++-- testing/lisp/test-ox.el | 11 +++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lisp/ox.el b/lisp/ox.el index 6ec9b62ec..af7626b55 100644 --- a/lisp/ox.el +++ b/lisp/ox.el @@ -1881,6 +1881,8 @@ Return a string." (cond ;; Ignored element/object. ((memq data (plist-get info :ignore-list)) nil) + ;; Raw code. + ((eq type 'raw) (car (org-element-contents data))) ;; Plain text. ((eq type 'plain-text) (org-export-filter-apply-functions @@ -1947,7 +1949,7 @@ Return a string." data (cond ((not results) "") - ((memq type '(org-data plain-text nil)) results) + ((memq type '(nil org-data plain-text raw)) results) ;; Append the same white space between elements or objects ;; as in the original buffer, and call appropriate filters. (t @@ -3668,7 +3670,8 @@ the communication channel used for export, as a plist." (when (symbolp backend) (setq backend (org-export-get-backend backend))) (org-export-barf-if-invalid-backend backend) (let ((type (org-element-type data))) - (when (memq type '(nil org-data)) (error "No foreign transcoder available")) + (when (memq type '(nil org-data raw)) + (error "No foreign transcoder available")) (let* ((all-transcoders (org-export-get-all-transcoders backend)) (transcoder (cdr (assq type all-transcoders)))) (unless (functionp transcoder) (error "No foreign transcoder available")) @@ -4562,6 +4565,17 @@ objects of the same type." ((funcall predicate el info) (cl-incf counter) nil))) info 'first-match))))) +;;;; For Raw objects +;; +;; `org-export-raw-string' builds a pseudo-object out of a string +;; that any export back-end returns as-is. + +(defun org-export-raw-string (s) + "Return a raw object containing string S. +A raw string is exported as-is, with no additional processing +from the export back-end." + (unless (stringp s) (error "Wrong raw contents type: %S" s)) + (org-element-create 'raw nil s)) ;;;; For Src-Blocks ;; diff --git a/testing/lisp/test-ox.el b/testing/lisp/test-ox.el index 34b2d203e..3f39645af 100644 --- a/testing/lisp/test-ox.el +++ b/testing/lisp/test-ox.el @@ -3811,6 +3811,17 @@ Another text. (ref:text) (org-element-adopt-elements paragraph "begin " object "end") (org-export-data-with-backend paragraph backend nil))))) + +;;; Raw objects + +(ert-deftest test-org-export/raw-strings () + "Test exporting raw objects." + (should + (equal "foo" + (let ((backend (org-export-create-backend)) + (object (org-export-raw-string "foo"))) + (org-export-data-with-backend object backend nil))))) + ;;; Src-block and example-block