From 29943cdb9a76d60664445627208499de19ed0dfe Mon Sep 17 00:00:00 2001 From: Fardeen Khan Date: Wed, 21 Oct 2026 20:04:55 +0530 Subject: [PATCH] Add slugify command --- fixit/cli.py | 7 ++++++- fixit/textutils.py | 14 ++++++++++++++ tests/test_textutils.py | 9 ++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/fixit/cli.py b/fixit/cli.py index a91820f..e0ed4d1 100644 --- a/fixit/cli.py +++ b/fixit/cli.py @@ -56,6 +56,8 @@ def run_palindrome(args): def run_reverse(args): print(textutils.reverse_words(args.text)) +def run_slugify(args): + print(textutils.slugify(args.text)) COMMAND_TABLE = { "wc": run_wc, "convert": run_convert, @@ -64,6 +66,7 @@ def run_reverse(args): "json": run_json, "palindrome": run_palindrome, "reverse": run_reverse, + "slugify": run_slugify, } def build_parser() -> argparse.ArgumentParser: @@ -103,9 +106,11 @@ def build_parser() -> argparse.ArgumentParser: rw = sub.add_parser("reverse", help="reverse the words in a sentence") rw.add_argument("text") - return parser + sg = sub.add_parser("slugify", help="convert text into a URL-friendly slug") + sg.add_argument("text") + return parser def main(argv=None) -> int: """Run a fixit command and return its exit status. diff --git a/fixit/textutils.py b/fixit/textutils.py index ce615a0..8b0a871 100644 --- a/fixit/textutils.py +++ b/fixit/textutils.py @@ -18,3 +18,17 @@ def reverse_words(text: str) -> str: reverse_words("hello world") returns "world hello". """ return " ".join(reversed(text.split())) + + +def slugify(text: str) -> str: + """Convert text into a URL-friendly slug. + + Example: + slugify("Hello World!") returns "hello-world". + """ + import re + + text = text.lower() + text = re.sub(r"[^\w\s-]", "", text) + text = re.sub(r"[\s_-]+", "-", text) + return text.strip("-") \ No newline at end of file diff --git a/tests/test_textutils.py b/tests/test_textutils.py index a51a607..d1684fc 100644 --- a/tests/test_textutils.py +++ b/tests/test_textutils.py @@ -1,5 +1,5 @@ import pytest -from fixit.textutils import is_palindrome, reverse_words +from fixit.textutils import is_palindrome, reverse_words, slugify def test_simple_palindrome(): @@ -18,3 +18,10 @@ def test_palindrome_ignores_punctuation(): def test_reverse_words_extra_spaces(): assert reverse_words("hello world") == "world hello" + +def test_slugify_basic(): + assert slugify("Hello World!") == "hello-world" + + +def test_slugify_punctuation_and_spaces(): + assert slugify("Open Source, Project!") == "open-source-project" \ No newline at end of file