reddit / emacs

Defining org-export backends that make use of subtree properties

graduale · 2022-08-19 02:22
I've been banging my head against this for far too long, so I come asking for some help. I want to define an export backend that will allow me to export individual subtrees to some particular format, using information from the relevant subtree properties. But I seem to be missing something and cannot get it to work. Here's a very simple version of what I'm trying to accomplish. Given a simple file like this, * Heading 1 :PROPERTIES: ????STATUS: foo ????WHEN: today :END: * Heading 2 :PROPERTIES: ????STATUS: bar ????WHEN: yesterday :END: I want to know what to place instead of `????`, and how to define a suitable backend, so that: * If I call `test-backend-export-as-ascii` on the a buffer narrowed to the first subtree, I get a buffer whose contents are, say: ​ Status as of today is: foo * If I call `test-backend-export-as-ascii` on a buffer narrowed to the second subtree, I get a buffer whose contents are: ​ Status as of yesterday is: bar So far, nothing I've tried seems to work. After reading the documentation for `org-export-define-derived-backend',`org-export-options-alist\`, and looking at a few of the backends out there (ox-hugo, ox-reveal, etc.), my best shot was this: * First, I need to replace `????` with `:EXPORT_`, so that e.g. the first subtree looks like this: ​ * Heading 1 :PROPERTIES: :EXPORT_STATUS: foo :EXPORT_WHEN: today :END: * Second, I should derive a backend using the following three steps: ​ (org-export-define-derived-backend 'test-backend 'ascii :options-alist '((:status "STATUS" nil nil t) (:when "WHEN" nil nil t)) :translate-alist '((template . test-backend-template))) (defun test-backend-template (contents info) "A template." (let ((status (plist-get info :status)) (when (plist-get info :when))) (concat "Status as of " when " is: " status))) (defun test-backend-export-as-ascii (&optional async subtreep visible-only body-only ext-plist) "Our export function. Arguments like those for `org-ascii-export-as-ascii'." (interactive) (org-export-to-buffer 'test-backend "*TEST ASCII Export*" async t visible-only body-only ext-plist (lambda () (text-mode)))) Unfortunately, this doesn't work. The resulting buffer(s) ignore the value of the relevant properties. If I use keywords instead, e.g. by having the first subtree look like this: * Heading 1 :PROPERTIES: #+STATUS: foo #+WHEN: today :END: then I can get the output to pickup the relevant values, but regardless of whether I've narrowed to the first or second subtree, the export function will only work with the values assigned in the last subtree. (Switching the elements assigned to the `:options-alist` so the last argument for each property is nil, I get the export function to ignore all but the values assigned in the first subtree.)
Phil-Hudson · 2022-08-19 14:02
Properties look like this: :PROPERTIES: :SOME_PROPERTY: Value of SOME_PROPERTY :OTHER_PROPERTY: Value of OTHER_PROPERTY :END: What you show above are not properties.
graduale · 2022-08-19 17:15
Yeah. That’s what I said that if I used keywords it worked. But I realize I put the keywords inside a properties drawer which was probably confusing. I guess org-mode seems not to mind?