sphinx-cjk-aware 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.
- sphinx_cjk_aware/__init__.py +18 -0
- sphinx_cjk_aware/_charclass.py +62 -0
- sphinx_cjk_aware/_doctree.py +134 -0
- sphinx_cjk_aware/_extension.py +89 -0
- sphinx_cjk_aware/_version.py +7 -0
- sphinx_cjk_aware/py.typed +0 -0
- sphinx_cjk_aware-0.1.0.dist-info/METADATA +167 -0
- sphinx_cjk_aware-0.1.0.dist-info/RECORD +10 -0
- sphinx_cjk_aware-0.1.0.dist-info/WHEEL +4 -0
- sphinx_cjk_aware-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""CJK-aware inline spacing for Sphinx.
|
|
2
|
+
|
|
3
|
+
The extension removes the artificial spaces around inline markup in
|
|
4
|
+
Chinese and Japanese text. See :mod:`sphinx_cjk_aware._extension` for the
|
|
5
|
+
rationale and the project README for usage.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from ._charclass import is_unspaced_char
|
|
9
|
+
from ._doctree import fix_inline_boundaries
|
|
10
|
+
from ._extension import setup
|
|
11
|
+
from ._version import __version__
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"__version__",
|
|
15
|
+
"fix_inline_boundaries",
|
|
16
|
+
"is_unspaced_char",
|
|
17
|
+
"setup",
|
|
18
|
+
]
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""Character classification: is a character written without word spaces?
|
|
2
|
+
|
|
3
|
+
Chinese and Japanese run characters together, so a space around inline
|
|
4
|
+
markup there is an artefact of the markup workaround. Korean separates
|
|
5
|
+
words with spaces, so a space next to Hangul is a real word boundary and
|
|
6
|
+
must be kept. The distinction is made by script, not by language.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
# Unicode blocks of scripts written without spaces between characters.
|
|
12
|
+
# Hangul is deliberately absent: Korean separates words with spaces, so a
|
|
13
|
+
# space next to Hangul is a real word boundary and must be kept. A missing
|
|
14
|
+
# block here can only leave a space in place — never remove a needed one.
|
|
15
|
+
_UNSPACED_BLOCKS = (
|
|
16
|
+
(0x2E80, 0x2EFF), # CJK Radicals Supplement
|
|
17
|
+
(0x2F00, 0x2FDF), # Kangxi Radicals
|
|
18
|
+
(0x2FF0, 0x2FFF), # Ideographic Description Characters
|
|
19
|
+
(0x3000, 0x303F), # CJK Symbols and Punctuation
|
|
20
|
+
(0x3040, 0x309F), # Hiragana
|
|
21
|
+
(0x30A0, 0x30FF), # Katakana
|
|
22
|
+
(0x3100, 0x312F), # Bopomofo
|
|
23
|
+
(0x3190, 0x31BF), # Kanbun, Bopomofo Extended
|
|
24
|
+
(0x31C0, 0x31EF), # CJK Strokes
|
|
25
|
+
(0x31F0, 0x31FF), # Katakana Phonetic Extensions
|
|
26
|
+
(0x3400, 0x4DBF), # CJK Unified Ideographs Extension A
|
|
27
|
+
(0x4E00, 0x9FFF), # CJK Unified Ideographs
|
|
28
|
+
(0xF900, 0xFAFF), # CJK Compatibility Ideographs
|
|
29
|
+
(0xFE10, 0xFE1F), # Vertical Forms
|
|
30
|
+
(0xFE30, 0xFE4F), # CJK Compatibility Forms
|
|
31
|
+
(0xFE50, 0xFE6F), # Small Form Variants
|
|
32
|
+
(0xFF01, 0xFF60), # Fullwidth Forms
|
|
33
|
+
(0xFF61, 0xFF9F), # Halfwidth CJK punctuation and katakana
|
|
34
|
+
(0xFFE0, 0xFFE6), # Fullwidth Signs
|
|
35
|
+
(0x1B000, 0x1B16F), # Kana Supplement, Kana Extended-A, Small Kana Ext.
|
|
36
|
+
(0x20000, 0x2FA1F), # CJK Extensions B-F, Compatibility Supplement
|
|
37
|
+
(0x30000, 0x323AF), # CJK Extensions G-H
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
# Punctuation shared by those scripts but living outside the blocks above:
|
|
41
|
+
# ambiguous-width dashes, ellipses and quotation marks.
|
|
42
|
+
_UNSPACED_PUNCTUATION = frozenset("—–―…‥“”‘’·")
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def is_unspaced_char(char: str) -> bool:
|
|
46
|
+
"""Return whether *char* is written without a space around it.
|
|
47
|
+
|
|
48
|
+
Han characters, hiragana, katakana, bopomofo and the punctuation those
|
|
49
|
+
scripts share qualify. Hangul does not — Korean separates words with
|
|
50
|
+
spaces — and neither do Latin, emoji or other scripts.
|
|
51
|
+
|
|
52
|
+
>>> is_unspaced_char("中") or is_unspaced_char("あ") or is_unspaced_char("。")
|
|
53
|
+
True
|
|
54
|
+
>>> is_unspaced_char("한") or is_unspaced_char("A") or is_unspaced_char("😀")
|
|
55
|
+
False
|
|
56
|
+
"""
|
|
57
|
+
if not char or char.isspace():
|
|
58
|
+
return False
|
|
59
|
+
if char in _UNSPACED_PUNCTUATION:
|
|
60
|
+
return True
|
|
61
|
+
codepoint = ord(char)
|
|
62
|
+
return any(start <= codepoint <= end for start, end in _UNSPACED_BLOCKS)
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"""Doctree pass: delete whitespace at text/inline-element seams.
|
|
2
|
+
|
|
3
|
+
A whitespace run at the seam between a ``Text`` node and a neighbouring
|
|
4
|
+
inline element (emphasis, strong, literal, reference, ...) is dropped when
|
|
5
|
+
both characters flanking it belong to a script written without word
|
|
6
|
+
spaces, as decided by :func:`._charclass.is_unspaced_char`.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import re
|
|
12
|
+
|
|
13
|
+
from docutils import nodes
|
|
14
|
+
|
|
15
|
+
from ._charclass import is_unspaced_char
|
|
16
|
+
|
|
17
|
+
# Whitespace runs at a Text node's seams. \s matches the fullwidth space
|
|
18
|
+
# U+3000 as well as newlines, so a source line wrapped right before
|
|
19
|
+
# markup joins too. The optional \x00 is docutils' escape marker: an
|
|
20
|
+
# escaped space (``\ ``, common in translated msgstrs so tooling keeps
|
|
21
|
+
# it) is stored as NUL + space and is just as artificial a seam space.
|
|
22
|
+
_LEADING_WS = re.compile(r"^(?:\x00?\s)+")
|
|
23
|
+
_TRAILING_WS = re.compile(r"(?:\x00?\s)+$")
|
|
24
|
+
|
|
25
|
+
# Parents whose children are left alone: whitespace is significant in
|
|
26
|
+
# literal blocks (``.. parsed-literal::`` shares the literal_block class)
|
|
27
|
+
# and in math.
|
|
28
|
+
_PRESERVE_PARENTS = (nodes.literal_block, nodes.math_block)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _edge_char(node: nodes.Element, *, last: bool) -> str:
|
|
32
|
+
"""First (or last) character that *node* renders.
|
|
33
|
+
|
|
34
|
+
``""`` when the node renders nothing visible (an image without alt
|
|
35
|
+
text, an inline target), which keeps the neighbouring space.
|
|
36
|
+
"""
|
|
37
|
+
text = node.astext()
|
|
38
|
+
return text[-1:] if last else text[:1]
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def _replace_text(parent: nodes.Element, index: int, raw: str) -> bool:
|
|
42
|
+
"""Swap the Text node at ``parent.children[index]`` for one holding *raw*.
|
|
43
|
+
|
|
44
|
+
docutils Text nodes are ``str`` subclasses with no mutable data slot,
|
|
45
|
+
so the node has to be replaced rather than edited. ``setup_child``
|
|
46
|
+
keeps the parent/document pointers intact, and assignment is by index
|
|
47
|
+
so identical-content siblings elsewhere in the same parent cannot be
|
|
48
|
+
confused (``index()`` would match them via str equality).
|
|
49
|
+
"""
|
|
50
|
+
fresh = nodes.Text(raw)
|
|
51
|
+
parent.setup_child(fresh)
|
|
52
|
+
parent.children[index] = fresh
|
|
53
|
+
return True
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _strip_trailing(parent: nodes.Element, index: int) -> bool:
|
|
57
|
+
"""Drop Text node *index*'s trailing whitespace run.
|
|
58
|
+
|
|
59
|
+
Fires when the character before the run in the Text node and the
|
|
60
|
+
first character of the element following it are both unspaced.
|
|
61
|
+
"""
|
|
62
|
+
raw = str(parent.children[index])
|
|
63
|
+
match = _TRAILING_WS.search(raw)
|
|
64
|
+
if match is None or match.start() == 0:
|
|
65
|
+
# no run, or the node is all whitespace (a middle case handled
|
|
66
|
+
# by _strip_inner_space)
|
|
67
|
+
return False
|
|
68
|
+
if not (
|
|
69
|
+
is_unspaced_char(raw[match.start() - 1])
|
|
70
|
+
and is_unspaced_char(_edge_char(parent.children[index + 1], last=False))
|
|
71
|
+
):
|
|
72
|
+
return False
|
|
73
|
+
return _replace_text(parent, index, raw[: match.start()])
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _strip_leading(parent: nodes.Element, index: int) -> bool:
|
|
77
|
+
"""Drop Text node *index*'s leading whitespace run.
|
|
78
|
+
|
|
79
|
+
Fires when the character after the run in the Text node and the last
|
|
80
|
+
character of the element preceding it are both unspaced.
|
|
81
|
+
"""
|
|
82
|
+
raw = str(parent.children[index])
|
|
83
|
+
match = _LEADING_WS.match(raw)
|
|
84
|
+
if match is None or match.end() == len(raw):
|
|
85
|
+
return False
|
|
86
|
+
rest = raw[match.end() :]
|
|
87
|
+
if not (
|
|
88
|
+
is_unspaced_char(rest[0])
|
|
89
|
+
and is_unspaced_char(_edge_char(parent.children[index - 1], last=True))
|
|
90
|
+
):
|
|
91
|
+
return False
|
|
92
|
+
return _replace_text(parent, index, rest)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _strip_inner_space(parent: nodes.Element) -> None:
|
|
96
|
+
"""Blank a whitespace-only Text node caught between two elements.
|
|
97
|
+
|
|
98
|
+
``*甲* *乙*`` needs the middle space to parse, yet renders as
|
|
99
|
+
``甲乙``: the space is artificial too. Only fires when both elements
|
|
100
|
+
render unspaced text at the seam.
|
|
101
|
+
"""
|
|
102
|
+
children = parent.children
|
|
103
|
+
for i in range(1, len(children) - 1):
|
|
104
|
+
node = children[i]
|
|
105
|
+
if not isinstance(node, nodes.Text) or str(node).replace("\x00", "").strip():
|
|
106
|
+
continue
|
|
107
|
+
left, right = children[i - 1], children[i + 1]
|
|
108
|
+
if isinstance(left, nodes.Text) or isinstance(right, nodes.Text):
|
|
109
|
+
continue
|
|
110
|
+
left_unspaced = is_unspaced_char(_edge_char(left, last=True))
|
|
111
|
+
right_unspaced = is_unspaced_char(_edge_char(right, last=False))
|
|
112
|
+
if left_unspaced and right_unspaced:
|
|
113
|
+
_replace_text(parent, i, "")
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def fix_inline_boundaries(parent: nodes.Element) -> None:
|
|
117
|
+
"""Strip artificial unspaced-script boundary spaces among children.
|
|
118
|
+
|
|
119
|
+
Only seams between a Text node and a neighbouring element are
|
|
120
|
+
considered; Text↔Text siblings (which do not occur in parsed docutils
|
|
121
|
+
trees) are left alone. Parents where whitespace is significant are
|
|
122
|
+
skipped.
|
|
123
|
+
"""
|
|
124
|
+
if isinstance(parent, _PRESERVE_PARENTS):
|
|
125
|
+
return
|
|
126
|
+
children = parent.children
|
|
127
|
+
for i in range(len(children) - 1):
|
|
128
|
+
left, right = children[i], children[i + 1]
|
|
129
|
+
if isinstance(left, nodes.Text):
|
|
130
|
+
if not isinstance(right, nodes.Text):
|
|
131
|
+
_strip_trailing(parent, i)
|
|
132
|
+
elif isinstance(right, nodes.Text):
|
|
133
|
+
_strip_leading(parent, i + 1)
|
|
134
|
+
_strip_inner_space(parent)
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"""Sphinx integration: when to run the doctree pass, and how to register it.
|
|
2
|
+
|
|
3
|
+
The problem (cf. https://github.com/tats-u/markdown-cjk-friendly)
|
|
4
|
+
----------------------------------------------------------------
|
|
5
|
+
reStructuredText — like CommonMark, cf. the ``markdown-cjk-friendly``
|
|
6
|
+
specification — only recognises inline markup delimiters (``*emphasis*``,
|
|
7
|
+
``**strong**``, literals, ...) when they are flanked by whitespace or
|
|
8
|
+
punctuation. Chinese and Japanese characters qualify as neither, so
|
|
9
|
+
emphasis embedded in that prose has to be padded with spaces to parse at
|
|
10
|
+
all::
|
|
11
|
+
|
|
12
|
+
即它 *为什么* 有效
|
|
13
|
+
|
|
14
|
+
Translators add these spaces especially on translation platforms such as
|
|
15
|
+
Transifex, where the rendered output cannot be previewed and the markup
|
|
16
|
+
rules are not understood — the exact scenario ``markdown-cjk-friendly``
|
|
17
|
+
was written for. The spaces then leak into the rendered page as unwanted
|
|
18
|
+
gaps between the text and the emphasised span::
|
|
19
|
+
|
|
20
|
+
即它 <em>为什么</em> 有效
|
|
21
|
+
|
|
22
|
+
Chinese and Japanese typography put no space between characters, so the
|
|
23
|
+
spaces are artefacts of the markup workaround, not of the prose.
|
|
24
|
+
|
|
25
|
+
The fix (after https://github.com/seisman/sphinx-cjkspace)
|
|
26
|
+
----------------------------------------------------------
|
|
27
|
+
Like ``sphinx-cjkspace`` removes newline-induced spaces between CJK
|
|
28
|
+
characters after parsing, this extension post-processes the resolved
|
|
29
|
+
doctree — at ``doctree-read``, before the navigation snapshots section
|
|
30
|
+
titles, and again at ``doctree-resolved``, the last stage before writing
|
|
31
|
+
— and deletes the artificial whitespace there. Parsing and the message
|
|
32
|
+
catalogs are never touched.
|
|
33
|
+
|
|
34
|
+
Newline-wrapped source counts as whitespace too, so markup at the start
|
|
35
|
+
of a continuation line is glued as well, mirroring sphinx-cjkspace's line
|
|
36
|
+
joining.
|
|
37
|
+
|
|
38
|
+
The ``gettext`` builder is skipped, so ``.pot`` msgids stay verbatim for
|
|
39
|
+
translators; literal and math blocks are handled by the pass itself.
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
from __future__ import annotations
|
|
43
|
+
|
|
44
|
+
from typing import TYPE_CHECKING
|
|
45
|
+
|
|
46
|
+
from docutils import nodes
|
|
47
|
+
|
|
48
|
+
from ._doctree import fix_inline_boundaries
|
|
49
|
+
from ._version import __version__
|
|
50
|
+
|
|
51
|
+
if TYPE_CHECKING:
|
|
52
|
+
from sphinx.application import Sphinx
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def process_cjk_boundaries(
|
|
56
|
+
app: Sphinx, doctree: nodes.document, docname: str | None = None
|
|
57
|
+
) -> None:
|
|
58
|
+
"""Strip artificial boundary spaces from one document tree.
|
|
59
|
+
|
|
60
|
+
Connected to two events:
|
|
61
|
+
|
|
62
|
+
``doctree-read`` (priority 490, before the toctree collector's 500)
|
|
63
|
+
so the section titles snapshotted for the navigation are already
|
|
64
|
+
stripped, and ``doctree-resolved`` — the last stage before writing —
|
|
65
|
+
so the page itself is final too. Translations are in place by then:
|
|
66
|
+
the ``Locale`` transform re-parses every msgstr during reading, and
|
|
67
|
+
both passes are idempotent.
|
|
68
|
+
|
|
69
|
+
The ``gettext`` builder is skipped: message extraction reads
|
|
70
|
+
``rawsource`` (which this pass never touches), so there is nothing to
|
|
71
|
+
gain there.
|
|
72
|
+
"""
|
|
73
|
+
if app.builder.name == "gettext":
|
|
74
|
+
return
|
|
75
|
+
for parent in list(doctree.findall(nodes.Element)):
|
|
76
|
+
fix_inline_boundaries(parent)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def setup(app: Sphinx) -> dict[str, object]:
|
|
80
|
+
"""Register the extension with Sphinx."""
|
|
81
|
+
# Before the toctree collector snapshots section titles for the
|
|
82
|
+
# navigation (it connects to doctree-read at the default priority).
|
|
83
|
+
app.connect("doctree-read", process_cjk_boundaries, priority=490)
|
|
84
|
+
app.connect("doctree-resolved", process_cjk_boundaries)
|
|
85
|
+
return {
|
|
86
|
+
"version": __version__,
|
|
87
|
+
"parallel_read_safe": True,
|
|
88
|
+
"parallel_write_safe": True,
|
|
89
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: sphinx-cjk-aware
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: CJK-aware inline spacing for Sphinx: remove artificial spaces around inline markup in Chinese and Japanese text
|
|
5
|
+
Project-URL: Homepage, https://github.com/yufeiminds/sphinx-cjk-aware
|
|
6
|
+
Project-URL: Documentation, https://github.com/yufeiminds/sphinx-cjk-aware#readme
|
|
7
|
+
Project-URL: Changelog, https://github.com/yufeiminds/sphinx-cjk-aware/blob/main/CHANGELOG.md
|
|
8
|
+
Project-URL: Issues, https://github.com/yufeiminds/sphinx-cjk-aware/issues
|
|
9
|
+
Author-email: Yufei Li <yufeiminds@gmail.com>
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: chinese,cjk,docutils,i18n,inline,japanese,spacing,sphinx
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Framework :: Sphinx :: Extension
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
23
|
+
Classifier: Topic :: Documentation
|
|
24
|
+
Classifier: Topic :: Documentation :: Sphinx
|
|
25
|
+
Classifier: Topic :: Text Processing :: Markup
|
|
26
|
+
Requires-Python: >=3.11
|
|
27
|
+
Requires-Dist: docutils<0.23,>=0.20
|
|
28
|
+
Requires-Dist: sphinx>=8.0
|
|
29
|
+
Provides-Extra: docs
|
|
30
|
+
Requires-Dist: furo>=2024.8.6; extra == 'docs'
|
|
31
|
+
Requires-Dist: myst-parser>=4.0; extra == 'docs'
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
|
|
34
|
+
# sphinx-cjk-aware
|
|
35
|
+
|
|
36
|
+
[](https://github.com/yufeiminds/sphinx-cjk-aware/actions/workflows/ci.yml)
|
|
37
|
+
[](https://pypi.org/project/sphinx-cjk-aware/)
|
|
38
|
+
[](https://pypi.org/project/sphinx-cjk-aware/)
|
|
39
|
+
[](LICENSE)
|
|
40
|
+
|
|
41
|
+
**CJK-aware inline spacing for Sphinx.** Removes the spaces that inline
|
|
42
|
+
markup needs in order to parse in Chinese and Japanese text, but that those
|
|
43
|
+
typographies do not want to show. Korean is left alone: its spaces are word
|
|
44
|
+
boundaries.
|
|
45
|
+
|
|
46
|
+
## Installation
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
uv add --dev sphinx-cjk-aware
|
|
50
|
+
# or
|
|
51
|
+
pip install sphinx-cjk-aware
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Usage
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
# conf.py
|
|
58
|
+
extensions = [
|
|
59
|
+
# ...
|
|
60
|
+
"sphinx_cjk_aware",
|
|
61
|
+
]
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
No configuration is required.
|
|
65
|
+
|
|
66
|
+
## The problem
|
|
67
|
+
|
|
68
|
+
reStructuredText — like CommonMark, cf.
|
|
69
|
+
[markdown-cjk-friendly](https://github.com/tats-u/markdown-cjk-friendly) —
|
|
70
|
+
recognises inline markup only when its delimiters sit next to whitespace or
|
|
71
|
+
punctuation. Chinese and Japanese characters count as neither, so emphasis
|
|
72
|
+
has to be padded to parse:
|
|
73
|
+
|
|
74
|
+
```rst
|
|
75
|
+
即它 *为什么* 有效 → 即它 <em>为什么</em> 有效
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
The padding then shows on the page. Translations hit the same thing in a
|
|
79
|
+
subtler way: Sphinx re-parses every `msgstr`, so `*...*` in a catalog becomes
|
|
80
|
+
real emphasis — together with the spaces that made it recognisable.
|
|
81
|
+
|
|
82
|
+
With this extension the rendered result is:
|
|
83
|
+
|
|
84
|
+
```html
|
|
85
|
+
即它<em>为什么</em>有效
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## What gets fixed
|
|
89
|
+
|
|
90
|
+
A space is removed at the seam between text and an inline element
|
|
91
|
+
(emphasis, strong, literal, link, role, …) when **both** sides belong to a
|
|
92
|
+
script written without word spaces:
|
|
93
|
+
|
|
94
|
+
| Source (or `msgstr`) | Renders as |
|
|
95
|
+
|---|---|
|
|
96
|
+
| `即它 *为什么* 有效。` | `即它<em>为什么</em>有效。` |
|
|
97
|
+
| `都兼具 **实践表达** 与 **概念把握**。` | `都兼具<strong>实践表达</strong>与<strong>概念把握</strong>。` |
|
|
98
|
+
| `句子 **粗体** 中文。` | `句子<strong>粗体</strong>中文。` |
|
|
99
|
+
| `英文。 **粗体** 结尾。` | `英文。<strong>粗体</strong>结尾。` |
|
|
100
|
+
| `**这些星号。** 都是因为这个句子。` | `<strong>这些星号。</strong>都是因为这个句子。` |
|
|
101
|
+
| `句子 **粗体** (注)结尾。` | `句子<strong>粗体</strong>(注)结尾。` |
|
|
102
|
+
| `**甲** **乙** 并列。` | `<strong>甲</strong><strong>乙</strong>并列。` |
|
|
103
|
+
| `它 *行之有效* —— 无论用户。` | `它<em>行之有效</em>—— 无论用户。` |
|
|
104
|
+
| `这是 ``内嵌代码`` 示例。` | `这是<code>内嵌代码</code>示例。` |
|
|
105
|
+
| `小节 *强调* 标题` | heading and navigation both glued |
|
|
106
|
+
|
|
107
|
+
It also handles source lines wrapped before markup, fullwidth space (U+3000),
|
|
108
|
+
runs of spaces, and NUL-escaped spaces (`\ `, common in translated msgstrs).
|
|
109
|
+
|
|
110
|
+
## What is left alone
|
|
111
|
+
|
|
112
|
+
- **Korean** — `요리를 하는 *동안* 읽는 책` keeps both spaces: they are word
|
|
113
|
+
boundaries, not markup padding.
|
|
114
|
+
- **English and other Latin-script prose**, including the desirable
|
|
115
|
+
CJK↔Latin space: `中文 *English* 中文`.
|
|
116
|
+
- **Wide emoji**, European accented letters, and symbols.
|
|
117
|
+
- **Literal content** — `code-block`, `parsed-literal`, doctest blocks and
|
|
118
|
+
math, where whitespace is significant.
|
|
119
|
+
- **Plain-text spaces** that are not adjacent to inline markup.
|
|
120
|
+
|
|
121
|
+
## What counts as unspaced text
|
|
122
|
+
|
|
123
|
+
Han characters (Chinese, and Japanese kanji — they share code points),
|
|
124
|
+
hiragana, katakana, bopomofo, and CJK punctuation: `。、「」()!:`,
|
|
125
|
+
fullwidth forms, and `—— …… “” ·`. Hangul, Latin, emoji and other scripts do
|
|
126
|
+
not qualify.
|
|
127
|
+
|
|
128
|
+
## How it works
|
|
129
|
+
|
|
130
|
+
Following [sphinx-cjkspace](https://github.com/seisman/sphinx-cjkspace), the
|
|
131
|
+
extension fixes the resolved doctree at the last stage instead of changing
|
|
132
|
+
the parser:
|
|
133
|
+
|
|
134
|
+
- `doctree-read` (priority 490, before the toctree collector snapshots
|
|
135
|
+
section titles) — so headings and the navigation agree;
|
|
136
|
+
- `doctree-resolved` — the last stage before writing.
|
|
137
|
+
|
|
138
|
+
Parsing, sources and message catalogs are never touched: the `gettext`
|
|
139
|
+
builder is skipped, so `.pot` msgids stay verbatim for translators.
|
|
140
|
+
|
|
141
|
+
## Compatibility
|
|
142
|
+
|
|
143
|
+
- Sphinx ≥ 8, docutils 0.20–0.22
|
|
144
|
+
- Python ≥ 3.11
|
|
145
|
+
- No configuration, no extra runtime dependencies
|
|
146
|
+
|
|
147
|
+
## Development
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
uv sync --all-extras # create the environment
|
|
151
|
+
uv run pytest # tests
|
|
152
|
+
uv run ruff check . # lint
|
|
153
|
+
uv run ruff format . # format
|
|
154
|
+
uv run sphinx-build -W -b html docs docs/_build/html # docs
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
Releases are published to PyPI from GitHub Actions using
|
|
158
|
+
[Trusted Publishing](https://docs.pypi.org/trusted-publishers/) (OIDC, no
|
|
159
|
+
tokens). The GitHub Release notes are generated by
|
|
160
|
+
[git-cliff](https://git-cliff.org/) from the commits since the previous
|
|
161
|
+
release, grouped by Conventional Commit type. See
|
|
162
|
+
[`.github/workflows/release.yml`](.github/workflows/release.yml) and
|
|
163
|
+
[`cliff.toml`](cliff.toml).
|
|
164
|
+
|
|
165
|
+
## License
|
|
166
|
+
|
|
167
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
sphinx_cjk_aware/__init__.py,sha256=KYYrpEMnUJkjB1nY0i2K9zMaRet_g4_nf8SEjQ-Al2s,481
|
|
2
|
+
sphinx_cjk_aware/_charclass.py,sha256=tQF6C7O9vp4Qzj_-drFUKUzAoSiz3jyyzctwQFg_zSA,2736
|
|
3
|
+
sphinx_cjk_aware/_doctree.py,sha256=vmUJFHSYyq_aaFBlvVT9ryz_TSKUh4w35S66eRVdsTc,5161
|
|
4
|
+
sphinx_cjk_aware/_extension.py,sha256=qsp_Hvqhh14vMu6H7thIx0sTj6stP0oYlVsMxn7Yf2o,3563
|
|
5
|
+
sphinx_cjk_aware/_version.py,sha256=aS7mqsvUzDayg5zXKJXMWfkHOnCIGfmIsbMACSz6GXY,183
|
|
6
|
+
sphinx_cjk_aware/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
+
sphinx_cjk_aware-0.1.0.dist-info/METADATA,sha256=B8lz3ujeuHirmOUlYOCHzJS6xMD-GpjgNYvzWeBjUU8,6586
|
|
8
|
+
sphinx_cjk_aware-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
9
|
+
sphinx_cjk_aware-0.1.0.dist-info/licenses/LICENSE,sha256=gBO9a_o5dDE6i61Cmywg8USZHLSuP_ap3kQ4VjknIN0,1065
|
|
10
|
+
sphinx_cjk_aware-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Yufei Li
|
|
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.
|