From 7cf4406088d4d7f50c35c6ba7ca3c351db2086bc Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Wed, 25 Sep 2013 18:49:39 +0200 Subject: [PATCH] ox: Ignore text before first headline with an :export: tag * lisp/ox.el (org-export--populate-ignore-list): Ignore text before first headline if an :export: tag (or a select tag) is found within the buffer. * testing/lisp/test-ox.el: Add tests. --- lisp/ox.el | 13 +++++++++---- testing/lisp/test-ox.el | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lisp/ox.el b/lisp/ox.el index 10354b283..6c593687c 100644 --- a/lisp/ox.el +++ b/lisp/ox.el @@ -1955,10 +1955,15 @@ for a footnotes section." "Return list of elements and objects to ignore during export. DATA is the parse tree to traverse. OPTIONS is the plist holding export options." - (let* (ignore - walk-data + (let* (walk-data ;; First find trees containing a select tag, if any. (selected (org-export--selected-trees data options)) + ;; If a select tag is active, also ignore the section before + ;; the first headline, if any. + (ignore (and selected + (let ((first-element (car (org-element-contents data)))) + (and (eq (org-element-type first-element) 'section) + first-element)))) (walk-data (lambda (data) ;; Collect ignored elements or objects into IGNORE-LIST. @@ -1969,8 +1974,8 @@ export options." (org-element-property :archivedp data)) ;; If headline is archived but tree below has ;; to be skipped, add it to ignore list. - (mapc (lambda (e) (push e ignore)) - (org-element-contents data)) + (dolist (element (org-element-contents data)) + (push element ignore)) ;; Move into secondary string, if any. (let ((sec-prop (cdr (assq type org-element-secondary-value-alist)))) diff --git a/testing/lisp/test-ox.el b/testing/lisp/test-ox.el index b5a4d3a5e..54248cb3f 100644 --- a/testing/lisp/test-ox.el +++ b/testing/lisp/test-ox.el @@ -338,6 +338,20 @@ Paragraph" (let ((org-tags-column 0)) (org-export-as (org-test-default-backend) nil nil nil '(:select-tags ("exp"))))))) + ;; If there is an include tag, ignore the section before the first + ;; headline, if any. + (should + (equal "* H1 :exp:\nBody\n" + (org-test-with-temp-text "First section\n* H1 :exp:\nBody" + (let ((org-tags-column 0)) + (org-export-as (org-test-default-backend) + nil nil nil '(:select-tags ("exp"))))))) + (should-not + (equal "* H1 :exp:\n" + (org-test-with-temp-text "* H1 :exp:\nBody" + (let ((org-tags-column 0)) + (org-export-as (org-test-default-backend) + nil nil nil '(:select-tags ("exp"))))))) ;; Test mixing include tags and exclude tags. (should (string-match