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 "\"\"")
  ...
This commit is contained in:
Nicolas Goaziou 2013-04-10 18:33:12 +02:00
parent 69ebb265be
commit 659edb40a5
2 changed files with 41 additions and 20 deletions

View File

@ -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)

View File

@ -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"