Browse Source

ox: Fix default title

* lisp/ox.el (org-export--get-global-options): Properly set default
  title, i.e. when to TITLE keyword is provided.
* testing/lisp/test-ox.el: Add tests.
Nicolas Goaziou 4 years ago
parent
commit
16f12e0aef
2 changed files with 68 additions and 14 deletions
  1. 18 14
      lisp/ox.el
  2. 50 0
      testing/lisp/test-ox.el

+ 18 - 14
lisp/ox.el

@@ -1731,20 +1731,24 @@ process."
      (lambda (cell)
        (let ((prop (car cell)))
 	 (unless (plist-member plist prop)
-	   (setq plist
-		 (plist-put
-		  plist
-		  prop
-		  ;; Eval default value provided.  If keyword is a member
-		  ;; of `org-element-document-properties', parse it as
-		  ;; a secondary string before storing it.
-		  (let ((value (eval (nth 3 cell))))
-		    (if (not (stringp value)) value
-		      (let ((keyword (nth 1 cell)))
-			(if (not (member keyword org-element-document-properties))
-			    value
-			  (org-element-parse-secondary-string
-			   value (org-element-restriction 'keyword)))))))))))
+	   (let ((value (eval (nth 3 cell))))
+	     ;; Only set property if default value is non-nil.
+	     (when value
+	       (setq plist
+		     (plist-put
+		      plist
+		      prop
+		      ;; If keyword belongs to
+		      ;; `org-element-document-properties', parse
+		      ;; default value as a secondary string before
+		      ;; storing it.
+		      (if (not (stringp value)) value
+			(let ((keyword (nth 1 cell)))
+			  (if (not (member keyword
+					   org-element-document-properties))
+			      value
+			    (org-element-parse-secondary-string
+			     value (org-element-restriction 'keyword))))))))))))
      all)
     ;; Return value.
     plist))

+ 50 - 0
testing/lisp/test-ox.el

@@ -268,6 +268,56 @@ Paragraph"
 	    (progn (forward-line 2)
 		   (plist-get (org-export-get-environment nil t) :title))))))
 
+(ert-deftest test-org-export/set-title ()
+  "Test title setting."
+  ;; If no title if specified, use file name.
+  (should
+   (apply
+    'equal
+    (org-test-with-temp-text-in-file "Test"
+      (org-mode)
+      (let (org-export-registered-backends)
+	(org-export-define-backend 'test
+	  '((template . (lambda (text info)
+			  (org-export-data (plist-get info :title) info)))))
+	(list (org-export-as 'test)
+	      (file-name-nondirectory
+	       (file-name-sans-extension (buffer-file-name))))))))
+  ;; If no title is specified, and no file is associated to the
+  ;; buffer, use buffer's name.
+  (should
+   (apply
+    'equal
+    (org-test-with-temp-text "Test"
+      (org-mode)
+      (let (org-export-registered-backends)
+	(org-export-define-backend 'test
+	  '((template . (lambda (text info)
+			  (org-export-data (plist-get info :title) info)))))
+	(list (org-export-as 'test) (buffer-name))))))
+  ;; If a title is specified, use it.
+  (should
+   (equal
+    "Title"
+    (org-test-with-temp-text-in-file "#+TITLE: Title\nTest"
+      (org-mode)
+      (let (org-export-registered-backends)
+	(org-export-define-backend 'test
+	  '((template . (lambda (text info)
+			  (org-export-data (plist-get info :title) info)))))
+	(org-export-as 'test)))))
+  ;; If an empty title is specified, do not set it.
+  (should
+   (equal
+    ""
+    (org-test-with-temp-text-in-file "#+TITLE:\nTest"
+      (org-mode)
+      (let (org-export-registered-backends)
+	(org-export-define-backend 'test
+	  '((template . (lambda (text info)
+			  (org-export-data (plist-get info :title) info)))))
+	(org-export-as 'test))))))
+
 (ert-deftest test-org-export/handle-options ()
   "Test if export options have an impact on output."
   ;; Test exclude tags for headlines and inlinetasks.