(in-package :clim-doc)

(defmacro with-node-attributes ((&rest atts) node &body body)
  (let ((node-var (gensym)))
    `(let ((,node-var ,node))
       (let (,@(mapcar (lambda (att)
                         `(,att (node-attr ,node-var ,(intern (symbol-name att) :keyword)))) 
                       atts))
         ,@body))))

(defun node-children (node)
  (and (consp node)
       (do ((q (cdr node) (cddr q)))
           ((not (keywordp (car q)))
            q))))

(defun node-attributes (node)
  (let ((atts nil))
    (and (consp node)
         (do ((q (cdr node) (cddr q)))
             ((not (keywordp (car q)))
              (reverse atts))
           (push (car q) atts)
           (push (cadr q) atts)))))

(defun node-attr (node attr &optional default)
  (and (consp node)
       (do ((q (cdr node) (cddr q)))
           ((not (keywordp (car q)))
            default)
         (when (eq (car q) attr)
           (return (cadr q))))))

(defun node-gi (node)
  (if (consp node) (car node) :pcdata))

