From 659edb40a57114d4ee7592322fa1d5bd1d588844 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Wed, 10 Apr 2013 18:33:12 +0200 Subject: [PATCH] ox: Allow to use empty strings in attributes * lisp/ox.el (org-export-read-attribute): Allow to use empty strings in attributes. * testing/lisp/test-ox.el: Add tests. With this patch, #+attr_backend: :a "" becomes (:a "") #+attr_backend: :a """" becomes (:a "\"\"") ... --- lisp/ox.el | 43 ++++++++++++++++++++++++----------------- testing/lisp/test-ox.el | 18 +++++++++++++++-- 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/lisp/ox.el b/lisp/ox.el index 519f568c2..7a00541d4 100644 --- a/lisp/ox.el +++ b/lisp/ox.el @@ -3423,24 +3423,31 @@ that property within attributes. This function assumes attributes are defined as \":keyword value\" pairs. It is appropriate for `:attr_html' like -properties. All values will become strings except the empty -string and \"nil\", which will become nil." - (let ((attributes - (let ((value (org-element-property attribute element))) - (when value - (let ((s (mapconcat 'identity value " ")) result) - (while (string-match - "\\(?:^\\|[ \t]+\\)\\(:[-a-zA-Z0-9_]+\\)\\([ \t]+\\|$\\)" - s) - (let ((value (substring s 0 (match-beginning 0)))) - (push (and (not (member value '("nil" ""))) value) result)) - (push (intern (match-string 1 s)) result) - (setq s (substring s (match-end 0)))) - ;; Ignore any string before the first property with `cdr'. - (cdr (nreverse (cons (and (org-string-nw-p s) - (not (equal s "nil")) - s) - result)))))))) +properties. + +All values will become strings except the empty string and +\"nil\", which will become nil. Also, values containing only +double quotes will be read as-is, which means that \"\" value +will become the empty string." + (let* ((prepare-value + (lambda (str) + (cond ((member str '(nil "" "nil")) nil) + ((string-match "^\"\\(\"+\\)?\"$" str) + (or (match-string 1 str) "")) + (t str)))) + (attributes + (let ((value (org-element-property attribute element))) + (when value + (let ((s (mapconcat 'identity value " ")) result) + (while (string-match + "\\(?:^\\|[ \t]+\\)\\(:[-a-zA-Z0-9_]+\\)\\([ \t]+\\|$\\)" + s) + (let ((value (substring s 0 (match-beginning 0)))) + (push (funcall prepare-value value) result)) + (push (intern (match-string 1 s)) result) + (setq s (substring s (match-end 0)))) + ;; Ignore any string before first property with `cdr'. + (cdr (nreverse (cons (funcall prepare-value s) result)))))))) (if property (plist-get attributes property) attributes))) (defun org-export-get-caption (element &optional shortp) diff --git a/testing/lisp/test-ox.el b/testing/lisp/test-ox.el index 6203f8b1d..46531a179 100644 --- a/testing/lisp/test-ox.el +++ b/testing/lisp/test-ox.el @@ -383,10 +383,10 @@ Paragraph" (org-test-with-temp-text "CLOSED: [2012-04-29 sun. 10:45]" (org-test-with-backend test (should - (equal (org-export-as 'test nil nil nil '(:with-plannings t)) + (equal (org-export-as 'test nil nil nil '(:with-planning t)) "CLOSED: [2012-04-29 sun. 10:45]\n")) (should - (equal (org-export-as 'test nil nil nil '(:with-plannings nil)) + (equal (org-export-as 'test nil nil nil '(:with-planning nil)) ""))))) ;; Statistics cookies. (should @@ -687,6 +687,20 @@ body\n"))) :attr_html (org-test-with-temp-text "#+ATTR_HTML: :a :b\nParagraph" (org-element-at-point))))) + ;; Return empty string when value is "". + (should + (equal '(:a "") + (org-export-read-attribute + :attr_html + (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\nParagraph" + (org-element-at-point))))) + ;; Return \"\" when value is """". + (should + (equal '(:a "\"\"") + (org-export-read-attribute + :attr_html + (org-test-with-temp-text "#+ATTR_HTML: :a \"\"\"\"\nParagraph" + (org-element-at-point))))) ;; Ignore text before first property. (should-not (member "ignore"