]> code.delx.au - gnu-emacs-elpa/blob - packages/web-server/examples/015-auto-mode-server.el
69e993f4bdc82a0d996e23f8d3d053aca8448882
[gnu-emacs-elpa] / packages / web-server / examples / 015-auto-mode-server.el
1 ;;; auto-mode-server.el --- files with fontification from the `auto-mode-alist'
2 (require 'htmlize)
3
4 (lexical-let ((docroot default-directory))
5 (ws-start
6 (lambda (request)
7 (with-slots (process headers) request
8 (let ((path (ws-in-directory-p
9 docroot (substring (cdr (assoc :GET headers)) 1))))
10 (if path
11 (if (file-directory-p path)
12 (ws-send-directory-list process
13 (expand-file-name path docroot) "^[^\.]")
14 ;; send htmlize version of file
15 (let ((mode (or (cdr (cl-assoc-if (lambda (re) (string-match re path))
16 auto-mode-alist))
17 'fundamental-mode)))
18 (ws-response-header process 200
19 '("Content-type" . "text/html; charset=utf-8"))
20 (process-send-string process
21 (with-temp-buffer
22 (insert-file-contents-literally path)
23 (funcall mode)
24 (let ((html (htmlize-buffer)))
25 (prog1 (with-current-buffer html (buffer-string))
26 (kill-buffer html)))))))
27 (ws-send-404 process)))))
28 9015))