eschulte pushed a commit to branch master
in repository elpa.
commit b82f15c5c2d4a6935ff7a4b753a1095fe95820ec
Author: Eric Schulte <[email protected]>
Date: Sat Jan 11 18:24:45 2014 -0700
example serving Org-mode files as JSON
---
examples/014-org-json.el | 24 ++++++++++++++++++++++++
1 files changed, 24 insertions(+), 0 deletions(-)
diff --git a/examples/014-org-json.el b/examples/014-org-json.el
new file mode 100644
index 0000000..822175a
--- /dev/null
+++ b/examples/014-org-json.el
@@ -0,0 +1,24 @@
+;;; org-json.el --- Serve Org-mode pages as json
+;; suggested by nicferrier
+(require 'json)
+(lexical-let ((docroot "/tmp/"))
+ (ws-start
+ (lambda (request)
+ (with-slots (process headers) request
+ (let ((path (ws-in-directory-p
+ docroot (substring (cdr (assoc :GET headers)) 1))))
+ (unless (and path (file-exists-p path))
+ (ws-send-404 process))
+ (save-window-excursion
+ (find-file path)
+ (ws-response-header process 200
+ '("Content-type" . "application/json"))
+ (process-send-string process
+ (let ((tree (org-element-parse-buffer)))
+ (org-element-map tree
+ (append org-element-all-objects org-element-all-elements)
+ (lambda (el)
+ (org-element-put-property el :parent "none")
+ (org-element-put-property el :structure "none")))
+ (json-encode tree)))))))
+ 9014))