Metadata-Version: 2.5
Name: sphinx-cjk-aware
Version: 0.1.0
Summary: CJK-aware inline spacing for Sphinx: remove artificial spaces around inline markup in Chinese and Japanese text
Project-URL: Homepage, https://github.com/yufeiminds/sphinx-cjk-aware
Project-URL: Documentation, https://github.com/yufeiminds/sphinx-cjk-aware#readme
Project-URL: Changelog, https://github.com/yufeiminds/sphinx-cjk-aware/blob/main/CHANGELOG.md
Project-URL: Issues, https://github.com/yufeiminds/sphinx-cjk-aware/issues
Author-email: Yufei Li <yufeiminds@gmail.com>
License-Expression: MIT
License-File: LICENSE
Keywords: chinese,cjk,docutils,i18n,inline,japanese,spacing,sphinx
Classifier: Development Status :: 4 - Beta
Classifier: Framework :: Sphinx :: Extension
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Documentation
Classifier: Topic :: Documentation :: Sphinx
Classifier: Topic :: Text Processing :: Markup
Requires-Python: >=3.11
Requires-Dist: docutils<0.23,>=0.20
Requires-Dist: sphinx>=8.0
Provides-Extra: docs
Requires-Dist: furo>=2024.8.6; extra == 'docs'
Requires-Dist: myst-parser>=4.0; extra == 'docs'
Description-Content-Type: text/markdown

# sphinx-cjk-aware

[![CI](https://github.com/yufeiminds/sphinx-cjk-aware/actions/workflows/ci.yml/badge.svg)](https://github.com/yufeiminds/sphinx-cjk-aware/actions/workflows/ci.yml)
[![PyPI](https://img.shields.io/pypi/v/sphinx-cjk-aware)](https://pypi.org/project/sphinx-cjk-aware/)
[![Python](https://img.shields.io/pypi/pyversions/sphinx-cjk-aware)](https://pypi.org/project/sphinx-cjk-aware/)
[![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)

**CJK-aware inline spacing for Sphinx.** Removes the spaces that inline
markup needs in order to parse in Chinese and Japanese text, but that those
typographies do not want to show. Korean is left alone: its spaces are word
boundaries.

## Installation

```bash
uv add --dev sphinx-cjk-aware
# or
pip install sphinx-cjk-aware
```

## Usage

```python
# conf.py
extensions = [
    # ...
    "sphinx_cjk_aware",
]
```

No configuration is required.

## The problem

reStructuredText — like CommonMark, cf.
[markdown-cjk-friendly](https://github.com/tats-u/markdown-cjk-friendly) —
recognises inline markup only when its delimiters sit next to whitespace or
punctuation. Chinese and Japanese characters count as neither, so emphasis
has to be padded to parse:

```rst
即它 *为什么* 有效        →  即它 <em>为什么</em> 有效
```

The padding then shows on the page. Translations hit the same thing in a
subtler way: Sphinx re-parses every `msgstr`, so `*...*` in a catalog becomes
real emphasis — together with the spaces that made it recognisable.

With this extension the rendered result is:

```html
即它<em>为什么</em>有效
```

## What gets fixed

A space is removed at the seam between text and an inline element
(emphasis, strong, literal, link, role, …) when **both** sides belong to a
script written without word spaces:

| Source (or `msgstr`) | Renders as |
|---|---|
| `即它 *为什么* 有效。` | `即它<em>为什么</em>有效。` |
| `都兼具 **实践表达** 与 **概念把握**。` | `都兼具<strong>实践表达</strong>与<strong>概念把握</strong>。` |
| `句子 **粗体** 中文。` | `句子<strong>粗体</strong>中文。` |
| `英文。 **粗体** 结尾。` | `英文。<strong>粗体</strong>结尾。` |
| `**这些星号。** 都是因为这个句子。` | `<strong>这些星号。</strong>都是因为这个句子。` |
| `句子 **粗体** （注）结尾。` | `句子<strong>粗体</strong>（注）结尾。` |
| `**甲** **乙** 并列。` | `<strong>甲</strong><strong>乙</strong>并列。` |
| `它 *行之有效* —— 无论用户。` | `它<em>行之有效</em>—— 无论用户。` |
| `这是 ``内嵌代码`` 示例。` | `这是<code>内嵌代码</code>示例。` |
| `小节 *强调* 标题` | heading and navigation both glued |

It also handles source lines wrapped before markup, fullwidth space (U+3000),
runs of spaces, and NUL-escaped spaces (`\ `, common in translated msgstrs).

## What is left alone

- **Korean** — `요리를 하는 *동안* 읽는 책` keeps both spaces: they are word
  boundaries, not markup padding.
- **English and other Latin-script prose**, including the desirable
  CJK↔Latin space: `中文 *English* 中文`.
- **Wide emoji**, European accented letters, and symbols.
- **Literal content** — `code-block`, `parsed-literal`, doctest blocks and
  math, where whitespace is significant.
- **Plain-text spaces** that are not adjacent to inline markup.

## What counts as unspaced text

Han characters (Chinese, and Japanese kanji — they share code points),
hiragana, katakana, bopomofo, and CJK punctuation: `。、「」（）！：`,
fullwidth forms, and `—— …… “” ·`. Hangul, Latin, emoji and other scripts do
not qualify.

## How it works

Following [sphinx-cjkspace](https://github.com/seisman/sphinx-cjkspace), the
extension fixes the resolved doctree at the last stage instead of changing
the parser:

- `doctree-read` (priority 490, before the toctree collector snapshots
  section titles) — so headings and the navigation agree;
- `doctree-resolved` — the last stage before writing.

Parsing, sources and message catalogs are never touched: the `gettext`
builder is skipped, so `.pot` msgids stay verbatim for translators.

## Compatibility

- Sphinx ≥ 8, docutils 0.20–0.22
- Python ≥ 3.11
- No configuration, no extra runtime dependencies

## Development

```bash
uv sync --all-extras      # create the environment
uv run pytest             # tests
uv run ruff check .       # lint
uv run ruff format .      # format
uv run sphinx-build -W -b html docs docs/_build/html   # docs
```

Releases are published to PyPI from GitHub Actions using
[Trusted Publishing](https://docs.pypi.org/trusted-publishers/) (OIDC, no
tokens). The GitHub Release notes are generated by
[git-cliff](https://git-cliff.org/) from the commits since the previous
release, grouped by Conventional Commit type. See
[`.github/workflows/release.yml`](.github/workflows/release.yml) and
[`cliff.toml`](cliff.toml).

## License

MIT — see [LICENSE](LICENSE).
