Function: htmlize-file
htmlize-file is an autoloaded, interactive and byte-compiled function
defined in htmlize.el.
Signature
(htmlize-file FILE &optional TARGET)
Documentation
Load FILE, fontify it, convert it to HTML, and save the result.
Contents of FILE are inserted into a temporary buffer, whose major mode
is set with normal-mode as appropriate for the file type. The buffer
is subsequently fontified with font-lock and converted to HTML. Note
that, unlike htmlize-buffer, this function explicitly turns on
font-lock. If a form of highlighting other than font-lock is desired,
please use htmlize-buffer directly on buffers so highlighted.
Buffers currently visiting FILE are unaffected by this function. The function does not change current buffer or move the point.
If TARGET is specified and names a directory, the resulting file will be saved there instead of to FILE's directory. If TARGET is specified and does not name a directory, it will be used as output file name.
Key Bindings
Source Code
;; Defined in ~/.emacs.d/elpa/htmlize-20250724.1703/htmlize.el
;;;###autoload
(defun htmlize-file (file &optional target)
"Load FILE, fontify it, convert it to HTML, and save the result.
Contents of FILE are inserted into a temporary buffer, whose major mode
is set with `normal-mode' as appropriate for the file type. The buffer
is subsequently fontified with `font-lock' and converted to HTML. Note
that, unlike `htmlize-buffer', this function explicitly turns on
font-lock. If a form of highlighting other than font-lock is desired,
please use `htmlize-buffer' directly on buffers so highlighted.
Buffers currently visiting FILE are unaffected by this function. The
function does not change current buffer or move the point.
If TARGET is specified and names a directory, the resulting file will be
saved there instead of to FILE's directory. If TARGET is specified and
does not name a directory, it will be used as output file name."
(interactive (list (read-file-name
"HTML-ize file: "
nil nil nil (and (buffer-file-name)
(file-name-nondirectory
(buffer-file-name))))))
(let ((output-file (if (and target (not (file-directory-p target)))
target
(expand-file-name
(htmlize-make-file-name (file-name-nondirectory file))
(or target (file-name-directory file))))))
(with-temp-buffer
;; Insert FILE into the temporary buffer.
(insert-file-contents file)
;; Set the file name so normal-mode and htmlize-buffer-1 pick it
;; up. Restore it afterwards so with-temp-buffer's kill-buffer
;; doesn't complain about killing a modified buffer.
(let ((buffer-file-name file))
;; Set the major mode for the sake of font-lock.
(normal-mode)
;; htmlize the buffer and save the HTML.
(with-current-buffer (htmlize-buffer-1)
(unwind-protect
(progn
(run-hooks 'htmlize-file-hook)
(write-region (point-min) (point-max) output-file))
(kill-buffer (current-buffer)))))))
;; I haven't decided on a useful return value yet, so just return
;; nil.
nil)