forked from plexus/.emacs.d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhiccup.el
76 lines (64 loc) · 2.58 KB
/
hiccup.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
;; threading macros copied from https://github.com/sroccaserra/emacs/blob/master/tools.el
(defmacro -> (x &optional form &rest more)
(cond ((not (null more))
`(-> (-> ,x ,form) ,@more))
((not (null form))
(if (sequencep form)
`(,(first form) ,x ,@(rest form))
(list form x)))
(t x)))
(defmacro ->> (x form &rest more)
(cond ((not (null more)) `(->> (->> ,x ,form) ,@more))
(t (if (sequencep form)
`(,(first form) ,@(rest form) ,x)
(list form x)))))
(defun plexus/sexp-to-hiccup-tag (attrs)
(let ((tag (symbol-name (car attrs)))
(attrs (cadr s)))
(if-let ((id (cdr (assoc 'id attrs))))
(setq tag (concat tag "#" id)))
(if-let ((class (cdr (assoc 'class attrs))))
(setq tag (concat tag "." (s-replace " " "." (s-trim class)))))
tag))
(defun plexus/sexp-to-hiccup-attrs (attrs)
(let ((attrs (->> attrs
copy-alist
(assq-delete-all 'id)
(assq-delete-all 'class))))
(if (and attrs (car attrs))
(concat " {"
(s-join " "
(loop for attr in attrs
collect (if (or (eq 'id (car attr)) (eq 'class (car attr)))
""
(concat
":"
(symbol-name (car attr))
" "
(format "%S" (cdr attr)))))) "}"))))
(defun plexus/all-whitespace? (str)
"Because Elisp regexes are terrible"
(-all? (lambda (x) (-contains? '(9 10 13 32) x)) (append str nil)))
(defun plexus/sexp-to-hiccup-children (cs)
(if cs
(loop for ch in cs
concat (concat " " (if (stringp ch)
(if (plexus/all-whitespace? ch)
""
(format "%S" ch))
(plexus/sexp-to-hiccup ch))))))
(defun plexus/sexp-to-hiccup (s)
(concat "[:"
(plexus/sexp-to-hiccup-tag s)
(plexus/sexp-to-hiccup-attrs (cadr s))
(plexus/sexp-to-hiccup-children (cddr s))
"]"))
(defun plexus/region-to-hiccup ()
(let* ((html (libxml-parse-html-region (point) (mark)))
(inner (caddr (caddr html))))
(plexus/sexp-to-hiccup inner)))
(defun plexus/convert-region-to-hiccup ()
(interactive)
(let ((hiccup (plexus/region-to-hiccup)))
(delete-region (point) (mark))
(insert hiccup)))