From mboxrd@z Thu Jan 1 00:00:00 1970 From: Rasmus Subject: Re: [bug, patch, ox] INCLUDE and footnotes Date: Sat, 13 Dec 2014 22:45:24 +0100 Message-ID: <87388j9qbv.fsf@gmx.us> References: <87h9x5hwso.fsf@gmx.us> <87oarcbppe.fsf@nicolasgoaziou.fr> <87fvcozfhf.fsf@gmx.us> <87h9x4bj33.fsf@nicolasgoaziou.fr> <87iohks4ne.fsf@gmx.us> <87d27rbvio.fsf@nicolasgoaziou.fr> <87bnnbhg2x.fsf@gmx.us> <878uifbjc7.fsf@nicolasgoaziou.fr> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:51777) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XzuVU-0002uk-0G for emacs-orgmode@gnu.org; Sat, 13 Dec 2014 16:45:48 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XzuVP-00082S-36 for emacs-orgmode@gnu.org; Sat, 13 Dec 2014 16:45:43 -0500 Received: from mout.gmx.net ([212.227.15.19]:52200) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XzuVO-00082O-O3 for emacs-orgmode@gnu.org; Sat, 13 Dec 2014 16:45:39 -0500 Received: from x200s ([37.133.201.102]) by mail.gmx.com (mrgmx002) with ESMTPSA (Nemesis) id 0M2Ldk-1Xh9AT1MYu-00sAJl for ; Sat, 13 Dec 2014 22:45:37 +0100 In-Reply-To: <878uifbjc7.fsf@nicolasgoaziou.fr> (Nicolas Goaziou's message of "Wed, 10 Dec 2014 16:44:24 +0100") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hi, Attached is a patch that enables footnotes in INCLUDEd documents when using :lines and friends. It stores the footnotes in a hash-table initialized in `org-export-expand-include-keyword' and updated via `org-export--prepare-file-contents'. The footnotes are then inserted when all include keywords are expanded. At the moment only footnotes from INCLUDEs with :lines-like arguments will be picket up here. But I think it might be nice to also use this functionality with footnotes when whole documents are included, and not include the footnote section directly from these documents. Though I expect the to be accused of worm-nurturing, do consider this curious exampl= e: $> cat t{1,0*}.org #+TITLE: This is t1.org=20 #+INCLUDE: "/tmp/t00.org" #+INCLUDE: "/tmp/t01.org" # This is t00.org Footnotes[fn:1], [fn:test] and [fn:inline:anonymous footnote]. * Footnotes [fn:1] Footnote 1 [fn:test] Footnote "test" =20=20=20=20=20 # t01.org Footnotes[fn:1], [fn:test] and [fn:inline:anonymous footnote]. * Footnotes [fn:1] Footnote 1 [fn:test] Footnote "test" ox will in fact interpret t1.org as: #+TITLE: This is t1.org Footnotes[fn:1-1], [fn:1-test] and [fn:1-inline:anonymous footnote]. [fn:1-1] Footnote 1 [fn:1-test] Footnote "test" Footnotes[fn:2-1], [fn:2-test] and [fn:2-inline:anonymous footnote]. So I see three approaches: 1. let the user shoot himself in the foot 2. fix the "bug" (IMO) that is that #+INCLUDE: "/tmp/t00.org" #+INCLUDE: "/tmp/t01.org" Is "read" as #+INCLUDE: "/tmp/t00.org" :minlevel N #+INCLUDE: "/tmp/t01.org" :minlevel N+1 The easiest way I can think of would be to do a pre-scan of the buffer to see if there exists any instances where include is only separated by whitespace in which case they should have the same level. 3. Fix the particular nastiness above by removing footnote sections and reinserting them using the mechanism of this patch. =E2=80=94Rasmus --=20 A page of history is worth a volume of logic --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-ox.el-Fix-footnote-bug-in-INCLUDE-keyword.patch >From 04726b6e5915fc47f3ecc261f9c7d9dfb2b44f56 Mon Sep 17 00:00:00 2001 From: rasmus Date: Tue, 9 Dec 2014 12:40:52 +0100 Subject: [PATCH] ox.el: Fix footnote-bug in #+INCLUDE-keyword * ox.el (org-export--prepare-file-contents): Preserve footnotes when using the LINES argument. New optional argument FOOTNOTES. (org-export-expand-include-keyword): New optional argument FOOTNOTES. * test-ox.el: Add test for INCLUDE with :lines and footnotes. * ORG-NEWS: Update. --- etc/ORG-NEWS | 3 +- lisp/ox.el | 94 +++++++++++++++++++++++++++++++++++-------------- testing/lisp/test-ox.el | 25 +++++++++++++ 3 files changed, 95 insertions(+), 27 deletions(-) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index f719886..f831c60 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -103,7 +103,8 @@ The content of the included file can now be optionally marked up, for instance as HTML. See the documentation for details. *** File links with =#+INCLUDE= keyword Objects can be extracted via =#+INCLUDE= using file links. It is -possible to include only the contents of the object. See manual for +possible to include only the contents of the object. Further, +footnotes are now supported when using =#+INCLUDE=. See manual for more information. *** Additional =:hline= processing to ob-shell If the argument =:hlines yes= is present in a babel call, an optional diff --git a/lisp/ox.el b/lisp/ox.el index 9d9e794..975f178 100644 --- a/lisp/ox.el +++ b/lisp/ox.el @@ -3052,16 +3052,18 @@ locally for the subtree through node properties." (car key) (if (org-string-nw-p val) (format " %s" val) "")))))))) -(defun org-export-expand-include-keyword (&optional included dir) +(defun org-export-expand-include-keyword (&optional included dir footnotes) "Expand every include keyword in buffer. Optional argument INCLUDED is a list of included file names along with their line restriction, when appropriate. It is used to avoid infinite recursion. Optional argument DIR is the current working directory. It is used to properly resolve relative -paths." +paths. Optional argument FOOTNOTES is a hash-table used for +storing and resolving footnotes. It is created automatically." (let ((case-fold-search t) (file-prefix (make-hash-table :test #'equal)) - (current-prefix 0)) + (current-prefix 0) + (footnotes (or footnotes (make-hash-table :test 'equal)))) (goto-char (point-min)) (while (re-search-forward "^[ \t]*#\\+INCLUDE:" nil t) (let ((element (save-match-data (org-element-at-point)))) @@ -3155,14 +3157,27 @@ paths." file location only-contents lines) lines))) (org-mode) - (insert - (org-export--prepare-file-contents - file lines ind minlevel - (or (gethash file file-prefix) - (puthash file (incf current-prefix) file-prefix))))) + (insert (org-export--prepare-file-contents + file lines ind minlevel + (or (gethash file file-prefix) + (puthash file (incf current-prefix) file-prefix)) + footnotes))) (org-export-expand-include-keyword (cons (list file lines) included) - (file-name-directory file)) + (file-name-directory file) + footnotes) + (goto-char (point-min)) + (while (and (search-forward-regexp org-footnote-re nil t)) + (let* ((reference (org-element-context)) + (type (org-element-type reference)) + (label (org-element-property :label reference))) + (when (and label (eq type 'footnote-reference)) + (unless (org-footnote-get-definition label) + (save-excursion + (org-footnote-create-definition label) + ;; We do not need an error here since ox + ;; will complain if a footnote is missing. + (insert (or (gethash label footnotes) ""))))))) (buffer-string))))))))))))) (defun org-export--inclusion-absolute-lines (file location only-contents lines) @@ -3227,8 +3242,8 @@ Return a string of lines to be included in the format expected by (while (< (point) end) (incf counter) (forward-line)) counter)))))))) -(defun org-export--prepare-file-contents (file &optional lines ind minlevel id) - "Prepare the contents of FILE for inclusion and return them as a string. +(defun org-export--prepare-file-contents (file &optional lines ind minlevel id footnotes) + "Prepare contents of FILE for inclusion and return it as a string. When optional argument LINES is a string specifying a range of lines, include only those lines. @@ -3246,7 +3261,11 @@ file should have. Optional argument ID is an integer that will be inserted before each footnote definition and reference if FILE is an Org file. This is useful to avoid conflicts when more than one Org file -with footnotes is included in a document." +with footnotes is included in a document. + +Optional argument FOOTNOTES is a hash-table to store footnotes in +the included document. +" (with-temp-buffer (insert-file-contents file) (when lines @@ -3308,20 +3327,43 @@ with footnotes is included in a document." ;; Append ID to all footnote references and definitions, so they ;; become file specific and cannot collide with footnotes in other ;; included files. - (when id - (goto-char (point-min)) - (while (re-search-forward org-footnote-re nil t) - (let ((reference (org-element-context))) - (when (memq (org-element-type reference) - '(footnote-reference footnote-definition)) - (goto-char (org-element-property :begin reference)) - (forward-char) - (let ((label (org-element-property :label reference))) - (cond ((not label)) - ((org-string-match-p "\\`[0-9]+\\'" label) - (insert (format "fn:%d-" id))) - (t (forward-char 3) (insert (format "%d-" id))))))))) - (org-element-normalize-string (buffer-string)))) + (let* ((include-footnotes (and id lines))) + (when id + (unless (eq major-mode 'org-mode) + (let ((org-inhibit-startup t)) (org-mode))) + (goto-char (point-min)) + (while (re-search-forward org-footnote-re nil t) + (let* ((reference (org-element-context)) + (type (org-element-type reference)) + (footnote-type (org-element-property :type reference)) + (label (org-element-property :label reference))) + (when (and (eq type 'footnote-reference)) + (goto-char (org-element-property :begin reference)) + (when label + (goto-char (org-element-property :begin reference)) + (forward-char 4) + (insert (format "%d-" id)) + (and (not (eq footnote-type 'inline)) + (let ((new-label (org-element-property + :label (org-element-context)))) + (save-restriction + (save-excursion + (widen) + (org-footnote-goto-definition label) + (let ((definition (org-element-context))) + (and include-footnotes + (puthash new-label + (org-element-normalize-string + (buffer-substring + (org-element-property + :contents-begin definition) + (org-element-property + :contents-end definition))) + footnotes)) + (goto-char (org-element-property :begin definition)) + (forward-char 4) + (insert (format "%d-" id)))))))))))) + (org-element-normalize-string (buffer-string))))) (defun org-export-execute-babel-code () "Execute every Babel code in the visible part of current buffer." diff --git a/testing/lisp/test-ox.el b/testing/lisp/test-ox.el index 9a0e787..da4850d 100644 --- a/testing/lisp/test-ox.el +++ b/testing/lisp/test-ox.el @@ -919,6 +919,31 @@ Footnotes[fn:1], [fn:test] and [fn:inline:anonymous footnote]. (org-element-map (org-element-parse-buffer) 'footnote-reference (lambda (ref) (org-element-property :label ref))))))))))) + ;; Footnotes are supported by :lines-like elements and unnecessary + ;; footnotes are dropped. + (should + (= 3 + (length + (delete-dups + (let ((contents " +* foo +Footnotes[fn:1] +* bar +Footnotes[fn:2], [fn:test], and [fn:inline:anonymous footnote] + +[fn:1] Footnote 1 +[fn:2] Footnote 1 +* Footnotes +[fn:test] Footnote \"test\"")) + (org-test-with-temp-text-in-file contents + (let ((file (buffer-file-name))) + (org-test-with-temp-text + (format "#+INCLUDE: \"%s::*bar\" +" file) + (org-export-expand-include-keyword) + (org-element-map (org-element-parse-buffer) + 'footnote-definition + (lambda (ref) (org-element-property :label ref))))))))))) ;; If only-contents is non-nil only include contents of element. (should (equal -- 2.1.3 --=-=-=--