Function: ENTITIFY

Documentation

Converts OBJECT to its string representation, if necessary, and then replaces the characters of OBJECT with their corresponding entities. Assumes that the characters of RESERVED have been registered in the entity table.

Source

(defun entitify (object)
  "Converts OBJECT to its string representation, if necessary, and then replaces 
the characters of OBJECT with their corresponding entities. Assumes that the 
characters of RESERVED have been registered in the entity table."
  (let ((str (typecase object
               (string (coerce object 'simple-base-string))
               (t (format nil "~A" object)))))
    (declare (type simple-base-string str)) ;; mollify sbcl compiler
    (loop with stream = (make-string-output-stream)
          for c across str
          for ent = (entity-of c)
          for code = (char-code c)
          do (cond
               (ent
                (progn
                  (write-char #\& stream)
                  (write-string ent stream)))
               ((and (or (< code 32) (> code 126))
                     (not (= code 10))
                     (not (= code 9)))
                (format stream "&#x~x;" code))
               (t
                (write-char c stream)))
          finally (return (get-output-stream-string stream)))))
Source Context