Skip to content

Commit 20aa278

Browse files
committed
Simplify slugify implementations
1 parent 653ad58 commit 20aa278

File tree

2 files changed

+7
-21
lines changed

2 files changed

+7
-21
lines changed

examples/deepzoom/deepzoom_server.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# deepzoom_server - Example web application for serving whole-slide images
44
#
5-
# Copyright (c) 2010-2011 Carnegie Mellon University
5+
# Copyright (c) 2010-2013 Carnegie Mellon University
66
#
77
# This library is free software; you can redistribute it and/or modify it
88
# under the terms of version 2.1 of the GNU Lesser General Public License
@@ -102,16 +102,9 @@ def tile(slug, level, col, row, format):
102102
return resp
103103

104104

105-
# Based on Flask snippet 5
106-
_punct_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')
107-
def slugify(text, delim=u'-'):
108-
"""Generates an ASCII-only slug."""
109-
result = []
110-
for word in _punct_re.split(unicode(text, 'UTF-8').lower()):
111-
word = normalize('NFKD', word).encode('ascii', 'ignore')
112-
if word:
113-
result.append(word)
114-
return unicode(delim.join(result))
105+
def slugify(text):
106+
text = normalize('NFKD', text.lower()).encode('ascii', 'ignore').decode()
107+
return re.sub('[^a-z0-9]+', '-', text)
115108

116109

117110
if __name__ == '__main__':

examples/deepzoom/deepzoom_tile.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
# deepzoom_tile - Convert whole-slide images to Deep Zoom format
44
#
5-
# Copyright (c) 2010-2011 Carnegie Mellon University
5+
# Copyright (c) 2010-2013 Carnegie Mellon University
66
#
77
# This library is free software; you can redistribute it and/or modify it
88
# under the terms of version 2.1 of the GNU Lesser General Public License
@@ -206,17 +206,10 @@ def _copydir(self, src, dest):
206206
if os.path.isfile(srcpath):
207207
shutil.copy(srcpath, os.path.join(dest, name))
208208

209-
_punct_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')
210209
@classmethod
211210
def _slugify(cls, text):
212-
"""Generates an ASCII-only slug."""
213-
# Based on Flask snippet 5
214-
result = []
215-
for word in cls._punct_re.split(unicode(text, 'UTF-8').lower()):
216-
word = normalize('NFKD', word).encode('ascii', 'ignore')
217-
if word:
218-
result.append(word)
219-
return unicode(u'_'.join(result))
211+
text = normalize('NFKD', text.lower()).encode('ascii', 'ignore').decode()
212+
return re.sub('[^a-z0-9]+', '_', text)
220213

221214
def _shutdown(self):
222215
for _i in range(self._workers):

0 commit comments

Comments
 (0)