3

This one is pretty straightforward. I'm translating in emacs org-mode, and my workflow looks a bit like this:

* Source text sentence

** Translation of source text sentence

* Another source text sentence

** The translation of that other sentence

What I'd like to do is take all (and only) second level headlines (which are my translations) and copy them into a separate document. Is this possible, or am I thinking too outside the box?

Thanks!

  • Do you want to delete them from the original? – NickD Jun 1 at 17:50
  • stackoverflow.com/questions/22200312/… might be helpful. Since there is a maxlevel option there should also be a minlevel option? Then all that is left is to generalise the org-refile to all headers – Ajned Jun 1 at 18:07
  • 1
    I'd follow another strategy less prone to pitfalls to separate original from translation; let's say two identically structured subtrees which you can edit in two windows in the same frame, then exporting based on something else more sensible than "second levels" (like noexport). Said that @gregoryg answer does what you've asked for. – Muihlinn Jun 1 at 19:10
  • Not an answer, just to give you an idea for future projects: Org provides you with other structures than just outlines. One or more of them may give you a better "semantic mapping" for text-to-translation pairs. Consider each of the following: tables (info "(org) Tables"), description lists (info "(org) Plain lists"), properties (info "(org) Properties and Columns"). – Phil Hudson Jun 3 at 11:53
5

Another approach is to delete all non-level-2 entries before parsing the export buffer. This is different to what @gregoryg is proposing in that it also skips the body/text directly following non-level-2 headings. No need to use tags.

(defun export-translation (backend)
  (org-map-entries
   (lambda ()
     (unless (= (org-current-level) 2)
       (let ((beg (line-beginning-position))
             (end (or (save-excursion
                        (outline-next-heading))
                      (point-max))))
         (delete-region beg end))))))

You can add this function to org-export-before-parsing-hook as a file variable by adding the following line to the beginning of the Org document.

# -*- org-export-before-parsing-hook: export-translation -*-

Now to also skip the contents of level-2 entries, use:

(defun export-translation (backend)
  (save-excursion
    (goto-char (point-max))
    (while (re-search-backward org-complex-heading-regexp nil t)
      (let ((beg (save-excursion
                   (when (= (org-current-level) 2)
                     (forward-line))
                   (point)))
            (end (or (save-excursion
                       (outline-next-heading))
                     (point-max))))
        (delete-region beg end)))))
| improve this answer | |
  • 1
    Nice! Note to the OP: note that this is a filter, so it does not change the Org mode buffer (or the file), just what is exported. And it is nicely generizable (e.g. this keeps headlines at level 2 only, but you can easily change it to keep headlines at level 2 or higher, or headlines between level 2 and 7 - if that fancy strikes you). – NickD Jun 2 at 1:14
  • That function is not meant to be called interactively. That's why you're not seeing it. You should export as usual with C-c C-e ... I added another function that also skips the contents of level-2 entries. Let me know if that works for you. – jagrg Jun 2 at 18:36
  • Nice! I got it working. It works great, stripping out just the headlines that I want (though when it exports it still makes one big contents page thing out of all the headers - but I think that's probably related to something else). I'd love to hear about the function that strips the body text out - thanks so much! I'm still learning so much about this software and I sometimes still don't know what to do with some code. Thanks for this though, you've made my work significantly easier! – Jtrans Jun 2 at 22:13
2

You can use ox-extra functions from the org-plus-contrib package.

This would require you to tag all top-level headlines with the "ignore" tag.

(require 'ox-org) ; if exporting to Org Mode
(require 'ox-extra)
(ox-extras-activate '(ignore-headlines))

After tagging your top-level headlines, export to a plain text or Org file, e.g. C-c C-e O o

You can cancel your filter with

(ox-extras-deactivate '(ignore-headlines))

As @NickD points out, this method removes the headlines only - it will still export any text under that headline.

| improve this answer | |
  • 2
    One problem here is that only the headline is ignored: any text under that headline is retained. That may not be a problem for the OP, but it might be a problem for others who find this in the future. You might want to note it in your answer. – NickD Jun 1 at 20:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.