diff options
author | Nicolas Goaziou <n.goaziou@gmail.com> | 2012-09-23 19:37:21 +0200 |
---|---|---|
committer | Nicolas Goaziou <n.goaziou@gmail.com> | 2012-09-23 19:37:21 +0200 |
commit | 66fe322120331453e65654cb0b9bd15058492467 (patch) | |
tree | e9b895bd34a5ea7e079f271012d232ee897bc2a9 | |
parent | b60445cfd44bf800f0c338cbf9795ceb2767a06d (diff) | |
download | org-mode-66fe322120331453e65654cb0b9bd15058492467.tar.gz |
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.
-rw-r--r-- | contrib/lisp/org-export.el | 20 | ||||
-rw-r--r-- | testing/lisp/test-org-export.el | 32 |
2 files changed, 52 insertions, 0 deletions
diff --git a/contrib/lisp/org-export.el b/contrib/lisp/org-export.el index 6c962e2..886d1bc 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 3d3c846..6ba4e5d 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. |