org-export: Run a hook just before expanding include keywords and macros

* contrib/lisp/org-export.el (org-export-before-processing-hook): New
  variable.
(org-export-as): Run a hook just before expanding include keywords and
macros, and evaluating Babel blocks.
* testing/lisp/test-org-export.el: Add test.
This commit is contained in:
Nicolas Goaziou 2012-11-02 14:06:50 +01:00
parent 22ac03bee5
commit 5d4243bccc
2 changed files with 50 additions and 17 deletions

View File

@ -2083,19 +2083,32 @@ Any element in `:ignore-list' will be skipped when using
;; Filters properties are installed in communication channel with ;; Filters properties are installed in communication channel with
;; `org-export-install-filters' function. ;; `org-export-install-filters' function.
;; ;;
;; Eventually, a hook (`org-export-before-parsing-hook') is run just ;; Eventually, two hooks (`org-export-before-processing-hook' and
;; before parsing to allow for heavy structure modifications. ;; `org-export-before-parsing-hook') are run at the beginning of the
;; export process and just before parsing to allow for heavy structure
;; modifications.
;;;; Before Parsing Hook ;;;; Hooks
(defvar org-export-before-processing-hook nil
"Hook run at the beginning of the export process.
This is run before include keywords and macros are expanded and
Babel code blocks executed, on a copy of the original buffer
being exported. Visibility and narrowing are preserved. Point
is at the beginning of the buffer.
Every function in this hook will be called with one argument: the
back-end currently used, as a symbol.")
(defvar org-export-before-parsing-hook nil (defvar org-export-before-parsing-hook nil
"Hook run before parsing an export buffer. "Hook run before parsing an export buffer.
This is run after include keywords have been expanded and Babel This is run after include keywords and macros have been expanded
code executed, on a copy of original buffer's area being and Babel code blocks executed, on a copy of the original buffer
exported. Visibility is the same as in the original one. Point being exported. Visibility and narrowing are preserved. Point
is left at the beginning of the new one. is at the beginning of the buffer.
Every function in this hook will be called with one argument: the Every function in this hook will be called with one argument: the
back-end currently used, as a symbol.") back-end currently used, as a symbol.")
@ -2580,6 +2593,8 @@ Return code as a string."
;; attributes, unavailable in its copy. ;; attributes, unavailable in its copy.
(let ((info (org-export--get-buffer-attributes)) tree) (let ((info (org-export--get-buffer-attributes)) tree)
(org-export-with-current-buffer-copy (org-export-with-current-buffer-copy
;; Run first hook with current back-end as argument.
(run-hook-with-args 'org-export-before-processing-hook backend)
;; Update communication channel and get parse tree. Buffer ;; Update communication channel and get parse tree. Buffer
;; isn't parsed directly. Instead, a temporary copy is ;; isn't parsed directly. Instead, a temporary copy is
;; created, where include keywords, macros are expanded and ;; created, where include keywords, macros are expanded and
@ -2600,8 +2615,7 @@ Return code as a string."
;; Update radio targets since keyword inclusion might have ;; Update radio targets since keyword inclusion might have
;; added some more. ;; added some more.
(org-update-radio-target-regexp) (org-update-radio-target-regexp)
;; Run hook `org-export-before-parsing-hook'. with current ;; Run last hook with current back-end as argument.
;; back-end as argument.
(goto-char (point-min)) (goto-char (point-min))
(run-hook-with-args 'org-export-before-parsing-hook backend) (run-hook-with-args 'org-export-before-parsing-hook backend)
;; Update communication channel with environment. Also ;; Update communication channel with environment. Also

View File

@ -492,8 +492,27 @@ body\n")))
(org-test-with-temp-text "* Head1\n* Head2 (note)\n" (org-test-with-temp-text "* Head1\n* Head2 (note)\n"
(should (equal (org-export-as 'test) "* Head1\n"))))))) (should (equal (org-export-as 'test) "* Head1\n")))))))
(ert-deftest test-org-export/before-processing-hook ()
"Test `org-export-before-processing-hook'."
(should
(equal
"#+MACRO: mac val\nTest\n"
(org-test-with-backend test
(org-test-with-temp-text "#+MACRO: mac val\n{{{mac}}} Test"
(let ((org-export-before-processing-hook
'((lambda (backend)
(while (re-search-forward "{{{" nil t)
(let ((object (org-element-context)))
(when (eq (org-element-type object) 'macro)
(delete-region
(org-element-property :begin object)
(org-element-property :end object)))))))))
(org-export-as 'test)))))))
(ert-deftest test-org-export/before-parsing-hook () (ert-deftest test-org-export/before-parsing-hook ()
"Test `org-export-before-parsing-hook'." "Test `org-export-before-parsing-hook'."
(should
(equal "Body 1\nBody 2\n"
(org-test-with-backend test (org-test-with-backend test
(org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2" (org-test-with-temp-text "* Headline 1\nBody 1\n* Headline 2\nBody 2"
(let ((org-export-before-parsing-hook (let ((org-export-before-parsing-hook
@ -501,7 +520,7 @@ body\n")))
(org-map-entries (org-map-entries
(lambda () (lambda ()
(delete-region (point) (progn (forward-line) (point))))))))) (delete-region (point) (progn (forward-line) (point)))))))))
(should (equal (org-export-as 'test) "Body 1\nBody 2\n")))))) (org-export-as 'test)))))))