all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Unable to retrieve :parameters for src-block [org-element]
@ 2017-10-17 12:51 Kaushal Modi
  2017-10-17 15:45 ` Kaushal Modi
  2017-10-17 16:31 ` Berry, Charles
  0 siblings, 2 replies; 7+ messages in thread
From: Kaushal Modi @ 2017-10-17 12:51 UTC (permalink / raw)
  To: emacs-org list

[-- Attachment #1: Type: text/plain, Size: 1924 bytes --]

Hello,

I am using the latest Org master.

I am trying to retrieving user-set parameters for a source block. While I
am able to retrieve the :switches property for the src-block element,
:parameters always returns as nil.

Here's a dummy version of the function I am using to parse source blocks in
my custom exporter:

=====
(defun org-hugo-src-block (src-block _contents info)
  (let ((lang (org-element-property :language src-block))
        ;; See `org-element-src-block-parser' for all SRC-BLOCK properties.
        (switches (org-element-property :switches src-block))
        (parameters (org-element-property :parameters src-block))
        (number-lines (org-element-property :number-lines src-block)))
;Non-nil if -n or +n switch is used
    (message "ox-hugo src [dbg] number-lines: %S" number-lines)
    (message "ox-hugo src [dbg] switches: %S" switches)
    (message "ox-hugo src [dbg] parameters: %S" parameters)))
=====

Here is the test Org snippet:

=====
#+BEGIN_SRC emacs-lisp -n :hl 1,3-4 :eval no
(message "This is line 1")
(message "This is line 2")
(message "This is line 3")
(message "This is line 4")
(message "This is line 5")
#+END_SRC
=====

(:hl 1,3-4 is something special that ox-hugo will handle.)

But on exporting that Org snippet, I get:

=====
ox-hugo src [dbg] number-lines: (new . 0)
ox-hugo src [dbg] switches: "-n"
ox-hugo src [dbg] parameters: nil
=====

If I put a debug statement directly in org-element-src-block-parser towards
the end:

=====
;; SNIP
            (message "dbg: parameters: %s" parameters)
    (list 'src-block
  (nconc
   (list :language language
;; SNIP
=====

I get these in the *Messages*:

=====
dbg: parameters:  :hl 1,3-4 :eval no [3 times]
=====

So it is clear that the parameters are read inside
org-element-src-block-parser, but I don't understand why
(org-element-property :parameters src-block) is unable to fetch the same.

Hints?
-- 

Kaushal Modi

[-- Attachment #2: Type: text/html, Size: 3055 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Unable to retrieve :parameters for src-block [org-element]
  2017-10-17 12:51 Unable to retrieve :parameters for src-block [org-element] Kaushal Modi
@ 2017-10-17 15:45 ` Kaushal Modi
  2017-10-17 16:31 ` Berry, Charles
  1 sibling, 0 replies; 7+ messages in thread
From: Kaushal Modi @ 2017-10-17 15:45 UTC (permalink / raw)
  To: emacs-org list

[-- Attachment #1: Type: text/plain, Size: 2182 bytes --]

On Tue, Oct 17, 2017 at 8:51 AM Kaushal Modi <kaushal.modi@gmail.com> wrote:

> Hello,
>
> I am using the latest Org master.
>
> I am trying to retrieving user-set parameters for a source block. While I
> am able to retrieve the :switches property for the src-block element,
> :parameters always returns as nil.
>

Here's a comprehensive/literate version (Org file) of the same test case..
requires only ox-ascii.

=====
#+TITLE: Org Element :parameters parsing issue
* Activating debug
1. Hit =C-c C-c= on the below block, and allow its evaluation.
   #+BEGIN_SRC emacs-lisp -n :eval no-export :results output silent
   (defun debug/org-ascii-src-block (orig-fun &rest args)
     "Print few debug messages before executing `org-ascii-src-block'."
     (let* ((src-block (car args))
            (switches (org-element-property :switches src-block))
            (parameters (org-element-property :parameters src-block))
            (number-lines (org-element-property :number-lines src-block)))
       (message "[src-block dbg] number-lines: %S" number-lines)
       (message "[src-block dbg] switches: %S" switches)
       (message "[src-block dbg] parameters: %S" parameters))
     (apply orig-fun args))
   (advice-add 'org-ascii-src-block :around #'debug/org-ascii-src-block)
   #+END_SRC
2. Do =C-c C-e t A= to export this file using Ascii backend, to a buffer.
/We do
   not need to see the exported file, just the messages output by debug
messages
   in above code snippet.
* Deactivating debug
Evaluate the below (and allow that evaluation) to deactivate these debug
messages.
#+BEGIN_SRC emacs-lisp :eval no-export :results output silent
(advice-remove 'org-ascii-src-block #'debug/org-ascii-src-block)
#+END_SRC
=====

After following the 2 steps in the "Activating debug" section, you will
find this in *Messages* buffer:

[src-block dbg] number-lines: (new . 0)
[src-block dbg] switches: "-n"
[src-block dbg] parameters: nil
[src-block dbg] number-lines: nil
[src-block dbg] switches: nil
[src-block dbg] parameters: nil

(First 3 messages are for the first code block, and the next 3 messages for
the second code block.)

Why is parameters always nil?
-- 

Kaushal Modi

[-- Attachment #2: Type: text/html, Size: 3197 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Unable to retrieve :parameters for src-block [org-element]
  2017-10-17 12:51 Unable to retrieve :parameters for src-block [org-element] Kaushal Modi
  2017-10-17 15:45 ` Kaushal Modi
@ 2017-10-17 16:31 ` Berry, Charles
  2017-10-17 17:22   ` Kaushal Modi
  1 sibling, 1 reply; 7+ messages in thread
From: Berry, Charles @ 2017-10-17 16:31 UTC (permalink / raw)
  To: Kaushal Modi; +Cc: emacs-org list


> On Oct 17, 2017, at 5:51 AM, Kaushal Modi <kaushal.modi@gmail.com> wrote:
> 
> So it is clear that the parameters are read inside org-element-src-block-parser, but I don't understand why (org-element-property :parameters src-block) is unable to fetch the same.
> 
> Hints?
> -- 


The copy buffer that org-export-as sets up will contain this src block *after* the babel process runs.

#+BEGIN_SRC emacs-lisp -n
(message "This is line 1")
(message "This is line 2")
(message "This is line 3")
(message "This is line 4")
(message "This is line 5")
#+END_SRC


As you can see the headers are stripped off of it.

So you need to do something tricky to hold onto those headers.  I do not know of a seamless way to do this.  FWIW, this is handled in ox-ravel by hacking babel so it produces #+ATTR_ lines just before the src block result in the copy buffer. Those lines hold the header info which the ravel exporter trancoders can consult.

There might be a tricky way to use :wrap to rewrite the src block with the other headers intact.

HTH,

Chuck

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Unable to retrieve :parameters for src-block [org-element]
  2017-10-17 16:31 ` Berry, Charles
@ 2017-10-17 17:22   ` Kaushal Modi
  2017-10-17 21:39     ` Berry, Charles
  0 siblings, 1 reply; 7+ messages in thread
From: Kaushal Modi @ 2017-10-17 17:22 UTC (permalink / raw)
  To: Berry, Charles; +Cc: emacs-org list

[-- Attachment #1: Type: text/plain, Size: 1125 bytes --]

On Tue, Oct 17, 2017 at 12:31 PM Berry, Charles <ccberry@ucsd.edu> wrote:

> The copy buffer that org-export-as sets up will contain this src block
> *after* the babel process runs.
>
> As you can see the headers are stripped off of it.
>

Oh! That explains!


> So you need to do something tricky to hold onto those headers.  I do not
> know of a seamless way to do this.  FWIW, this is handled in ox-ravel by
> hacking babel so it produces #+ATTR_ lines just before the src block result
> in the copy buffer. Those lines hold the header info which the ravel
> exporter trancoders can consult.
>

I like that approach. I found your ox-ravel project on GitHub and have
tangled it to ox-ravel.el.

It would be great if you an paste the revelant snippets of code here as
that library is ~800 lines.

I still hope there is some way to prevent doing this hack, or if a
non-intrusive change in Org code can still have the :parameters available
during export. Would it be possible to remove *only* babel-recognized
parameters and leave the unidentified parameters (which could be specific
to an exporter) intact?
-- 

Kaushal Modi

[-- Attachment #2: Type: text/html, Size: 1691 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Unable to retrieve :parameters for src-block [org-element]
  2017-10-17 17:22   ` Kaushal Modi
@ 2017-10-17 21:39     ` Berry, Charles
  2017-10-18 21:20       ` Kaushal Modi
  0 siblings, 1 reply; 7+ messages in thread
From: Berry, Charles @ 2017-10-17 21:39 UTC (permalink / raw)
  To: Kaushal Modi; +Cc: emacs-org list


> On Oct 17, 2017, at 10:22 AM, Kaushal Modi <kaushal.modi@gmail.com> wrote:
> 
> On Tue, Oct 17, 2017 at 12:31 PM Berry, Charles <ccberry@ucsd.edu> wrote:
> The copy buffer that org-export-as sets up will contain this src block *after* the babel process runs.
> 
> As you can see the headers are stripped off of it.


Actually, I was wrong! It is only the headers that are in the #+BEGIN_SRC line that get stripped.  The contents of any #+header: lines are left  alone and the src-block transcoder will have access to them as the :header element of a plist in the src-block argument. 

[snip ox-ravel suggestion]


> I still hope there is some way to prevent doing this hack, or if a non-intrusive change in Org code can still have the :parameters available during export. Would it be possible to remove *only* babel-recognized parameters and leave the unidentified parameters (which could be specific to an exporter) intact?


I think this might work: Add `after' advice to `org-babel-exp-code' that copies the header args you want to retain and prepends a #+header: line with them to the string returned by org-babel-exp-code.  Then your src-block transcoder can find them.

Chuck

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Unable to retrieve :parameters for src-block [org-element]
  2017-10-17 21:39     ` Berry, Charles
@ 2017-10-18 21:20       ` Kaushal Modi
  2017-10-18 23:19         ` Kaushal Modi
  0 siblings, 1 reply; 7+ messages in thread
From: Kaushal Modi @ 2017-10-18 21:20 UTC (permalink / raw)
  To: Berry, Charles; +Cc: emacs-org list

[-- Attachment #1: Type: text/plain, Size: 1920 bytes --]

On Tue, Oct 17, 2017 at 5:39 PM Berry, Charles <ccberry@ucsd.edu> wrote:

> I think this might work: Add `after' advice to `org-babel-exp-code' that
> copies the header args you want to retain and prepends a #+header: line
> with them to the string returned by org-babel-exp-code.  Then your
> src-block transcoder can find them.
>

Thanks, TIL about org-babel-exp-code.

Thanks for the instructions, below works perfectly!

=====
(defun org-babel-exp-code--retain (orig-fun &rest args)
  "Return the original code block formatted for export."
  (let* ((param-keys-to-be-retained '(:hl_lines :foo)) ;Example of keys
whose conses need to be retained
         (info (car args))
         (parameters (nth 2 info))
         (ox-hugo-params-str (let ((str ""))
                               (dolist (param parameters)
                                 (dolist (retain-key
param-keys-to-be-retained)
                                   (when (equal retain-key (car param))
                                     (setq str (concat str " "
                                                       (symbol-name
retain-key) " "
                                                       (cdr param))))))
                               (org-string-nw-p (org-trim str))))
         ret)
    (setq ret (apply orig-fun args))    ;Original return value
    (when ox-hugo-params-str
      (setq ret (replace-regexp-in-string "\\`#\\+BEGIN_SRC .*" (format
"\\& %s" ox-hugo-params-str) ret)))
    ret))
(advice-add 'org-babel-exp-code :around #'org-babel-exp-code--retain)
=====

Test:

=====
#+BEGIN_SRC emacs-lisp :eval no-export :results output silent :hl_lines
1,3-4 :foo bar
(message "foo")
#+END_SRC
=====

The messages based on the debug code I pasted earlier in this thread now
look like:

=====
[src-block dbg] number-lines: nil
[src-block dbg] switches: nil
[src-block dbg] parameters: ":foo bar :hl_lines 1,3-4"
=====

-- 

Kaushal Modi

[-- Attachment #2: Type: text/html, Size: 3024 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Unable to retrieve :parameters for src-block [org-element]
  2017-10-18 21:20       ` Kaushal Modi
@ 2017-10-18 23:19         ` Kaushal Modi
  0 siblings, 0 replies; 7+ messages in thread
From: Kaushal Modi @ 2017-10-18 23:19 UTC (permalink / raw)
  To: Charles C. Berry; +Cc: emacs-org list

[-- Attachment #1: Type: text/plain, Size: 292 bytes --]

On Wed, Oct 18, 2017, 5:20 PM Kaushal Modi <kaushal.modi@gmail.com> wrote:

>
> Thanks for the instructions, below works perfectly!
>

As a quick follow up, it's already implemented in ox-hugo:
https://ox-hugo.scripter.co/doc/source-blocks/#highlighting

Thanks again! :)

> --

Kaushal Modi

[-- Attachment #2: Type: text/html, Size: 1051 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2017-10-18 23:20 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-10-17 12:51 Unable to retrieve :parameters for src-block [org-element] Kaushal Modi
2017-10-17 15:45 ` Kaushal Modi
2017-10-17 16:31 ` Berry, Charles
2017-10-17 17:22   ` Kaushal Modi
2017-10-17 21:39     ` Berry, Charles
2017-10-18 21:20       ` Kaushal Modi
2017-10-18 23:19         ` Kaushal Modi

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.