* Reading the parameters of a special-block @ 2022-10-17 8:37 Damien Cassou 2022-10-17 19:28 ` PATCH: " Bruno Barbier 0 siblings, 1 reply; 6+ messages in thread From: Damien Cassou @ 2022-10-17 8:37 UTC (permalink / raw) To: emacs-orgmode Hi, in Worg's description of Org's syntax, there is a section about "greater blocks" [1]. The syntax is #+begin_NAME PARAMETERS CONTENTS #+end_NAME There is then a "PARAMETERS" optional string attached to special blocks. Unfortunately, in the special-block transcoder I'm writing, I have no clue how to access the parameters. I can't find any parameters function in org that would be related. What is the best way to access the parameters of a special-block? [1] https://orgmode.org/worg/dev/org-syntax.html#Greater_Blocks -- Damien Cassou "Success is the ability to go from one failure to another without losing enthusiasm." --Winston Churchill ^ permalink raw reply [flat|nested] 6+ messages in thread
* PATCH: Re: Reading the parameters of a special-block 2022-10-17 8:37 Reading the parameters of a special-block Damien Cassou @ 2022-10-17 19:28 ` Bruno Barbier 2022-10-18 4:56 ` Ihor Radchenko 0 siblings, 1 reply; 6+ messages in thread From: Bruno Barbier @ 2022-10-17 19:28 UTC (permalink / raw) To: Damien Cassou, emacs-orgmode [-- Attachment #1: Type: text/plain, Size: 951 bytes --] Damien Cassou <damien@cassou.me> writes: > Hi, > > in Worg's description of Org's syntax, there is a section about "greater > blocks" [1]. The syntax is > > #+begin_NAME PARAMETERS > CONTENTS > #+end_NAME > > There is then a "PARAMETERS" optional string attached to special > blocks. Unfortunately, in the special-block transcoder I'm writing, I > have no clue how to access the parameters. I can't find any parameters > function in org that would be related. > > What is the best way to access the parameters of a special-block? My understanding is that the parameters line is just ignored for special blocks. I wrote and used a patch a while ago to fix this; it was on my todo list to clean it up and submit it to org. Now looks like a good time. My patch adds a `:parameters-line' property to org elements that are special blocks, storing the PARAMETERS line as a string. See attached patch. Could this be added to org ? Bruno [-- Warning: decoded text below may be mangled, UTF-8 assumed --] [-- Attachment #2: Patch to add parameters-line to special blocks --] [-- Type: text/x-diff, Size: 4451 bytes --] From a57412351a21cecf0a83264139cc99bcc77f1093 Mon Sep 17 00:00:00 2001 From: Bruno BARBIER <brubar.cs@gmail.com> 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))) \f 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 [-- Attachment #3: Type: text/plain, Size: 211 bytes --] > > [1] https://orgmode.org/worg/dev/org-syntax.html#Greater_Blocks > > -- > Damien Cassou > > "Success is the ability to go from one failure to another without > losing enthusiasm." --Winston Churchill ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: PATCH: Re: Reading the parameters of a special-block 2022-10-17 19:28 ` PATCH: " Bruno Barbier @ 2022-10-18 4:56 ` Ihor Radchenko 2022-10-19 18:18 ` Bruno Barbier 0 siblings, 1 reply; 6+ messages in thread From: Ihor Radchenko @ 2022-10-18 4:56 UTC (permalink / raw) To: Bruno Barbier; +Cc: Damien Cassou, emacs-orgmode Bruno Barbier <brubar.cs@gmail.com> writes: > My understanding is that the parameters line is just ignored for special > blocks. > > I wrote and used a patch a while ago to fix this; it was on my todo list > to clean it up and submit it to org. Now looks like a good time. > > My patch adds a `:parameters-line' property to org elements that are > special blocks, storing the PARAMETERS line as a string. See attached > patch. > > Could this be added to org ? I am in favour of it. The property name could be simply :parameters. Just like in src blocks (see org-element-src-block-parser). > - (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 We probably want something like :parameters (and (org-string-nw-p parameters) (org-trim parameters)) Just as in org-element-src-block-parser. > - (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)) > + ) No dangling ")" please. > + (format "#+begin_%s%s%s\n%s#+end_%s" block-type > + (if (string= "" block-parameters-line) "" " ") block-parameters-line > + contents block-type))) We will not need to test against ="" with my above comment incorporated. > @@ -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)))))) May also test against empty parameters and space-only parameters. -- Ihor Radchenko // yantar92, Org mode contributor, Learn more about Org mode at <https://orgmode.org/>. Support Org development at <https://liberapay.com/org-mode>, or support my work at <https://liberapay.com/yantar92> ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: PATCH: Re: Reading the parameters of a special-block 2022-10-18 4:56 ` Ihor Radchenko @ 2022-10-19 18:18 ` Bruno Barbier 2022-10-20 5:31 ` Ihor Radchenko 0 siblings, 1 reply; 6+ messages in thread From: Bruno Barbier @ 2022-10-19 18:18 UTC (permalink / raw) To: Ihor Radchenko; +Cc: Damien Cassou, emacs-orgmode [-- Attachment #1: Type: text/plain, Size: 1092 bytes --] Ihor Radchenko <yantar92@posteo.net> writes: > The property name could be simply :parameters. Just like in src blocks > (see org-element-src-block-parser). Indeed. Done. > We probably want something like > > :parameters (and (org-string-nw-p parameters) (org-trim parameters)) > > Just as in org-element-src-block-parser. Actually, I forgot to raise the question; but, you answered it anyway! ;-) Done. > > No dangling ")" please. Sorry. Fixed. > >> + (format "#+begin_%s%s%s\n%s#+end_%s" block-type >> + (if (string= "" block-parameters-line) "" " ") block-parameters-line >> + contents block-type))) > > We will not need to test against ="" with my above comment incorporated. For `:parameters', I'm now doing the same as in `org-element-src-block-interpreter'. > May also test against empty parameters and space-only parameters. I added more tests. I also added a new independent patch, to fix a bug when interpreting a special block that has no content, and some tests. Let me know if I did miss something, And thank you for the review! Bruno [-- Warning: decoded text below may be mangled, UTF-8 assumed --] [-- Attachment #2: Fix no content --] [-- Type: text/x-diff, Size: 2478 bytes --] From ca4ab60a11330b97e1fb8c8d2941e383189fd82e Mon Sep 17 00:00:00 2001 From: Bruno BARBIER <brubar.cs@gmail.com> Date: Wed, 19 Oct 2022 00:37:05 +0200 Subject: [PATCH 1/2] org-element-special-block-interpreter: Fix when no content * lisp/org-element.el (org-element-special-block-interpreter): Use empty string when content is nil. * testing/lisp/test-org-element.el (test-org-element/special-block-interpreter): Test the case with no content. --- lisp/org-element.el | 2 +- testing/lisp/test-org-element.el | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lisp/org-element.el b/lisp/org-element.el index 7b26e877e..ca3b61b49 100644 --- a/lisp/org-element.el +++ b/lisp/org-element.el @@ -1910,7 +1910,7 @@ (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))) + (format "#+begin_%s\n%s#+end_%s" block-type (or contents "") block-type))) \f diff --git a/testing/lisp/test-org-element.el b/testing/lisp/test-org-element.el index 187cadf7a..985108e37 100644 --- a/testing/lisp/test-org-element.el +++ b/testing/lisp/test-org-element.el @@ -2425,7 +2425,11 @@ (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)))) + ;; When contents is empty, the parsed contents is nil. + (should + (org-test-with-temp-text "#+BEGIN_SPECIAL\n#+END_SPECIAL" + (eq nil (org-element-contents (org-element-at-point)))))) ;;;; Src Block @@ -2943,7 +2947,11 @@ (ert-deftest test-org-element/special-block-interpreter () "Test special block interpreter." (should (equal (org-test-parse-and-interpret "#+BEGIN_SPECIAL\nTest\n#+END_SPECIAL") - "#+begin_SPECIAL\nTest\n#+end_SPECIAL\n"))) + "#+begin_SPECIAL\nTest\n#+end_SPECIAL\n")) + ;; No content + (should (equal (org-test-parse-and-interpret + "#+BEGIN_SPECIAL\n#+END_SPECIAL") + "#+begin_SPECIAL\n#+end_SPECIAL\n"))) (ert-deftest test-org-element/babel-call-interpreter () "Test Babel call interpreter." -- 2.37.3 [-- Warning: decoded text below may be mangled, UTF-8 assumed --] [-- Attachment #3: Add :parameters to special blocks --] [-- Type: text/x-diff, Size: 5349 bytes --] From 584bdbc161abde761876bfa2f0c484f0363afb72 Mon Sep 17 00:00:00 2001 From: Bruno BARBIER <brubar.cs@gmail.com> Date: Mon, 17 Oct 2022 20:19:02 +0200 Subject: [PATCH 2/2] lisp/org-element: Add a parameters-line property to special blocks Add a property `:parameters' 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'. (org-element-special-block-interpreter): Interpret the property `:parameters'. * testing/lisp/test-org-element.el (test-org-element/special-block-parser): Add a new test for PARAMETERS. (test-org-element/special-block-interpreter): Add new tests for PARAMETERS. --- lisp/org-element.el | 19 ++++++++++++------ testing/lisp/test-org-element.el | 33 ++++++++++++++++++++++++++++++-- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/lisp/org-element.el b/lisp/org-element.el index ca3b61b49..ce6daaef8 100644 --- a/lisp/org-element.el +++ b/lisp/org-element.el @@ -1868,13 +1868,15 @@ (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', `: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 (match-string-no-properties 2))) (if (not (save-excursion (re-search-forward (format "^[ \t]*#\\+END_%s[ \t]*$" (regexp-quote type)) @@ -1898,6 +1900,8 @@ (defun org-element-special-block-parser (limit affiliated) (list 'special-block (nconc (list :type type + :parameters (and (org-string-nw-p parameters) + (org-trim parameters)) :begin begin :end end :contents-begin contents-begin @@ -1909,8 +1913,11 @@ (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 (or contents "") block-type))) + (let ((block-type (org-element-property :type special-block)) + (parameters (org-element-property :parameters special-block))) + (format "#+begin_%s%s\n%s#+end_%s" block-type + (if parameters (concat " " parameters) "") + (or contents "") block-type))) \f diff --git a/testing/lisp/test-org-element.el b/testing/lisp/test-org-element.el index 985108e37..eb5b95e86 100644 --- a/testing/lisp/test-org-element.el +++ b/testing/lisp/test-org-element.el @@ -2429,7 +2429,19 @@ (ert-deftest test-org-element/special-block-parser () ;; When contents is empty, the parsed contents is nil. (should (org-test-with-temp-text "#+BEGIN_SPECIAL\n#+END_SPECIAL" - (eq nil (org-element-contents (org-element-at-point)))))) + (eq nil (org-element-contents (org-element-at-point))))) + ;; Parse parameters if any, trimming blanks. + (should + (org-test-with-temp-text "#+BEGIN_SPECIAL* s p :w 3 \nContent.\n#+END_SPECIAL*" + (equal "s p :w 3" + (org-element-property :parameters (org-element-at-point))))) + ;; When parameters is blank, `:parameters' is nil. + (should + (org-test-with-temp-text "#+BEGIN_SPECIAL* \t \nContent.\n#+END_SPECIAL*" + (eq nil (org-element-property :parameters (org-element-at-point)))) + )) + + ;;;; Src Block @@ -2945,13 +2957,30 @@ (ert-deftest test-org-element/quote-block-interpreter () (ert-deftest test-org-element/special-block-interpreter () "Test special block interpreter." + ;; No parameters (should (equal (org-test-parse-and-interpret "#+BEGIN_SPECIAL\nTest\n#+END_SPECIAL") "#+begin_SPECIAL\nTest\n#+end_SPECIAL\n")) ;; No content (should (equal (org-test-parse-and-interpret "#+BEGIN_SPECIAL\n#+END_SPECIAL") - "#+begin_SPECIAL\n#+end_SPECIAL\n"))) + "#+begin_SPECIAL\n#+end_SPECIAL\n")) + ;; Some parameters + (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")) + ;; No parameters (blanks only) + (should + (equal (org-test-parse-and-interpret + "#+BEGIN_special \t \nA very special content\n#+END_special") + "#+begin_special\nA very special content\n#+end_special\n")) + ;; Some parameters with leading and trailing blanks, no content, and + ;; a /special/ name. + (should + (equal (org-test-parse-and-interpret + "#+BEGIN_spe<c>ial :a :b \t :c \t \n#+END_spe<c>ial") + "#+begin_spe<c>ial :a :b \t :c\n#+end_spe<c>ial\n"))) (ert-deftest test-org-element/babel-call-interpreter () "Test Babel call interpreter." -- 2.37.3 ^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: PATCH: Re: Reading the parameters of a special-block 2022-10-19 18:18 ` Bruno Barbier @ 2022-10-20 5:31 ` Ihor Radchenko [not found] ` <notmuch-sha1-5aa2ae4e8031bcbe3c55fd813dfb0f61229b24d1> 0 siblings, 1 reply; 6+ messages in thread From: Ihor Radchenko @ 2022-10-20 5:31 UTC (permalink / raw) To: Bruno Barbier; +Cc: Damien Cassou, emacs-orgmode Bruno Barbier <brubar.cs@gmail.com> writes: > I also added a new independent patch, to fix a bug when interpreting a special > block that has no content, and some tests. > > Let me know if I did miss something, > And thank you for the review! Thanks! LGTM. Now, can you also make a patch against https://git.sr.ht/~bzg/worg adding the new parameter to https://orgmode.org/worg/dev/org-element-api.html? -- Ihor Radchenko // yantar92, Org mode contributor, Learn more about Org mode at <https://orgmode.org/>. Support Org development at <https://liberapay.com/org-mode>, or support my work at <https://liberapay.com/yantar92> ^ permalink raw reply [flat|nested] 6+ messages in thread
[parent not found: <notmuch-sha1-5aa2ae4e8031bcbe3c55fd813dfb0f61229b24d1>]
* Re: PATCH: Re: Reading the parameters of a special-block [not found] ` <notmuch-sha1-5aa2ae4e8031bcbe3c55fd813dfb0f61229b24d1> @ 2022-10-20 9:45 ` Ihor Radchenko 0 siblings, 0 replies; 6+ messages in thread From: Ihor Radchenko @ 2022-10-20 9:45 UTC (permalink / raw) To: Bruno Barbier; +Cc: emacs-orgmode [adding Org ML back to CC] Bruno Barbier <brubar.cs@gmail.com> writes: >> Thanks! LGTM. >> > >> Now, can you also make a patch against https://git.sr.ht/~bzg/worg >> adding the new parameter to https://orgmode.org/worg/dev/org-element-api.html? > > Sure. Let me know if the attached patch is good enough. Applied onto main branch of Org and master branch of WORG. https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=7d1e3dc38ea7fcd9ff57ace883370de1d5f902d8 https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=98cae03b7d9a612334d5ea461e73ac0b37b0285d https://git.sr.ht/~bzg/worg/commit/dd8e6fc4393ccf72bda957544079c6e00802b742 Thanks for your contribution! -- Ihor Radchenko // yantar92, Org mode contributor, Learn more about Org mode at <https://orgmode.org/>. Support Org development at <https://liberapay.com/org-mode>, or support my work at <https://liberapay.com/yantar92> ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2022-10-20 9:57 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2022-10-17 8:37 Reading the parameters of a special-block Damien Cassou 2022-10-17 19:28 ` PATCH: " Bruno Barbier 2022-10-18 4:56 ` Ihor Radchenko 2022-10-19 18:18 ` Bruno Barbier 2022-10-20 5:31 ` Ihor Radchenko [not found] ` <notmuch-sha1-5aa2ae4e8031bcbe3c55fd813dfb0f61229b24d1> 2022-10-20 9:45 ` Ihor Radchenko
Code repositories for project(s) associated with this external index https://git.savannah.gnu.org/cgit/emacs.git https://git.savannah.gnu.org/cgit/emacs/org-mode.git This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.