Browse Source

org-export: Expand include keywords before macros

* contrib/lisp/org-export.el (org-export-as): Expand include keywords
  before macros.  This allows to specify macro templates in the
  included file.
* testing/examples/macro-templates.org: New file for testing purposes.
* testing/lisp/test-org-export.el: Add test.
Nicolas Goaziou 12 years ago
parent
commit
89d6a26f8d

+ 4 - 1
contrib/lisp/org-export.el

@@ -2552,8 +2552,8 @@ Return code as a string."
 				  (current-buffer))))
 		     (org-export-with-current-buffer-copy
 		      (unless noexpand
-			(org-export-expand-macro info)
 			(org-export-expand-include-keyword)
+			(org-export-expand-macro info)
 			;; TODO: Setting `org-current-export-file' is
 			;; required by Org Babel to properly resolve
 			;; noweb references.  Once "org-exp.el" is
@@ -2724,6 +2724,9 @@ Point is at buffer's beginning when BODY is applied."
 (defun org-export-expand-macro (info)
   "Expand every macro in buffer.
 INFO is a plist containing export options and buffer properties."
+  ;; First update macro templates since #+INCLUDE keywords might have
+  ;; added some new ones.
+  (org-macro-initialize-templates)
   (org-macro-replace-all
    ;; Before expanding macros, install {{{author}}}, {{{date}}},
    ;; {{{email}}} and {{{title}}} templates.

+ 3 - 0
testing/examples/macro-templates.org

@@ -0,0 +1,3 @@
+# Time-stamp: <2012-10-10 13:51:10 nicolas>
+#+TITLE: Macro templates
+#+MACRO: included-macro success

+ 12 - 2
testing/lisp/test-org-export.el

@@ -436,7 +436,6 @@ body\n")))
    (equal "#+MACRO: macro1 value\nvalue"
 	  (org-test-with-temp-text "#+MACRO: macro1 value\n{{{macro1}}}"
 	    (let (info)
-	      (org-macro-initialize-templates)
 	      (org-export-expand-macro info) (buffer-string)))))
   ;; Export specific macros.
   (should
@@ -449,7 +448,18 @@ body\n")))
 #+EMAIL: me@here
 {{{author}}} {{{date}}} {{{email}}} {{{title}}}"
 	    (let ((info (org-export-get-environment)))
-	      (org-macro-initialize-templates)
+	      (org-export-expand-macro info)
+	      (goto-char (point-max))
+	      (buffer-substring (line-beginning-position)
+				(line-end-position))))))
+  ;; Expand macros with templates in included files.
+  (should
+   (equal "success"
+	  (org-test-with-temp-text
+	      (format "#+INCLUDE: \"%s/examples/macro-templates.org\"
+{{{included-macro}}}" org-test-dir)
+	    (let (info)
+	      (org-export-expand-include-keyword)
 	      (org-export-expand-macro info)
 	      (goto-char (point-max))
 	      (buffer-substring (line-beginning-position)