emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] [bug, patch, ox] INCLUDE and footnotes


From: Rasmus
Subject: Re: [O] [bug, patch, ox] INCLUDE and footnotes
Date: Sun, 21 Dec 2014 23:39:12 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux)

Hi,

Thanks for the comments.  I've attached a new version.

Nicolas Goaziou <address@hidden> writes:

>> The recognition regexp is still not great, but the idea of the regexp
>> is to only act on includes where there's no :minlevel already and no
>> plain words (most obviously src and example, but any block really)
>> when disregarding ":key value" pairs.
>
> This is not necessary. Even if :minlevel is used on these include
> keywords, its value is ignored when inserting contents of the file.

It's not neural to the org export buffer, though it's probably neutral to
the *exported* document.

E.g.

(org-test-with-temp-text
        (format "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp" 
org-test-dir)
      (org-export-expand-include-keyword)
      (buffer-string))
      
=>

"#+BEGIN_src emacs-lisp :minlevel 1
Success!
#+END_src
"

Which is turn is passed to babel (which also seems to ignoring it).  I
cannot easily condition on src (without using the previous complex regexp)
since the file "~/my src folder/FILE" is not totally unlikely...

If you are happy with the alteration, which might only be manifested in
slightly uglier tests then we can go with the simpler solution.

Note, for blocks the minlevel is not inserted:

(org-test-with-temp-text
        (format "#+INCLUDE: \"%s/examples/include2.org\" foo" org-test-dir)
      (org-export-expand-include-keyword)
      (buffer-string))
      
=> 

"#+BEGIN_foo
Success!
#+END_foo
"

>> +      (while (re-search-forward
>> +          (concat include-re "[ \t]*" "\\(?:\".+?\"\\|[^ \t]+\\)[ \t]*" 
>> "\\(.*\\)$")
>> +          nil t)
>
>   (re-search-forward include-re nil t)
>
> is enough.

The regexp was motivated by the above concerns.

>> +        (insert (format " :minlevel %d"
>> +                        (1+ (org-with-wide-buffer
>> +                             (if (search-backward-regexp org-heading-regexp 
>> nil t)
>> +                                 (length (match-string 1))
>> +                               0)))))))))
>   (insert (format " :minlevel %d" (1+ (org-outline-level))))

But this if the buffer is narrowed this would not work "correctly" in
getting the "right" level.  But it probably does not matter for export.

See this test for my initial reasoning:

  (should
   (org-test-with-temp-text
       (format "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\"" 
org-test-dir)
     (org-narrow-to-element)
     (org-export-expand-include-keyword)
     (goto-char (point-min))
     (eq 2 (org-element-property :level (org-element-at-point)))))

This is changed now.

—Rasmus

-- 
Enough with the bla bla!
>From b7112471b3e4b5334d98caf528e1e687232dee2f Mon Sep 17 00:00:00 2001
From: Rasmus <address@hidden>
Date: Thu, 18 Dec 2014 16:48:49 +0100
Subject: [PATCH 2/2] ox.el: Guess the :minlevel for INCLUDE-keywords

* ox.el (org-export-expand-include-keyword): Guess :minlevel if
missing and relevant.
* test-ox.el (org-export-expand-include-keyword): Tests for automatic :minlevel.
---
 lisp/ox.el              |  6 ++++++
 testing/lisp/test-ox.el | 41 +++++++++++++++++++++++++++++++++++++++--
 2 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/lisp/ox.el b/lisp/ox.el
index 99c4e9b..b65cea0 100644
--- a/lisp/ox.el
+++ b/lisp/ox.el
@@ -3065,8 +3065,14 @@ storing and resolving footnotes.  It is created 
automatically."
        (current-prefix 0)
        (footnotes (or footnotes (make-hash-table :test #'equal)))
        (include-re "^[ \t]*#\\+INCLUDE:"))
+    ;; Add :minlevel to all include words that no explicitly have one.
     (goto-char (point-min))
+    (while (re-search-forward include-re nil t)
+      (unless (search-forward-regexp "[ \t]+:minlevel\\>" (line-end-position) 
t)
+       (end-of-line)
+       (insert (format " :minlevel %d" (1+ (org-outline-level))))))
     ;; Expand INCLUDE keywords.
+    (goto-char (point-min))
     (while (re-search-forward include-re nil t)
       (let ((element (save-match-data (org-element-at-point))))
        (when (eq (org-element-type element) 'keyword)
diff --git a/testing/lisp/test-ox.el b/testing/lisp/test-ox.el
index 140c0a8..794de1f 100644
--- a/testing/lisp/test-ox.el
+++ b/testing/lisp/test-ox.el
@@ -854,7 +854,7 @@ text
   ;; Inclusion within a src-block.
   (should
    (equal
-    "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n"
+    "#+BEGIN_SRC emacs-lisp :minlevel 1\n(+ 2 1)\n#+END_SRC\n"
     (org-test-with-temp-text
      (format
       "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" SRC emacs-lisp"
@@ -1045,7 +1045,44 @@ baz
     (org-test-with-temp-text
        (format "#+INCLUDE: \"%s/examples/include.org::#dh\" :only-contents t" 
org-test-dir)
       (org-export-expand-include-keyword)
-      (buffer-string)))))
+      (buffer-string))))
+  ;; Adjacent INCLUDE-keywords should have the same :minlevel if unspecified.
+  (should
+   (org-every (lambda (level) (zerop (1- level)))
+             (org-test-with-temp-text
+                 (concat
+                  (format "#+INCLUDE: \"%s/examples/include.org::#ah\"\n" 
org-test-dir)
+                  (format "#+INCLUDE: \"%s/examples/include.org::*Heading\"" 
org-test-dir))
+               (org-export-expand-include-keyword)
+               (org-element-map (org-element-parse-buffer) 'headline
+                 (lambda (head) (org-element-property :level head))))))
+  ;; INCLUDE is automatically inserted for src elements.
+  (should
+   (equal
+    (org-test-with-temp-text
+       (format "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp" 
org-test-dir)
+      (org-export-expand-include-keyword)
+      (buffer-string))
+    (org-test-with-temp-text
+       (format "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp 
:minlevel 1" org-test-dir)
+      (org-export-expand-include-keyword)
+      (buffer-string))))
+  ;; INCLUDE assigns the relative :minlevel conditional on narrowing.
+  (should
+   (org-test-with-temp-text
+       (format "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\"" 
org-test-dir)
+     (org-narrow-to-element)
+     (org-export-expand-include-keyword)
+     (goto-char (point-min))
+     (eq 1 (org-element-property :level (org-element-at-point)))))
+  ;; If :minlevel is present do not alter it.
+  (should
+   (org-test-with-temp-text
+       (format "* h1\n<point>#+INCLUDE: \"%s/examples/include.org::#ah\" 
:minlevel 1" org-test-dir)
+     (org-narrow-to-element)
+     (org-export-expand-include-keyword)
+     (goto-char (point-min))
+     (eq 1 (org-element-property :level (org-element-at-point))))))
 
 (ert-deftest test-org-export/expand-macro ()
   "Test macro expansion in an Org buffer."
-- 
2.2.1


reply via email to

[Prev in Thread] Current Thread [Next in Thread]