Function: htmlize-protect-string

htmlize-protect-string is a byte-compiled function defined in htmlize.el.

Signature

(htmlize-protect-string STRING)

Documentation

HTML-protect string, escaping HTML metacharacters and I18N chars.

Source Code

;; Defined in ~/.emacs.d/elpa/htmlize-20250724.1703/htmlize.el
(defun htmlize-protect-string (string)
  "HTML-protect string, escaping HTML metacharacters and I18N chars."
  ;; Only protecting strings that actually contain unsafe or non-ASCII
  ;; chars removes a lot of unnecessary funcalls and consing.
  (if (not (string-match "[^\r\n\t -%'-;=?-~]" string))
      string
    (mapconcat (lambda (char)
                 (cond
                  ((< char 128)
                   ;; ASCII: use htmlize-basic-character-table.
                   (aref htmlize-basic-character-table char))
                  ((gethash char htmlize-extended-character-cache)
                   ;; We've already seen this char; return the cached
                   ;; string.
                   )
                  ((not htmlize-convert-nonascii-to-entities)
                   ;; If conversion to entities is not desired, always
                   ;; copy the char literally.
                   (setf (gethash char htmlize-extended-character-cache)
                         (char-to-string char)))
                  ((< char 256)
                   ;; Latin 1: no need to call encode-char.
                   (setf (gethash char htmlize-extended-character-cache)
                         (format "&#%d;" char)))
                  ((encode-char char 'ucs)
                   ;; Must check if encode-char works for CHAR;
                   ;; it fails for Arabic and possibly elsewhere.
                   (setf (gethash char htmlize-extended-character-cache)
                         (format "&#%d;" (encode-char char 'ucs))))
                  (t
                   ;; encode-char doesn't work for this char.  Copy it
                   ;; unchanged and hope for the best.
                   (setf (gethash char htmlize-extended-character-cache)
                         (char-to-string char)))))
               string "")))