Skip to content

Commit f1c1fae

Browse files
committed
Close #108: Allow for leading and trailing hyphens in slugs.
1 parent 711e061 commit f1c1fae

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

logya/util.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,11 @@ def paths(dir_site: str) -> Paths:
5959

6060
@lru_cache(maxsize=256)
6161
def slugify(s: str) -> str:
62-
"""Return string with forbidden characters replaced with hyphens.
62+
"""Return string with forbidden characters replaced with hyphens. Different input strings may result in the same output.
6363
64-
Consecutive forbidden characters are replaced with a single hyphen.
65-
Leading and trailing whitespace and hyphens are stripped.
66-
Different input strings may result in the same output.
64+
Consecutive forbidden characters are replaced with a single hyphen. Leading and trailing whitespace is stripped.
6765
"""
68-
return re.sub(re_forbidden, '-', s.strip()).strip('-')
66+
return re.sub(re_forbidden, '#', s.strip()).strip('#').replace('#', '-')
6967

7068

7169
def latest_file_change(root: str):
@@ -84,9 +82,9 @@ def walk_dir(path):
8482
for entry in walk_dir(root):
8583
try:
8684
mtime = entry.stat().st_mtime
87-
except FileNotFoundError: # skip race condition
85+
except FileNotFoundError: # skip race condition
8886
continue
8987
if mtime > latest_mtime:
9088
latest_mtime = mtime
9189

92-
return latest_mtime
90+
return latest_mtime

tests/test_util.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ def test_slugify():
1919
('multiple spaces', 'multiple-spaces'),
2020
('c♯dim', 'c♯dim'),
2121
('_85QotzbzHY', '_85QotzbzHY'),
22+
('-08LooL_3Gw', '-08LooL_3Gw'),
23+
('c++', 'c++'),
24+
('c--', 'c--'),
25+
('#hash#', 'hash'),
2226
('dot.dot', 'dot.dot'),
27+
('@username', '@username'),
2328
]:
2429
assert logya.util.slugify(value) == expected
2530

0 commit comments

Comments
 (0)