From 9d95ad617f890142fe0d31ae355d205ef40ce6f3 Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Sun, 15 Jun 2014 19:46:31 -0400 Subject: [PATCH 1/2] export remove ignore headline and promote children * lisp/ox.el (org-export-ignore-headlines-retain-and-promoting-children): A new function. (org-export-as): Include `org-export-ignore-headlines-retain-and-promoting-children' into the export process. * testing/lisp/test-ox.el (test-org-export/ignored-headlines-text): Example org-mode file for ignoreexport headline tests. (test-org-export/handle-ignored-headlines-system): System tests for org-export-ignore-headlines-retain-and-promoting-children. (test-org-export/handle-ignored-headlines-unit): Unit tests for org-export-ignore-headlines-retain-and-promoting-children. --- lisp/ox.el | 31 ++++++++++++++++++++++++++++ testing/lisp/test-ox.el | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/lisp/ox.el b/lisp/ox.el index 4bfef52..ae3a11c 100644 --- a/lisp/ox.el +++ b/lisp/ox.el @@ -2320,6 +2320,34 @@ tree is modified by side effect and returned by the function." (plist-get info prop) info)))) +(defun org-export-ignore-headlines-retain-and-promoting-children (data info) + "Remove headlines tagged \"ignoreexport\" retaining sub-headlines. +DATA is the parse tree. INFO is a plist containing export +options. Each headline tagged \"ignoreexport\" will be removed +removing its contents but retaining and promoting any children +headlines by a single level." + (org-element-map data org-element-all-elements + (lambda (object) + (when (and (equal 'headline (org-element-type object)) + (member "ignoreexport" (org-element-property :tags object))) + (mapc (lambda (el) + ;; recursively promote all nested headlines + (org-element-map el 'headline + (lambda (el) + (when (equal 'headline (org-element-type el)) + (org-element-put-property el + :level (1- (org-element-property :level el)))))) + ;; insert back into parse tree + (org-element-insert-before el object)) + ;; drop first three elements of headline + ;; 1. headline tag + ;; 2. properties + ;; 3. section + (cdddr object)) + (org-element-extract-element object))) + info nil) + data) + (defun org-export--remove-uninterpreted-data-1 (data info) "Change uninterpreted elements back into Org syntax. DATA is a parse tree or a secondary string. INFO is a plist @@ -3124,6 +3152,9 @@ Return code as a string." ;; Handle left-over uninterpreted elements or objects in ;; parse tree and communication channel. (org-export-remove-uninterpreted-data tree info) + ;; Remove headlines tagged "ignoreexport" and promote their + ;; children. + (org-export-ignore-headlines-retain-and-promoting-children tree info) ;; Call options filters and update export options. We do not ;; use `org-export-filter-apply-functions' here since the ;; arity of such filters is different. diff --git a/testing/lisp/test-ox.el b/testing/lisp/test-ox.el index 234032e..2ed0af9 100644 --- a/testing/lisp/test-ox.el +++ b/testing/lisp/test-ox.el @@ -1782,6 +1782,60 @@ Paragraph[fn:1]" (org-export-as (org-test-default-backend) nil nil nil '(:with-tasks nil)))))))) +(defvar test-org-export/ignored-headlines-text + "* H1 +Text1 +** H2 :ignoreexport: +Text2 +*** H3 +Text3 +**** H4 +Text4 +*** H5 +Text5 +**** H6 :ignoreexport: +Text6 +***** H7 +Text7 +***** H8 +Text8\n") + +(ert-deftest test-org-export/handle-ignored-headlines-system () + "Test `org-export-ignore-headlines-retain-and-promoting-children'." + (let ((exported + (org-test-with-temp-text test-org-export/ignored-headlines-text + (org-export-as (org-test-default-backend) nil nil nil nil)))) + ;; ensure ignored headlines and contents are not present + (mapc (lambda (s) (should-not (string-match (regexp-quote s) exported))) + (list "H2" "Text2" "H6" "Text6")) + ;; ensure all other headlines and contents are present + (mapc (lambda (s) (should (string-match (regexp-quote s) exported))) + ;; should not be included + (list "H1" "Text1" "H3" "Text3" "H4" "Text4" + "H5" "Text5" "H7" "Text7" "H8" "Text8")))) + +(ert-deftest test-org-export/handle-ignored-headlines-unit () + "Test `org-export-ignore-headlines-retain-and-promoting-children'." + (let ((data (org-export-ignore-headlines-retain-and-promoting-children + (org-test-with-temp-text test-org-export/ignored-headlines-text + (org-element-parse-buffer)) + nil))) + (flet ((level-of (name) + (let (out) + (org-element-map data 'headline + (lambda (el) + (when (string= name + (org-element-property :raw-value el)) + (setq out (org-element-property :level el))))) + out))) + ;; ensure ignored headlines and contents are not present + (mapc (lambda (pair) (should (= (car pair) (level-of (cdr pair))))) + '((1 . "H1") + (2 . "H3") + (3 . "H4") + (2 . "H5") + (3 . "H7") + (3 . "H8")))))) ;;; Keywords -- 2.0.0