metadata-utils 1.0.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.
- metadata_utils/__init__.py +39 -0
- metadata_utils/py.typed +0 -0
- metadata_utils-1.0.0.dist-info/METADATA +69 -0
- metadata_utils-1.0.0.dist-info/RECORD +7 -0
- metadata_utils-1.0.0.dist-info/WHEEL +5 -0
- metadata_utils-1.0.0.dist-info/licenses/LICENSE +20 -0
- metadata_utils-1.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# stdlib
|
|
2
|
+
import unicodedata
|
|
3
|
+
from typing import Dict
|
|
4
|
+
from xml.sax.saxutils import escape
|
|
5
|
+
from xml.sax.saxutils import unescape
|
|
6
|
+
|
|
7
|
+
__VERSION__ = "1.0.0"
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
# ==============================================================================
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
# https://wiki.python.org/moin/EscapingHtml
|
|
14
|
+
# escape() and unescape() takes care of "&" , "<" and ">"
|
|
15
|
+
# we need to handle quotes, so we don't break things
|
|
16
|
+
html_attribute_escape_table: Dict[str, str] = {'"': """, "'": "'"}
|
|
17
|
+
html_attribute_unescape_table: Dict[str, str] = {
|
|
18
|
+
v: k for (k, v) in html_attribute_escape_table.items()
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def html_attribute_escape(text: str) -> str:
|
|
23
|
+
"""Escape text for insertion into a quoted HTML attribute."""
|
|
24
|
+
return escape(text, html_attribute_escape_table)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def html_attribute_unescape(text: str) -> str:
|
|
28
|
+
"""Decode the five named entities emitted by html_attribute_escape."""
|
|
29
|
+
return unescape(text, html_attribute_unescape_table)
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def force_clean_ascii_NFKD(text: str) -> bytes:
|
|
33
|
+
"""Apply NFKD, then return ASCII bytes, dropping non-ASCII characters."""
|
|
34
|
+
return unicodedata.normalize("NFKD", text).encode("ascii", "ignore")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def force_clean_ascii_NFKC(text: str) -> bytes:
|
|
38
|
+
"""Apply NFKC, then return ASCII bytes, dropping non-ASCII characters."""
|
|
39
|
+
return unicodedata.normalize("NFKC", text).encode("ascii", "ignore")
|
metadata_utils/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: metadata_utils
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Lightweight Metadata Support
|
|
5
|
+
Home-page: https://github.com/jvanasco/metadata_utils
|
|
6
|
+
Author: Jonathan Vanasco
|
|
7
|
+
Author-email: jvanasco@2xlp.com
|
|
8
|
+
License: MIT
|
|
9
|
+
Keywords: web
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.6
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
22
|
+
Requires-Python: >=3.6
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Provides-Extra: testing
|
|
26
|
+
Requires-Dist: pytest; extra == "testing"
|
|
27
|
+
Requires-Dist: mypy; extra == "testing"
|
|
28
|
+
Dynamic: license-file
|
|
29
|
+
|
|
30
|
+

|
|
31
|
+
|
|
32
|
+
metadata_utils
|
|
33
|
+
==============
|
|
34
|
+
|
|
35
|
+
This package offers lightweight tools for dealing with metadata in HTML.
|
|
36
|
+
|
|
37
|
+
## Quoted HTML attributes
|
|
38
|
+
|
|
39
|
+
Use `html_attribute_escape` on text before inserting it into a quoted HTML
|
|
40
|
+
attribute.
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
from metadata_utils import html_attribute_escape, html_attribute_unescape
|
|
44
|
+
|
|
45
|
+
title = 'Fish & "Chips"'
|
|
46
|
+
escaped = html_attribute_escape(title)
|
|
47
|
+
assert f'<span title="{escaped}"></span>' == (
|
|
48
|
+
'<span title="Fish & "Chips""></span>'
|
|
49
|
+
)
|
|
50
|
+
assert html_attribute_unescape(escaped) == title
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
`html_attribute_unescape` decodes one layer of the five named entities emitted
|
|
54
|
+
by the escape function (`&`, `<`, `>`, `"`, and `'`).
|
|
55
|
+
Other named and numeric references pass through unchanged.
|
|
56
|
+
|
|
57
|
+
## ASCII normalization
|
|
58
|
+
|
|
59
|
+
Both helpers apply Unicode normalization, drop any remaining non-ASCII
|
|
60
|
+
characters, and return `bytes`. Both expand the `fi` ligature into `fi`.
|
|
61
|
+
NFKD decomposes `é` so the ASCII `e` survives. NFKC keeps the composed
|
|
62
|
+
`é`, which is dropped during ASCII encoding.
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
from metadata_utils import force_clean_ascii_NFKC, force_clean_ascii_NFKD
|
|
66
|
+
|
|
67
|
+
assert force_clean_ascii_NFKD("fi café") == b"fi cafe"
|
|
68
|
+
assert force_clean_ascii_NFKC("fi café") == b"fi caf"
|
|
69
|
+
```
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
metadata_utils/__init__.py,sha256=4RQRccBLVi4xSRzE4SajOnML1f9DRdrKg4CFLp46kE4,1326
|
|
2
|
+
metadata_utils/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
+
metadata_utils-1.0.0.dist-info/licenses/LICENSE,sha256=We9VXp3Rba9vYbeWWEX6B5IX7E51lMXnerTQm7O64s0,1105
|
|
4
|
+
metadata_utils-1.0.0.dist-info/METADATA,sha256=JE2Kl76qdn3sQSBOiubWl8xyKuUxrllL8okotuipe4s,2377
|
|
5
|
+
metadata_utils-1.0.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
6
|
+
metadata_utils-1.0.0.dist-info/top_level.txt,sha256=856ves0QhFzSuRhgtFZwDp6O6XT1_Mxq8f2wE0NAkpE,15
|
|
7
|
+
metadata_utils-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2013 "jonathan vanasco <jonathan@2xlp.com>"
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
|
7
|
+
the Software without restriction, including without limitation the rights to
|
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
10
|
+
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, FITNESS
|
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
metadata_utils
|