浏览代码

org-export: Add `org-export-read-attribute' for normalized attr lines

* contrib/lisp/org-export.el (org-export-read-attribute): New function.
* testing/lisp/test-org-export.el: Add test.
Nicolas Goaziou 13 年之前
父节点
当前提交
68a595ce9d
共有 2 个文件被更改,包括 34 次插入1 次删除
  1. 14 0
      contrib/lisp/org-export.el
  2. 20 1
      testing/lisp/test-org-export.el

+ 14 - 0
contrib/lisp/org-export.el

@@ -2649,6 +2649,20 @@ file should have."
 ;; macros, references, src-blocks, tables and tables of contents are
 ;; implemented.
 
+;;;; For Affiliated Keywords
+;;
+;; `org-export-read-attribute' is a tool
+
+(defun org-export-read-attribute (attribute element)
+  "Turn ATTRIBUTE property from ELEMENT into a plist.
+This function assumes attributes are defined as \":keyword
+value\" pairs.  It is appropriate for `:attr_html' like
+properties."
+  (let ((value (org-element-property attribute element)))
+    (and value
+	 (read (format "(%s)" (mapconcat 'identity value " "))))))
+
+
 ;;;; For Export Snippets
 ;;
 ;; Every export snippet is transmitted to the back-end.  Though, the

+ 20 - 1
testing/lisp/test-org-export.el

@@ -370,7 +370,7 @@ body\n")))
 	(should (equal (org-export-as 'test) "Body 1\nBody 2\n"))))))
 
 (ert-deftest test-org-export/set-element ()
-  "Test `org-export-set-element' property."
+  "Test `org-export-set-element' specifications."
   (org-test-with-parsed-data "* Headline\n*a*"
     (org-export-set-element
      (org-element-map tree 'bold 'identity nil t)
@@ -386,6 +386,25 @@ body\n")))
       (org-element-map tree 'paragraph 'identity nil t)))))
 
 
+
+;;; Affiliated Keywords
+
+(ert-deftest test-org-export/read-attribute ()
+  "Test `org-export-read-attribute' specifications."
+  ;; Standard test.
+  (should
+   (equal
+    (org-export-read-attribute
+     :attr_html
+     (org-test-with-temp-text "#+ATTR_HTML: :a 1 :b 2\nParagraph"
+       (org-element-current-element)))
+    '(:a 1 :b 2)))
+  ;; Return nil on empty attribute.
+  (should-not
+   (org-export-read-attribute
+    :attr_html
+    (org-test-with-temp-text "Paragraph" (org-element-current-element)))))
+
 
 ;;; Footnotes