>From a57412351a21cecf0a83264139cc99bcc77f1093 Mon Sep 17 00:00:00 2001 From: Bruno BARBIER Date: Mon, 17 Oct 2022 20:19:02 +0200 Subject: [PATCH] lisp/org-element: Add a parameters-line property to special blocks Add a property `parameters-line' to special blocks, to store the PARAMETERS as a string. * lisp/org-element.el (org-element-special-block-parser): Parse PARAMETERS and set the property `:parameters-line'. (org-element-special-block-interpreter): Interpret the new property `:parameters-line'. * testing/lisp/test-org-element.el (test-org-element/special-block-parser): Update to test parsing the block PARAMETERS. (test-org-element/special-block-interpreter-with-params): New test. --- lisp/org-element.el | 20 ++++++++++++++------ testing/lisp/test-org-element.el | 16 +++++++++++++++- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/lisp/org-element.el b/lisp/org-element.el index 7b26e877e..23751ad30 100644 --- a/lisp/org-element.el +++ b/lisp/org-element.el @@ -1868,13 +1868,16 @@ (defun org-element-special-block-parser (limit affiliated) their value. Return a list whose CAR is `special-block' and CDR is a plist -containing `:type', `:begin', `:end', `:contents-begin', -`:contents-end', `:post-blank' and `:post-affiliated' keywords. +containing `:type', `:parameters-line', `:begin', `:end', +`:contents-begin', `:contents-end', `:post-blank' and +`:post-affiliated' keywords. Assume point is at the beginning of the block." (let* ((case-fold-search t) - (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)") - (match-string-no-properties 1)))) + (type (progn (looking-at "[ \t]*#\\+BEGIN_\\(\\S-+\\)[ \t]*\\(.*\\)[ \t]*$") + (match-string-no-properties 1))) + (parameters-line (match-string-no-properties 2)) + ) (if (not (save-excursion (re-search-forward (format "^[ \t]*#\\+END_%s[ \t]*$" (regexp-quote type)) @@ -1898,6 +1901,7 @@ (defun org-element-special-block-parser (limit affiliated) (list 'special-block (nconc (list :type type + :parameters-line parameters-line :begin begin :end end :contents-begin contents-begin @@ -1909,8 +1913,12 @@ (defun org-element-special-block-parser (limit affiliated) (defun org-element-special-block-interpreter (special-block contents) "Interpret SPECIAL-BLOCK element as Org syntax. CONTENTS is the contents of the element." - (let ((block-type (org-element-property :type special-block))) - (format "#+begin_%s\n%s#+end_%s" block-type contents block-type))) + (let ((block-type (org-element-property :type special-block)) + (block-parameters-line (org-element-property :parameters-line special-block)) + ) + (format "#+begin_%s%s%s\n%s#+end_%s" block-type + (if (string= "" block-parameters-line) "" " ") block-parameters-line + contents block-type))) diff --git a/testing/lisp/test-org-element.el b/testing/lisp/test-org-element.el index 187cadf7a..396c329b7 100644 --- a/testing/lisp/test-org-element.el +++ b/testing/lisp/test-org-element.el @@ -2425,7 +2425,13 @@ (ert-deftest test-org-element/special-block-parser () ;; Handle non-empty blank line at the end of buffer. (should (org-test-with-temp-text "#+BEGIN_SPECIAL\nC\n#+END_SPECIAL\n " - (= (org-element-property :end (org-element-at-point)) (point-max))))) + (= (org-element-property :end (org-element-at-point)) (point-max)))) + ;; Parse the parameter line if any + (should + (org-test-with-temp-text "#+BEGIN_SPECIAL* s p :w 3\nC\n#+END_SPECIAL*" + (equal "s p :w 3" + (org-element-property :parameters-line (org-element-at-point)))))) + ;;;; Src Block @@ -2791,6 +2797,14 @@ (ert-deftest test-org-element/dynamic-block-interpreter () "#+BEGIN: myblock :parameter value1\nTest\n#+END:") "#+begin: myblock :parameter value1\nTest\n#+end:\n"))) +(ert-deftest test-org-element/special-block-interpreter-with-params () + "Test special block interpreter." + (should + (equal (org-test-parse-and-interpret + "#+BEGIN_special some parameters until EOL\nA very special content\n#+END_special") + "#+begin_special some parameters until EOL\nA very special content\n#+end_special\n"))) + + (ert-deftest test-org-element/footnote-definition-interpreter () "Test footnote definition interpreter." (should (equal (org-test-parse-and-interpret "[fn:1] Test") "[fn:1] Test\n")) -- 2.37.3