md2linkedin 0.5.2__tar.gz → 0.7.0__tar.gz
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.
- {md2linkedin-0.5.2 → md2linkedin-0.7.0}/PKG-INFO +30 -15
- {md2linkedin-0.5.2 → md2linkedin-0.7.0}/README.md +28 -13
- {md2linkedin-0.5.2 → md2linkedin-0.7.0}/pyproject.toml +45 -17
- {md2linkedin-0.5.2 → md2linkedin-0.7.0}/pyproject.toml.orig +47 -16
- {md2linkedin-0.5.2 → md2linkedin-0.7.0}/src/md2linkedin/__init__.py +5 -1
- {md2linkedin-0.5.2 → md2linkedin-0.7.0}/src/md2linkedin/_converter.py +189 -54
- {md2linkedin-0.5.2 → md2linkedin-0.7.0}/src/md2linkedin/_unicode.py +96 -23
- {md2linkedin-0.5.2 → md2linkedin-0.7.0}/LICENSE.md +0 -0
- {md2linkedin-0.5.2 → md2linkedin-0.7.0}/src/md2linkedin/_cli.py +0 -0
- {md2linkedin-0.5.2 → md2linkedin-0.7.0}/src/md2linkedin/py.typed +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: md2linkedin
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.7.0
|
|
4
4
|
Summary: Convert Markdown to LinkedIn-friendly Unicode text
|
|
5
5
|
Keywords: markdown,linkedin,unicode,formatting,conversion
|
|
6
6
|
Author: Indrajeet Patil
|
|
@@ -20,7 +20,7 @@ Classifier: Programming Language :: Python :: 3 :: Only
|
|
|
20
20
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
21
|
Classifier: Topic :: Text Processing :: Markup
|
|
22
22
|
Classifier: Topic :: Utilities
|
|
23
|
-
Requires-Dist: click>=8.5.
|
|
23
|
+
Requires-Dist: click>=8.5.1
|
|
24
24
|
Requires-Dist: comrak>=0.0.16
|
|
25
25
|
Requires-Dist: selectolax>=1.0.2
|
|
26
26
|
Requires-Python: >=3.12
|
|
@@ -42,14 +42,29 @@ replacing bold, italic, and bold-italic markers with Unicode Mathematical
|
|
|
42
42
|
Sans-Serif characters. This preserves visual formatting when pasting into
|
|
43
43
|
platforms like LinkedIn that do not support Markdown natively.
|
|
44
44
|
|
|
45
|
+
## Use it in your browser
|
|
46
|
+
|
|
47
|
+
Just want to write a post? The
|
|
48
|
+
[LinkedIn post formatter](https://www.indrapatil.com/linkedin-post-formatter/)
|
|
49
|
+
runs `md2linkedin` in your browser, with nothing to install. Format your post
|
|
50
|
+
with a toolbar (bold, italics, headings, lists, and code), watch the LinkedIn
|
|
51
|
+
version update as you type, and copy it with one click. Your text never leaves
|
|
52
|
+
your device.
|
|
53
|
+
|
|
54
|
+
[](https://www.indrapatil.com/linkedin-post-formatter/)
|
|
55
|
+
|
|
56
|
+
Install the package when you want to convert Markdown files, script the
|
|
57
|
+
conversion, or call it from Python.
|
|
58
|
+
|
|
45
59
|
## Installation
|
|
46
60
|
|
|
47
61
|
```bash
|
|
48
62
|
uv add md2linkedin
|
|
49
63
|
```
|
|
50
64
|
|
|
51
|
-
> **Tip:** Run the CLI without installing it
|
|
52
|
-
> `uvx md2linkedin post.md`.
|
|
65
|
+
> **Tip:** Run the CLI without installing it through
|
|
66
|
+
> [`uvx`](https://docs.astral.sh/uv/guides/tools/): `uvx md2linkedin post.md`.
|
|
67
|
+
> Every CLI example below works the same way.
|
|
53
68
|
|
|
54
69
|
## Usage
|
|
55
70
|
|
|
@@ -131,24 +146,24 @@ md2linkedin post.md --no-monospace-code
|
|
|
131
146
|
- **HTML entities**: decoded (`&` → `&`, etc.)
|
|
132
147
|
- **Backslash escapes**: resolved (`\*` → `*`)
|
|
133
148
|
- **Windows line endings**: normalised automatically
|
|
134
|
-
- **
|
|
149
|
+
- **Accented letters**: styled like the letters around them (`**Grüße**` → 𝗚𝗿𝘂̈ß𝗲); `ß`, `ł` and other letters with no ASCII base stay plain
|
|
150
|
+
- **Greek**: bold and bold-italic in sans-serif (`**Ωμέγα**` → 𝝮𝝻𝝴́𝝲𝝰), italic in serif, the closest Unicode has; code stays plain
|
|
151
|
+
- **Emojis & other scripts**: pass through unchanged — no accidental corruption
|
|
135
152
|
|
|
136
153
|
## Limitations
|
|
137
154
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
- **Code blocks**: Monospace Unicode characters do not enforce true fixed-width alignment on proportional fonts (like LinkedIn's default font). As a result, indentation and column alignment in code blocks will often break visually.
|
|
141
|
-
- **Tables**: Markdown tables are flattened to one line per row, with a bold header and cells joined by ` | `. Nothing keeps the columns aligned on LinkedIn's proportional font, so wide tables are better rewritten as lists.
|
|
142
|
-
- **Accessibility**: Screen readers often read Unicode mathematical characters aloud individually (e.g., "mathematical sans-serif bold b") instead of as complete words, making the content difficult for visually impaired users to understand.
|
|
143
|
-
- **Searchability**: Text styled with these Unicode characters may not be indexed properly by LinkedIn's search algorithm, meaning people searching for your keywords might not find your post.
|
|
144
|
-
- **Markdown extensions**: Only CommonMark plus tables, strikethrough, autolinks and `==highlight==` are read. Task-list checkboxes stay as literal `[ ]`/`[x]` and footnotes are flattened (`text[^1]` → `text^1`); see [What Gets Transformed](https://www.indrapatil.com/md2linkedin/advanced/#what-gets-transformed-and-what-doesnt).
|
|
155
|
+
Styling relies on Unicode Mathematical Alphanumeric Symbols, which has trade-offs:
|
|
145
156
|
|
|
146
|
-
|
|
147
|
-
|
|
157
|
+
- **Other scripts**: Unicode has styled letters only for Latin, Greek, and digits, so text in every other script (Cyrillic, Arabic, Hebrew, Devanagari and other Indic scripts, Thai, Chinese, Japanese, Korean, …) stays plain inside bold, italic, headings, and code. md2linkedin cannot fix this: the characters it would need do not exist, and LinkedIn posts have no other way to carry formatting. `find_unstyled()` lists the affected words so you can warn about them. See [Scripts without styled letters](https://www.indrapatil.com/md2linkedin/advanced/#scripts-without-styled-letters).
|
|
158
|
+
- **Code blocks**: LinkedIn's proportional font does not keep monospace Unicode characters fixed-width, so indentation and column alignment often break.
|
|
159
|
+
- **Tables**: for the same reason, flattened table columns do not line up; rewrite wide tables as lists.
|
|
160
|
+
- **Accessibility**: screen readers often spell out each styled character (e.g. "mathematical sans-serif bold b") instead of reading whole words.
|
|
161
|
+
- **Searchability**: LinkedIn search may not index styled text, so people searching for those keywords might not find your post.
|
|
162
|
+
- **Markdown extensions**: only CommonMark plus tables, strikethrough, autolinks and `==highlight==` are read. Task-list checkboxes stay as literal `[ ]`/`[x]` and footnotes are flattened (`text[^1]` → `text^1`); see [What Gets Transformed](https://www.indrapatil.com/md2linkedin/advanced/#what-gets-transformed-and-what-doesnt).
|
|
148
163
|
|
|
149
164
|
## See Also
|
|
150
165
|
|
|
151
|
-
- [
|
|
166
|
+
- [Documentation](https://www.indrapatil.com/md2linkedin/) — advanced usage, the full conversion table, and the API reference.
|
|
152
167
|
|
|
153
168
|
## License
|
|
154
169
|
|
|
@@ -9,14 +9,29 @@ replacing bold, italic, and bold-italic markers with Unicode Mathematical
|
|
|
9
9
|
Sans-Serif characters. This preserves visual formatting when pasting into
|
|
10
10
|
platforms like LinkedIn that do not support Markdown natively.
|
|
11
11
|
|
|
12
|
+
## Use it in your browser
|
|
13
|
+
|
|
14
|
+
Just want to write a post? The
|
|
15
|
+
[LinkedIn post formatter](https://www.indrapatil.com/linkedin-post-formatter/)
|
|
16
|
+
runs `md2linkedin` in your browser, with nothing to install. Format your post
|
|
17
|
+
with a toolbar (bold, italics, headings, lists, and code), watch the LinkedIn
|
|
18
|
+
version update as you type, and copy it with one click. Your text never leaves
|
|
19
|
+
your device.
|
|
20
|
+
|
|
21
|
+
[](https://www.indrapatil.com/linkedin-post-formatter/)
|
|
22
|
+
|
|
23
|
+
Install the package when you want to convert Markdown files, script the
|
|
24
|
+
conversion, or call it from Python.
|
|
25
|
+
|
|
12
26
|
## Installation
|
|
13
27
|
|
|
14
28
|
```bash
|
|
15
29
|
uv add md2linkedin
|
|
16
30
|
```
|
|
17
31
|
|
|
18
|
-
> **Tip:** Run the CLI without installing it
|
|
19
|
-
> `uvx md2linkedin post.md`.
|
|
32
|
+
> **Tip:** Run the CLI without installing it through
|
|
33
|
+
> [`uvx`](https://docs.astral.sh/uv/guides/tools/): `uvx md2linkedin post.md`.
|
|
34
|
+
> Every CLI example below works the same way.
|
|
20
35
|
|
|
21
36
|
## Usage
|
|
22
37
|
|
|
@@ -98,24 +113,24 @@ md2linkedin post.md --no-monospace-code
|
|
|
98
113
|
- **HTML entities**: decoded (`&` → `&`, etc.)
|
|
99
114
|
- **Backslash escapes**: resolved (`\*` → `*`)
|
|
100
115
|
- **Windows line endings**: normalised automatically
|
|
101
|
-
- **
|
|
116
|
+
- **Accented letters**: styled like the letters around them (`**Grüße**` → 𝗚𝗿𝘂̈ß𝗲); `ß`, `ł` and other letters with no ASCII base stay plain
|
|
117
|
+
- **Greek**: bold and bold-italic in sans-serif (`**Ωμέγα**` → 𝝮𝝻𝝴́𝝲𝝰), italic in serif, the closest Unicode has; code stays plain
|
|
118
|
+
- **Emojis & other scripts**: pass through unchanged — no accidental corruption
|
|
102
119
|
|
|
103
120
|
## Limitations
|
|
104
121
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
- **Code blocks**: Monospace Unicode characters do not enforce true fixed-width alignment on proportional fonts (like LinkedIn's default font). As a result, indentation and column alignment in code blocks will often break visually.
|
|
108
|
-
- **Tables**: Markdown tables are flattened to one line per row, with a bold header and cells joined by ` | `. Nothing keeps the columns aligned on LinkedIn's proportional font, so wide tables are better rewritten as lists.
|
|
109
|
-
- **Accessibility**: Screen readers often read Unicode mathematical characters aloud individually (e.g., "mathematical sans-serif bold b") instead of as complete words, making the content difficult for visually impaired users to understand.
|
|
110
|
-
- **Searchability**: Text styled with these Unicode characters may not be indexed properly by LinkedIn's search algorithm, meaning people searching for your keywords might not find your post.
|
|
111
|
-
- **Markdown extensions**: Only CommonMark plus tables, strikethrough, autolinks and `==highlight==` are read. Task-list checkboxes stay as literal `[ ]`/`[x]` and footnotes are flattened (`text[^1]` → `text^1`); see [What Gets Transformed](https://www.indrapatil.com/md2linkedin/advanced/#what-gets-transformed-and-what-doesnt).
|
|
122
|
+
Styling relies on Unicode Mathematical Alphanumeric Symbols, which has trade-offs:
|
|
112
123
|
|
|
113
|
-
|
|
114
|
-
|
|
124
|
+
- **Other scripts**: Unicode has styled letters only for Latin, Greek, and digits, so text in every other script (Cyrillic, Arabic, Hebrew, Devanagari and other Indic scripts, Thai, Chinese, Japanese, Korean, …) stays plain inside bold, italic, headings, and code. md2linkedin cannot fix this: the characters it would need do not exist, and LinkedIn posts have no other way to carry formatting. `find_unstyled()` lists the affected words so you can warn about them. See [Scripts without styled letters](https://www.indrapatil.com/md2linkedin/advanced/#scripts-without-styled-letters).
|
|
125
|
+
- **Code blocks**: LinkedIn's proportional font does not keep monospace Unicode characters fixed-width, so indentation and column alignment often break.
|
|
126
|
+
- **Tables**: for the same reason, flattened table columns do not line up; rewrite wide tables as lists.
|
|
127
|
+
- **Accessibility**: screen readers often spell out each styled character (e.g. "mathematical sans-serif bold b") instead of reading whole words.
|
|
128
|
+
- **Searchability**: LinkedIn search may not index styled text, so people searching for those keywords might not find your post.
|
|
129
|
+
- **Markdown extensions**: only CommonMark plus tables, strikethrough, autolinks and `==highlight==` are read. Task-list checkboxes stay as literal `[ ]`/`[x]` and footnotes are flattened (`text[^1]` → `text^1`); see [What Gets Transformed](https://www.indrapatil.com/md2linkedin/advanced/#what-gets-transformed-and-what-doesnt).
|
|
115
130
|
|
|
116
131
|
## See Also
|
|
117
132
|
|
|
118
|
-
- [
|
|
133
|
+
- [Documentation](https://www.indrapatil.com/md2linkedin/) — advanced usage, the full conversion table, and the API reference.
|
|
119
134
|
|
|
120
135
|
## License
|
|
121
136
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "md2linkedin"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.7.0"
|
|
4
4
|
description = "Convert Markdown to LinkedIn-friendly Unicode text"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
requires-python = ">=3.12"
|
|
@@ -29,7 +29,7 @@ classifiers = [
|
|
|
29
29
|
"Topic :: Utilities",
|
|
30
30
|
]
|
|
31
31
|
dependencies = [
|
|
32
|
-
"click>=8.5.
|
|
32
|
+
"click>=8.5.1",
|
|
33
33
|
"comrak>=0.0.16",
|
|
34
34
|
"selectolax>=1.0.2",
|
|
35
35
|
]
|
|
@@ -50,20 +50,21 @@ Changelog = "https://github.com/IndrajeetPatil/md2linkedin/blob/main/CHANGELOG.m
|
|
|
50
50
|
|
|
51
51
|
[dependency-groups]
|
|
52
52
|
dev = [
|
|
53
|
-
"coverage>=7.17.
|
|
53
|
+
"coverage>=7.17.5",
|
|
54
54
|
"deptry>=0.25.1",
|
|
55
|
-
"mkdocstrings-python>=2.0.
|
|
56
|
-
"mutmut>=3.8.
|
|
57
|
-
"prek>=0.5.
|
|
58
|
-
"pyrefly>=1.
|
|
59
|
-
"pytest>=9.
|
|
55
|
+
"mkdocstrings-python>=2.0.11",
|
|
56
|
+
"mutmut>=3.8.1",
|
|
57
|
+
"prek>=0.5.11",
|
|
58
|
+
"pyrefly>=1.5.0",
|
|
59
|
+
"pytest>=9.2.0",
|
|
60
60
|
"pytest-codeblocks>=0.18.0",
|
|
61
|
-
"pytest-codspeed==5.0.
|
|
61
|
+
"pytest-codspeed==5.0.4",
|
|
62
62
|
"pytest-cov>=7.1.0",
|
|
63
63
|
"pytest-random-order>=1.2.0",
|
|
64
|
-
"ruff>=0.17.
|
|
65
|
-
"ty>=0.0.
|
|
66
|
-
"
|
|
64
|
+
"ruff>=0.17.6",
|
|
65
|
+
"ty>=0.0.111",
|
|
66
|
+
"unref>=0.5.1",
|
|
67
|
+
"zensical>=0.1.3",
|
|
67
68
|
]
|
|
68
69
|
|
|
69
70
|
[build-system]
|
|
@@ -101,16 +102,43 @@ ignore = [
|
|
|
101
102
|
"subprocess-without-shell-equals-true",
|
|
102
103
|
]
|
|
103
104
|
|
|
105
|
+
[tool.ruff.lint.pydocstyle]
|
|
106
|
+
convention = "google"
|
|
107
|
+
|
|
108
|
+
[tool.ruff.lint.isort]
|
|
109
|
+
combine-as-imports = true
|
|
110
|
+
known-first-party = ["md2linkedin"]
|
|
111
|
+
|
|
112
|
+
[tool.ruff.lint.flake8-tidy-imports]
|
|
113
|
+
ban-relative-imports = "parents"
|
|
114
|
+
|
|
115
|
+
[tool.ruff.lint.flake8-type-checking]
|
|
116
|
+
strict = true
|
|
117
|
+
|
|
104
118
|
[tool.ruff.format]
|
|
105
119
|
preview = true
|
|
106
120
|
docstring-code-format = true
|
|
107
121
|
|
|
108
122
|
[tool.pytest]
|
|
109
123
|
strict = true
|
|
124
|
+
strict_config = true
|
|
125
|
+
strict_markers = true
|
|
126
|
+
strict_parametrization_ids = true
|
|
127
|
+
xfail_strict = true
|
|
128
|
+
empty_parameter_set_mark = "fail_at_collect"
|
|
129
|
+
collect_imported_tests = false
|
|
130
|
+
minversion = "9.2"
|
|
131
|
+
required_plugins = [
|
|
132
|
+
"pytest-random-order",
|
|
133
|
+
"pytest-cov",
|
|
134
|
+
"pytest_codeblocks",
|
|
135
|
+
"pytest-codspeed",
|
|
136
|
+
]
|
|
110
137
|
addopts = [
|
|
111
138
|
"-ra",
|
|
112
139
|
"--verbose",
|
|
113
140
|
"--random-order",
|
|
141
|
+
"--import-mode=importlib",
|
|
114
142
|
]
|
|
115
143
|
testpaths = ["tests"]
|
|
116
144
|
filterwarnings = ["error"]
|
|
@@ -127,6 +155,7 @@ source = ["md2linkedin"]
|
|
|
127
155
|
|
|
128
156
|
[tool.coverage.report]
|
|
129
157
|
fail_under = 100
|
|
158
|
+
precision = 2
|
|
130
159
|
format = "markdown"
|
|
131
160
|
sort = "-Cover"
|
|
132
161
|
show_missing = true
|
|
@@ -134,10 +163,7 @@ skip_empty = true
|
|
|
134
163
|
|
|
135
164
|
[tool.mutmut]
|
|
136
165
|
source_paths = ["src/md2linkedin/"]
|
|
137
|
-
also_copy = [
|
|
138
|
-
"scripts/",
|
|
139
|
-
"README.md",
|
|
140
|
-
]
|
|
166
|
+
also_copy = ["README.md"]
|
|
141
167
|
pytest_add_cli_args_test_selection = ["tests/"]
|
|
142
168
|
mutate_only_covered_lines = true
|
|
143
169
|
|
|
@@ -163,6 +189,8 @@ strict-generic-narrowing = true
|
|
|
163
189
|
[tool.uv]
|
|
164
190
|
required-version = ">=0.13.0"
|
|
165
191
|
|
|
192
|
+
[tool.unref]
|
|
193
|
+
show-transitive = true
|
|
194
|
+
|
|
166
195
|
[tool.deptry]
|
|
167
|
-
extend_exclude = ["scripts"]
|
|
168
196
|
known_first_party = ["md2linkedin"]
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "md2linkedin"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.7.0"
|
|
4
4
|
description = "Convert Markdown to LinkedIn-friendly Unicode text"
|
|
5
5
|
readme = "README.md"
|
|
6
6
|
authors = [
|
|
@@ -26,7 +26,7 @@ classifiers = [
|
|
|
26
26
|
"Topic :: Utilities",
|
|
27
27
|
]
|
|
28
28
|
dependencies = [
|
|
29
|
-
"click>=8.5.
|
|
29
|
+
"click>=8.5.1",
|
|
30
30
|
"comrak>=0.0.16",
|
|
31
31
|
"selectolax>=1.0.2",
|
|
32
32
|
]
|
|
@@ -43,20 +43,21 @@ Changelog = "https://github.com/IndrajeetPatil/md2linkedin/blob/main/CHANGELOG.m
|
|
|
43
43
|
|
|
44
44
|
[dependency-groups]
|
|
45
45
|
dev = [
|
|
46
|
-
"coverage>=7.17.
|
|
46
|
+
"coverage>=7.17.5",
|
|
47
47
|
"deptry>=0.25.1",
|
|
48
|
-
"mkdocstrings-python>=2.0.
|
|
49
|
-
"mutmut>=3.8.
|
|
50
|
-
"prek>=0.5.
|
|
51
|
-
"pyrefly>=1.
|
|
52
|
-
"pytest>=9.
|
|
48
|
+
"mkdocstrings-python>=2.0.11",
|
|
49
|
+
"mutmut>=3.8.1",
|
|
50
|
+
"prek>=0.5.11",
|
|
51
|
+
"pyrefly>=1.5.0",
|
|
52
|
+
"pytest>=9.2.0",
|
|
53
53
|
"pytest-codeblocks>=0.18.0",
|
|
54
|
-
"pytest-codspeed==5.0.
|
|
54
|
+
"pytest-codspeed==5.0.4",
|
|
55
55
|
"pytest-cov>=7.1.0",
|
|
56
56
|
"pytest-random-order>=1.2.0",
|
|
57
|
-
"ruff>=0.17.
|
|
58
|
-
"ty>=0.0.
|
|
59
|
-
"
|
|
57
|
+
"ruff>=0.17.6",
|
|
58
|
+
"ty>=0.0.111",
|
|
59
|
+
"unref>=0.5.1",
|
|
60
|
+
"zensical>=0.1.3",
|
|
60
61
|
]
|
|
61
62
|
|
|
62
63
|
[build-system]
|
|
@@ -113,6 +114,19 @@ ignore = [
|
|
|
113
114
|
"subprocess-without-shell-equals-true",
|
|
114
115
|
]
|
|
115
116
|
|
|
117
|
+
[tool.ruff.lint.pydocstyle]
|
|
118
|
+
convention = "google"
|
|
119
|
+
|
|
120
|
+
[tool.ruff.lint.isort]
|
|
121
|
+
combine-as-imports = true
|
|
122
|
+
known-first-party = ["md2linkedin"]
|
|
123
|
+
|
|
124
|
+
[tool.ruff.lint.flake8-tidy-imports]
|
|
125
|
+
ban-relative-imports = "parents"
|
|
126
|
+
|
|
127
|
+
[tool.ruff.lint.flake8-type-checking]
|
|
128
|
+
strict = true
|
|
129
|
+
|
|
116
130
|
[tool.ruff.format]
|
|
117
131
|
preview = true
|
|
118
132
|
docstring-code-format = true
|
|
@@ -122,6 +136,19 @@ docstring-code-format = true
|
|
|
122
136
|
# and strict_xfail, plus any strictness options future pytest releases add. uv.lock
|
|
123
137
|
# pins pytest, so new options only arrive through a reviewed dependency update.
|
|
124
138
|
strict = true
|
|
139
|
+
strict_config = true
|
|
140
|
+
strict_markers = true
|
|
141
|
+
strict_parametrization_ids = true
|
|
142
|
+
xfail_strict = true
|
|
143
|
+
empty_parameter_set_mark = "fail_at_collect"
|
|
144
|
+
collect_imported_tests = false
|
|
145
|
+
minversion = "9.2"
|
|
146
|
+
required_plugins = [
|
|
147
|
+
"pytest-random-order",
|
|
148
|
+
"pytest-cov",
|
|
149
|
+
"pytest_codeblocks",
|
|
150
|
+
"pytest-codspeed",
|
|
151
|
+
]
|
|
125
152
|
addopts = [
|
|
126
153
|
# show extra test summary info for everything
|
|
127
154
|
"-ra",
|
|
@@ -129,6 +156,8 @@ addopts = [
|
|
|
129
156
|
"--verbose",
|
|
130
157
|
# using pytest-random-order plugin option
|
|
131
158
|
"--random-order",
|
|
159
|
+
# isolate test discovery and execution without altering sys.path
|
|
160
|
+
"--import-mode=importlib",
|
|
132
161
|
]
|
|
133
162
|
testpaths = ["tests"]
|
|
134
163
|
filterwarnings = ["error"]
|
|
@@ -140,6 +169,7 @@ source = ["md2linkedin"]
|
|
|
140
169
|
|
|
141
170
|
[tool.coverage.report]
|
|
142
171
|
fail_under = 100
|
|
172
|
+
precision = 2
|
|
143
173
|
format = "markdown"
|
|
144
174
|
sort = "-Cover"
|
|
145
175
|
show_missing = true
|
|
@@ -147,9 +177,8 @@ skip_empty = true
|
|
|
147
177
|
|
|
148
178
|
[tool.mutmut]
|
|
149
179
|
source_paths = ["src/md2linkedin/"]
|
|
150
|
-
#
|
|
151
|
-
|
|
152
|
-
also_copy = ["scripts/", "README.md"]
|
|
180
|
+
# tests/test_readme.py runs the examples in README.md.
|
|
181
|
+
also_copy = ["README.md"]
|
|
153
182
|
pytest_add_cli_args_test_selection = ["tests/"]
|
|
154
183
|
# Only mutate lines exercised by the test suite so mutmut concentrates on
|
|
155
184
|
# meaningful mutations that the tests should actually catch.
|
|
@@ -180,6 +209,8 @@ strict-generic-narrowing = true
|
|
|
180
209
|
[tool.uv]
|
|
181
210
|
required-version = ">=0.13.0"
|
|
182
211
|
|
|
212
|
+
[tool.unref]
|
|
213
|
+
show-transitive = true
|
|
214
|
+
|
|
183
215
|
[tool.deptry]
|
|
184
|
-
extend_exclude = ["scripts"]
|
|
185
216
|
known_first_party = ["md2linkedin"]
|
|
@@ -6,6 +6,9 @@ The package exposes two main entry points:
|
|
|
6
6
|
* :func:`convert_file` — read a ``.md`` file, convert it, and write a
|
|
7
7
|
``.linkedin.txt`` output file.
|
|
8
8
|
|
|
9
|
+
:func:`find_unstyled` lists the styled words whose letters Unicode cannot
|
|
10
|
+
style, such as words in Devanagari or Chinese.
|
|
11
|
+
|
|
9
12
|
Lower-level Unicode mapping utilities (:func:`to_sans_bold`,
|
|
10
13
|
:func:`to_sans_italic`, :func:`to_sans_bold_italic`, :func:`to_monospace`,
|
|
11
14
|
:func:`apply_style`) are also public for programmatic use.
|
|
@@ -15,7 +18,7 @@ from __future__ import annotations
|
|
|
15
18
|
|
|
16
19
|
from importlib.metadata import version
|
|
17
20
|
|
|
18
|
-
from ._converter import convert, convert_file
|
|
21
|
+
from ._converter import convert, convert_file, find_unstyled
|
|
19
22
|
from ._unicode import (
|
|
20
23
|
apply_style,
|
|
21
24
|
to_monospace,
|
|
@@ -31,6 +34,7 @@ __all__ = [
|
|
|
31
34
|
"apply_style",
|
|
32
35
|
"convert",
|
|
33
36
|
"convert_file",
|
|
37
|
+
"find_unstyled",
|
|
34
38
|
"to_monospace",
|
|
35
39
|
"to_sans_bold",
|
|
36
40
|
"to_sans_bold_italic",
|
|
@@ -1,20 +1,29 @@
|
|
|
1
|
+
"""Markdown to LinkedIn-ready Unicode text conversion.
|
|
2
|
+
|
|
3
|
+
``comrak`` renders the Markdown as HTML, and ``Renderer`` walks the
|
|
4
|
+
``selectolax`` tree of that HTML, writing its text with the Unicode styles of
|
|
5
|
+
the enclosing tags applied.
|
|
6
|
+
"""
|
|
7
|
+
|
|
1
8
|
# Python 3.15+ (PEP 810) defers these imports until first use, so that
|
|
2
9
|
# ``import md2linkedin``, ``--help`` and ``--version`` skip loading the parser
|
|
3
10
|
# extensions. Earlier versions ignore this and import eagerly.
|
|
4
11
|
__lazy_modules__ = ["comrak", "selectolax.lexbor"]
|
|
5
12
|
|
|
6
13
|
import re
|
|
14
|
+
import unicodedata
|
|
7
15
|
from dataclasses import dataclass
|
|
8
16
|
from pathlib import Path
|
|
17
|
+
from typing import Literal
|
|
9
18
|
|
|
10
19
|
import comrak
|
|
11
20
|
from selectolax.lexbor import LexborHTMLParser, LexborNode
|
|
12
21
|
|
|
13
|
-
from
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
22
|
+
from ._unicode import (
|
|
23
|
+
_MONOSPACE_TABLE,
|
|
24
|
+
_SANS_BOLD_ITALIC_TABLE,
|
|
25
|
+
_SANS_BOLD_TABLE,
|
|
26
|
+
_SANS_ITALIC_TABLE,
|
|
18
27
|
)
|
|
19
28
|
|
|
20
29
|
_ENCODING = "utf-8"
|
|
@@ -22,11 +31,21 @@ _ENCODING = "utf-8"
|
|
|
22
31
|
# selectolax spells a text node's tag this way.
|
|
23
32
|
_TEXT_TAG = "-text"
|
|
24
33
|
|
|
34
|
+
# The styles a tag can switch on for the text inside it; ``_emit_text`` applies
|
|
35
|
+
# them. Spelled as a type so that pushing or popping a misspelt style is a type
|
|
36
|
+
# error (``in`` checks are not narrowed, so keep those in step by hand).
|
|
37
|
+
type _Style = Literal["bold", "italic", "upper", "monospace"]
|
|
38
|
+
|
|
25
39
|
_HEADING_TAGS = frozenset({"h1", "h2", "h3", "h4", "h5", "h6"})
|
|
26
40
|
# Tags that render as their own paragraph, separated by a blank line.
|
|
27
41
|
_BLOCK_TAGS = frozenset({"p", "pre", "blockquote", "table"})
|
|
28
42
|
# Tags that only switch a style on for the text they wrap.
|
|
29
|
-
_STYLE_TAGS
|
|
43
|
+
_STYLE_TAGS: dict[str, _Style] = {
|
|
44
|
+
"strong": "bold",
|
|
45
|
+
"b": "bold",
|
|
46
|
+
"em": "italic",
|
|
47
|
+
"i": "italic",
|
|
48
|
+
}
|
|
30
49
|
_LIST_TAGS = frozenset({"ul", "ol"})
|
|
31
50
|
_CELL_TAGS = frozenset({"th", "td"})
|
|
32
51
|
# Tags whose children are rows and cells rather than prose, so the whitespace
|
|
@@ -58,6 +77,44 @@ class _ListLevel:
|
|
|
58
77
|
number: int
|
|
59
78
|
|
|
60
79
|
|
|
80
|
+
# Marks each letter that a style leaves plain when ``find_unstyled`` renders.
|
|
81
|
+
# comrak turns NUL in its input into U+FFFD, so no source text can carry one.
|
|
82
|
+
_PLAIN_MARK = "\0"
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _stays_plain(char: str, table: dict[int, int | str]) -> bool:
|
|
86
|
+
"""Report whether a style leaves a letter plain for want of a styled form.
|
|
87
|
+
|
|
88
|
+
Letters that are styled already, such as pasted Mathematical
|
|
89
|
+
Alphanumeric letters or ``ℎ``, decompose with a ``<font>`` tag and need
|
|
90
|
+
no styled form.
|
|
91
|
+
"""
|
|
92
|
+
return (
|
|
93
|
+
char.isalpha()
|
|
94
|
+
and ord(char) not in table
|
|
95
|
+
and not unicodedata.decomposition(char).startswith("<font>")
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _is_edge(char: str) -> bool:
|
|
100
|
+
"""Report whether a character is punctuation or a symbol, such as a quote."""
|
|
101
|
+
return unicodedata.category(char)[0] in {"P", "S"}
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _trim_word(word: str) -> str:
|
|
105
|
+
"""Strip the punctuation and symbols around a word with a letter in it.
|
|
106
|
+
|
|
107
|
+
The letter stops both scans, so neither needs a bound.
|
|
108
|
+
"""
|
|
109
|
+
start = 0
|
|
110
|
+
while _is_edge(word[start]):
|
|
111
|
+
start += 1
|
|
112
|
+
end = len(word)
|
|
113
|
+
while _is_edge(word[end - 1]):
|
|
114
|
+
end -= 1
|
|
115
|
+
return word[start:end]
|
|
116
|
+
|
|
117
|
+
|
|
61
118
|
def _list_start(node: LexborNode) -> int:
|
|
62
119
|
"""Read the first number of an ordered list, defaulting to one."""
|
|
63
120
|
start = node.attributes.get("start")
|
|
@@ -89,14 +146,27 @@ def _follows_a_cell(node: LexborNode) -> bool:
|
|
|
89
146
|
class Renderer:
|
|
90
147
|
"""Renders a parsed HTML tree as LinkedIn-ready styled plain text."""
|
|
91
148
|
|
|
92
|
-
def __init__(
|
|
149
|
+
def __init__(
|
|
150
|
+
self,
|
|
151
|
+
*,
|
|
152
|
+
preserve_links: bool,
|
|
153
|
+
monospace_code: bool,
|
|
154
|
+
mark_plain: bool = False,
|
|
155
|
+
) -> None:
|
|
93
156
|
self.preserve_links: bool = preserve_links
|
|
94
|
-
|
|
157
|
+
# Write styled text unstyled, with ``_PLAIN_MARK`` before each letter
|
|
158
|
+
# the style would leave plain, for ``find_unstyled`` to collect.
|
|
159
|
+
self.mark_plain: bool = mark_plain
|
|
160
|
+
# Code is just another style tag when it is to be monospaced, and plain
|
|
161
|
+
# text, like any tag without a style, when it is not.
|
|
162
|
+
self.style_tags: dict[str, _Style] = (
|
|
163
|
+
{**_STYLE_TAGS, "code": "monospace"} if monospace_code else _STYLE_TAGS
|
|
164
|
+
)
|
|
95
165
|
self.out: list[str] = []
|
|
96
166
|
# Newlines at the end of ``out``, tracked so that block separation
|
|
97
167
|
# does not have to re-join everything rendered so far.
|
|
98
168
|
self.trailing: int = 0
|
|
99
|
-
self.styles: list[
|
|
169
|
+
self.styles: list[_Style] = []
|
|
100
170
|
self.lists: list[_ListLevel] = []
|
|
101
171
|
self.links: list[_Link] = []
|
|
102
172
|
|
|
@@ -160,21 +230,34 @@ class Renderer:
|
|
|
160
230
|
# and ``_append`` drops it.
|
|
161
231
|
if text is None:
|
|
162
232
|
return
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
styled = styled.upper()
|
|
166
|
-
|
|
167
|
-
if "bold" in self.styles and "italic" in self.styles:
|
|
168
|
-
styled = to_sans_bold_italic(styled)
|
|
169
|
-
elif "bold" in self.styles:
|
|
170
|
-
styled = to_sans_bold(styled)
|
|
171
|
-
elif "italic" in self.styles:
|
|
172
|
-
styled = to_sans_italic(styled)
|
|
173
|
-
|
|
233
|
+
# Unicode has no monospace bold, italic or capitals, so monospace takes
|
|
234
|
+
# precedence over the other styles rather than being mapped over them.
|
|
174
235
|
if "monospace" in self.styles:
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
236
|
+
table = _MONOSPACE_TABLE
|
|
237
|
+
else:
|
|
238
|
+
if "upper" in self.styles:
|
|
239
|
+
text = text.upper()
|
|
240
|
+
if "bold" in self.styles and "italic" in self.styles:
|
|
241
|
+
table = _SANS_BOLD_ITALIC_TABLE
|
|
242
|
+
elif "bold" in self.styles:
|
|
243
|
+
table = _SANS_BOLD_TABLE
|
|
244
|
+
elif "italic" in self.styles:
|
|
245
|
+
table = _SANS_ITALIC_TABLE
|
|
246
|
+
else:
|
|
247
|
+
self._write(text)
|
|
248
|
+
return
|
|
249
|
+
if not self.mark_plain:
|
|
250
|
+
self._write(text.translate(table))
|
|
251
|
+
elif text.isascii():
|
|
252
|
+
# Every ASCII letter has a styled form.
|
|
253
|
+
self._write(text)
|
|
254
|
+
else:
|
|
255
|
+
self._write(
|
|
256
|
+
"".join(
|
|
257
|
+
_PLAIN_MARK + char if _stays_plain(char, table) else char
|
|
258
|
+
for char in text
|
|
259
|
+
),
|
|
260
|
+
)
|
|
178
261
|
|
|
179
262
|
# ── tags ──────────────────────────────────────────────────────────────────
|
|
180
263
|
|
|
@@ -203,12 +286,9 @@ class Renderer:
|
|
|
203
286
|
self._enter_inline(node, tag)
|
|
204
287
|
|
|
205
288
|
def _enter_inline(self, node: LexborNode, tag: str) -> None:
|
|
206
|
-
style =
|
|
289
|
+
style = self.style_tags.get(tag)
|
|
207
290
|
if style is not None:
|
|
208
291
|
self.styles.append(style)
|
|
209
|
-
elif tag == "code":
|
|
210
|
-
if self.monospace_code:
|
|
211
|
-
self.styles.append("monospace")
|
|
212
292
|
elif tag == "a":
|
|
213
293
|
self._start_link(node)
|
|
214
294
|
elif tag == "img":
|
|
@@ -231,12 +311,9 @@ class Renderer:
|
|
|
231
311
|
self._exit_inline(tag)
|
|
232
312
|
|
|
233
313
|
def _exit_inline(self, tag: str) -> None:
|
|
234
|
-
style =
|
|
314
|
+
style = self.style_tags.get(tag)
|
|
235
315
|
if style is not None:
|
|
236
316
|
self.styles.remove(style)
|
|
237
|
-
elif tag == "code":
|
|
238
|
-
if self.monospace_code:
|
|
239
|
-
self.styles.remove("monospace")
|
|
240
317
|
elif tag == "a":
|
|
241
318
|
self._end_link()
|
|
242
319
|
|
|
@@ -347,30 +424,14 @@ class Renderer:
|
|
|
347
424
|
self._write(f"[{text}]({link.url}{title})")
|
|
348
425
|
|
|
349
426
|
|
|
350
|
-
def
|
|
427
|
+
def _render(
|
|
351
428
|
text: str,
|
|
352
429
|
*,
|
|
430
|
+
monospace_code: bool,
|
|
353
431
|
preserve_links: bool = False,
|
|
354
|
-
|
|
355
|
-
) ->
|
|
356
|
-
"""
|
|
357
|
-
|
|
358
|
-
Args:
|
|
359
|
-
text: The Markdown source string.
|
|
360
|
-
preserve_links: When ``True``, link syntax (``[text](url)``) is left
|
|
361
|
-
unchanged in the output instead of being reduced to display text.
|
|
362
|
-
monospace_code: When ``True`` (the default), inline code spans and
|
|
363
|
-
fenced code blocks are rendered in Unicode Mathematical Monospace.
|
|
364
|
-
When ``False``, code is kept as plain text, with the backticks
|
|
365
|
-
and fences stripped.
|
|
366
|
-
|
|
367
|
-
Returns:
|
|
368
|
-
A plain-text string suitable for pasting into LinkedIn.
|
|
369
|
-
|
|
370
|
-
"""
|
|
371
|
-
if not text.strip():
|
|
372
|
-
return ""
|
|
373
|
-
|
|
432
|
+
mark_plain: bool = False,
|
|
433
|
+
) -> Renderer:
|
|
434
|
+
"""Parse Markdown and render it, returning the finished renderer."""
|
|
374
435
|
render_options = comrak.RenderOptions()
|
|
375
436
|
render_options.compact_html = True
|
|
376
437
|
# Raw HTML is passed through to the parser below, which reads the text out
|
|
@@ -391,17 +452,91 @@ def convert(
|
|
|
391
452
|
render_options=render_options,
|
|
392
453
|
)
|
|
393
454
|
|
|
394
|
-
tree = LexborHTMLParser(html)
|
|
395
455
|
renderer = Renderer(
|
|
396
456
|
preserve_links=preserve_links,
|
|
397
457
|
monospace_code=monospace_code,
|
|
458
|
+
mark_plain=mark_plain,
|
|
459
|
+
)
|
|
460
|
+
renderer.render(LexborHTMLParser(html).root)
|
|
461
|
+
return renderer
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
def convert(
|
|
465
|
+
text: str,
|
|
466
|
+
*,
|
|
467
|
+
preserve_links: bool = False,
|
|
468
|
+
monospace_code: bool = True,
|
|
469
|
+
) -> str:
|
|
470
|
+
"""Convert Markdown text to LinkedIn-compatible Unicode plain text.
|
|
471
|
+
|
|
472
|
+
Args:
|
|
473
|
+
text: The Markdown source string.
|
|
474
|
+
preserve_links: When ``True``, link syntax (``[text](url)``) is left
|
|
475
|
+
unchanged in the output instead of being reduced to display text.
|
|
476
|
+
monospace_code: When ``True`` (the default), inline code spans and
|
|
477
|
+
fenced code blocks are rendered in Unicode Mathematical Monospace.
|
|
478
|
+
When ``False``, code is kept as plain text, with the backticks
|
|
479
|
+
and fences stripped.
|
|
480
|
+
|
|
481
|
+
Returns:
|
|
482
|
+
A plain-text string suitable for pasting into LinkedIn.
|
|
483
|
+
|
|
484
|
+
"""
|
|
485
|
+
if not text.strip():
|
|
486
|
+
return ""
|
|
487
|
+
|
|
488
|
+
out = "".join(
|
|
489
|
+
_render(text, preserve_links=preserve_links, monospace_code=monospace_code).out,
|
|
398
490
|
)
|
|
399
|
-
out = renderer.render(tree.root)
|
|
400
491
|
# An item that holds nothing but a nested list leaves its marker alone on
|
|
401
492
|
# a line; no line is meant to end in blank space.
|
|
402
493
|
return _TRAILING_SPACE.sub("", out).strip() + "\n"
|
|
403
494
|
|
|
404
495
|
|
|
496
|
+
def find_unstyled(text: str, *, monospace_code: bool = True) -> list[str]:
|
|
497
|
+
"""List the styled words that keep plain letters in the converted text.
|
|
498
|
+
|
|
499
|
+
Unicode has bold, italic, and monospace letters only for Latin, Greek
|
|
500
|
+
(no monospace), and digits (no italic). Letters of every other script,
|
|
501
|
+
such as Cyrillic, Arabic, Devanagari, Chinese, Japanese, or Korean, and a
|
|
502
|
+
few Latin letters such as ``ß``, have no styled form, so :func:`convert`
|
|
503
|
+
leaves them plain inside bold, italic, headings, and code. Use this to
|
|
504
|
+
warn the writer before they post.
|
|
505
|
+
|
|
506
|
+
Args:
|
|
507
|
+
text: The Markdown source string.
|
|
508
|
+
monospace_code: Whether code is styled, as in :func:`convert`. When
|
|
509
|
+
``False``, code is plain anyway, so its words are not listed.
|
|
510
|
+
|
|
511
|
+
Returns:
|
|
512
|
+
The words with plain letters, stripped of surrounding punctuation, in
|
|
513
|
+
order of first appearance and without repeats. Words in an H1 are
|
|
514
|
+
listed in capitals, as :func:`convert` writes them.
|
|
515
|
+
|
|
516
|
+
Examples:
|
|
517
|
+
>>> find_unstyled("**देवनागरी** (Devanagari) *script*")
|
|
518
|
+
['देवनागरी']
|
|
519
|
+
|
|
520
|
+
>>> find_unstyled("`Привет` and **Ωμέγα**")
|
|
521
|
+
['Привет']
|
|
522
|
+
|
|
523
|
+
>>> find_unstyled("`Привет`", monospace_code=False)
|
|
524
|
+
[]
|
|
525
|
+
|
|
526
|
+
"""
|
|
527
|
+
if not text.strip():
|
|
528
|
+
return []
|
|
529
|
+
renderer = _render(text, monospace_code=monospace_code, mark_plain=True)
|
|
530
|
+
# Words are split in the finished text, so a word that inline markup
|
|
531
|
+
# breaks into several text nodes is still one word.
|
|
532
|
+
words = (
|
|
533
|
+
_trim_word(word.replace(_PLAIN_MARK, ""))
|
|
534
|
+
for word in "".join(renderer.out).split()
|
|
535
|
+
if _PLAIN_MARK in word
|
|
536
|
+
)
|
|
537
|
+
return list(dict.fromkeys(words))
|
|
538
|
+
|
|
539
|
+
|
|
405
540
|
def convert_file(
|
|
406
541
|
input_path: str | Path,
|
|
407
542
|
output_path: str | Path | None = None,
|
|
@@ -2,12 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
Converts ASCII letters and digits to their Unicode Mathematical
|
|
4
4
|
Sans-Serif and Monospace equivalents, enabling bold, italic, bold-italic
|
|
5
|
-
and monospace styling in plain-text environments like LinkedIn.
|
|
5
|
+
and monospace styling in plain-text environments like LinkedIn. Greek
|
|
6
|
+
letters take bold, italic and bold-italic styling too, and accented Latin
|
|
7
|
+
and Greek letters become the styled base letter followed by combining marks.
|
|
8
|
+
Unicode has no styled letters for any other script.
|
|
6
9
|
"""
|
|
7
10
|
|
|
8
11
|
from __future__ import annotations
|
|
9
12
|
|
|
10
13
|
import string
|
|
14
|
+
import unicodedata
|
|
11
15
|
from typing import Literal
|
|
12
16
|
|
|
13
17
|
__all__ = [
|
|
@@ -35,32 +39,90 @@ _MONOSPACE_UPPER = 0x1D670 # 𝙰
|
|
|
35
39
|
_MONOSPACE_LOWER = 0x1D68A # 𝚊
|
|
36
40
|
_MONOSPACE_DIGIT = 0x1D7F6 # 𝟶
|
|
37
41
|
|
|
42
|
+
# Unicode has bold and bold-italic Greek only in sans-serif, and italic Greek
|
|
43
|
+
# only in serif, so italic Greek is the closest available match. There is no
|
|
44
|
+
# monospace Greek.
|
|
45
|
+
_SANS_BOLD_GREEK_UPPER = 0x1D756 # 𝝖
|
|
46
|
+
_SANS_BOLD_GREEK_LOWER = 0x1D770 # 𝝰
|
|
47
|
+
|
|
48
|
+
_ITALIC_GREEK_UPPER = 0x1D6E2 # 𝛢
|
|
49
|
+
_ITALIC_GREEK_LOWER = 0x1D6FC # 𝛼
|
|
50
|
+
|
|
51
|
+
_SANS_BOLD_ITALIC_GREEK_UPPER = 0x1D790 # 𝞐
|
|
52
|
+
_SANS_BOLD_ITALIC_GREEK_LOWER = 0x1D7AA # 𝞪
|
|
53
|
+
|
|
54
|
+
# The order of each styled Greek alphabet. ``ϴ`` fills the slot of the
|
|
55
|
+
# unassigned U+03A2 between ``Ρ`` and ``Σ``, and ``ς`` sits between ``ρ`` and
|
|
56
|
+
# ``σ``, as in the Greek and Coptic block.
|
|
57
|
+
_GREEK_UPPERCASE = "ΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡϴΣΤΥΦΧΨΩ"
|
|
58
|
+
_GREEK_LOWERCASE = "αβγδεζηθικλμνξοπρςστυφχψω"
|
|
59
|
+
|
|
60
|
+
# Every precomposed Latin or Greek letter whose canonical decomposition starts
|
|
61
|
+
# with a styled letter lies in Latin-1 Supplement through Latin Extended-B, in
|
|
62
|
+
# Greek and Coptic, or in Latin Extended Additional and Greek Extended.
|
|
63
|
+
_ACCENTED_RANGES = (range(0xC0, 0x250), range(0x370, 0x400), range(0x1E00, 0x2000))
|
|
64
|
+
|
|
38
65
|
|
|
39
66
|
# ── Translation tables ─────────────────────────────────────────────────────────
|
|
40
67
|
|
|
41
68
|
|
|
42
|
-
def _build_table(
|
|
69
|
+
def _build_table(
|
|
70
|
+
upper: int,
|
|
71
|
+
lower: int,
|
|
72
|
+
digit: int | None = None,
|
|
73
|
+
greek: tuple[int, int] | None = None,
|
|
74
|
+
) -> dict[int, int | str]:
|
|
43
75
|
"""Build a :meth:`str.translate` table for one Unicode style block.
|
|
44
76
|
|
|
45
77
|
The table is derived from the codepoints of the block's styled ``A``,
|
|
46
|
-
``a
|
|
47
|
-
|
|
78
|
+
``a``, ``0``, and the styled ``Α`` and ``α`` in *greek*. *digit* is
|
|
79
|
+
``None`` for the italic blocks, which have no digits, and *greek* is
|
|
80
|
+
``None`` for monospace, which has no Greek. Characters absent from the
|
|
81
|
+
table are left unchanged.
|
|
82
|
+
|
|
83
|
+
Unicode has no styled forms of accented letters, so each precomposed Latin
|
|
84
|
+
or Greek letter maps to its NFD form with the base letter styled: ``é``
|
|
85
|
+
becomes styled ``e`` followed by U+0301 COMBINING ACUTE ACCENT, which
|
|
86
|
+
renders as a styled ``é``. Letters without a decomposition, such as ``ß``
|
|
87
|
+
and ``ł``, stay plain. Only the Latin and Greek blocks are decomposed, so
|
|
88
|
+
Hangul syllables and other scripts are never split apart.
|
|
48
89
|
"""
|
|
49
90
|
blocks = [(string.ascii_uppercase, upper), (string.ascii_lowercase, lower)]
|
|
50
91
|
if digit is not None:
|
|
51
92
|
blocks.append((string.digits, digit))
|
|
52
|
-
|
|
93
|
+
if greek is not None:
|
|
94
|
+
greek_upper, greek_lower = greek
|
|
95
|
+
blocks += [(_GREEK_UPPERCASE, greek_upper), (_GREEK_LOWERCASE, greek_lower)]
|
|
96
|
+
table: dict[int, int | str] = {
|
|
53
97
|
ord(char): base + offset
|
|
54
98
|
for chars, base in blocks
|
|
55
99
|
for offset, char in enumerate(chars)
|
|
56
100
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
101
|
+
for codepoint in (cp for block in _ACCENTED_RANGES for cp in block):
|
|
102
|
+
base, *marks = unicodedata.normalize("NFD", chr(codepoint))
|
|
103
|
+
styled = table.get(ord(base))
|
|
104
|
+
if marks and isinstance(styled, int):
|
|
105
|
+
table[codepoint] = chr(styled) + "".join(marks)
|
|
106
|
+
return table
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
_SANS_BOLD_TABLE = _build_table(
|
|
110
|
+
_SANS_BOLD_UPPER,
|
|
111
|
+
_SANS_BOLD_LOWER,
|
|
112
|
+
_SANS_BOLD_DIGIT,
|
|
113
|
+
(_SANS_BOLD_GREEK_UPPER, _SANS_BOLD_GREEK_LOWER),
|
|
114
|
+
)
|
|
115
|
+
_SANS_ITALIC_TABLE = _build_table(
|
|
116
|
+
_SANS_ITALIC_UPPER,
|
|
117
|
+
_SANS_ITALIC_LOWER,
|
|
118
|
+
None,
|
|
119
|
+
(_ITALIC_GREEK_UPPER, _ITALIC_GREEK_LOWER),
|
|
120
|
+
)
|
|
61
121
|
_SANS_BOLD_ITALIC_TABLE = _build_table(
|
|
62
122
|
_SANS_BOLD_ITALIC_UPPER,
|
|
63
123
|
_SANS_BOLD_ITALIC_LOWER,
|
|
124
|
+
None,
|
|
125
|
+
(_SANS_BOLD_ITALIC_GREEK_UPPER, _SANS_BOLD_ITALIC_GREEK_LOWER),
|
|
64
126
|
)
|
|
65
127
|
_MONOSPACE_TABLE = _build_table(_MONOSPACE_UPPER, _MONOSPACE_LOWER, _MONOSPACE_DIGIT)
|
|
66
128
|
|
|
@@ -71,9 +133,11 @@ _MONOSPACE_TABLE = _build_table(_MONOSPACE_UPPER, _MONOSPACE_LOWER, _MONOSPACE_D
|
|
|
71
133
|
def to_sans_bold(text: str) -> str:
|
|
72
134
|
"""Convert text to Unicode Mathematical Sans-Serif Bold.
|
|
73
135
|
|
|
74
|
-
ASCII uppercase letters, lowercase letters, and
|
|
75
|
-
their bold sans-serif counterparts.
|
|
76
|
-
|
|
136
|
+
ASCII uppercase letters, lowercase letters, digits, and Greek letters are
|
|
137
|
+
mapped to their bold sans-serif counterparts. Accented Latin and Greek
|
|
138
|
+
letters become the bold base letter followed by combining marks. All
|
|
139
|
+
other characters (spaces, punctuation, ``ß``, emojis, and other scripts
|
|
140
|
+
such as Devanagari or Chinese) pass through unchanged.
|
|
77
141
|
|
|
78
142
|
Args:
|
|
79
143
|
text: The input string to convert.
|
|
@@ -86,8 +150,11 @@ def to_sans_bold(text: str) -> str:
|
|
|
86
150
|
>>> to_sans_bold("Hello, World! 123")
|
|
87
151
|
'𝗛𝗲𝗹𝗹𝗼, 𝗪𝗼𝗿𝗹𝗱! 𝟭𝟮𝟯'
|
|
88
152
|
|
|
89
|
-
>>> to_sans_bold("café")
|
|
90
|
-
|
|
153
|
+
>>> to_sans_bold("café") == "𝗰𝗮𝗳𝗲\u0301"
|
|
154
|
+
True
|
|
155
|
+
|
|
156
|
+
>>> to_sans_bold("Ωμέγα") == "𝝮𝝻𝝴\u0301𝝲𝝰"
|
|
157
|
+
True
|
|
91
158
|
|
|
92
159
|
>>> to_sans_bold("")
|
|
93
160
|
''
|
|
@@ -100,8 +167,11 @@ def to_sans_italic(text: str) -> str:
|
|
|
100
167
|
"""Convert text to Unicode Mathematical Sans-Serif Italic.
|
|
101
168
|
|
|
102
169
|
ASCII uppercase and lowercase letters are mapped to their italic
|
|
103
|
-
sans-serif counterparts
|
|
104
|
-
|
|
170
|
+
sans-serif counterparts, and Greek letters to serif italic ones, since
|
|
171
|
+
Unicode has no sans-serif italic Greek. Accented Latin and Greek letters
|
|
172
|
+
become the italic base letter followed by combining marks. Digits and all
|
|
173
|
+
other characters pass through unchanged (there are no italic digit
|
|
174
|
+
codepoints in this Unicode block).
|
|
105
175
|
|
|
106
176
|
Args:
|
|
107
177
|
text: The input string to convert.
|
|
@@ -127,9 +197,10 @@ def to_sans_italic(text: str) -> str:
|
|
|
127
197
|
def to_sans_bold_italic(text: str) -> str:
|
|
128
198
|
"""Convert text to Unicode Mathematical Sans-Serif Bold Italic.
|
|
129
199
|
|
|
130
|
-
ASCII uppercase and
|
|
131
|
-
sans-serif counterparts.
|
|
132
|
-
|
|
200
|
+
ASCII uppercase, lowercase, and Greek letters are mapped to their
|
|
201
|
+
bold-italic sans-serif counterparts. Accented Latin and Greek letters
|
|
202
|
+
become the bold-italic base letter followed by combining marks. Digits
|
|
203
|
+
and all other characters pass through unchanged.
|
|
133
204
|
|
|
134
205
|
Args:
|
|
135
206
|
text: The input string to convert.
|
|
@@ -153,8 +224,10 @@ def to_monospace(text: str) -> str:
|
|
|
153
224
|
"""Convert text to Unicode Mathematical Monospace.
|
|
154
225
|
|
|
155
226
|
ASCII uppercase letters, lowercase letters, and digits are mapped to
|
|
156
|
-
their monospace counterparts.
|
|
157
|
-
|
|
227
|
+
their monospace counterparts. Accented Latin letters become the
|
|
228
|
+
monospace base letter followed by combining marks. All other characters
|
|
229
|
+
(spaces, punctuation, ``ß``, emojis, and other scripts, including Greek,
|
|
230
|
+
which has no monospace form) pass through unchanged.
|
|
158
231
|
|
|
159
232
|
Args:
|
|
160
233
|
text: The input string to convert.
|
|
@@ -167,8 +240,8 @@ def to_monospace(text: str) -> str:
|
|
|
167
240
|
>>> to_monospace("Hello, World! 123")
|
|
168
241
|
'𝙷𝚎𝚕𝚕𝚘, 𝚆𝚘𝚛𝚕𝚍! 𝟷𝟸𝟹'
|
|
169
242
|
|
|
170
|
-
>>> to_monospace("café")
|
|
171
|
-
|
|
243
|
+
>>> to_monospace("café") == "𝚌𝚊𝚏𝚎\u0301"
|
|
244
|
+
True
|
|
172
245
|
|
|
173
246
|
>>> to_monospace("")
|
|
174
247
|
''
|
|
File without changes
|
|
File without changes
|
|
File without changes
|