contrib/lisp/org-export.el: Simplify headlines collection

* contrib/lisp/org-export.el (org-export-collect-headlines): Simplify
  function, in order to allow greater flexibility to build a proper
  table of contents.
This commit is contained in:
Nicolas Goaziou 2011-12-20 19:16:19 +01:00
parent c3972265bb
commit 3210994bc4
1 changed files with 10 additions and 38 deletions

View File

@ -2586,57 +2586,29 @@ it also."
;;;; For Tables Of Contents ;;;; For Tables Of Contents
;; `org-export-get-headlines' builds a table of contents in the shape ;; `org-export-collect-headlines' builds a list of all exportable
;; of a nested list of cons cells whose car is headline's name and cdr ;; headline elements, maybe limited to a certain depth. One can then
;; an unique identifier. One can then easily parse it and transcode ;; easily parse it and transcode it.
;; it in a back-end. Identifiers can be used to construct internal
;; links.
;; Building lists of tables, figures or listings is quite similar. ;; Building lists of tables, figures or listings is quite similar.
;; Once the generic function `org-export-collect-elements' is defined, ;; Once the generic function `org-export-collect-elements' is defined,
;; `org-export-collect-tables', `org-export-collect-figures' and ;; `org-export-collect-tables', `org-export-collect-figures' and
;; `org-export-collect-listings' can be derived from it. ;; `org-export-collect-listings' can be derived from it.
(defun org-export-get-headlines (backend info &optional n) (defun org-export-collect-headlines (info &optional n)
"Build a table of contents. "Collect headlines in order to build a table of contents.
BACKEND is the back-end used to transcode headline's name. INFO
is a plist holding export options.
When non-nil, optional argument N must be an integer. It When non-nil, optional argument N must be an integer. It
specifies the depth of the table of contents. specifies the depth of the table of contents.
Return an alist whose keys are headlines' name and value their Return a list of all exportable headlines as parsed elements."
relative level and an unique identifier that might be used for
internal links.
For example, on the following tree, where numbers in parens are
buffer position at beginning of the line:
* Title 1 (1)
** Sub-title 1 (21)
** Sub-title 2 (42)
* Title 2 (62)
the function will return:
\(\(\"Title 1\" 1 1\)
\(\"Sub-title 1\" 2 21\)
\(\"Sub-title 2\" 2 42\)
\(\"Title 2\" 1 62\)\)"
(org-element-map (org-element-map
(plist-get info :parse-tree) (plist-get info :parse-tree)
'headline 'headline
(lambda (headline local-info) (lambda (headline local)
;; Get HEADLINE's relative level. ;; Strip contents from HEADLINE.
(let ((level (+ (or (plist-get local-info :headline-offset) 0) (let ((relative-level (org-export-get-relative-level headline local)))
(org-element-get-property :level headline)))) (unless (and n (> relative-level n)) headline)))
(unless (and (wholenump n) (> level n))
(list
(org-export-secondary-string
(org-element-get-property :title headline) backend info)
level
(org-element-get-property :begin headline)))))
info)) info))
(defun org-export-collect-elements (type backend info) (defun org-export-collect-elements (type backend info)