emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Bruno Barbier <brubar.cs@gmail.com>
To: Damien Cassou <damien@cassou.me>, emacs-orgmode@gnu.org
Subject: PATCH: Re: Reading the parameters of a special-block
Date: Mon, 17 Oct 2022 21:28:39 +0200	[thread overview]
Message-ID: <E1okViG-0002eo-8E@lists.gnu.org> (raw)
In-Reply-To: <87zgdu26dd.fsf@cassou.me>

[-- 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

  reply	other threads:[~2022-10-17 19:34 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-17  8:37 Reading the parameters of a special-block Damien Cassou
2022-10-17 19:28 ` Bruno Barbier [this message]
2022-10-18  4:56   ` PATCH: " 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E1okViG-0002eo-8E@lists.gnu.org \
    --to=brubar.cs@gmail.com \
    --cc=damien@cassou.me \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).