From 8f40d7f7a171b4e664996c594ad18b4f8fa68859 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Sun, 3 Mar 2013 19:10:42 +0100 Subject: [PATCH] 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. --- lisp/ox-ascii.el | 15 ++++++------ lisp/ox.el | 63 +++++++++++++++++++++++++------------------------ testing/lisp/test-ox.el | 25 ++++++++++++++++++++ 3 files changed, 65 insertions(+), 38 deletions(-) diff --git a/lisp/ox-ascii.el b/lisp/ox-ascii.el index f7a713f97..078035eaf 100644 --- a/lisp/ox-ascii.el +++ b/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 diff --git a/lisp/ox.el b/lisp/ox.el index c44b1c38d..63b5601ed 100644 --- a/lisp/ox.el +++ b/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 diff --git a/testing/lisp/test-ox.el b/testing/lisp/test-ox.el index 7584d5fd1..6fa1f25fa 100644 --- a/testing/lisp/test-ox.el +++ b/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 -- 2.11.4.GIT