Ver Fonte

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 "\"\"")
  ...
Nicolas Goaziou há 12 anos atrás
pai
commit
659edb40a5
2 ficheiros alterados com 41 adições e 20 exclusões
  1. 25 18
      lisp/ox.el
  2. 16 2
      testing/lisp/test-ox.el

+ 25 - 18
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)

+ 16 - 2
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"