org-element: Fix footnote definition parser

* lisp/org-element.el (org-element-footnote-definition-parser):
* testing/lisp/test-org-element.el (test-org-element/footnote-definition-parser):
  Add tests.
This commit is contained in:
Nicolas Goaziou 2017-06-07 23:45:17 +02:00
parent 1c71172c54
commit 84cfa58d4a
2 changed files with 35 additions and 27 deletions

View file

@ -879,14 +879,14 @@ Assume point is at the beginning of the footnote definition."
(match-string-no-properties 1))) (match-string-no-properties 1)))
(begin (car affiliated)) (begin (car affiliated))
(post-affiliated (point)) (post-affiliated (point))
(ending (end
(save-excursion (save-excursion
(end-of-line) (end-of-line)
(cond (cond
((not ((not
(re-search-forward org-element--footnote-separator limit t)) (re-search-forward org-element--footnote-separator limit t))
limit) limit)
((eq (char-after (match-beginning 0)) ?\[) ((eq ?\[ (char-after (match-beginning 0)))
;; At a new footnote definition, make sure we end ;; At a new footnote definition, make sure we end
;; before any affiliated keyword above. ;; before any affiliated keyword above.
(forward-line -1) (forward-line -1)
@ -894,26 +894,27 @@ Assume point is at the beginning of the footnote definition."
(looking-at-p org-element--affiliated-re)) (looking-at-p org-element--affiliated-re))
(forward-line -1)) (forward-line -1))
(line-beginning-position 2)) (line-beginning-position 2))
(t (match-beginning 0))))) ((eq ?* (char-after (match-beginning 0))) (match-beginning 0))
(t (skip-chars-forward " \r\t\n" limit)
(if (= limit (point)) limit (line-beginning-position))))))
(contents-begin (contents-begin
(progn (progn (search-forward "]")
(search-forward "]") (skip-chars-forward " \r\t\n" end)
(skip-chars-forward " \r\t\n" ending) (cond ((= (point) end) nil)
(cond ((= (point) ending) nil) ((= (line-beginning-position) post-affiliated) (point))
((= (line-beginning-position) post-affiliated) (point)) (t (line-beginning-position)))))
(t (line-beginning-position))))) (contents-end
(contents-end (and contents-begin ending)) (progn (goto-char end)
(end (progn (goto-char ending) (skip-chars-backward " \r\t\n")
(skip-chars-forward " \r\t\n" limit) (line-beginning-position 2))))
(if (eobp) (point) (line-beginning-position)))))
(list 'footnote-definition (list 'footnote-definition
(nconc (nconc
(list :label label (list :label label
:begin begin :begin begin
:end end :end end
:contents-begin contents-begin :contents-begin contents-begin
:contents-end contents-end :contents-end (and contents-begin contents-end)
:post-blank (count-lines ending end) :post-blank (count-lines contents-end end)
:post-affiliated post-affiliated) :post-affiliated post-affiliated)
(cdr affiliated)))))) (cdr affiliated))))))

View file

@ -920,20 +920,21 @@ Some other text
"Test `footnote-definition' parser." "Test `footnote-definition' parser."
(should (should
(org-test-with-temp-text "[fn:1] Definition" (org-test-with-temp-text "[fn:1] Definition"
(org-element-map (org-element-parse-buffer) 'footnote-definition (eq (org-element-type (org-element-at-point)) 'footnote-definition)))
'identity nil t))) ;; Footnote with more contents.
;; Footnote with more contents
(should (should
(= 29 (= 29 (org-test-with-temp-text "[fn:1] Definition\n\n| a | b |"
(org-element-property (org-element-property :end (org-element-at-point)))))
:end ;; Test difference between :contents-end and :end property
(org-test-with-temp-text "[fn:1] Definition\n\n| a | b |" (should
(org-element-map (org-element-parse-buffer) 'footnote-definition (< (org-test-with-temp-text "[fn:1] Definition\n\n\n"
'identity nil t))))) (org-element-property :contents-end (org-element-at-point)))
(org-test-with-temp-text "[fn:1] Definition\n\n\n"
(org-element-property :end (org-element-at-point)))))
;; Footnote starting with special syntax. ;; Footnote starting with special syntax.
(should-not (should-not
(org-test-with-temp-text "[fn:1] - no item" (org-test-with-temp-text "[fn:1] <point>- no item"
(org-element-map (org-element-parse-buffer) 'item 'identity))) (eq (org-element-type (org-element-at-point)) 'item)))
;; Correctly handle footnote starting with an empty line. ;; Correctly handle footnote starting with an empty line.
(should (should
(= 9 (= 9
@ -953,7 +954,13 @@ Some other text
(should (should
(org-test-with-temp-text "[fn:1] 1\n\n#+attr_latex: :offset 0in\n[fn:2] 2" (org-test-with-temp-text "[fn:1] 1\n\n#+attr_latex: :offset 0in\n[fn:2] 2"
(goto-char (org-element-property :end (org-element-at-point))) (goto-char (org-element-property :end (org-element-at-point)))
(looking-at "#")))) (looking-at "#")))
;; An empty footnote has no contents.
(should-not
(org-test-with-temp-text "[fn:1]\n\n"
(let ((footnote (org-element-at-point)))
(or (org-element-property :contents-begin footnote)
(org-element-property :contents-end footnote))))))
;;;; Footnotes Reference. ;;;; Footnotes Reference.