How can I prevent org from adding a paragraph inside the div?
Closed this issue · 2 comments
Hi, thanks for making the great package.
I was messing around a bit to be able to create a theorem environment for mathematics. It is quite common to write these as
Theorem n. bla bla
However, in the produced html code, a paragraph <p> bla bla </p>
environment is added inside the div. How can I prevent this from happening?
My current code is
(o-defblock theorem (format "<div class=\"theorem\">%s</div>" contents))
Hey @bbardsley, I'm glad you like the package 😁
The answer to your problem is hinted at in the docstrings: C-h o RET o-defblock
yields ...In, hopefully, rare circumstances, one may refer to RAW-CONTENTS to look at the fully unparsed contents....
With that in mind, here is a possible solution, a self-contained Org-file:
#+begin_src emacs-lisp
(defun my/make-things-html-friendly (html)
"Replace $...$ with approriate HTML tags for rendering math."
(s-replace-regexp "\\$\\(.+?\\)\\$"
"<span class=\"MathJax\"> <script type=\"math/tex\"> \\1 </script> </span>"
html))
(o-defblock theorem
(title) nil
"Show block contents prefixed with “Theorem 𝒏”, where the 𝒏umbering automatically increments."
(defvar my/theorem-counter 0)
(format "<div class=\"theorem\"><b>Theorem %s%s.</b> %s</div>"
(cl-incf my/theorem-counter)
(if title (format " [“%s”]" title) "")
(my/make-things-html-friendly raw-contents)))
#+end_src
To enable MathJax to load, we need some inline math, such as $x = x$. With that
out of the way, we can look at some example uses of our “theorem” block.
#+begin_theorem Russel & Whitehead
After 372 pages, it was shown that $1 + 1 = 2$
#+end_theorem
#+begin_theorem
It is said that $e^{i \cdot \pi} + 1 = 0$ is the truth.
#+end_theorem
However, this is just a hack. The underlying issue is discussed in #8, where it appears in a different context. The key problem is that contents
is not an HTML-tagged string (for which we can strip out the enclosing tags) but is instead, currently, escaped via #+export_html
blocks. An adequate solution has yet to be found.
@bbardsley This can be solved with a touch of CSS ;-)
Here is a MWE Org file:
The following style ensures that the ~<p>~ tags produced by Org have their line feeds disabled
/only while/ in a ~<div class="theorem">~.
#+html: <style> .theorem p { display:inline; } </style>
With that in-hand, let's define our theorem block.
#+begin_src emacs-lisp
(setq my/theorem-counter 0)
(org-defblock theorem (title)
"Show block contents prefixed with “Theorem 𝒏”, where the 𝒏umbering automatically increments."
(format "<div class=\"theorem\"><b>Theorem %s%s.</b> %s</div>"
(cl-incf my/theorem-counter)
(if title (format " [“%s”]" title) "")
(org-parse raw-contents)))
#+end_src
To enable MathJax to load, we need some inline math, such as $x = x$. With that
out of the way, we can look at some example uses of our “theorem” block.
#+begin_theorem Russel & Whitehead
After 372 pages, it was shown that $1 + 1 = 2$
#+end_theorem
#+begin_theorem
It is said that $e^{i \cdot \pi} + 1 = 0$ is the truth.
#+end_theorem