branch: externals-release/org
commit fbdf1bbe9c53bb1a117a54b1ca4e5a63a571649e
Author: Ihor Radchenko <[email protected]>
Commit: Ihor Radchenko <[email protected]>

    ox-odt: Avoid trailing spaces at the end of paragraphs
    
    * lisp/ox-odt.el (org-odt--strip-trailing-newlines): Strip trailing
    newlines in final plain text objects inside paragraph-level elements.
    These newlines are normally present in the output of Org parser, but
    are not needed in ODT: (1) there are contents tags to mark boundaries;
    (2) newlines are interpreted as spaces in ODT spec, which is not
    expected.
    
    Link: 
https://old.reddit.com/r/orgmode/comments/1mzdds8/odt_export_adding_one_space_to_end_of_lines/
---
 lisp/ox-odt.el | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/lisp/ox-odt.el b/lisp/ox-odt.el
index ba8b4d9d3b..2e649324ad 100644
--- a/lisp/ox-odt.el
+++ b/lisp/ox-odt.el
@@ -91,7 +91,8 @@
     (verbatim . org-odt-verbatim)
     (verse-block . org-odt-verse-block))
   :filters-alist '((:filter-parse-tree
-                   . (org-odt--translate-latex-fragments
+                   . (org-odt--strip-trailing-newlines
+                       org-odt--translate-latex-fragments
                       org-odt--translate-description-lists
                       org-odt--translate-list-tables
                       org-odt--translate-image-links)))
@@ -3719,6 +3720,27 @@ contextual information."
 
 ;;; Filters
 
+;;; Plain text
+
+;; Trailing newlines appear as spaces in ODT.
+;; We do not want that in, for example, in paragraphs where
+;; end of paragraph is already signaled by ODT tag.
+(defun org-odt--strip-trailing-newlines (data _backend info)
+  "Strip trailing newlines in DATA at the end of contents.
+INFO is the communication channel."
+  (org-element-map data org-element-all-elements
+    (lambda (el)
+      (when-let* ((last-child (car (last (org-element-contents el)))))
+        (when (and (org-element-type-p last-child 'plain-text)
+                   (string-suffix-p "\n" last-child))
+          (when (length> last-child 1)
+            (org-element-insert-before
+             (substring last-child 0 (1- (length last-child)))
+             last-child))
+          (org-element-extract last-child))))
+    info nil nil t)
+  data)
+
 ;;; Images
 
 (defun org-odt--translate-image-links (data _backend info)

Reply via email to