under .
(defun htmlize-font-doctype ()
nil ; no doc-string
"")
(defun htmlize-font-body-tag ()
(let ((face-object (gethash 'default htmlize-face-hash)))
(format ""
(htmlize-face-rgb-foreground face-object)
(htmlize-face-rgb-background face-object))))
(defun htmlize-font-face-prejunk (face-object)
(concat ""
(and (htmlize-face-boldp face-object) "")
(and (htmlize-face-italicp face-object) "")
(and (htmlize-face-underlinep face-object) "")
(and (htmlize-face-strikep face-object) "")))
(defun htmlize-font-face-postjunk (face-object)
(concat (and (htmlize-face-strikep face-object) "")
(and (htmlize-face-underlinep face-object) "")
(and (htmlize-face-italicp face-object) "")
(and (htmlize-face-boldp face-object) "")
""))
(defmacro htmlize-method (method &rest args)
(let ((func (gensym "hm-")))
`(let ((,func (intern (format "htmlize-%s-%s" htmlize-output-type ',method))))
(and (fboundp ,func)
(funcall ,func ,@args)))))
;;;###autoload
(defun htmlize-buffer (&optional buffer)
"HTML-ize BUFFER."
(interactive)
(or buffer
(setq buffer (current-buffer)))
(save-excursion
(set-buffer buffer)
(run-hooks 'htmlize-before-hook)
(htmlize-make-face-hash (cons 'default (htmlize-faces-in-buffer))))
(let* ((newbuf (generate-new-buffer "*html*"))
next-change face face-object)
(switch-to-buffer newbuf)
(buffer-disable-undo)
(insert (htmlize-method doctype) ?\n
(format "\n"
htmlize-version htmlize-output-type))
(insert "\n \n "
(htmlize-protect-string (if (stringp buffer) buffer
(buffer-name buffer)))
"\n" htmlize-head-tags)
(htmlize-method insert-head)
(insert " ")
(insert "\n "
(or (htmlize-method body-tag)
"")
"\n \n")
(with-current-buffer buffer
(save-excursion
(goto-char (point-min))
(while (not (eobp))
(setq face (get-text-property (point) 'face)
next-change (or (next-single-property-change (point) 'face)
(point-max)))
(and (consp face)
;; Choose the first face.
(setq face (car face)))
(and (eq face 'default)
(setq face nil))
;; FSF Emacs allows `face' property to contain arbitrary
;; stuff.
(or (htmlize-symbol-face-p face)
(setq face nil))
(when face
(setq face-object (gethash face htmlize-face-hash))
(princ (htmlize-method face-prejunk face-object) newbuf))
(princ (htmlize-protect-string
(buffer-substring-no-properties (point) next-change))
newbuf)
(when face
(princ (htmlize-method face-postjunk face-object) newbuf))
(goto-char next-change))))
(insert "
\n \n\n")
(goto-char (point-min))
(run-hooks 'htmlize-after-hook)
(buffer-enable-undo)
;; We won't be needing the stored data anymore, so allow next gc
;; to free up the used conses.
(clrhash htmlize-face-hash)))
(defun htmlize-make-file-name (file dir)
(let* ((nondir (file-name-nondirectory file))
(extension (htmlize-file-name-extension file))
(sans-extension (file-name-sans-extension nondir)))
(expand-file-name (if (or (equal extension "html")
(equal extension "htm")
(equal sans-extension ""))
(concat nondir ".html")
(concat sans-extension ".html"))
(or dir (file-name-directory file)))))
;;;###autoload
(defun htmlize-file (file &optional target-directory)
"HTML-ize FILE, and save the result.
If TARGET-DIRECTORY is non-nil, the resulting HTML file will be saved
to that directory, instead of to the FILE's directory."
(interactive "fHTML-ize file: ")
(let* ((was-visited (get-file-buffer file))
;; Set these to nil to prevent double fontification; we'll
;; fontify manually below.
(font-lock-auto-fontify nil)
(global-font-lock-mode nil)
(origbuf (set-buffer (find-file-noselect file t))))
(font-lock-fontify-buffer)
(htmlize-buffer)
(run-hooks 'htmlize-file-hook)
(write-region (point-min) (point-max)
(htmlize-make-file-name file target-directory))
(kill-buffer (current-buffer))
(unless was-visited
(kill-buffer origbuf))))
;;;###autoload
(defun htmlize-many-files (files &optional target-directory)
"HTML-ize files specified by FILES, and save them to `.html' files.
If TARGET-DIRECTORY is specified, the HTML files will be saved to that
directory. Normally, each HTML file is saved to the directory of the
corresponding source file."
(interactive
(list
(let (list file)
;; Check for `ommadawn', because checking against nil doesn't do
;; what you'd expect.
(while (not (eq (setq file (read-file-name "HTML-ize file (RET to finish): "
(and list (file-name-directory
(car list)))
'ommadawn t))
'ommadawn))
(push file list))
list)))
(dolist (file files)
(htmlize-file file target-directory)))
;;;###autoload
(defun htmlize-many-files-dired (arg &optional target-directory)
"HTMLize dired-marked files."
(interactive "P")
(htmlize-many-files (dired-get-marked-files nil arg) target-directory))
(provide 'htmlize)
;;; htmlize.el ends here