fixed bugs related to sxml that had namespaced attributes (not sure if this fixes...
[cl-mediawiki.git] / src / util.lisp
blobce0e821df95dfe22de76cd547b98dce3cdaf35e0
1 (in-package :cl-mediawiki)
2 ;; This file has a few necessary random utility functions
4 (defun ensure-list (x)
5 "just ensure that you have alist"
6 (if (listp x) x (list x)))
8 (defun symbolize-string (str &optional (package :keyword))
9 "Turns a string into a happy symbol
11 ex: ''foo bar_bast'' -> FOO-BAR-BAST
13 (etypecase str
14 (string (intern (nsubstitute
15 #\- #\_
16 (nsubstitute #\- #\space (string-upcase str) :test #'char=)
17 :test #'char=)
18 package))
19 (symbol str)))
21 (defun map-sxml-tree (fn tree)
22 "Do a depth first traversal of some set of trees calling fn on every non-nil element. "
23 (when tree
24 (labels ((rec (tree)
25 (funcall fn tree)
26 (dolist (n (cddr tree))
27 (when (listp n)
28 (rec n)))))
29 (rec tree))))
31 (defun find-tree (pred tree)
32 "find a tree based on a predicate"
33 (let ((results))
34 (flet ((handler (node)
35 (when (funcall pred node)
36 (push node results))))
37 (map-sxml-tree #'handler tree)
38 (nreverse results))))
40 (defun find-nodes-by-name (name tree)
41 "find all sxml nodes with a given name "
42 (find-tree (lambda (n)
43 (string-equal
44 (when (and (listp n) (stringp (car n)))
45 (car n)) name)) tree))