summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Goaziou <mail@nicolasgoaziou.fr>2015-11-12 11:29:55 +0100
committerNicolas Goaziou <mail@nicolasgoaziou.fr>2015-11-12 11:29:55 +0100
commitbd921f0858f28783a33647c30ed4d3d38d94a55f (patch)
treeb8a6940375f77af0d50906204a99b3a470e36189
parentf0a48b9064ee9fd3e569149647bacfbb38de75a4 (diff)
parent53a4209003642032cdc44daa46c38a73dc8202b0 (diff)
downloadorg-mode-bd921f0858f28783a33647c30ed4d3d38d94a55f.tar.gz
Merge branch 'maint'
-rw-r--r--lisp/ox.el52
-rw-r--r--testing/lisp/test-ox.el62
2 files changed, 98 insertions, 16 deletions
diff --git a/lisp/ox.el b/lisp/ox.el
index 0a212eb..e705196 100644
--- a/lisp/ox.el
+++ b/lisp/ox.el
@@ -2669,25 +2669,45 @@ The function assumes BUFFER's major mode is `org-mode'."
"Delete commented areas in the buffer.
Commented areas are comments, comment blocks, commented trees and
inlinetasks. Trailing blank lines after a comment or a comment
-block are preserved. Narrowing, if any, is ignored."
+block are removed, as long as it doesn't alter the structure of
+the document. Narrowing, if any, is ignored."
(org-with-wide-buffer
(goto-char (point-min))
- (let ((regexp (concat org-outline-regexp-bol ".*" org-comment-string
- "\\|"
- "^[ \t]*#\\(?: \\|$\\|\\+begin_comment\\)"))
- (case-fold-search t))
+ (let* ((case-fold-search t)
+ (comment-re "^[ \t]*#\\(?: \\|$\\|\\+end_comment\\)")
+ (regexp (concat org-outline-regexp-bol ".*" org-comment-string "\\|"
+ comment-re)))
(while (re-search-forward regexp nil t)
- (let ((e (org-element-at-point)))
- (cl-case (org-element-type e)
- ((comment comment-block)
- (delete-region (org-element-property :begin e)
- (progn (goto-char (org-element-property :end e))
- (skip-chars-backward " \r\t\n")
- (line-beginning-position 2))))
- ((headline inlinetask)
- (when (org-element-property :commentedp e)
- (delete-region (org-element-property :begin e)
- (org-element-property :end e))))))))))
+ (let ((element (org-element-at-point)))
+ (pcase (org-element-type element)
+ ((or `headline `inlinetask)
+ (when (org-element-property :commentedp element)
+ (delete-region (org-element-property :begin element)
+ (org-element-property :end element))))
+ ((or `comment `comment-block)
+ (let* ((parent (org-element-property :parent element))
+ (start (org-element-property :begin element))
+ (end (org-element-property :end element))
+ ;; We remove trailing blank lines. Doing so could
+ ;; modify the structure of the document. Therefore
+ ;; we ensure that any comment between elements is
+ ;; replaced with one empty line, so as to keep them
+ ;; separated.
+ (add-blank?
+ (save-excursion
+ (goto-char start)
+ (not (or (bobp)
+ (eq (org-element-property :contents-begin parent)
+ start)
+ (eq (org-element-property :contents-end parent)
+ end)
+ (progn
+ (forward-line -1)
+ (or (org-looking-at-p "^[ \t]*$")
+ (org-with-limited-levels
+ (org-at-heading-p)))))))))
+ (delete-region start end)
+ (when add-blank? (insert "\n"))))))))))
(defun org-export--prune-tree (data info)
"Prune non exportable elements from DATA.
diff --git a/testing/lisp/test-ox.el b/testing/lisp/test-ox.el
index e48979b..5e8259f 100644
--- a/testing/lisp/test-ox.el
+++ b/testing/lisp/test-ox.el
@@ -1666,6 +1666,68 @@ Footnotes[fn:2], foot[fn:test], digit only[3], and [fn:inline:anonymous footnote
+;;; Comments
+
+(ert-deftest test-org-export/comments ()
+ "Test comments handling during export.
+In particular, structure of the document mustn't be altered after
+comments removal."
+ (should
+ (equal (org-test-with-temp-text "
+Para1
+# Comment
+
+# Comment
+Para2"
+ (org-export-as (org-test-default-backend)))
+ "Para1\n\nPara2\n"))
+ (should
+ (equal (org-test-with-temp-text "
+Para1
+# Comment
+Para2"
+ (org-export-as (org-test-default-backend)))
+ "Para1\n\nPara2\n"))
+ (should
+ (equal (org-test-with-temp-text "
+\[fn:1] Para1
+# Inside definition
+
+
+# Outside definition
+Para2"
+ (org-export-as (org-test-default-backend)))
+ "[fn:1] Para1\n\n\nPara2\n"))
+ (should
+ (equal (org-test-with-temp-text "
+\[fn:1] Para1
+
+# Inside definition
+
+# Inside definition
+
+Para2"
+ (org-export-as (org-test-default-backend)))
+ "[fn:1] Para1\n\nPara2\n"))
+ (should
+ (equal (org-test-with-temp-text "
+\[fn:1] Para1
+# Inside definition
+
+Para2"
+ (org-export-as (org-test-default-backend)))
+ "[fn:1] Para1\n\nPara2\n"))
+ (should
+ (equal (org-test-with-temp-text "
+\[fn:1] Para1
+
+# Inside definition
+Para2"
+ (org-export-as (org-test-default-backend)))
+ "[fn:1] Para1\n\nPara2\n")))
+
+
+
;;; Export Snippets
(ert-deftest test-org-export/export-snippet ()