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