-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: centralize slugify in utils (#30) #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+95
−25
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d13b071
refactor: centralize slugify in utils (#30)
clean6378-max-it c5db2b5
fix(export): default empty slugify slugs to session/project (#30)
clean6378-max-it f94bde7
refactor(slugify): default= kwarg, caller parity, review feedback (#30)
clean6378-max-it fd88ba4
removed unused import
clean6378-max-it File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| """Regression tests for utils.slugify (Issue #30 / CCC8). | ||
|
|
||
| Historically ``scripts/export.py`` used ``isalnum()`` (Unicode letters preserved) | ||
| while ``api/export_api.py`` used ASCII-only ``[^a-z0-9]+``. The canonical | ||
| implementation matches the API for portable zip / download filenames. | ||
| """ | ||
|
|
||
| import os | ||
|
|
||
| from utils.slugify import slugify | ||
|
|
||
|
|
||
| def test_ascii_words_hyphenated(): | ||
| assert slugify("Hello World") == "hello-world" | ||
|
|
||
|
|
||
| def test_punctuation_collapses_to_single_hyphen(): | ||
| assert slugify("foo__bar") == "foo-bar" | ||
| assert slugify("a.b.c") == "a-b-c" | ||
|
|
||
|
|
||
| def test_unicode_letters_become_ascii_safe(): | ||
| """Old CLI kept Latin-1 letters (e.g. é); canonical slug strips to ASCII.""" | ||
| assert slugify("Café noir") == "caf-noir" | ||
|
|
||
|
|
||
| def test_empty_after_strip(): | ||
| assert slugify("!!!") == "" | ||
|
|
||
|
|
||
| def test_digits_preserved(): | ||
| assert slugify("Issue 42 Fix") == "issue-42-fix" | ||
|
|
||
|
|
||
| def test_punctuation_examples_match_regex_behavior(): | ||
| assert slugify("AT&T") == "at-t" | ||
| assert slugify("issue#42") == "issue-42" | ||
|
|
||
|
|
||
| def test_default_used_when_slug_empty(): | ||
| assert slugify("!!!", default="session") == "session" | ||
| assert slugify("!!!") == "" | ||
|
|
||
|
|
||
| def test_export_leaf_path_parity_api_zip_vs_cli(): | ||
| """Same session inputs → same ``proj_slug``, ``title_slug``, and file leaf as API vs CLI.""" | ||
| title = "Issue #42: AT&T" | ||
| project = "Foo/Bar!" | ||
| sid = "a1b2c3d4-e5f6-7890-abcd-ef1234567890" | ||
| ts_file = "2026-05-07T12-00-00" | ||
| short_id = sid[:8] | ||
| title_slug = slugify(title, default="session") | ||
| proj_slug = slugify(project, default="project") | ||
| leaf_md = f"{ts_file}__{title_slug}__{short_id}.md" | ||
| api_zip_inner = f"{proj_slug}/{leaf_md}" | ||
| date_str = ts_file[:10] | ||
| cli_rel = os.path.join(date_str, proj_slug, leaf_md) | ||
| assert api_zip_inner.endswith(leaf_md) | ||
| assert os.path.basename(cli_rel) == leaf_md | ||
| assert cli_rel.replace("\\", "/").endswith(f"{proj_slug}/{leaf_md}") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| """Filesystem- and URL-safe slugs for export paths and download names. | ||
|
|
||
| Uses ASCII letters and digits only; other characters (including Unicode | ||
| letters and punctuation) become hyphen runs, then trimmed. Matches the | ||
| historical behavior of ``api/export_api.py`` and avoids platform-specific | ||
| issues with non-ASCII paths inside zip archives. | ||
| """ | ||
|
|
||
| import re | ||
|
|
||
|
|
||
| def slugify(text: str, *, default: str = "") -> str: | ||
| """Lowercase *text* and replace each run of non-[a-z0-9] with one hyphen. | ||
|
|
||
| After stripping leading/trailing hyphens, returns that string; if it is | ||
| empty, returns *default*. Export code passes ``default="session"`` or | ||
| ``default="project"``. | ||
| Examples (handled by the ``[^a-z0-9]+`` substitution below): | ||
|
|
||
| - ``AT&T`` → ``at-t`` | ||
| - ``issue#42`` → ``issue-42`` | ||
| """ | ||
| text = text.lower() | ||
| # Non-ASCII-alphanumeric runs → '-'; e.g. AT&T → at-t, issue#42 → issue-42. | ||
| text = re.sub(r"[^a-z0-9]+", "-", text) | ||
|
clean6378-max-it marked this conversation as resolved.
|
||
| stripped = text.strip("-") | ||
| return stripped if stripped else default | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.