org-element: Store value of example-blocks and src-blocks unescaped

* lisp/org-element.el (org-element-example-block-parser,
  org-element-src-block-parser): Store value of example-blocks and
  src-blocks unescaped.
(org-element-example-block-interpreter,
org-element-src-block-interpreter): Escape value again when storing
it.
* contrib/lisp/org-export.el (org-export-unravel-code): Don't clean
  commas from code since org-element already took care of it.
* testing/lisp/test-org-export.el: Update test.
* testing/lisp/test-org-element.el: Add tests.
This commit is contained in:
Nicolas Goaziou 2012-09-30 17:45:21 +02:00
parent fac86b03fe
commit 3bd22fb045
4 changed files with 40 additions and 32 deletions

View File

@ -3516,22 +3516,17 @@ relative line number (integer) and name of code reference on that
line (string)."
(let* ((line 0) refs
;; Get code and clean it. Remove blank lines at its
;; beginning and end. Also remove protective commas.
;; beginning and end.
(code (let ((c (replace-regexp-in-string
"\\`\\([ \t]*\n\\)+" ""
(replace-regexp-in-string
"\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n"
(org-element-property :value element)))))
;; If appropriate, remove global indentation.
(unless (or org-src-preserve-indentation
(org-element-property :preserve-indent element))
(setq c (org-remove-indentation c)))
;; Free up the protected lines. Note: Org blocks
;; have commas at the beginning or every line.
(if (string= (org-element-property :language element) "org")
(replace-regexp-in-string "^," "" c)
(replace-regexp-in-string
"^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
(if (or org-src-preserve-indentation
(org-element-property :preserve-indent element))
c
(org-remove-indentation c))))
;; Get format used for references.
(label-fmt (regexp-quote
(or (org-element-property :label-fmt element)

View File

@ -1504,7 +1504,9 @@ containing `:begin', `:end', `:number-lines', `:preserve-indent',
(begin (car keywords))
(contents-begin (progn (forward-line) (point)))
(hidden (org-invisible-p2))
(value (buffer-substring-no-properties contents-begin contents-end))
(value (org-unescape-code-in-string
(buffer-substring-no-properties
contents-begin contents-end)))
(pos-before-blank (progn (goto-char contents-end)
(forward-line)
(point)))
@ -1531,7 +1533,8 @@ CONTENTS is nil."
(let ((switches (org-element-property :switches example-block)))
(concat "#+BEGIN_EXAMPLE" (and switches (concat " " switches)) "\n"
(org-remove-indentation
(org-element-property :value example-block))
(org-escape-code-in-string
(org-element-property :value example-block)))
"#+END_EXAMPLE")))
@ -2032,7 +2035,8 @@ Assume point is at the beginning of the block."
;; Get visibility status.
(hidden (progn (forward-line) (org-invisible-p2)))
;; Retrieve code.
(value (buffer-substring-no-properties (point) contents-end))
(value (org-unescape-code-in-string
(buffer-substring-no-properties (point) contents-end)))
(pos-before-blank (progn (goto-char contents-end)
(forward-line)
(point)))
@ -2080,7 +2084,7 @@ CONTENTS is nil."
(concat (and lang (concat " " lang))
(and switches (concat " " switches))
(and params (concat " " params))))
value
(org-escape-code-in-string value)
"#+END_SRC")))

View File

@ -465,7 +465,13 @@ CLOCK: [2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02] => 0:01"
(should-not
(org-test-with-temp-text "#+BEGIN_EXAMPLE"
(org-element-map
(org-element-parse-buffer) 'example-block 'identity nil t))))
(org-element-parse-buffer) 'example-block 'identity nil t)))
;; Properly un-escape code.
(should
(equal "* Headline\n #+keyword\nText\n"
(org-test-with-temp-text
"#+BEGIN_EXAMPLE\n,* Headline\n ,#+keyword\nText\n#+END_EXAMPLE"
(org-element-property :value (org-element-at-point))))))
(ert-deftest test-org-element/block-switches ()
"Test `example-block' and `src-block' switches parsing."
@ -1450,7 +1456,13 @@ Outside list"
;; Ignore incomplete block.
(should-not
(org-test-with-temp-text "#+BEGIN_SRC"
(org-element-map (org-element-parse-buffer) 'src-block 'identity))))
(org-element-map (org-element-parse-buffer) 'src-block 'identity)))
;; Properly un-escape code.
(should
(equal "* Headline\n #+keyword\nText\n"
(org-test-with-temp-text
"#+BEGIN_SRC org\n,* Headline\n ,#+keyword\nText\n#+END_SRC"
(org-element-property :value (org-element-at-point))))))
;;;; Statistics Cookie
@ -1911,7 +1923,12 @@ CLOCK: [2012-01-01 sun. 00:01]--[2012-01-01 sun. 00:02] => 0:01"))
(should
(equal (org-test-parse-and-interpret
"#+BEGIN_EXAMPLE -n -k\n(+ 1 1)\n#+END_EXAMPLE")
"#+BEGIN_EXAMPLE -n -k\n(+ 1 1)\n#+END_EXAMPLE\n")))
"#+BEGIN_EXAMPLE -n -k\n(+ 1 1)\n#+END_EXAMPLE\n"))
;; Preserve code escaping.
(should
(equal (org-test-parse-and-interpret
"#+BEGIN_EXAMPLE\n,* Headline\n ,#+keyword\nText #+END_EXAMPLE")
"#+BEGIN_EXAMPLE\n,* Headline\n ,#+keyword\nText #+END_EXAMPLE\n")))
(ert-deftest test-org-element/export-block-interpreter ()
"Test export block interpreter."
@ -1975,7 +1992,12 @@ CLOSED: [2012-01-01] DEADLINE: <2012-01-01> SCHEDULED: <2012-01-01>\n"))))
(equal (let ((org-edit-src-content-indentation 2))
(org-test-parse-and-interpret
"#+BEGIN_SRC emacs-lisp -n -k\n(+ 1 1)\n#+END_SRC"))
"#+BEGIN_SRC emacs-lisp -n -k\n (+ 1 1)\n#+END_SRC\n")))
"#+BEGIN_SRC emacs-lisp -n -k\n (+ 1 1)\n#+END_SRC\n"))
;; Preserve code escaping.
(should
(equal (org-test-parse-and-interpret
"#+BEGIN_SRC org\n,* Headline\n ,#+keyword\nText #+END_SRC")
"#+BEGIN_SRC org\n,* Headline\n ,#+keyword\nText #+END_SRC\n")))
(ert-deftest test-org-element/table-interpreter ()
"Test table, table-row and table-cell interpreters."

View File

@ -1068,20 +1068,7 @@ Another text. (ref:text)
#+END_EXAMPLE"
(goto-line 5)
(should (equal (org-export-unravel-code (org-element-at-point))
'("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))
;; 5. Free up comma-protected lines.
;;
;; 5.1. In an Org source block, every line is protected.
(org-test-with-temp-text
"#+BEGIN_SRC org\n,* Test\n,# comment\n,Text\n#+END_SRC"
(should (equal (org-export-unravel-code (org-element-at-point))
'("* Test\n# comment\nText\n"))))
;; 5.2. In other blocks, only headlines, comments and keywords are
;; protected.
(org-test-with-temp-text
"#+BEGIN_EXAMPLE\n,* Headline\n, * Not headline\n,Keep\n#+END_EXAMPLE"
(should (equal (org-export-unravel-code (org-element-at-point))
'("* Headline\n, * Not headline\n,Keep\n"))))))
'("(+ 2 2)\n(+ 3 3)\n" (2 . "one")))))))