1

I want to split one big org file into smaller files. I have found function which do this but it's also exports it to html. So how can I just export the content? Without this function (org-html-export-as-html) ?

(defun my-org-export-each-level-1-headline-to-html (&optional scope)
 (interactive)
 (org-map-entries
  (lambda ()
    (let* ((title (car (last (org-get-outline-path t))))
           (dir (file-name-directory buffer-file-name))
           (filename (concat dir title ".html")))
      (org-narrow-to-subtree)
      (org-html-export-as-html)
      (write-file filename)
      (kill-current-buffer)
      (widen)))
  "LEVEL=1" scope))

Here is similar topic but with exporting to md files split every single org headline in a org file to separate md/org files

1 Answer 1

4

Try the following functions.

Below it's your function adapted to split top level headlines into different Org files.

(defun my-org-export-each-level-1-headline-to-org (&optional scope)
  (interactive)
  (org-map-entries
   (lambda ()
     (let* ((title (car (last (org-get-outline-path t))))
            (dir (file-name-directory buffer-file-name))
            (filename (concat dir title ".org"))
            content)
       (org-narrow-to-subtree)
       (setq content (buffer-substring-no-properties (point-min) (point-max)))
       (with-temp-buffer
         (insert content)
         (write-file filename))
       (widen)))
   "LEVEL=1" scope))

This one is the function you referred adapted to split every headlines into different Org files.

(defun my-org-export-each-headline-to-org (&optional scope)
  "Export each headline to an Org file with the title as filename.
    If SCOPE is nil headlines in the current buffer are exported.
    For other valid values for SCOPE see `org-map-entries'.
    Already existing files are overwritten."
  (interactive)
  ;; Widen buffer temporarily as narrowing would affect the exporting.
  (org-with-wide-buffer
   (save-mark-and-excursion
     ;; Loop through each headline.
     (org-map-entries
      (lambda ()
        ;; Get the plain headline text without statistics and make filename.
        (let* ((title (car (last (org-get-outline-path t))))
               (buffer (current-buffer))
               (dir (file-name-directory buffer-file-name))
               (filename (concat dir title ".org"))
               beg end)
          (setq beg (point))
          (outline-next-preface)
          (setq end (point))
          (with-temp-buffer
            (insert-buffer-substring buffer beg end)
            (write-file filename))))
      nil scope))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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