From 66fe322120331453e65654cb0b9bd15058492467 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Sun, 23 Sep 2012 19:37:21 +0200 Subject: [PATCH] org-export: New function to return a node property, even inherited * contrib/lisp/org-export.el (org-export-get-node-property): New function. * testing/lisp/test-org-export.el: Add tests. --- contrib/lisp/org-export.el | 20 ++++++++++++++++++++ testing/lisp/test-org-export.el | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/contrib/lisp/org-export.el b/contrib/lisp/org-export.el index 6c962e20b..886d1bcff 100644 --- a/contrib/lisp/org-export.el +++ b/contrib/lisp/org-export.el @@ -3148,6 +3148,26 @@ Any tag belonging to this list will also be removed." (member tag tags))) (org-element-property :tags element))) +(defun org-export-get-node-property (property blob &optional inherited) + "Return node PROPERTY value for BLOB. + +PROPERTY is normalized symbol (i.e. `:cookie-data'). BLOB is an +element or object. + +If optional argument INHERITED is non-nil, the value can be +inherited from a parent headline. + +Return value is a string or nil." + (let ((headline (if (eq (org-element-type blob) 'headline) blob + (org-export-get-parent-headline blob)))) + (if (not inherited) (org-element-property property blob) + (let ((parent headline) value) + (catch 'found + (while parent + (when (plist-member (nth 1 parent) property) + (throw 'found (org-element-property property parent))) + (setq parent (org-element-property :parent parent)))))))) + (defun org-export-first-sibling-p (headline info) "Non-nil when HEADLINE is the first sibling in its sub-tree. INFO is a plist used as a communication channel." diff --git a/testing/lisp/test-org-export.el b/testing/lisp/test-org-export.el index 3d3c846a2..6ba4e5da3 100644 --- a/testing/lisp/test-org-export.el +++ b/testing/lisp/test-org-export.el @@ -716,6 +716,38 @@ Paragraph[fn:1]" (org-export-get-tags (org-element-map tree 'headline 'identity info t) info '("ignore")))))) +(ert-deftest test-org-export/get-node-property () + "Test`org-export-get-node-property' specifications." + ;; Standard test. + (should + (equal "value" + (org-test-with-parsed-data "* Headline + :PROPERTIES: + :prop: value + :END:" + (org-export-get-node-property + :prop (org-element-map tree 'headline 'identity nil t))))) + ;; Test inheritance. + (should + (equal "value" + (org-test-with-parsed-data "* Parent + :PROPERTIES: + :prop: value + :END: +** Headline + Paragraph" + (org-export-get-node-property + :prop (org-element-map tree 'paragraph 'identity nil t) t)))) + ;; Cannot return a value before the first headline. + (should-not + (org-test-with-parsed-data "Paragraph +* Headline + :PROPERTIES: + :prop: value + :END:" + (org-export-get-node-property + :prop (org-element-map tree 'paragraph 'identity nil t))))) + (ert-deftest test-org-export/first-sibling-p () "Test `org-export-first-sibling-p' specifications." ;; Standard test. -- 2.11.4.GIT