slugread 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
slugread/__init__.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""Tiny text helpers: slugify, truncate, word_count, reading_time."""
|
|
2
|
+
|
|
3
|
+
import math
|
|
4
|
+
import re
|
|
5
|
+
import unicodedata
|
|
6
|
+
|
|
7
|
+
__all__ = ["slugify", "truncate", "word_count", "reading_time"]
|
|
8
|
+
__version__ = "0.1.0"
|
|
9
|
+
|
|
10
|
+
_MARKS = re.compile("[\u0300-\u036f]")
|
|
11
|
+
_NON_ALNUM = re.compile("[^a-z0-9]+")
|
|
12
|
+
# Letters NFD can't split into base + accent: ß æ ø œ đ ł.
|
|
13
|
+
_TRANSLIT = str.maketrans(
|
|
14
|
+
{"ß": "ss", "æ": "ae", "ø": "o", "œ": "oe", "đ": "d", "ł": "l"}
|
|
15
|
+
)
|
|
16
|
+
# Same whitespace set as Rust's char::is_whitespace, so all languages split identically.
|
|
17
|
+
_WS = re.compile("[\t-\r \x85\xa0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000]+")
|
|
18
|
+
_CJK = re.compile(
|
|
19
|
+
"[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\U00020000-\U0002ffff]"
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def slugify(text: str) -> str:
|
|
24
|
+
"""Lowercase, strip accents (ß→ss, æ→ae, ø→o, œ→oe, đ→d, ł→l), turn every run
|
|
25
|
+
of non [a-z0-9] into '-', trim '-'."""
|
|
26
|
+
text = _MARKS.sub("", unicodedata.normalize("NFD", text.lower())).translate(_TRANSLIT)
|
|
27
|
+
return _NON_ALNUM.sub("-", text).strip("-")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def truncate(text: str, n: int) -> str:
|
|
31
|
+
"""Cut to n characters and append an ellipsis if anything was cut."""
|
|
32
|
+
if n < 0:
|
|
33
|
+
raise ValueError("n must be >= 0")
|
|
34
|
+
return text[:n] + "\u2026" if len(text) > n else text
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def word_count(text: str) -> int:
|
|
38
|
+
"""Count words; each CJK character (Han, Hiragana, Katakana) counts as one."""
|
|
39
|
+
cjk = len(_CJK.findall(text))
|
|
40
|
+
rest = _WS.split(_CJK.sub(" ", text))
|
|
41
|
+
return cjk + sum(any(c.isalnum() for c in w) for w in rest)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def reading_time(text: str, wpm: int = 200) -> int:
|
|
45
|
+
"""Minutes to read text at wpm words per minute, rounded up."""
|
|
46
|
+
if wpm <= 0:
|
|
47
|
+
raise ValueError("wpm must be > 0")
|
|
48
|
+
return math.ceil(word_count(text) / wpm)
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: slugread
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Tiny zero-dependency text helpers: slugify, truncate, word_count, reading_time.
|
|
5
|
+
Project-URL: Source, https://github.com/VinsOrl/_alg/tree/main/homework/slugread
|
|
6
|
+
Author: VinsOrl
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Keywords: reading-time,slug,slugify,truncate,word-count
|
|
10
|
+
Classifier: Operating System :: OS Independent
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Topic :: Text Processing
|
|
13
|
+
Requires-Python: >=3.9
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
# slugread
|
|
17
|
+
|
|
18
|
+
Tiny zero-dependency text helpers. Same behavior in Python, JavaScript and Rust.
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pip install slugread
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
from slugread import slugify, truncate, word_count, reading_time
|
|
26
|
+
|
|
27
|
+
slugify("Crème Brûlée!") # 'creme-brulee'
|
|
28
|
+
truncate("Hello world", 5) # 'Hello…'
|
|
29
|
+
word_count("我爱Rust和Python") # 5 (each CJK character is one word)
|
|
30
|
+
reading_time("one two three", 2) # 2 (minutes, rounded up; default wpm=200)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
- `slugify` keeps only `a-z0-9` after stripping accents (`ß`→`ss`, `æ`→`ae`, `ø`→`o`, `œ`→`oe`, `đ`→`d`, `ł`→`l`); other characters become `-`.
|
|
34
|
+
- `truncate` counts Unicode code points; `n` must be `>= 0`.
|
|
35
|
+
- `reading_time` needs `wpm > 0`.
|
|
36
|
+
|
|
37
|
+
MIT license.
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
slugread/__init__.py,sha256=0Zh7mZO-4l9pJ0I-Y0xiojVqxpxvFvP5IEe_w-4CD9I,1757
|
|
2
|
+
slugread-0.1.0.dist-info/METADATA,sha256=utwZ7a4M2gk_zR5VfkMJRXb-XrV6nUfGn88fREzC8ac,1264
|
|
3
|
+
slugread-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
4
|
+
slugread-0.1.0.dist-info/licenses/LICENSE,sha256=l3ylGIkEY2vV3PzTZcvtaL1utu8ZrQiaJ1bTN625ghE,1064
|
|
5
|
+
slugread-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 VinsOrl
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|