contrib/lisp/org-export: Make export to file compatible with org-publish
* contrib/lisp/org-export.el (org-export-to-file): Automatically retrieve output file name. Also add a PUB-DIR optional argument.
This commit is contained in:
parent
3dac42e589
commit
3ada986ba3
|
@ -2017,16 +2017,12 @@ Return buffer."
|
||||||
(goto-char (point-min)))
|
(goto-char (point-min)))
|
||||||
buffer))
|
buffer))
|
||||||
|
|
||||||
(defun org-export-to-file (backend filename &optional post-process subtreep
|
(defun org-export-to-file (backend &optional post-process subtreep visible-only
|
||||||
visible-only body-only ext-plist)
|
body-only ext-plist pub-dir)
|
||||||
"Call `org-export-as' with output to a specified file.
|
"Call `org-export-as' with output to a specified file.
|
||||||
|
|
||||||
BACKEND is the back-end used for transcoding, as a symbol.
|
BACKEND is the back-end used for transcoding, as a symbol.
|
||||||
|
|
||||||
FILENAME is the output file name. If it already exists, it will
|
|
||||||
be erased first, unless it isn't writable, in which case an error
|
|
||||||
will be returned. Otherwise, the file will be created.
|
|
||||||
|
|
||||||
Optional argument POST-PROCESS, when non-nil, is a function
|
Optional argument POST-PROCESS, when non-nil, is a function
|
||||||
applied to the output file. It expects one argument: the file
|
applied to the output file. It expects one argument: the file
|
||||||
name, as a string. It can be used to call shell commands on that
|
name, as a string. It can be used to call shell commands on that
|
||||||
|
@ -2036,22 +2032,45 @@ Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
|
||||||
EXT-PLIST are similar to those used in `org-export-as', which
|
EXT-PLIST are similar to those used in `org-export-as', which
|
||||||
see.
|
see.
|
||||||
|
|
||||||
Return file name."
|
When optional argument PUB-DIR is set, use it as the publishing
|
||||||
|
directory.
|
||||||
|
|
||||||
|
Return output file's name."
|
||||||
|
;; First get output directory and output file name.
|
||||||
|
(let ((out-file
|
||||||
|
(concat (file-name-as-directory (or pub-dir "."))
|
||||||
|
;; Output file name either comes from
|
||||||
|
;; EXPORT_FILE_NAME sub-tree property, assuming input
|
||||||
|
;; is narrowed to said sub-tree, or to the name of
|
||||||
|
;; buffer's associated file.
|
||||||
|
(file-name-sans-extension
|
||||||
|
(or (and subtreep
|
||||||
|
(org-entry-get
|
||||||
|
(save-excursion
|
||||||
|
(ignore-errors
|
||||||
|
(org-back-to-heading (not visible-only))
|
||||||
|
(point)))
|
||||||
|
"EXPORT_FILE_NAME" t))
|
||||||
|
(file-name-nondirectory
|
||||||
|
(or (buffer-file-name (buffer-base-buffer))
|
||||||
|
(error "Output file's name undefined")))))
|
||||||
|
".tex")))
|
||||||
;; Checks for file and directory permissions.
|
;; Checks for file and directory permissions.
|
||||||
(cond
|
(cond
|
||||||
((not (file-exists-p filename))
|
((not (file-exists-p out-file))
|
||||||
(let ((dir (or (file-name-directory filename) default-directory)))
|
(unless (file-writable-p (or pub-dir "."))
|
||||||
(unless (file-writable-p dir) (error "Output directory not writable"))))
|
(error "Output directory not writable")))
|
||||||
((not (file-writable-p filename)) (error "Output file not writable")))
|
((not (file-writable-p out-file)) (error "Output file not writable")))
|
||||||
;; All checks passed: insert contents to a temporary buffer and
|
;; All checks passed: insert contents to a temporary buffer and
|
||||||
;; write it to the specified file.
|
;; write it to the specified file.
|
||||||
(let ((out (org-export-as backend subtreep visible-only body-only ext-plist)))
|
(let ((out (org-export-as
|
||||||
|
backend subtreep visible-only body-only ext-plist)))
|
||||||
(with-temp-buffer
|
(with-temp-buffer
|
||||||
(insert out)
|
(insert out)
|
||||||
(write-file filename)))
|
(write-file out-file)))
|
||||||
(when post-process (funcall post-process filename))
|
(when post-process (funcall post-process out-file))
|
||||||
;; Return value.
|
;; Return full path.
|
||||||
filename)
|
out-file))
|
||||||
|
|
||||||
(defmacro org-export-with-current-buffer-copy (&rest body)
|
(defmacro org-export-with-current-buffer-copy (&rest body)
|
||||||
"Apply BODY in a copy of the current buffer.
|
"Apply BODY in a copy of the current buffer.
|
||||||
|
|
Loading…
Reference in New Issue