emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Way to mark contents of an Org special block as verbatim?
@ 2022-01-06 17:00 Kaushal Modi
  2022-01-06 18:27 ` Juan Manuel Macías
  0 siblings, 1 reply; 13+ messages in thread
From: Kaushal Modi @ 2022-01-06 17:00 UTC (permalink / raw)
  To: emacs-org list

Hello,

Is there a way to mark contents of an Org special block as verbatim?

In my custom exporter, when I do the below,

#+begin_katex
E = -J \sum_{i=1}^N s_i s_{i+1}
#+end_katex

it gets exported to:

{{< katex display >}}
E = -J &sum;<sub>i=1</sub>^N s\_i s<sub>i+1</sub>
{{< /katex >}}

I would like to treat that block /like/ how Org treats the latex
environment blocks, but it needs to be a special block as
begin_katex/end_katex here is special and it could be a different
equation rendering backend for another user.

So is that a header switch or arg that can be passed on to this
special block that enables the verbatim mode (prevent Org from
replacing the Org entities, do the sub/superscript expansion, etc.)?

--
Kaushal Modi


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

* Re: Way to mark contents of an Org special block as verbatim?
  2022-01-06 17:00 Way to mark contents of an Org special block as verbatim? Kaushal Modi
@ 2022-01-06 18:27 ` Juan Manuel Macías
  2022-01-06 19:14   ` Kaushal Modi
  0 siblings, 1 reply; 13+ messages in thread
From: Juan Manuel Macías @ 2022-01-06 18:27 UTC (permalink / raw)
  To: Kaushal Modi; +Cc: orgmode

Hi Kaushal,

Kaushal Modi writes:

> I would like to treat that block /like/ how Org treats the latex
> environment blocks, but it needs to be a special block as
> begin_katex/end_katex here is special and it could be a different
> equation rendering backend for another user.
>
> So is that a header switch or arg that can be passed on to this
> special block that enables the verbatim mode (prevent Org from
> replacing the Org entities, do the sub/superscript expansion, etc.)?

I think one possibility would be using a parse tree export filter:

#+BIND: org-export-filter-parse-tree-functions (katex-verbatim)
#+begin_src emacs-lisp :exports results :results none
  (defun katex-verbatim (tree backend info)
    (when (org-export-derived-backend-p backend 'latex)
      (org-element-map tree 'special-block
	(lambda (block)
	  (when (equal "katex" (org-element-property :type block))
	    (let ((cont (org-element-interpret-data (org-element-contents block)))
		  (create-export-snippet ;; idea from Nicolas Goaziou
		   (lambda (v)
		     (org-element-create 'export-snippet (list :back-end "latex" :value v)))))
	      (apply #'org-element-set-contents
		     block
		     (list (funcall create-export-snippet cont))))))
	info)
      tree))
#+end_src

#+begin_katex
E = -J \sum_{i=1}^N s_i s_{i+1}
#+end_katex

==>

\begin{katex}
E = -J \sum_{i=1}^N s_i s_{i+1}
\end{katex}

Best regards,

Juan Manuel 


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

* Re: Way to mark contents of an Org special block as verbatim?
  2022-01-06 18:27 ` Juan Manuel Macías
@ 2022-01-06 19:14   ` Kaushal Modi
  2022-01-06 19:33     ` Juan Manuel Macías
  0 siblings, 1 reply; 13+ messages in thread
From: Kaushal Modi @ 2022-01-06 19:14 UTC (permalink / raw)
  To: Juan Manuel Macías; +Cc: orgmode

On Thu, Jan 6, 2022 at 1:27 PM Juan Manuel Macías
<maciaschain@posteo.net> wrote:
> I think one possibility would be using a parse tree export filter:
>
> #+BIND: org-export-filter-parse-tree-functions (katex-verbatim)
> #+begin_src emacs-lisp :exports results :results none
>   (defun katex-verbatim (tree backend info)
>     (when (org-export-derived-backend-p backend 'latex)
>       (org-element-map tree 'special-block
>         (lambda (block)
>           (when (equal "katex" (org-element-property :type block))
>             (let ((cont (org-element-interpret-data (org-element-contents block)))
>                   (create-export-snippet ;; idea from Nicolas Goaziou
>                    (lambda (v)
>                      (org-element-create 'export-snippet (list :back-end "latex" :value v)))))
>               (apply #'org-element-set-contents
>                      block
>                      (list (funcall create-export-snippet cont))))))
>         info)
>       tree))
> #+end_src
>
> #+begin_katex
> E = -J \sum_{i=1}^N s_i s_{i+1}
> #+end_katex
>
> ==>
>
> \begin{katex}
> E = -J \sum_{i=1}^N s_i s_{i+1}
> \end{katex}

Wow, thanks for that complete code. I didn't know about the parse tree
functions.

I was thinking if below is possible as any user running my exporter
would need to use this feature easily.

#+begin_katex :verbatim t
E = -J \sum_{i=1}^N s_i s_{i+1}
#+end_katex

Is it possible to do anything inside the org-hugo-special-block
function that's defined in the exporter?


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

* Re: Way to mark contents of an Org special block as verbatim?
  2022-01-06 19:14   ` Kaushal Modi
@ 2022-01-06 19:33     ` Juan Manuel Macías
  2022-01-06 19:46       ` Kaushal Modi
  0 siblings, 1 reply; 13+ messages in thread
From: Juan Manuel Macías @ 2022-01-06 19:33 UTC (permalink / raw)
  To: Kaushal Modi; +Cc: orgmode

Kaushal Modi writes:

> Wow, thanks for that complete code. I didn't know about the parse tree
> functions.
>
> I was thinking if below is possible as any user running my exporter
> would need to use this feature easily.
>
> #+begin_katex :verbatim t
> E = -J \sum_{i=1}^N s_i s_{i+1}
> #+end_katex

I just realized that there is a much simpler solution for your katex
environment :-)

You can use an example block, and define your custom environment using
the attribute :environment

#+ATTR_LaTeX: :environment katex
#+begin_example
E = -J \sum_{i=1}^N s_i s_{i+1}
#+end_example


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

* Re: Way to mark contents of an Org special block as verbatim?
  2022-01-06 19:33     ` Juan Manuel Macías
@ 2022-01-06 19:46       ` Kaushal Modi
  2022-01-08 21:52         ` Berry, Charles
  0 siblings, 1 reply; 13+ messages in thread
From: Kaushal Modi @ 2022-01-06 19:46 UTC (permalink / raw)
  To: Juan Manuel Macías; +Cc: orgmode

On Thu, Jan 6, 2022 at 2:33 PM Juan Manuel Macías
<maciaschain@posteo.net> wrote:
> I just realized that there is a much simpler solution for your katex
> environment :-)
>
> You can use an example block, and define your custom environment using
> the attribute :environment

Sorry, but this exporter is derived from md, and before exporting the
verbatim body of the special block needs to be surrounded with some
special syntax, with some optional stuff that the user specifies. Also
it could any special block name:
- katex
- tikz
- tikzjax

In any case, if user has this in Org:

#+begin_FOO
<body verbatim>
#+end_FOO

I need to export:

{{< FOO custom stuff >}}
<body verbatim>
{{< /FOO >}}


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

* Re: Way to mark contents of an Org special block as verbatim?
  2022-01-06 19:46       ` Kaushal Modi
@ 2022-01-08 21:52         ` Berry, Charles
  2022-01-08 23:29           ` Kaushal Modi
  0 siblings, 1 reply; 13+ messages in thread
From: Berry, Charles @ 2022-01-08 21:52 UTC (permalink / raw)
  To: Kaushal Modi; +Cc: Juan Manuel Macías, orgmode



> On Jan 6, 2022, at 11:46 AM, Kaushal Modi <kaushal.modi@gmail.com> wrote:
> 
> On Thu, Jan 6, 2022 at 2:33 PM Juan Manuel Macías
> <maciaschain@posteo.net> wrote:
>> I just realized that there is a much simpler solution for your katex
>> environment :-)
>> 
>> You can use an example block, and define your custom environment using
>> the attribute :environment
> 
> Sorry, but this exporter is derived from md, and before exporting the
> verbatim body of the special block needs to be surrounded with some
> special syntax, with some optional stuff that the user specifies. Also
> it could any special block name:
> - katex
> - tikz
> - tikzjax
> 
> In any case, if user has this in Org:
> 
> #+begin_FOO
> <body verbatim>
> #+end_FOO
> 
> I need to export:
> 
> {{< FOO custom stuff >}}
> <body verbatim>
> {{< /FOO >}}
> 
> 

What am I missing? 

It seems like you want your derived backend to transcode special blocks somewhat differently than the parent backend. And adding a special block export filter doesn't quite do the job.

For that purpose, you should write a special block transcoder - perhaps falling back to the parent backend for block types you do not wish to handle as described above.

Block specific customizations could rely on a backend specific attribute.

HTH,
Chuck


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

* Re: Way to mark contents of an Org special block as verbatim?
  2022-01-08 21:52         ` Berry, Charles
@ 2022-01-08 23:29           ` Kaushal Modi
  2022-01-09  3:01             ` Berry, Charles
  0 siblings, 1 reply; 13+ messages in thread
From: Kaushal Modi @ 2022-01-08 23:29 UTC (permalink / raw)
  To: Berry, Charles; +Cc: Juan Manuel Macías, orgmode

> What am I missing?
>
> It seems like you want your derived backend to transcode special blocks somewhat differently than the parent backend. And adding a special block export filter doesn't quite do the job.

I tried out the example Juan posted and it works perfectly well. But
it would require to user to do something similar for each arbitrary
new special block they need. E.g. #+being_katex, #+begin_tikz,
#+begin_tikzjax (could be anything!) where the content needs to be
kept unmodified.

> For that purpose, you should write a special block transcoder - perhaps falling back to the parent backend for block types you do not wish to handle as described above.

Can you please point me to an example?

> Block specific customizations could rely on a backend specific attribute.

Yes, I am doing block-specific customization (like support
#+attr_shortcode above special blocks here[1]). But I don't know how
to get back the original content of the special block because the
`contents` arg received by the exporter's org-hugo-special-block
already has the Org entities, and sub/super replacements done.

[1]: https://github.com/kaushalmodi/ox-hugo/blob/458142675bb5a0e7ee26ecea07d75c10aa52184b/ox-hugo.el#L2872


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

* Re: Way to mark contents of an Org special block as verbatim?
  2022-01-08 23:29           ` Kaushal Modi
@ 2022-01-09  3:01             ` Berry, Charles
  2022-01-09  4:03               ` Kaushal Modi
  0 siblings, 1 reply; 13+ messages in thread
From: Berry, Charles @ 2022-01-09  3:01 UTC (permalink / raw)
  To: Kaushal Modi; +Cc: Juan Manuel Macías, orgmode



> On Jan 8, 2022, at 3:29 PM, Kaushal Modi <kaushal.modi@gmail.com> wrote:
> 
>> What am I missing?
>> 
>> It seems like you want your derived backend to transcode special blocks somewhat differently than the parent backend. And adding a special block export filter doesn't quite do the job.
> 
> I tried out the example Juan posted and it works perfectly well. But
> it would require to user to do something similar for each arbitrary
> new special block they need. E.g. #+being_katex, #+begin_tikz,
> #+begin_tikzjax (could be anything!) where the content needs to be
> kept unmodified.
> 

Right, so maybe use `(member type <list of accepted types>)' rather than '(equals type "newlatex")' in the3 example below.


>> For that purpose, you should write a special block transcoder - perhaps falling back to the parent backend for block types you do not wish to handle as described above.
> 
> Can you please point me to an example?
> 

A simple one here:

#+begin_src emacs-lisp
  ;; minimal backend with latex parent
  (org-export-define-derived-backend 'newlatex 'latex
    :translate-alist
    '((special-block . org-newlatex-special-block)))

  ;; special block transcoder
  (defun org-newlatex-special-block (special-block contents info)
    "Newlatex special block transcoder."
    (let
	 ((type (org-element-property :type special-block)))
      (if (equal type "newlatex")
	  (format "{{<FOO>}}\n%s{{<//FOO>}}\n" contents)
	;; not `newlatex` so default to latex transcoder
	(org-export-with-backend 'latex special-block contents info))))
#+end_src

Eval the above, then use this as an example by copying the following
src block to an org buffer and entering

M-; (org-export-to-buffer 'newlatex "*Org NEW LATEX Export*" nil nil nil t) RET

#+begin_src org

  ,#+begin_newlatex
  This is the new content
  ,#+end_newlatex


  ,#+attr_latex: :caption \MyCaption{HeadingA}
  ,#+BEGIN_proof
	 dot-dot-dot
  ,#+END_proof

#+end_src

I get an *Org NEW LATEX Export* buffer like this:

#+begin_example
{{<FOO>}}
This is the new content
{{<//FOO>}}


\begin{proof}
dot-dot-dot
\MyCaption{HeadingA}
\end{proof}

#+end_example


>> Block specific customizations could rely on a backend specific attribute.
> 
> Yes, I am doing block-specific customization (like support
> #+attr_shortcode above special blocks here[1]). But I don't know how
> to get back the original content of the special block because the
> `contents` arg received by the exporter's org-hugo-special-block
> already has the Org entities, and sub/super replacements done.
> 
> [1]: https://urldefense.com/v3/__https://github.com/kaushalmodi/ox-hugo/blob/458142675bb5a0e7ee26ecea07d75c10aa52184b/ox-hugo.el*L2872__;Iw!!LLK065n_VXAQ!xZhh--YwGpo06BbAHLF_A2MY_4mk4gBxHgFG3InZakP_7mdFDpyBNMgqvDVvBimzWQ$ 




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

* Re: Way to mark contents of an Org special block as verbatim?
  2022-01-09  3:01             ` Berry, Charles
@ 2022-01-09  4:03               ` Kaushal Modi
  2022-01-09 16:58                 ` Kaushal Modi
  0 siblings, 1 reply; 13+ messages in thread
From: Kaushal Modi @ 2022-01-09  4:03 UTC (permalink / raw)
  To: Berry, Charles; +Cc: Juan Manuel Macías, orgmode

On Sat, Jan 8, 2022 at 10:01 PM Berry, Charles <ccberry@health.ucsd.edu> wrote:
>
>
> A simple one here:
>
> #+begin_src emacs-lisp
>   ;; minimal backend with latex parent
>   (org-export-define-derived-backend 'newlatex 'latex
>     :translate-alist
>     '((special-block . org-newlatex-special-block)))
>
>   ;; special block transcoder
>   (defun org-newlatex-special-block (special-block contents info)
>     "Newlatex special block transcoder."
>     (let
>          ((type (org-element-property :type special-block)))
>       (if (equal type "newlatex")
>           (format "{{<FOO>}}\n%s{{<//FOO>}}\n" contents)

I am already doing something like this, but at this point in the code,
the `contents' already is the modified content i.e. Org entities
replaced and _{..} converted to <sub>, etc.

Note that ox-hugo exporter is extended from ox-md, which is extended
from ox-html.

My problem statement is that the contents I am receiving is already
modified and I don't get to access the original/raw/verbatim content.
Thanking the original example in this thread:

#+begin_katex
E = -J \sum_{i=1}^N s_i s_{i+1}
#+end_katex

- \sum will have converted to &sum;
- _{i=1} will have converted to <sub>i=1</sub> by the time I receive
the contents in ox-hugo's org-hugo-special-block.

[ May be I am doing something wrong in my exporter? ]

Or may be it's due to the fact that my eventual base exporter is ox-html?

>         ;; not `newlatex` so default to latex transcoder
>         (org-export-with-backend 'latex special-block contents info))))
> #+end_src
>
> Eval the above, then use this as an example by copying the following
> src block to an org buffer and entering
>
> M-; (org-export-to-buffer 'newlatex "*Org NEW LATEX Export*" nil nil nil t) RET
>
> #+begin_src org
>
>   ,#+begin_newlatex
>   This is the new content
>   ,#+end_newlatex
>
>
>   ,#+attr_latex: :caption \MyCaption{HeadingA}
>   ,#+BEGIN_proof
>          dot-dot-dot
>   ,#+END_proof
>
> #+end_src
>
> I get an *Org NEW LATEX Export* buffer like this:
>
> #+begin_example
> {{<FOO>}}
> This is the new content
> {{<//FOO>}}
>
>
> \begin{proof}
> dot-dot-dot
> \MyCaption{HeadingA}
> \end{proof}
>
> #+end_example


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

* Re: Way to mark contents of an Org special block as verbatim?
  2022-01-09  4:03               ` Kaushal Modi
@ 2022-01-09 16:58                 ` Kaushal Modi
  2022-01-09 19:57                   ` Berry, Charles
  0 siblings, 1 reply; 13+ messages in thread
From: Kaushal Modi @ 2022-01-09 16:58 UTC (permalink / raw)
  To: Berry, Charles; +Cc: Juan Manuel Macías, orgmode

> On Sat, Jan 8, 2022 at 10:01 PM Berry, Charles <ccberry@health.ucsd.edu> wrote:

Hi Charles,

I used your minimal example to show the issue I am seeing when using
ox-md as the base exporter.

=====
#+property: header-args :results none :exports none :eval never-export
#+options: toc:nil

* Define newmd                                                     :noexport:
Eval below to define a minimal "newmd" exporter:
#+begin_src emacs-lisp
;; minimal backend with md parent
(org-export-define-derived-backend 'newmd 'md
  :translate-alist
  '((special-block . org-newmd-special-block)))

;; special block transcoder
(defun org-newmd-special-block (special-block contents info)
  "Newmd special block transcoder."
  (let
       ((type (org-element-property :type special-block)))
    (if (equal type "katex")
        (format "{{<%s>}}\n%s{{<//%s>}}\n" type contents type)
      ;; not `katex' so default to md transcoder
      (org-export-with-backend 'md special-block contents info))))
#+end_src
* Test block for exporting
#+begin_katex
E = -J \sum_{i=1}^N s_i s_{i+1}
#+end_katex
* Do export                                                        :noexport:
Eval below to export this buffer using the ~newmd~ exporter:
#+begin_src emacs-lisp
(org-export-to-buffer 'newmd "*Org NEW LATEX Export*" nil nil nil :body-only)
#+end_src
=====

1. Eval the block in first heading
2. Eval the block in second heading to export

Exporter buffer content:

=====
# Test block for exporting

{{<katex>}}
E = -J &sum;<sub>i=1</sub>^N s\_i s<sub>i+1</sub>
{{<//katex>}}
=====


How do we disable Org from transforming this:

    E = -J \sum_{i=1}^N s_i s_{i+1}

to this:


    E = -J &sum;<sub>i=1</sub>^N s\_i s<sub>i+1</sub>


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

* Re: Way to mark contents of an Org special block as verbatim?
  2022-01-09 16:58                 ` Kaushal Modi
@ 2022-01-09 19:57                   ` Berry, Charles
  2022-01-09 20:01                     ` Nicolas Goaziou
  0 siblings, 1 reply; 13+ messages in thread
From: Berry, Charles @ 2022-01-09 19:57 UTC (permalink / raw)
  To: Kaushal Modi; +Cc: Juan Manuel Macías, orgmode



> On Jan 9, 2022, at 8:58 AM, Kaushal Modi <kaushal.modi@gmail.com> wrote:
> 
> How do we disable Org from transforming this:
> 
>    E = -J \sum_{i=1}^N s_i s_{i+1}
> 
> to this:
> 
> 
>    E = -J &sum;<sub>i=1</sub>^N s\_i s<sub>i+1</sub>

Ahh! Sorry!

So,
:       (format "{{<%s>}}\n%s{{<//%s>}}\n" type contents type)

has the contents already parsed and transcoded which you do not want. So use something like:

#+begin_src emacs-lisp

(let ((raw-contents
	      (buffer-substring-no-properties
	       (org-element-property :contents-begin special-block)
	       (org-element-property :contents-end special-block))))
	 (format "{{<%s>}}\n%s{{<//%s>}}\n" type raw-contents type))

#+end_src

HTH,

Chuck


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

* Re: Way to mark contents of an Org special block as verbatim?
  2022-01-09 19:57                   ` Berry, Charles
@ 2022-01-09 20:01                     ` Nicolas Goaziou
  2022-01-09 21:53                       ` Kaushal Modi
  0 siblings, 1 reply; 13+ messages in thread
From: Nicolas Goaziou @ 2022-01-09 20:01 UTC (permalink / raw)
  To: Berry, Charles; +Cc: Juan Manuel Macías, orgmode, Kaushal Modi

Hello,

"Berry, Charles" <ccberry@health.ucsd.edu> writes:

> So,
> :       (format "{{<%s>}}\n%s{{<//%s>}}\n" type contents type)
>
> has the contents already parsed and transcoded which you do not want. So use something like:
>
> #+begin_src emacs-lisp
>
> (let ((raw-contents
> 	      (buffer-substring-no-properties
> 	       (org-element-property :contents-begin special-block)
> 	       (org-element-property :contents-end special-block))))
> 	 (format "{{<%s>}}\n%s{{<//%s>}}\n" type raw-contents type))

I suggest to use

  (org-element-interpret-data (org-element-contents special-block))

Regards,
-- 
Nicolas Goaziou


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

* Re: Way to mark contents of an Org special block as verbatim?
  2022-01-09 20:01                     ` Nicolas Goaziou
@ 2022-01-09 21:53                       ` Kaushal Modi
  0 siblings, 0 replies; 13+ messages in thread
From: Kaushal Modi @ 2022-01-09 21:53 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: Juan Manuel Macías, Berry, Charles, orgmode

On Sun, Jan 9, 2022 at 3:01 PM Nicolas Goaziou <mail@nicolasgoaziou.fr> wrote:
>
> I suggest to use
>
>   (org-element-interpret-data (org-element-contents special-block))


That does it!! Thank you!

Also thanks to Juan and Charles who helped lead me to the final solution.


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

end of thread, other threads:[~2022-01-09 21:54 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-06 17:00 Way to mark contents of an Org special block as verbatim? Kaushal Modi
2022-01-06 18:27 ` Juan Manuel Macías
2022-01-06 19:14   ` Kaushal Modi
2022-01-06 19:33     ` Juan Manuel Macías
2022-01-06 19:46       ` Kaushal Modi
2022-01-08 21:52         ` Berry, Charles
2022-01-08 23:29           ` Kaushal Modi
2022-01-09  3:01             ` Berry, Charles
2022-01-09  4:03               ` Kaushal Modi
2022-01-09 16:58                 ` Kaushal Modi
2022-01-09 19:57                   ` Berry, Charles
2022-01-09 20:01                     ` Nicolas Goaziou
2022-01-09 21:53                       ` Kaushal Modi

Code repositories for project(s) associated with this 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).