-
Notifications
You must be signed in to change notification settings - Fork 5
/
shampoo-xml.el
70 lines (61 loc) · 2.32 KB
/
shampoo-xml.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
;;; shampoo-xml.el --- Shampoo XML processing functions
;;
;; Copyright (C) 2010 - 2012 Dmitry Matveev <[email protected]>
;;
;; This software is released under terms of the MIT license,
;; please refer to the LICENSE file for details.
(eval-when-compile (require 'cl))
(require 'cl)
(require 'xml)
(defsubst shampoo-replace-in-string (str regexp newtext)
(replace-regexp-in-string regexp newtext str t t))
;; This function has been taken from emacs-jabber.
;; Thanks to its authors.
(defun shampoo-escape-xml (str)
(if (stringp str)
(let ((newstr (concat str))
(re "[\000-\010\013\014\016-\037]"))
(setq newstr (shampoo-replace-in-string newstr "\f" "\n"))
(setq newstr (shampoo-replace-in-string newstr re " "))
(setq newstr (shampoo-replace-in-string newstr "&" "&"))
(setq newstr (shampoo-replace-in-string newstr "<" "<"))
(setq newstr (shampoo-replace-in-string newstr ">" ">"))
(setq newstr (shampoo-replace-in-string newstr "'" "'"))
(setq newstr (shampoo-replace-in-string newstr "\"" """))
newstr)
str))
(defun shampoo-xml (tagname attrs &optional text subnodes)
(with-output-to-string
(princ (concat "<" (symbol-name tagname)))
(mapc (lambda (attr)
(if (keywordp attr)
(princ (concat
" "
(substring (symbol-name attr) 1)
"=\""))
(progn (princ attr)
(princ "\""))))
attrs)
(if (or text subnodes)
(progn
(princ ">")
(when text (princ (shampoo-escape-xml text)))
(when subnodes (dolist (subnode subnodes) (princ subnode)))
(princ (concat "</" (symbol-name tagname) ">")))
(princ " />"))))
(defun shampoo-parse-xml (str)
(with-temp-buffer
(insert str)
(car (xml-parse-region (point-min) (point-max)))))
(defun shampoo-xml-attrs-hash (xml-attrs-list)
(let ((result (make-hash-table :test 'equal)))
(dolist (pair xml-attrs-list)
(puthash (car pair) (cdr pair) result))
result))
(defun shampoo-xml-nodes-named (symbol data)
(remove-if (lambda (x)
(or (stringp x)
(not (equal (car x) symbol))))
data))
(provide 'shampoo-xml)
;;; shampoo-xml ends here.