-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjss-io-pretty-printers.el
61 lines (52 loc) · 2.17 KB
/
jss-io-pretty-printers.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
;;; jss-io-pretty-printers.el -- utility code for formating/highlighting/indenting browser responses
;;
;; Copyright (C) 2013 Edward Marco Baringer
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2 of
;; the License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be
;; useful, but WITHOUT ANY WARRANTY; without even the implied
;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
;; PURPOSE. See the GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public
;; License along with this program; if not, write to the Free
;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
;; MA 02111-1307 USA
(require 'cl)
(require 'json)
(defvar jss-io-cleaners (make-hash-table :test 'equal))
(defmacro* define-jss-io-cleaner (content-type (data) &body body)
(declare (indent 2))
(let ((type (cl-gensym)))
`(let ((,type ',content-type))
(dolist (type (if (listp ,type) ,type (list ,type)))
(setf (gethash type jss-io-cleaners) (lambda (,data) ,@body)))
',content-type)))
(defun jss-io-fontify-data-with-mode (data mode)
(let (out (buffer (generate-new-buffer " *jss-fontification*")))
(with-current-buffer buffer
(insert data)
(funcall mode)
(indent-region (point-min) (point-max))
(setf out (buffer-substring (point-min) (point-max))))
out))
(define-jss-io-cleaner "text/html" (html)
(jss-io-fontify-data-with-mode html 'nxml-mode))
(define-jss-io-cleaner "text/css" (css)
(jss-io-fontify-data-with-mode css 'css-mode))
(define-jss-io-cleaner ("application/javascript" "text/javascript") (js)
(jss-io-fontify-data-with-mode js (if (fboundp 'js2-mode)
'js2-mode
'js-mode)))
(define-jss-io-cleaner ("application/json") (json)
(let (parsed)
(with-temp-buffer
(insert json)
(goto-char (point-min))
(setf parsed (json-read)))
(pp-to-string parsed)))
(provide 'jss-io-pretty-printers)