diff --git a/contrib/lisp/org-export.el b/contrib/lisp/org-export.el index 41e7a38ef..bbaf99524 100644 --- a/contrib/lisp/org-export.el +++ b/contrib/lisp/org-export.el @@ -2900,6 +2900,19 @@ properties." (read (format "(%s)" (mapconcat 'identity value " "))))))) (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 ;; diff --git a/lisp/org-element.el b/lisp/org-element.el index 47628c788..bcdd33684 100644 --- a/lisp/org-element.el +++ b/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 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 returned as the value of the property. diff --git a/testing/lisp/test-org-element.el b/testing/lisp/test-org-element.el index d6cbbbd95..a044e6544 100644 --- a/testing/lisp/test-org-element.el +++ b/testing/lisp/test-org-element.el @@ -177,14 +177,20 @@ Some other text ;; Parse "parsed" keywords. (should (equal - '("caption") + '(("caption")) (org-test-with-temp-text "#+CAPTION: caption\nParagraph" (car (org-element-property :caption (org-element-at-point)))))) ;; Parse dual keywords. (should (equal - '(("long") "short") + '((("long") "short")) (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)))))) @@ -1688,14 +1694,21 @@ Outside list" (should (equal (org-element-interpret-data - '(org-data nil (paragraph (:caption ("caption")) "Paragraph"))) + '(org-data nil (paragraph (:caption (("caption"))) "Paragraph"))) "#+CAPTION: caption\nParagraph\n")) ;; Interpret dual keywords. (should (equal (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 () "Test center block interpreter." diff --git a/testing/lisp/test-org-export.el b/testing/lisp/test-org-export.el index 59987a093..3d3c846a2 100644 --- a/testing/lisp/test-org-export.el +++ b/testing/lisp/test-org-export.el @@ -479,6 +479,27 @@ body\n"))) :attr_html (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