eschulte pushed a commit to branch master in repository elpa. commit bde88e9a22be202fe62e91ffa4aa988e07fc09c1 Author: Eric Schulte <schulte.e...@gmail.com> Date: Wed Jan 15 15:53:44 2014 -0800
serve files with htmlize Emacs fontification --- examples/015-auto-mode-server.el | 28 ++++++++++++++++++++++++++++ 1 files changed, 28 insertions(+), 0 deletions(-) diff --git a/examples/015-auto-mode-server.el b/examples/015-auto-mode-server.el new file mode 100644 index 0000000..69e993f --- /dev/null +++ b/examples/015-auto-mode-server.el @@ -0,0 +1,28 @@ +;;; auto-mode-server.el --- files with fontification from the `auto-mode-alist' +(require 'htmlize) + +(lexical-let ((docroot default-directory)) + (ws-start + (lambda (request) + (with-slots (process headers) request + (let ((path (ws-in-directory-p + docroot (substring (cdr (assoc :GET headers)) 1)))) + (if path + (if (file-directory-p path) + (ws-send-directory-list process + (expand-file-name path docroot) "^[^\.]") + ;; send htmlize version of file + (let ((mode (or (cdr (cl-assoc-if (lambda (re) (string-match re path)) + auto-mode-alist)) + 'fundamental-mode))) + (ws-response-header process 200 + '("Content-type" . "text/html; charset=utf-8")) + (process-send-string process + (with-temp-buffer + (insert-file-contents-literally path) + (funcall mode) + (let ((html (htmlize-buffer))) + (prog1 (with-current-buffer html (buffer-string)) + (kill-buffer html))))))) + (ws-send-404 process))))) + 9015))