forked from mirrors/org-mode
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:
parent
a3e554f4cf
commit
8f40d7f7a1
|
@ -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))
|
||||||
|
(code (org-export-format-code-default src-block info)))
|
||||||
|
(if (equal code "") ""
|
||||||
(concat
|
(concat
|
||||||
(when (and caption org-ascii-caption-above) (concat caption "\n"))
|
(when (and caption org-ascii-caption-above) (concat caption "\n"))
|
||||||
(org-ascii--box-string
|
(org-ascii--box-string code info)
|
||||||
(org-export-format-code-default src-block info) info)
|
|
||||||
(when (and caption (not org-ascii-caption-above))
|
(when (and caption (not org-ascii-caption-above))
|
||||||
(concat "\n" caption)))))
|
(concat "\n" caption))))))
|
||||||
|
|
||||||
|
|
||||||
;;;; Statistics Cookie
|
;;;; Statistics Cookie
|
||||||
|
|
|
@ -4003,8 +4003,9 @@ 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) ""
|
||||||
|
(let* ((refs (and (org-element-property :retain-labels element)
|
||||||
(cdr code-info)))
|
(cdr code-info)))
|
||||||
;; Handle line numbering.
|
;; Handle line numbering.
|
||||||
(num-start (case (org-element-property :number-lines element)
|
(num-start (case (org-element-property :number-lines element)
|
||||||
|
@ -4033,7 +4034,7 @@ code."
|
||||||
(- (+ 6 max-width)
|
(- (+ 6 max-width)
|
||||||
(+ (length loc) (length number-str))) ? )
|
(+ (length loc) (length number-str))) ? )
|
||||||
(format "(%s)" ref))))))
|
(format "(%s)" ref))))))
|
||||||
num-start refs)))
|
num-start refs)))))
|
||||||
|
|
||||||
|
|
||||||
;;;; For Tables
|
;;;; For Tables
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue