made slugs unicode-safe like

used mozilla's slugify implementation as reference:
https://github.com/mozilla/unicode-slugify/blob/master/slugify/__init__.py
This commit is contained in:
lukasepple 2014-12-06 19:51:06 +01:00
parent 1d5b9117e2
commit fbacd3b81f
2 changed files with 16 additions and 8 deletions

View file

@ -13,7 +13,8 @@
:inferior-shell :inferior-shell
:cl-fad :cl-fad
:cl-ppcre :cl-ppcre
:closer-mop) :closer-mop
:cl-unicode)
:serial t :serial t
:components ((:file "packages") :components ((:file "packages")
(:file "util") (:file "util")

View file

@ -20,18 +20,25 @@
"Test if the slugs for tag A and B are equal." "Test if the slugs for tag A and B are equal."
(string= (tag-slug a) (tag-slug b))) (string= (tag-slug a) (tag-slug b)))
;; Slugs ; Slugs
(defun slug-char-p (char) (defun slug-char-p (char &key (allowed-chars (list #\- #\Space #\~)))
"Determine if CHAR is a valid slug (i.e. URL) character." "Determine if CHAR is a valid slug (i.e. URL) character."
(or (char<= #\0 char #\9) ; use the first char of the general unicode category as kind of
(char<= #\a char #\z) ; hyper general category
(char<= #\A char #\Z) (let ((cat (char (cl-unicode:general-category char) 0))
(member char '(#\_ #\-)))) (allowed-cats (list #\L #\N)))
(cond
((member cat allowed-cats) 't)
((member char allowed-chars) 't)
(t 'nil))))
(defun unicode-space-p (char)
(equal (char (cl-unicode:general-category char) 0) #\Z))
(defun slugify (string) (defun slugify (string)
"Return a version of STRING suitable for use as a URL." "Return a version of STRING suitable for use as a URL."
(remove-if-not #'slug-char-p (substitute #\- #\Space string))) (remove-if-not #'slug-char-p (substitute-if #\- #'unicode-space-p string)))
;; Content Types ;; Content Types