1 ;;; org-mode-file-server.el --- serve on-demand exported Org-mode files
2 (lexical-let ((docroot "/tmp/"))
5 (with-slots (process headers) request
6 (let ((path (ws-in-directory-p ; check if path is in docroot
7 docroot (substring (cdr (assoc :GET headers)) 1))))
8 (unless path (ws-send-404 process)) ; send 404 if not in docroot
9 (if (file-directory-p path)
10 (progn ;; send directory listing, convert org files to html/tex/txt
11 (ws-response-header proc 200 (cons "Content-type" "text/html"))
12 (process-send-string proc
16 (let* ((full (expand-file-name f path))
17 (end (if (file-directory-p full) "/" ""))
18 (url (url-encode-url (concat f end))))
19 (format "<li><a href=%s>%s</li>" url f)))
23 (list (concat f ".txt")
26 (mapcar #'file-name-sans-extension
27 (directory-files path nil
30 ;; Export the file as requested and return the result
31 (let* ((base (file-name-sans-extension path))
32 (type (case (intern (downcase (file-name-extension path)))
36 (t (ws-error process "%S export not supported"
37 (file-name-extension path)))))
38 (orig (concat base ".org")))
39 (unless (file-exists-p orig) (ws-send-404 process))
40 (save-window-excursion (find-file orig)
41 (org-export-to-file type path))
42 (ws-send-file process path))))))