Browse Source

ox: Fix export of empty src blocks

* lisp/ox.el (org-export-format-code-default): Handle empty source
  blocks more gracefully.
* lisp/ox-ascii.el (org-ascii-src-block): Handle empty blocks more
  gracefully.
* testing/lisp/test-ox.el: Add tests.
Nicolas Goaziou 12 years ago
parent
commit
8f40d7f7a1
3 changed files with 65 additions and 38 deletions
  1. 8 7
      lisp/ox-ascii.el
  2. 32 31
      lisp/ox.el
  3. 25 0
      testing/lisp/test-ox.el

+ 8 - 7
lisp/ox-ascii.el

@@ -1542,13 +1542,14 @@ holding contextual information."
   "Transcode a SRC-BLOCK element from Org to ASCII.
 CONTENTS holds the contents of the item.  INFO is a plist holding
 contextual information."
-  (let ((caption (org-ascii--build-caption src-block info)))
-    (concat
-     (when (and caption org-ascii-caption-above) (concat caption "\n"))
-     (org-ascii--box-string
-      (org-export-format-code-default src-block info) info)
-     (when (and caption (not org-ascii-caption-above))
-       (concat "\n" caption)))))
+  (let ((caption (org-ascii--build-caption src-block info))
+	(code (org-export-format-code-default src-block info)))
+    (if (equal code "") ""
+      (concat
+       (when (and caption org-ascii-caption-above) (concat caption "\n"))
+       (org-ascii--box-string code info)
+       (when (and caption (not org-ascii-caption-above))
+	 (concat "\n" caption))))))
 
 
 ;;;; Statistics Cookie

+ 32 - 31
lisp/ox.el

@@ -4003,37 +4003,38 @@ code."
   ;; Extract code and references.
   (let* ((code-info (org-export-unravel-code element))
          (code (car code-info))
-         (code-lines (org-split-string code "\n"))
-	 (refs (and (org-element-property :retain-labels element)
-		    (cdr code-info)))
-         ;; Handle line numbering.
-         (num-start (case (org-element-property :number-lines element)
-                      (continued (org-export-get-loc element info))
-                      (new 0)))
-         (num-fmt
-          (and num-start
-               (format "%%%ds  "
-                       (length (number-to-string
-                                (+ (length code-lines) num-start))))))
-         ;; Prepare references display, if required.  Any reference
-         ;; should start six columns after the widest line of code,
-         ;; wrapped with parenthesis.
-	 (max-width
-	  (+ (apply 'max (mapcar 'length code-lines))
-	     (if (not num-start) 0 (length (format num-fmt num-start))))))
-    (org-export-format-code
-     code
-     (lambda (loc line-num ref)
-       (let ((number-str (and num-fmt (format num-fmt line-num))))
-         (concat
-          number-str
-          loc
-          (and ref
-               (concat (make-string
-                        (- (+ 6 max-width)
-                           (+ (length loc) (length number-str))) ? )
-                       (format "(%s)" ref))))))
-     num-start refs)))
+	 (code-lines (org-split-string code "\n")))
+    (if (null code-lines) ""
+      (let* ((refs (and (org-element-property :retain-labels element)
+			(cdr code-info)))
+	     ;; Handle line numbering.
+	     (num-start (case (org-element-property :number-lines element)
+			  (continued (org-export-get-loc element info))
+			  (new 0)))
+	     (num-fmt
+	      (and num-start
+		   (format "%%%ds  "
+			   (length (number-to-string
+				    (+ (length code-lines) num-start))))))
+	     ;; Prepare references display, if required.  Any reference
+	     ;; should start six columns after the widest line of code,
+	     ;; wrapped with parenthesis.
+	     (max-width
+	      (+ (apply 'max (mapcar 'length code-lines))
+		 (if (not num-start) 0 (length (format num-fmt num-start))))))
+	(org-export-format-code
+	 code
+	 (lambda (loc line-num ref)
+	   (let ((number-str (and num-fmt (format num-fmt line-num))))
+	     (concat
+	      number-str
+	      loc
+	      (and ref
+		   (concat (make-string
+			    (- (+ 6 max-width)
+			       (+ (length loc) (length number-str))) ? )
+			   (format "(%s)" ref))))))
+	 num-start refs)))))
 
 
 ;;;; For Tables

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

@@ -1586,6 +1586,31 @@ Another text. (ref:text)
       (should (equal (org-export-unravel-code (org-element-at-point))
 		     '("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))))
 
+(ert-deftest test-org-export/format-code-default ()
+  "Test `org-export-format-code-default' specifications."
+  ;; Return the empty string when code is empty.
+  (should
+   (equal ""
+	  (org-test-with-parsed-data "#+BEGIN_SRC emacs-lisp\n\n\n#+END_SRC"
+	    (org-export-format-code-default
+	     (org-element-map tree 'src-block 'identity info t) info))))
+  ;; Number lines, two whitespace characters before the actual loc.
+  (should
+   (equal "1  a\n2  b\n"
+	  (org-test-with-parsed-data
+	      "#+BEGIN_SRC emacs-lisp +n\na\nb\n#+END_SRC"
+	    (org-export-format-code-default
+	     (org-element-map tree 'src-block 'identity info t) info))))
+  ;; Put references 6 whitespace characters after the widest line,
+  ;; wrapped within parenthesis.
+  (should
+   (equal "123      (a)\n1        (b)\n"
+	  (let ((org-coderef-label-format "(ref:%s)"))
+	    (org-test-with-parsed-data
+		"#+BEGIN_SRC emacs-lisp\n123 (ref:a)\n1 (ref:b)\n#+END_SRC"
+	      (org-export-format-code-default
+	       (org-element-map tree 'src-block 'identity info t) info))))))
+
 
 
 ;;; Smart Quotes