emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Converting a string to valid XHTML id?


From: Lennart Borgman
Subject: Re: Converting a string to valid XHTML id?
Date: Wed, 1 Dec 2010 15:53:46 +0100

On Tue, Nov 30, 2010 at 3:50 PM, Ralf Mattes <address@hidden> wrote:
>
> But this is wrong - it'll possibly generate invalid html.
> Consider the following:
>
>  (org-newhtml-escape-id "this is cool!")
>
> ⇒ "this-is-cool-"
>
>  (org-newhtml-escape-id "this is cool?")
>
> ⇒ "this-is-cool-"
>
> collapsing two different strings to the same ID, resulting in
> invalid html.

Thanks Ralf, I thought it was a bit too much too handle, but here is a
new version that tries to handle this. (You might perhaps sometimes
want to set org-newhtml-escaped-ids to nil.)


(defvar org-newhtml-escaped-ids nil)
(make-variable-buffer-local 'org-newhtml-escaped-ids)

(defun org-newhtml-escape-id (id)
  "Return a valid xhtml id attribute string.
See URL `http://xhtml.com/en/xhtml/reference/attribute-data-types/#id'.

Try to make this unique.  Note that this cannot be done unless we
know all used ids since the resulting string might be an already
used id."
  (let ((old (assoc id org-newhtml-escaped-ids))
        new-id)
    (if old
        (cdr old)
      (setq new-id (replace-regexp-in-string "\\`\\([^A-Za-z]\\)"
"ANON-\\1" id nil))
      (setq new-id (replace-regexp-in-string "[^A-Za-z0-9_.-]" "-" new-id t))
      (setq old t)
      (while old
        (setq old (rassoc new-id org-newhtml-escaped-ids))
        (when old
          (setq new-id (concat new-id "X"))))
      (push (cons id new-id) org-newhtml-escaped-ids)
      new-id)))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]