Improve slugify function, add tests, and enhance Makefile
Applied improvements from feature-code-quality branch: - Refactored slugify() for better readability with inline comments - Renamed log() parameter *args to *log_args (avoid shadowing built-in) - Added comprehensive unit tests for slugify function - Enhanced Makefile with typecheck and test targets - Updated build target to run type checks and tests before Docker build All tests pass and type checking succeeds.
This commit is contained in:
parent
13d3d653de
commit
3297ce5e67
4 changed files with 38 additions and 14 deletions
23
Makefile
23
Makefile
|
|
@ -1,15 +1,28 @@
|
||||||
# Makefile
|
# Makefile
|
||||||
|
|
||||||
# build target should be built even if no files depend on it
|
# declare phony targets
|
||||||
.PHONY: build
|
.PHONY: build test typecheck
|
||||||
|
|
||||||
# run locally
|
# run locally
|
||||||
site:
|
site:
|
||||||
uv run python makesite.py --params params-local.json
|
uv run python makesite.py --params params-local.json
|
||||||
cd _site && python -m SimpleHTTPServer 2> /dev/null || python3 -m http.server
|
cd _site && python -m SimpleHTTPServer 2> /dev/null || python3 -m http.server
|
||||||
|
|
||||||
|
# run type checks
|
||||||
|
typecheck:
|
||||||
|
uv run mypy makesite.py monitor.py
|
||||||
|
|
||||||
|
# run unit tests
|
||||||
|
test:
|
||||||
|
uv run python -m unittest discover test -v
|
||||||
|
|
||||||
# docker build image
|
# docker build image
|
||||||
build:
|
build:
|
||||||
|
@echo "Running type checks..."
|
||||||
|
uv run mypy makesite.py monitor.py
|
||||||
|
@echo "Running tests..."
|
||||||
|
uv run python -m unittest discover test -v
|
||||||
|
@echo "Building Docker image..."
|
||||||
docker build -t source.madyanne.fr/yax/blog .
|
docker build -t source.madyanne.fr/yax/blog .
|
||||||
|
|
||||||
# docker publish image
|
# docker publish image
|
||||||
|
|
|
||||||
15
makesite.py
15
makesite.py
|
|
@ -66,9 +66,9 @@ def fwrite(filename, text):
|
||||||
f.write(text)
|
f.write(text)
|
||||||
|
|
||||||
|
|
||||||
def log(msg, *args):
|
def log(msg, *log_args):
|
||||||
"""Log message with specified arguments."""
|
"""Log message with specified arguments."""
|
||||||
sys.stderr.write(msg.format(*args) + "\n")
|
sys.stderr.write(msg.format(*log_args) + "\n")
|
||||||
|
|
||||||
|
|
||||||
def _strip_tags_and_truncate(text, words=25):
|
def _strip_tags_and_truncate(text, words=25):
|
||||||
|
|
@ -98,13 +98,10 @@ def slugify(value):
|
||||||
underscores) and converts spaces to hyphens. Also strips leading and
|
underscores) and converts spaces to hyphens. Also strips leading and
|
||||||
trailing whitespace.
|
trailing whitespace.
|
||||||
"""
|
"""
|
||||||
value = (
|
value = unicodedata.normalize("NFKD", value).encode("ascii", "ignore").decode("ascii")
|
||||||
unicodedata.normalize("NFKD", value)
|
value = re.sub(r"[^\w\s-]", "", value) # Remove non-word characters and spaces
|
||||||
.encode("ascii", "ignore")
|
value = re.sub(r"\s+", "-", value) # Replace multiple spaces with a single hyphen
|
||||||
.decode("ascii")
|
return value.lower() # Convert to lowercase
|
||||||
)
|
|
||||||
value = re.sub(r"[^\w\s-]", "", value).strip().lower()
|
|
||||||
return re.sub(r"[-\s]+", "-", value)
|
|
||||||
|
|
||||||
|
|
||||||
def parse_post_file(filename, params):
|
def parse_post_file(filename, params):
|
||||||
|
|
|
||||||
1
test/__init__.py
Normal file
1
test/__init__.py
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
# Test package
|
||||||
13
test/test_slugify.py
Normal file
13
test/test_slugify.py
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
import unittest
|
||||||
|
import makesite
|
||||||
|
|
||||||
|
|
||||||
|
class SlugifyTest(unittest.TestCase):
|
||||||
|
|
||||||
|
def test_slugify(self):
|
||||||
|
self.assertEqual(makesite.slugify('NginX est brillant'), 'nginx-est-brillant')
|
||||||
|
self.assertEqual(makesite.slugify('Bilan hébergement 2023'), 'bilan-hebergement-2023')
|
||||||
|
self.assertEqual(makesite.slugify('Sécurisation Docker : des pistes'), 'securisation-docker-des-pistes')
|
||||||
|
self.assertEqual(makesite.slugify('Il court, il court, le furet'), 'il-court-il-court-le-furet')
|
||||||
|
self.assertEqual(makesite.slugify('De GNU/Linux à gnuSystemlinuxdGnomeOs'), 'de-gnulinux-a-gnusystemlinuxdgnomeos')
|
||||||
|
self.assertEqual(makesite.slugify('Au fait... mon téléphone'), 'au-fait-mon-telephone')
|
||||||
Loading…
Add table
Add a link
Reference in a new issue