Browse Source

Implement `org-export-excluded-from-toc-p'

* lisp/ox.el (org-export-excluded-from-toc-p): New function.

* testing/lisp/test-ox.el (test-org-export/excluded-from-toc-p): New
  test.
Nicolas Goaziou 7 years ago
parent
commit
b401d3a44b
3 changed files with 38 additions and 0 deletions
  1. 6 0
      etc/ORG-NEWS
  2. 15 0
      lisp/ox.el
  3. 17 0
      testing/lisp/test-ox.el

+ 6 - 0
etc/ORG-NEWS

@@ -108,6 +108,12 @@ you should expect to see something like:
   ,#+STARTUP: shrink
 #+END_EXAMPLE
 
+** New functions
+
+*** ~org-export-excluded-from-toc-p~
+
+See docstring for details.
+
 ** Miscellaneous
 
 *** ~org-publish-resolve-external-link~ accepts a new optional argument.

+ 15 - 0
lisp/ox.el

@@ -5288,6 +5288,21 @@ INFO is a plist used as a communication channel.
 Return a list of src-block elements with a caption."
   (org-export-collect-elements 'src-block info))
 
+(defun org-export-excluded-from-toc-p (headline info)
+  "Non-nil if HEADLINE should be excluded from tables of contents.
+
+INFO is a plist used as a communication channel.
+
+Note that such headlines are already excluded from
+`org-export-collect-headlines'.  Therefore, this function is not
+necessary if you only need to list headlines in the table of
+contents.  However, it is useful if some additional processing is
+required on headlines excluded from table of contents."
+  (or (org-element-property :footnote-section-p headline)
+      (org-export-low-level-p headline info)
+      (cl-some (lambda (h) (equal "notoc" (org-element-property :UNNUMBERED h)))
+	       (org-element-lineage headline nil t))))
+
 (defun org-export-toc-entry-backend (parent &rest transcoders)
   "Return an export back-end appropriate for table of contents entries.
 

+ 17 - 0
testing/lisp/test-ox.el

@@ -4344,6 +4344,23 @@ Another text. (ref:text)
 	      (mapcar (lambda (h) (org-element-property :raw-value h))
 		      (org-export-collect-headlines info 1 scope)))))))
 
+(ert-deftest test-org-export/excluded-from-toc-p ()
+  "Test `org-export-excluded-from-toc-p' specifications."
+  (should-not
+   (org-test-with-parsed-data "* H1"
+     (org-element-map tree 'headline
+       (lambda (h) (org-export-excluded-from-toc-p h info)) info t)))
+  (should
+   (org-test-with-parsed-data "* H1\n:PROPERTIES:\n:UNNUMBERED: notoc\n:END:"
+     (org-element-map tree 'headline
+       (lambda (h) (org-export-excluded-from-toc-p h info)) info t)))
+  (should
+   (equal '(in out)
+	  (org-test-with-parsed-data "#+OPTIONS: H:1\n* H1\n** H2"
+	    (org-element-map tree 'headline
+	      (lambda (h) (if (org-export-excluded-from-toc-p h info) 'out 'in))
+	      info)))))
+
 (ert-deftest test-org-export/toc-entry-backend ()
   "Test `org-export-toc-entry-backend' specifications."
   ;; Ignore targets.