ox: Do not check footnote validity when including files

* lisp/ox.el (org-export--update-footnote-label): Remove function.
(org-export--prepare-file-contents): Do not error when a footnote
definition cannot be found.

In particular, it should not throw an error when a footnote is not
defined outside of export scope.

Reported-by: Leonard Randall <leonard.a.randall@gmail.com>
<http://permalink.gmane.org/gmane.emacs.orgmode/100924>
This commit is contained in:
Nicolas Goaziou 2015-09-07 19:09:17 +02:00
parent 366dc4e369
commit a6514e995b
1 changed files with 38 additions and 52 deletions

View File

@ -3343,24 +3343,6 @@ Return a string of lines to be included in the format expected by
(while (< (point) end) (incf counter) (forward-line)) (while (< (point) end) (incf counter) (forward-line))
counter)))))))) counter))))))))
(defun org-export--update-footnote-label (ref-begin digit-label id)
"Prefix footnote-label at point REF-BEGIN in buffer with ID.
REF-BEGIN corresponds to the property `:begin' of objects of type
footnote-definition and footnote-reference.
If DIGIT-LABEL is non-nil the label is assumed to be of the form
\[N] where N is one or more numbers.
Return the new label."
(goto-char (1+ ref-begin))
(buffer-substring (point)
(progn
(if digit-label (insert (format "fn:--%d-" id))
(forward-char 3)
(insert (format "-%d-" id)))
(1- (search-forward "]")))))
(defun org-export--prepare-file-contents (defun org-export--prepare-file-contents
(file &optional lines ind minlevel id footnotes) (file &optional lines ind minlevel id footnotes)
"Prepare contents of FILE for inclusion and return it as a string. "Prepare contents of FILE for inclusion and return it as a string.
@ -3446,51 +3428,55 @@ the included document."
(insert (make-string offset ?*))))))))))) (insert (make-string offset ?*)))))))))))
;; Append ID to all footnote references and definitions, so they ;; Append ID to all footnote references and definitions, so they
;; become file specific and cannot collide with footnotes in other ;; become file specific and cannot collide with footnotes in other
;; included files. Further, collect relevant footnotes outside of ;; included files. Further, collect relevant footnote definitions
;; LINES. ;; outside of LINES, in order to reintroduce them later.
(when id (when id
(let ((marker-min (point-min-marker)) (let ((marker-min (point-min-marker))
(marker-max (point-max-marker)) (marker-max (point-max-marker))
seen) (get-new-label
(lambda (label)
;; Generate new label from LABEL. If LABEL is akin to
;; [1] convert it to [fn:--ID-1]. Otherwise add "-ID-"
;; after "fn:".
(if (org-string-match-p "\\`[0-9]+\\'" label)
(format "fn:--%d-%s" id label)
(format "fn:-%d-%s" id (substring label 3)))))
(set-new-label
(lambda (f old new)
;; Replace OLD label with NEW in footnote F.
(save-excursion
(goto-char (1+ (org-element-property :begin f)))
(looking-at (regexp-quote old))
(replace-match new))))
(seen-alist))
(goto-char (point-min)) (goto-char (point-min))
(while (re-search-forward org-footnote-re nil t) (while (re-search-forward org-footnote-re nil t)
(let ((reference (org-element-context))) (let ((footnote (save-excursion
(when (eq (org-element-type reference) 'footnote-reference) (backward-char)
(let* ((label (org-element-property :label reference)) (org-element-context))))
(digit-label (when (memq (org-element-type footnote)
(and label (org-string-match-p "\\`[0-9]+\\'" label)))) '(footnote-definition footnote-reference))
(let* ((label (org-element-property :label footnote)))
;; Update the footnote-reference at point and collect ;; Update the footnote-reference at point and collect
;; the new label, which is only used for footnotes ;; the new label, which is only used for footnotes
;; outsides LINES. ;; outsides LINES.
(when label (when label
;; If label is akin to [1] convert it to (let ((seen (cdr (assoc label seen-alist))))
;; [fn:--ID-1]. Otherwise add "-ID-" after "fn:". (if seen (funcall set-new-label footnote label seen)
(let ((new-label (org-export--update-footnote-label (let ((new (funcall get-new-label label)))
(org-element-property :begin reference) (push (cons label new) seen-alist)
digit-label id)))
(unless (or (eq (org-element-property :type reference)
'inline)
(member label seen))
(push label seen)
(org-with-wide-buffer (org-with-wide-buffer
(let* ((definition (org-footnote-get-definition label)) (let* ((def (org-footnote-get-definition label))
(beginning (nth 1 definition))) (beg (nth 1 def)))
(unless definition (when (and def
(error (or (< beg marker-min)
"Definition not found for footnote %s in file %s" (>= beg marker-max)))
label file))
(if (or (< beginning marker-min)
(>= beginning marker-max))
;; Store since footnote-definition is ;; Store since footnote-definition is
;; outside of LINES. ;; outside of LINES.
(puthash new-label (puthash new
(org-element-normalize-string (org-element-normalize-string (nth 3 def))
(nth 3 definition)) footnotes))))
footnotes) (funcall set-new-label footnote label new)))))))))
;; Update label of definition since it is
;; included directly.
(org-export--update-footnote-label
beginning digit-label id)))))))))))
(set-marker marker-min nil) (set-marker marker-min nil)
(set-marker marker-max nil))) (set-marker marker-max nil)))
(org-element-normalize-string (buffer-string)))) (org-element-normalize-string (buffer-string))))