Browse Source

org-element: Allow multiple caption keywords

* lisp/org-element.el (org-element-multiple-keywords): Allow multiple
  caption keywords.
* contrib/lisp/org-export.el (org-export-get-caption): New function.
* testing/lisp/test-org-element.el: Add tests.
* testing/lisp/test-org-export.el: Add tests.
Nicolas Goaziou 13 years ago
parent
commit
fe140488aa

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

@@ -2900,6 +2900,19 @@ properties."
 		(read (format "(%s)" (mapconcat 'identity value " ")))))))
 		(read (format "(%s)" (mapconcat 'identity value " ")))))))
     (if property (plist-get attributes property) attributes)))
     (if property (plist-get attributes property) attributes)))
 
 
+(defun org-export-get-caption (element &optional shortp)
+  "Return caption from ELEMENT as a secondary string.
+
+When optional argument SHORTP is non-nil, return short caption,
+as a secondary string, instead.
+
+Caption lines are separated by a white space."
+  (let ((full-caption (org-element-property :caption element)) caption)
+    (dolist (line full-caption (cdr caption))
+      (let ((cap (funcall (if shortp 'cdr 'car) line)))
+	(when cap
+	  (setq caption (nconc caption (list " ") (copy-sequence cap))))))))
+
 
 
 ;;;; For Export Snippets
 ;;;; For Export Snippets
 ;;
 ;;

+ 2 - 2
lisp/org-element.el

@@ -248,8 +248,8 @@ Don't modify it, set `org-element-affiliated-keywords' instead.")
 The key is the old name and the value the new one.  The property
 The key is the old name and the value the new one.  The property
 holding their value will be named after the translated name.")
 holding their value will be named after the translated name.")
 
 
-(defconst org-element-multiple-keywords '("HEADER")
-  "List of affiliated keywords that can occur more that once in an element.
+(defconst org-element-multiple-keywords '("CAPTION" "HEADER")
+  "List of affiliated keywords that can occur more than once in an element.
 
 
 Their value will be consed into a list of strings, which will be
 Their value will be consed into a list of strings, which will be
 returned as the value of the property.
 returned as the value of the property.

+ 18 - 5
testing/lisp/test-org-element.el

@@ -177,14 +177,20 @@ Some other text
   ;; Parse "parsed" keywords.
   ;; Parse "parsed" keywords.
   (should
   (should
    (equal
    (equal
-    '("caption")
+    '(("caption"))
     (org-test-with-temp-text "#+CAPTION: caption\nParagraph"
     (org-test-with-temp-text "#+CAPTION: caption\nParagraph"
       (car (org-element-property :caption (org-element-at-point))))))
       (car (org-element-property :caption (org-element-at-point))))))
   ;; Parse dual keywords.
   ;; Parse dual keywords.
   (should
   (should
    (equal
    (equal
-    '(("long") "short")
+    '((("long") "short"))
     (org-test-with-temp-text "#+CAPTION[short]: long\nParagraph"
     (org-test-with-temp-text "#+CAPTION[short]: long\nParagraph"
+      (org-element-property :caption (org-element-at-point)))))
+  ;; Allow multiple caption keywords.
+  (should
+   (equal
+    '((("l1") "s1") (("l2") "s2"))
+    (org-test-with-temp-text "#+CAPTION[s1]: l1\n#+CAPTION[s2]: l2\nParagraph"
       (org-element-property :caption (org-element-at-point))))))
       (org-element-property :caption (org-element-at-point))))))
 
 
 
 
@@ -1688,14 +1694,21 @@ Outside list"
   (should
   (should
    (equal
    (equal
     (org-element-interpret-data
     (org-element-interpret-data
-     '(org-data nil (paragraph (:caption ("caption")) "Paragraph")))
+     '(org-data nil (paragraph (:caption (("caption"))) "Paragraph")))
     "#+CAPTION: caption\nParagraph\n"))
     "#+CAPTION: caption\nParagraph\n"))
   ;; Interpret dual keywords.
   ;; Interpret dual keywords.
   (should
   (should
    (equal
    (equal
     (org-element-interpret-data
     (org-element-interpret-data
-     '(org-data nil (paragraph (:caption (("long") "short")) "Paragraph")))
-    "#+CAPTION[short]: long\nParagraph\n")))
+     '(org-data nil (paragraph (:caption ((("long") "short"))) "Paragraph")))
+    "#+CAPTION[short]: long\nParagraph\n"))
+  ;; Interpret multiple parsed dual keywords.
+  (should
+   (equal
+    (org-element-interpret-data
+     '(org-data nil (paragraph
+		     (:caption ((("l1") "s1") (("l2") "s2"))) "Paragraph")))
+    "#+CAPTION[s1]: l1\n#+CAPTION[s2]: l2\nParagraph\n")))
 
 
 (ert-deftest test-org-element/center-block-interpreter ()
 (ert-deftest test-org-element/center-block-interpreter ()
   "Test center block interpreter."
   "Test center block interpreter."

+ 21 - 0
testing/lisp/test-org-export.el

@@ -479,6 +479,27 @@ body\n")))
     :attr_html
     :attr_html
     (org-test-with-temp-text "Paragraph" (org-element-at-point)))))
     (org-test-with-temp-text "Paragraph" (org-element-at-point)))))
 
 
+(ert-deftest test-org-export/get-caption ()
+  "Test `org-export-get-caption' specifications."
+  ;; Without optional argument, return long caption
+  (should
+   (equal
+    '("l")
+    (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
+      (org-export-get-caption (org-element-at-point)))))
+  ;; With optional argument, return short caption.
+  (should
+   (equal
+    '("s")
+    (org-test-with-temp-text "#+CAPTION[s]: l\nPara"
+      (org-export-get-caption (org-element-at-point) t))))
+  ;; Multiple lines are separated by white spaces.
+  (should
+   (equal
+    '("a" " " "b")
+    (org-test-with-temp-text "#+CAPTION: a\n#+CAPTION: b\nPara"
+      (org-export-get-caption (org-element-at-point))))))
+
 
 
 
 
 ;;; Export Snippets
 ;;; Export Snippets