emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* custom function for org-babel src block export
@ 2023-12-25 21:49 David Bremner
  2023-12-25 21:57 ` Ihor Radchenko
  2023-12-26 13:12 ` Rudolf Adamkovič
  0 siblings, 2 replies; 6+ messages in thread
From: David Bremner @ 2023-12-25 21:49 UTC (permalink / raw)
  To: emacs-orgmode


I am generating slides using org-beamer. I would like certain code
blocks to export as links containing URL-encoded content of the
block. As an example I would like the following block 

#+begin_src stacker 
(defvar x 1)
(deffun (f)
  (defvar y 2)
  (deffun (h)
    (+ x y))
  (h))
(f)
#+end_src

would be exported as something like

\href{https://www.example.com/stacker/?program=%28defvar%20x%201%29%0A%28deffun%20%28f%29%0A%20%20%28defvar%20y%202%29%0A%20%20%28deffun%20%28h%29%0A%20%20%20%20%28+%20x%20y%29%29%0A%20%20%28h%29%29%0A%28f%29}{execute on stacker}

I can do the encoding easily enough (see end), but I did not see a way
of hooking that up to org-babel, while retaining the ability to also use
minted or listings for other blocks.

I need the ability to use noweb expansion, so using a custom block type
seems unattractive (at least at first glance).

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar stacker-base
"\href{https://www.example.com/stacker/?program=%}{execute on stacker}")


(defun db-stacker-link (prog)
  (format stacker-base 
          (org-link-encode prog '(?? ?  ?( ?) ?\n))))



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

* Re: custom function for org-babel src block export
  2023-12-25 21:49 custom function for org-babel src block export David Bremner
@ 2023-12-25 21:57 ` Ihor Radchenko
  2023-12-26  2:26   ` David Bremner
  2023-12-26 13:12 ` Rudolf Adamkovič
  1 sibling, 1 reply; 6+ messages in thread
From: Ihor Radchenko @ 2023-12-25 21:57 UTC (permalink / raw)
  To: David Bremner; +Cc: emacs-orgmode

David Bremner <david@tethera.net> writes:

> I am generating slides using org-beamer. I would like certain code
> blocks to export as links containing URL-encoded content of the
> block.

Check out https://orgmode.org/manual/Advanced-Export-Configuration.html
You may use export filter or extend ox-beamer backend, creating a new
custom backend that will export your code blocks as desired.

-- 
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: custom function for org-babel src block export
  2023-12-25 21:57 ` Ihor Radchenko
@ 2023-12-26  2:26   ` David Bremner
  2023-12-26  6:31     ` David Bremner
  0 siblings, 1 reply; 6+ messages in thread
From: David Bremner @ 2023-12-26  2:26 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode

Ihor Radchenko <yantar92@posteo.net> writes:

> David Bremner <david@tethera.net> writes:
>
>> I am generating slides using org-beamer. I would like certain code
>> blocks to export as links containing URL-encoded content of the
>> block.
>
> Check out https://orgmode.org/manual/Advanced-Export-Configuration.html
> You may use export filter or extend ox-beamer backend, creating a new
> custom backend that will export your code blocks as desired.
>

Thanks for the suggestion(s).

I tried defining org-export-filter-src-block-functions (see end)
but I encountered two issues

1) The text already includes the latex environment (in my case
listings). This is solvable, but did make me wonder if I was using the
right filter.

2) I could not figure out how to cleanly access the source language
   (e.g. "stacker" in my example). It didn't seem to be in "info".  I
   guess I can recover it from the text using e.g. regex, but this seems fragile, since
   it would break when switching backends.

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun db-ox-src-filter (text backend info)
  (when (org-export-derived-backend-p backend 'latex)
    (db-stacker-link text)))

(add-to-list 'org-export-filter-src-block-functions
             #'db-ox-src-filter)


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

* Re: custom function for org-babel src block export
  2023-12-26  2:26   ` David Bremner
@ 2023-12-26  6:31     ` David Bremner
  0 siblings, 0 replies; 6+ messages in thread
From: David Bremner @ 2023-12-26  6:31 UTC (permalink / raw)
  To: emacs-orgmode

David Bremner <david@tethera.net> writes:

> Ihor Radchenko <yantar92@posteo.net> writes:
>
>> David Bremner <david@tethera.net> writes:
>>
>>> I am generating slides using org-beamer. I would like certain code
>>> blocks to export as links containing URL-encoded content of the
>>> block.
>>
>> Check out https://orgmode.org/manual/Advanced-Export-Configuration.html
>> You may use export filter or extend ox-beamer backend, creating a new
>> custom backend that will export your code blocks as desired.
>>
>
> Thanks for the suggestion(s).
>
> I tried defining org-export-filter-src-block-functions (see end)
> but I encountered two issues
>

My "good enough for now" solution is to use a second emacs-lisp code
block as below.  This requires a bit of care to keep the link in sync
with the exported code, but is otherwise workable.

#+name: smol-test
#+begin_src smol
  (deffun (f x) (+ x 1))
#+end_src

#+begin_src emacs-lisp :eval true :exports results :noweb yes :results value latex
  (db-stacker-link "<<smol-test>>")
#+end_src
#+results:
#+begin_export latex
\href{https://www.example.com/stacker/?program=%28deffun%20%28f%20x%29%20%28+%20x%201%29%29}{execute on stacker}
#+end_export


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

* Re: custom function for org-babel src block export
  2023-12-25 21:49 custom function for org-babel src block export David Bremner
  2023-12-25 21:57 ` Ihor Radchenko
@ 2023-12-26 13:12 ` Rudolf Adamkovič
  2023-12-26 14:36   ` David Bremner
  1 sibling, 1 reply; 6+ messages in thread
From: Rudolf Adamkovič @ 2023-12-26 13:12 UTC (permalink / raw)
  To: David Bremner, emacs-orgmode

How about:

(defvar stacker-base
  "\href{https://www.example.com/stacker/?program=%s}{execute on stacker}")

(defun org-babel-execute:stacker (body params)
  (format stacker-base 
          (org-link-encode body '(?? ?  ?( ?) ?\n))))

It works with noweb too:

#+NAME: last
#+BEGIN_SRC stacker :eval no
(f)
#+END_SRC

#+BEGIN_SRC stacker :noweb yes
(defvar x 1)
(deffun (f)
  (defvar y 2)
  (deffun (h)
    (+ x y))
  (h))
<<last>>
#+END_SRC

#+RESULTS:
: href{https://www.example.com/stacker/?program=%28defvar%20x%201%29%0A%28deffun%20%28f%29%0A%20%20%28defvar%20y%202%29%0A%20%20%28deffun%20%28h%29%0A%20%20%20%20%28+%20x%20y%29%29%0A%20%20%28h%29%29%0A%28f%29}{execute on stacker}

Rudy
-- 
"Genius is 1% inspiration and 99% perspiration."
--- Thomas Alva Edison, 1932

Rudolf Adamkovič <salutis@me.com> [he/him]
Studenohorská 25
84103 Bratislava
Slovakia


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

* Re: custom function for org-babel src block export
  2023-12-26 13:12 ` Rudolf Adamkovič
@ 2023-12-26 14:36   ` David Bremner
  0 siblings, 0 replies; 6+ messages in thread
From: David Bremner @ 2023-12-26 14:36 UTC (permalink / raw)
  To: Rudolf Adamkovič, emacs-orgmode

Rudolf Adamkovič <salutis@me.com> writes:

> How about:
>
> (defvar stacker-base
>   "\href{https://www.example.com/stacker/?program=%s}{execute on stacker}")
>
> (defun org-babel-execute:stacker (body params)
>   (format stacker-base 
>           (org-link-encode body '(?? ?  ?( ?) ?\n))))
>

Thanks, that's a definite improvement on my version. I feel less like
I'm fighting org-mode now ;). It just needs the same ":results value
latex" to insert properly into the beamer export.


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

end of thread, other threads:[~2023-12-26 14:37 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-25 21:49 custom function for org-babel src block export David Bremner
2023-12-25 21:57 ` Ihor Radchenko
2023-12-26  2:26   ` David Bremner
2023-12-26  6:31     ` David Bremner
2023-12-26 13:12 ` Rudolf Adamkovič
2023-12-26 14:36   ` David Bremner

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).