org-element: Fix latex environment parsing

* contrib/lisp/org-element.el (org-element-latex-environment-parser):
  Grab value until closing of main environment block.
(org-element-current-element): Check if main block is properly closed.
* testing/lisp/test-org-element.el: Add test.
This commit is contained in:
Nicolas Goaziou 2012-05-10 22:55:12 +02:00
parent 294589270e
commit d308ea3172
2 changed files with 33 additions and 9 deletions

View File

@ -1265,20 +1265,23 @@ keywords.
Assume point is at the beginning of the latex environment."
(save-excursion
(let* ((case-fold-search t)
(contents-begin (point))
(code-begin (point))
(keywords (org-element-collect-affiliated-keywords))
(begin (car keywords))
(contents-end (progn (re-search-forward "^[ \t]*\\\\end")
(env (progn (looking-at "^[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
(regexp-quote (match-string 1))))
(code-end
(progn (re-search-forward (format "^[ \t]*\\\\end{%s}" env))
(forward-line)
(point)))
(value (buffer-substring-no-properties contents-begin contents-end))
(value (buffer-substring-no-properties code-begin code-end))
(end (progn (org-skip-whitespace)
(if (eobp) (point) (point-at-bol)))))
`(latex-environment
(:begin ,begin
:end ,end
:value ,value
:post-blank ,(count-lines contents-end end)
:post-blank ,(count-lines code-end end)
,@(cadr keywords))))))
(defun org-element-latex-environment-interpreter (latex-environment contents)
@ -3076,9 +3079,12 @@ element it has to parse."
;; Inlinetask.
((org-at-heading-p) (org-element-inlinetask-parser raw-secondary-p))
;; LaTeX Environment.
((looking-at "[ \t]*\\\\begin{")
((looking-at "[ \t]*\\\\begin{\\([A-Za-z0-9*]+\\)}")
(if (save-excursion
(re-search-forward "[ \t]*\\\\end{[^}]*}[ \t]*" nil t))
(re-search-forward
(format "[ \t]*\\\\end{%s}[ \t]*"
(regexp-quote (match-string 1)))
nil t))
(org-element-latex-environment-parser)
(org-element-paragraph-parser)))
;; Drawer and Property Drawer.

View File

@ -841,7 +841,25 @@ Paragraph"
(should
(org-test-with-temp-text "\\begin{equation}\ne^{i\\pi}+1=0\n\\end{equation}"
(org-element-map
(org-element-parse-buffer) 'latex-environment 'identity))))
(org-element-parse-buffer) 'latex-environment 'identity)))
;; Allow nested environments.
(should
(equal
"\\begin{outer}
\\begin{inner}
e^{i\\pi}+1=0
\\end{inner}
\\end{outer}"
(org-test-with-temp-text "
\\begin{outer}
\\begin{inner}
e^{i\\pi}+1=0
\\end{inner}
\\end{outer}"
(org-element-property
:value
(org-element-map
(org-element-parse-buffer) 'latex-environment 'identity nil t))))))
;;;; Latex Fragment