Skip to content

Instantly share code, notes, and snippets.

@mogigoma
Created August 11, 2012 06:25
Show Gist options
  • Save mogigoma/3321806 to your computer and use it in GitHub Desktop.
Save mogigoma/3321806 to your computer and use it in GitHub Desktop.
A hack to publish any buffer exactly as it appears in Emacs on demand over HTTP.
(defvar mak/elnode-publish-port
4444
"The port for published buffers to be served on.")
(defvar mak/elnode-publish-buffers
(make-hash-table :test 'equal)
"List of the buffers to publish over HTTP.")
(defun mak/elnode-publish-handler (httpcon)
"Handler for buffers published by Elnode."
(let* ((path (substring (url-unhex-string (elnode-http-pathinfo httpcon)) 1))
(name (gethash path mak/elnode-publish-buffers)))
(if name
(progn
(elnode-http-start httpcon 200 '("Content-Type" . "text/html"))
(elnode-http-return
httpcon
(with-current-buffer
(htmlize-buffer name)
(buffer-string))))
(progn
(elnode-http-start httpcon 404 '("Content-Type" . "text/plain"))
(elnode-http-return
httpcon
(format "'%s' is not in the list of published buffers." path))))))
(defun mak/elnode-publish-buffer (buffer url)
"Publish a buffer through Elnode."
(interactive "bBuffer name: \nMURL: ")
(puthash url buffer mak/elnode-publish-buffers))
(defun mak/elnode-unpublish-buffer (buffer)
"Unpublish a buffer through Elnode."
(interactive "MURL: ")
(remhash buffer mak/elnode-publish-buffers))
(defun mak/elnode-publish-start ()
"Start listening for requests for published buffers."
(interactive)
(when (y-or-n-p "Start publishing buffers? ")
(elnode-start 'mak/elnode-publish-handler :port mak/elnode-publish-port :host "*")))
(defun mak/elnode-publish-stop ()
"Stop listening for requests for published buffers."
(interactive)
(when (y-or-n-p "Stop publishing buffers? ")
(elnode-stop mak/elnode-publish-port)))
@nicferrier
Copy link

the unhex at L11? should I offer that as an option on pathinfo funcall?

it would be cool not just to htmlize but to add some header and footer, maybe editor stuff so that people could offer updates?

@mogigoma
Copy link
Author

the unhex at L11? should I offer that as an option on pathinfo funcall?

Personally, I think that all returned data from the URL should be unhexed by default, perhaps functions could have an optional flag to indicate if they would like the data raw?

Regarding URL functions, two things I was looking for last night were a function to access the entire URL -- path, fragment, and query string -- as a string, and a function to access the URL fragment directly. Those would be very handy, especially if they can be accessed in both raw and decoded form.

it would be cool not just to htmlize but to add some header and footer, maybe editor stuff so that people could offer updates?

That's the plan. I'm hoping to add in ACE/Skywriter support.

@nicferrier
Copy link

servers don't get fragments do they?

I will update the readme in the REPO with information about the other bits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment