add docstrings, simplify INDENTATION
This commit is contained in:
parent
82bafb94ae
commit
73cdbc98ad
20
cl-xml.lisp
20
cl-xml.lisp
|
@ -2,20 +2,21 @@
|
|||
|
||||
(in-package #:cl-xml)
|
||||
|
||||
(declaim (optimize (debug 3)))
|
||||
|
||||
(defun tagp (exp)
|
||||
(symbolp (car exp)))
|
||||
|
||||
(defun print-contents (contents indentation stream)
|
||||
"Print the contents of a tag with correct indentation. Every element in CONTENTS is on a separate line."
|
||||
(mapc (lambda (s) (format stream "~a~a~%" indentation s)) contents))
|
||||
|
||||
(defun next-tags (list)
|
||||
"Get the next tags in the tree."
|
||||
(cond ((null list) nil)
|
||||
((eq 'cons (type-of (car list))) list)
|
||||
(t (next-tags (cdr list)))))
|
||||
|
||||
(defun handle-keywords (keywords)
|
||||
"Turn keyword-value pairs on the form :key \"value\" into key=\"value\"."
|
||||
(cond ((or (null keywords)
|
||||
(consp (car keywords)))
|
||||
"")
|
||||
|
@ -26,17 +27,11 @@
|
|||
(handle-keywords (cddr keywords))))))
|
||||
|
||||
(defun indentation (level)
|
||||
(labels ((create-indentation-string (count)
|
||||
(if (= count 0)
|
||||
'()
|
||||
(cons " " (create-indentation-string (1- count))))))
|
||||
(let ((spaces (create-indentation-string level)))
|
||||
(if (null spaces)
|
||||
""
|
||||
(reduce (lambda (s1 s2) (concatenate 'string s1 s2))
|
||||
spaces)))))
|
||||
"Create a string of LEVEL length consisting of spaces."
|
||||
(make-string level :initial-element #\Space))
|
||||
|
||||
(defun sexp-to-xml (sexp indentation-level &optional (stream t))
|
||||
"Turn an s-expression into an xml form."
|
||||
(cond ((null sexp) nil)
|
||||
((stringp (car sexp)) (print-contents sexp (indentation indentation-level) stream))
|
||||
((consp (car sexp))
|
||||
|
@ -55,12 +50,15 @@
|
|||
(format stream "~a</~a>~%" spaces tag-name)))))
|
||||
|
||||
(defmacro xml (form)
|
||||
"Macro interface to SEXP-TO-XML."
|
||||
(sexp-to-xml form 0))
|
||||
|
||||
(defun sexp-to-xml-string (form)
|
||||
"Captures the output of SEXP-TO-XML into a string."
|
||||
(with-open-stream (stream (make-string-output-stream))
|
||||
(sexp-to-xml form 0 stream)
|
||||
(get-output-stream-string stream)))
|
||||
|
||||
(defmacro xml-string (form)
|
||||
"Macro interface to SEXP-TO-XML-STRING."
|
||||
(sexp-to-xml-string form))
|
||||
|
|
Loading…
Reference in New Issue