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
|
||||||
;; Checks for file and directory permissions.
|
directory.
|
||||||
(cond
|
|
||||||
((not (file-exists-p filename))
|
Return output file's name."
|
||||||
(let ((dir (or (file-name-directory filename) default-directory)))
|
;; First get output directory and output file name.
|
||||||
(unless (file-writable-p dir) (error "Output directory not writable"))))
|
(let ((out-file
|
||||||
((not (file-writable-p filename)) (error "Output file not writable")))
|
(concat (file-name-as-directory (or pub-dir "."))
|
||||||
;; All checks passed: insert contents to a temporary buffer and
|
;; Output file name either comes from
|
||||||
;; write it to the specified file.
|
;; EXPORT_FILE_NAME sub-tree property, assuming input
|
||||||
(let ((out (org-export-as backend subtreep visible-only body-only ext-plist)))
|
;; is narrowed to said sub-tree, or to the name of
|
||||||
(with-temp-buffer
|
;; buffer's associated file.
|
||||||
(insert out)
|
(file-name-sans-extension
|
||||||
(write-file filename)))
|
(or (and subtreep
|
||||||
(when post-process (funcall post-process filename))
|
(org-entry-get
|
||||||
;; Return value.
|
(save-excursion
|
||||||
filename)
|
(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.
|
||||||
|
(cond
|
||||||
|
((not (file-exists-p out-file))
|
||||||
|
(unless (file-writable-p (or pub-dir "."))
|
||||||
|
(error "Output directory not writable")))
|
||||||
|
((not (file-writable-p out-file)) (error "Output file not writable")))
|
||||||
|
;; All checks passed: insert contents to a temporary buffer and
|
||||||
|
;; write it to the specified file.
|
||||||
|
(let ((out (org-export-as
|
||||||
|
backend subtreep visible-only body-only ext-plist)))
|
||||||
|
(with-temp-buffer
|
||||||
|
(insert out)
|
||||||
|
(write-file out-file)))
|
||||||
|
(when post-process (funcall post-process out-file))
|
||||||
|
;; Return full path.
|
||||||
|
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