From 3fa3ed068fcfc58470430a5c4bae3a5ffd1ca3ed Mon Sep 17 00:00:00 2001 Message-ID: <3fa3ed068fcfc58470430a5c4bae3a5ffd1ca3ed.1713812419.git.yantar92@posteo.net> In-Reply-To: <229a563dc38e1fdfd63be2dfebb1a9e9023e44b2.1713812419.git.yantar92@posteo.net> References: <229a563dc38e1fdfd63be2dfebb1a9e9023e44b2.1713812419.git.yantar92@posteo.net> From: Ihor Radchenko Date: Sun, 21 Apr 2024 15:54:48 +0300 Subject: [PATCH v2 2/2] ox-md, ox-ascii, ox-texinfo: Strip blank lines from paragraphs * lisp/org-macs.el (org-remove-blank-lines): New helper function to strip blank lines from string. * lisp/ox-ascii.el (org-ascii-paragraph): * lisp/ox-latex.el (org-latex-paragraph): * lisp/ox-md.el (org-md-paragraph): * lisp/ox-texinfo.el (org-texinfo-paragraph): Strip blank lines from paragraphs - these exporters are using blank lines as paragraph separators. Reported-by: Max Nikulin Link: https://orgmode.org/list/v00le7$frp$1@ciao.gmane.io --- lisp/org-macs.el | 4 ++++ lisp/ox-ascii.el | 6 ++++++ lisp/ox-latex.el | 4 +--- lisp/ox-md.el | 6 ++++++ lisp/ox-texinfo.el | 7 ++++++- 5 files changed, 23 insertions(+), 4 deletions(-) diff --git a/lisp/org-macs.el b/lisp/org-macs.el index 1254ddb54..93803bfe9 100644 --- a/lisp/org-macs.el +++ b/lisp/org-macs.el @@ -1244,6 +1244,10 @@ (defun org-remove-tabs (s &optional width) t t s))) s) +(defun org-remove-blank-lines (s) + "Remove blank lines in S." + (replace-regexp-in-string (rx "\n" (1+ (0+ space) "\n")) "\n" s)) + (defun org-wrap (string &optional width lines) "Wrap string to either a number of lines, or a width in characters. If WIDTH is non-nil, the string is wrapped to that width, however many lines diff --git a/lisp/ox-ascii.el b/lisp/ox-ascii.el index db4356ec6..e767f66cf 100644 --- a/lisp/ox-ascii.el +++ b/lisp/ox-ascii.el @@ -1651,6 +1651,12 @@ (defun org-ascii-paragraph (paragraph contents info) "Transcode a PARAGRAPH element from Org to ASCII. CONTENTS is the contents of the paragraph, as a string. INFO is the plist used as a communication channel." + ;; Ensure that we do not create multiple paragraphs, when a single + ;; paragraph is expected. + ;; Multiple newlines may appear in CONTENTS, for example, when + ;; certain objects are stripped from export, leaving single newlines + ;; before and after. + (setq contents (org-remove-blank-lines contents)) (org-ascii--justify-element (let ((indented-line-width (plist-get info :ascii-indented-line-width))) (if (not (wholenump indented-line-width)) contents diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el index 5c19e1fe7..2267a604e 100644 --- a/lisp/ox-latex.el +++ b/lisp/ox-latex.el @@ -3039,9 +3039,7 @@ (defun org-latex-paragraph (_paragraph contents _info) ;; Multiple newlines may appear in CONTENTS, for example, when ;; certain objects are stripped from export, leaving single newlines ;; before and after. - (replace-regexp-in-string - (rx "\n" (1+ (0+ space) "\n")) "\n" - contents)) + (org-remove-blank-lines contents)) ;;;; Plain List diff --git a/lisp/ox-md.el b/lisp/ox-md.el index fa2beeb95..28f0a4cf6 100644 --- a/lisp/ox-md.el +++ b/lisp/ox-md.el @@ -628,6 +628,12 @@ (defun org-md-paragraph (paragraph contents _info) "Transcode PARAGRAPH element into Markdown format. CONTENTS is the paragraph contents. INFO is a plist used as a communication channel." + ;; Ensure that we do not create multiple paragraphs, when a single + ;; paragraph is expected. + ;; Multiple newlines may appear in CONTENTS, for example, when + ;; certain objects are stripped from export, leaving single newlines + ;; before and after. + (setq contents (org-remove-blank-lines contents)) (let ((first-object (car (org-element-contents paragraph)))) ;; If paragraph starts with a #, protect it. (if (and (stringp first-object) (string-prefix-p "#" first-object)) diff --git a/lisp/ox-texinfo.el b/lisp/ox-texinfo.el index 4aef9c41c..fc9ec9209 100644 --- a/lisp/ox-texinfo.el +++ b/lisp/ox-texinfo.el @@ -1517,7 +1517,12 @@ (defun org-texinfo-paragraph (_paragraph contents _info) "Transcode a PARAGRAPH element from Org to Texinfo. CONTENTS is the contents of the paragraph, as a string. INFO is the plist used as a communication channel." - contents) + ;; Ensure that we do not create multiple paragraphs, when a single + ;; paragraph is expected. + ;; Multiple newlines may appear in CONTENTS, for example, when + ;; certain objects are stripped from export, leaving single newlines + ;; before and after. + (org-remove-blank-lines contents)) ;;;; Plain List -- 2.44.0