Browse Source

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.
Nicolas Goaziou 12 years ago
parent
commit
66fe322120
2 changed files with 52 additions and 0 deletions
  1. 20 0
      contrib/lisp/org-export.el
  2. 32 0
      testing/lisp/test-org-export.el

+ 20 - 0
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."

+ 32 - 0
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.