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.
This commit is contained in:
Nicolas Goaziou 2013-03-03 19:10:42 +01:00
parent a3e554f4cf
commit 8f40d7f7a1
3 changed files with 65 additions and 38 deletions

View File

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

View File

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

View File

@ -1586,6 +1586,31 @@ Another text. (ref:text)
(should (equal (org-export-unravel-code (org-element-at-point)) (should (equal (org-export-unravel-code (org-element-at-point))
'("(+ 2 2)\n(+ 3 3)\n" (2 . "one"))))))) '("(+ 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 ;;; Smart Quotes