-
Notifications
You must be signed in to change notification settings - Fork 3
/
slugify.py
43 lines (35 loc) · 1.99 KB
/
slugify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
###############################################################################
# #
# Adds "slugify" support to parse2plone, which means that if a path like this #
# is discovered: #
# #
# /2000/01/01/foo/index.html #
# #
# And --slugify is called, then instead of creating /2000/01/01/foo/index.html#
# (in Plone), parse2plone will create: #
# #
# /foo-20000101.html #
# #
# thereby "slugifying" the content, if you will. #
# #
###############################################################################
from re import compile
slug = compile('(\d\d\d\d)/(\d\d)/(\d\d)/(.+)/index.html')
def convert_path_to_slug(files, slug_map, base):
"""
Returns a slug_map which is forward/reverse mapping of paths to slugified
paths and vice versa. E.g.:
slug_map{'forward': {'/var/www/html/2000/01/01/foo/index.html':
'/var/www/html/foo-20000101.html'}}
slug_map{'reverse': {'/var/www/html/foo-20000101.html':
'/var/www/html/2000/01/01/foo/index.html'}}
"""
for f in files[base]:
result = slug.search(f)
if result:
groups = result.groups()
slugfile = '%s-%s%s%s.html' % (groups[3], groups[0], groups[1],
groups[2])
slug_map['forward'][f] = slugfile
slug_map['reverse'][slugfile] = f
return slug_map