Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 52 lines (45 sloc) 1.688 kB
bf501ac inside out - a silly little elnode example
nferrier authored
1 ;; Expose your running emacs to the local web by using elnode
2 ;; Copyright (C) 2010 by Nic Ferrier
3
4 (defun insideout-html-escape (str)
5 "Escape significant HTML characters in 'str'"
6 (replace-regexp-in-string
7 "<\\|\\&"
8 (lambda (src)
9 (cond
10 ((equal src "&") "&amp;")
11 ((equal src "<") "&lt;")))
12 str))
13
b1c87ae @tinku99 on emacs24 required changing buf to buf-name in 2 places.
tinku99 authored
14 (defun insideout-render (buf-name)
bf501ac inside out - a silly little elnode example
nferrier authored
15 "Render buffer 'buf' as HTML"
16 (with-current-buffer (get-buffer buf-name)
17 (format "<html>
18 <body><pre>%s</pre></body>
19 </html>"
20 (insideout-html-escape
21 (buffer-substring-no-properties (point-min) (point-max))))))
22
23 (defun insideout-handler (httpcon)
7aa6b8b help for the handler
nferrier authored
24 "Turn your emacs insideout with elnode.
25
26 This exposes all your non * buffers to localhost - so beware."
bf501ac inside out - a silly little elnode example
nferrier authored
27 (let ((p (elnode-http-pathinfo httpcon)))
28 (let ((buf-name (progn
29 (string-match "/\\(.*\\)" p)
30 (match-string 1 p))))
31 ;; An individual buffer
32 (progn
33 (elnode-http-start httpcon 200 '("Content-Type" . "text/html"))
34 (elnode-http-return
35 httpcon
36 (if (bufferp (get-buffer buf-name))
b1c87ae @tinku99 on emacs24 required changing buf to buf-name in 2 places.
tinku99 authored
37 (insideout-render buf-name)
bf501ac inside out - a silly little elnode example
nferrier authored
38 ;; The buffer index
39 (format "<html><body><ul>%s</ul></body></html>"
40 (mapconcat
41 (lambda (buf)
42 (if (not (string-match "\\*" (buffer-name buf)))
43 (format "<li><a href='/%s'>%s</a></li>"
44 (buffer-name buf)
45 (buffer-name buf))))
46 (buffer-list)
47 "\n"))))))))
48
49 (elnode-start 'insideout-handler 8028 "localhost")
50
51 ;; End
Something went wrong with that request. Please try again.