In an elisp program, I am trying to convert strings from org syntax to (say) html. For that, I use org-html-convert-region-to-html
with a temporary buffer, and everything is fine:
(defun tv/org-string-to-html (string &optional macros)
(if (or (not string) (string-empty-p string)) ""
(with-temp-buffer
(insert string)
(set-mark (point-min))
(goto-char (point-max))
(when macros (setq org-macro-templates macros)) ;; not working
(org-html-convert-region-to-html)
(substring
(buffer-substring-no-properties (point-min) (point-max))
4 -5))))
;; ELISP> (tv/org-string-to-html "This is [[file:test][a link]], this is *bold*, and this is /italic/")
;; "This is <a href=\"test\">a link</a>, this is <b>bold</b>, and this is <i>italic</i>"
However, it does not work if the string I am trying to convert has a macro, because my temporary buffer does not know about this macro.
I have tried to pass the content of the org-macro-templates
variable to my converter function in the function above, but it doesn't appear to work, the macro is still not recognized.
Examining org-macro-expand
with edebug, it appears that the macro templates are overwritten by org -- which makes sense. And the org-macro-templates
variable does warn not to set it directly, but to use #+MACRO:
lines instead.
Now, the obvious solution would be to collect all the macros in the org buffer and insert them in the temporary buffer, or to simply use the current org buffer instead of a temporary buffer. Neither option is really nice...
Is there a proper way to achieve what I want?