]> code.delx.au - gnu-emacs-elpa/blobdiff - packages/web-server/examples/015-auto-mode-server.el
Add 'packages/web-server/' from commit 'd0b6ae9df6014db2195da0081dc97cc8246f1fda'
[gnu-emacs-elpa] / packages / web-server / examples / 015-auto-mode-server.el
diff --git a/packages/web-server/examples/015-auto-mode-server.el b/packages/web-server/examples/015-auto-mode-server.el
new file mode 100644 (file)
index 0000000..69e993f
--- /dev/null
@@ -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))