Browse Source

ox-extra: Merge sections from ignored headlines

* contrib/lisp/ox-extra.el (org-extra--merge-sections): New function.
(org-export-ignore-headlines): Merge multiple sections that result
from removing ignored headlines.

Prevent org-export-ignore-headlines from violating the one headline to
one section mapping that is relied on by at least one export backend,
ox-texinfo.  (ox-texinfo uses each section to generate the menu.)
Kyle Meyer 2 years ago
parent
commit
fef346798f
1 changed files with 19 additions and 0 deletions
  1. 19 0
      contrib/lisp/ox-extra.el

+ 19 - 0
contrib/lisp/ox-extra.el

@@ -152,8 +152,27 @@ parent."
                 (org-element-contents object)))
         (org-element-extract-element object)))
     info nil)
+  (org-extra--merge-sections data backend info)
   data)
 
+(defun org-extra--merge-sections (data _backend info)
+  (org-element-map data 'headline
+    (lambda (hl)
+      (let ((sections
+             (cl-loop
+              for el in (org-element-map (org-element-contents hl)
+                            '(headline section) #'identity info)
+              until (eq (org-element-type el) 'headline)
+              collect el)))
+        (when (and sections
+                   (> (length sections) 1))
+          (apply #'org-element-adopt-elements
+                 (car sections)
+                 (cl-mapcan (lambda (s) (org-element-contents s))
+                            (cdr sections)))
+          (mapc #'org-element-extract-element (cdr sections)))))
+    info))
+
 (defconst ox-extras
   '((latex-header-blocks org-latex-header-blocks-filter org-export-before-parsing-hook)
     (ignore-headlines org-export-ignore-headlines org-export-filter-parse-tree-functions))