fix: render strong emphasis beside CJK punctuation
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# MDHelper
|
||||
|
||||
当前版本:`0.2.2`
|
||||
当前版本:`0.2.3`
|
||||
|
||||
MDHelper 是一个本地 Markdown / 代码文档阅读器,使用 PySide6 构建桌面窗口,用 Qt WebEngine 展示由 Markdown 转换得到的 HTML 页面。它面向本地项目文档、技术手册、源码说明和离线帮助文件,重点是离线可用、多标签阅读、全文搜索、样式管理和导出。
|
||||
|
||||
@@ -10,6 +10,7 @@ MDHelper 是一个本地 Markdown / 代码文档阅读器,使用 PySide6 构
|
||||
- 支持同时运行多个程序窗口,分别查看不同文件夹,互不影响。
|
||||
- 浏览 Markdown、HTML/网页文件,并可在设置中选择展示 Python、C/C++、Fortran 等代码文件。
|
||||
- Markdown 支持标题、表格、引用、任务列表、提示块、链接、图片、GIF、音频、视频、代码块、Mermaid 图表和数学公式。
|
||||
- 兼容中文标点紧邻双星号的加粗写法,例如 `**注意:**请先核对`,阅读器与 Word 导出均可正确显示。
|
||||
- 数学公式支持 KaTeX 和 MathJax,可在菜单中切换。
|
||||
- 代码块和代码文件使用 Pygments 高亮,代码块支持行号、复制、展开/收起。
|
||||
- Python 文件可提取类/函数作为目录;C/C++ 文件可提取基础函数、类、结构体目录;Fortran 文件可提取 program、module、type、subroutine、function 目录。
|
||||
@@ -116,7 +117,7 @@ MDHelper 是一个本地 Markdown / 代码文档阅读器,使用 PySide6 构
|
||||
首次缺少 Inno Setup 时会从官方来源下载、校验发布者签名并安装到 `pack\.tools`;后续构建复用。
|
||||
需要可用的 64 位 Python 3.10+;兼容当前项目的两种 `mdhelper_env` 目录布局。
|
||||
|
||||
安装程序位于 `pack\output\MDHelper-Setup-0.2.2-x64.exe`(版本号自动读取),同目录包含 SHA256 校验文件。
|
||||
安装程序位于 `pack\output\MDHelper-Setup-0.2.3-x64.exe`(版本号自动读取),同目录包含 SHA256 校验文件。
|
||||
默认按当前用户安装,提供开始菜单入口、可选桌面快捷方式,以及 Windows 卸载入口。
|
||||
程序运行时无需单独安装 Python 或 Pandoc。
|
||||
|
||||
|
||||
+2
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
MDHelper 是一个本地 Markdown / 代码文档阅读器,适合阅读项目说明、技术手册、源码旁文档和离线帮助文件。它使用 PySide6 提供桌面窗口,用 Qt WebEngine 渲染 Markdown 转换后的 HTML 页面,并在本地完成全文搜索、样式管理、导出和自动刷新。
|
||||
|
||||
当前版本:`0.2.2`
|
||||
当前版本:`0.2.3`
|
||||
|
||||
## 1. 适用场景
|
||||
|
||||
@@ -52,6 +52,7 @@ MDHelper 支持同时运行多个程序窗口。不同窗口可以打开不同
|
||||
Markdown 支持:
|
||||
|
||||
- 标题、段落、列表、引用、表格、任务列表。
|
||||
- 中文加粗支持 `**先完成一项指标样稿。**选择一个指标` 这类标点后直接接正文的写法,也支持中文引号、括号与嵌套斜体。代码块、行内代码、公式和转义的星号保持原样。
|
||||
- 提示块:`note`、`tip`、`important`、`warning`、`caution`。
|
||||
- 代码块高亮、行号、复制、展开/收起。
|
||||
- Mermaid 图表。
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
"""MDHelper package."""
|
||||
|
||||
__version__ = "0.2.2"
|
||||
__version__ = "0.2.3"
|
||||
|
||||
@@ -13,6 +13,8 @@ from xml.sax.saxutils import escape
|
||||
from markdown_it import MarkdownIt
|
||||
from markdown_it.token import Token
|
||||
|
||||
from .markdown_compat import cjk_strong_plugin
|
||||
|
||||
|
||||
WP_NS = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
|
||||
DOCX_EXPORT_ENGINES = {
|
||||
@@ -163,7 +165,7 @@ def export_markdown_to_docx_python_docx(source: str, output_path: Path) -> Path:
|
||||
|
||||
|
||||
def _markdown_blocks(source: str) -> list[ParagraphBlock | TableBlock]:
|
||||
markdown = MarkdownIt("commonmark", {"html": True}).enable(["table", "strikethrough"])
|
||||
markdown = MarkdownIt("commonmark", {"html": True}).enable(["table", "strikethrough"]).use(cjk_strong_plugin)
|
||||
return _DocxBuilder(markdown.parse(source)).build()
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
"""Small, shared Markdown compatibility rules for East Asian prose."""
|
||||
from __future__ import annotations
|
||||
|
||||
import unicodedata
|
||||
|
||||
from markdown_it import MarkdownIt
|
||||
from markdown_it.rules_inline import emphasis
|
||||
from markdown_it.rules_inline.state_inline import StateInline
|
||||
|
||||
|
||||
def _wide_letter(char: str) -> bool:
|
||||
return unicodedata.east_asian_width(char) in ("W", "F") and unicodedata.category(char)[0] in ("L", "N")
|
||||
|
||||
|
||||
def _punctuation(char: str) -> bool:
|
||||
return unicodedata.category(char).startswith("P")
|
||||
|
||||
|
||||
def _cjk_punctuation_boundary(punctuation: str, outside: str, inside: str) -> bool:
|
||||
return _punctuation(punctuation) and (
|
||||
unicodedata.east_asian_width(punctuation) in ("W", "F")
|
||||
or _wide_letter(outside)
|
||||
or _wide_letter(inside)
|
||||
)
|
||||
|
||||
|
||||
def _tokenize_emphasis(state: StateInline, silent: bool) -> bool:
|
||||
start, first_delimiter = state.pos, len(state.delimiters)
|
||||
matched = emphasis.tokenize(state, silent)
|
||||
if not matched or state.src[start] != "*" or state.pos - start < 2:
|
||||
return matched
|
||||
|
||||
def char_at(position: int) -> str:
|
||||
return state.src[position] if 0 <= position < state.posMax else " "
|
||||
|
||||
before, after = char_at(start - 1), char_at(state.pos)
|
||||
# Keep the upstream tokenizer/pairing algorithm, including nesting and the
|
||||
# rule of three. Relax only punctuation boundaries of ** (or *** etc.) in
|
||||
# CJK prose. Escapes, code, links and protected math never reach this rule.
|
||||
allow_open = not before.isspace() and _cjk_punctuation_boundary(after, before, char_at(state.pos + 1))
|
||||
allow_close = not after.isspace() and _cjk_punctuation_boundary(before, after, char_at(start - 2))
|
||||
if allow_open or allow_close:
|
||||
for delimiter in state.delimiters[first_delimiter:]:
|
||||
delimiter.open = delimiter.open or allow_open
|
||||
delimiter.close = delimiter.close or allow_close
|
||||
return matched
|
||||
|
||||
|
||||
def cjk_strong_plugin(markdown: MarkdownIt) -> None:
|
||||
markdown.inline.ruler.at("emphasis", _tokenize_emphasis)
|
||||
@@ -18,6 +18,7 @@ from pygments.lexers.special import TextLexer
|
||||
from pygments.util import ClassNotFound
|
||||
|
||||
from .markdown_utils import slugify_heading
|
||||
from .markdown_compat import cjk_strong_plugin
|
||||
from .media import is_qt_video_player_url, media_kind, media_mime_type
|
||||
|
||||
|
||||
@@ -44,7 +45,7 @@ class MarkdownRenderer:
|
||||
"typographer": False,
|
||||
"breaks": False,
|
||||
},
|
||||
).enable(["table", "strikethrough"])
|
||||
).enable(["table", "strikethrough"]).use(cjk_strong_plugin)
|
||||
|
||||
self.markdown.renderer.rules["fence"] = self._render_fence
|
||||
self.markdown.renderer.rules["code_block"] = self._render_code_block
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
import zipfile
|
||||
|
||||
import pytest
|
||||
from lxml import etree as ET
|
||||
from markdown_it import MarkdownIt
|
||||
|
||||
from mdhelper.markdown_compat import cjk_strong_plugin
|
||||
from mdhelper.markdown_renderer import MarkdownRenderer
|
||||
from mdhelper.docx_exporter import export_markdown_to_docx
|
||||
|
||||
|
||||
SCREENSHOT_SOURCE = '''1. **先完成一项指标样稿。**选择一个有代表性的指标,提交完整写法。
|
||||
2. **按分组开展调研。**每完成一组,检查指标覆盖情况。
|
||||
3. **提交PPT并开展分享。**原则上每组安排一次。
|
||||
4. **根据讨论修订。**将答辩中发现的问题补充到文档。
|
||||
'''
|
||||
|
||||
|
||||
@pytest.mark.parametrize("source,strong", [
|
||||
("**先完成一项指标样稿。**选择一个指标", "先完成一项指标样稿。"),
|
||||
("前文**中文。**后文", "中文。"),
|
||||
("前文**“术语”**后文", "“术语”"),
|
||||
("**注意:**请先核对", "注意:"),
|
||||
("**说明(可选)**补充", "说明(可选)"),
|
||||
("**中文:**下一段", "中文:"),
|
||||
('前文**"术语"**后文', '"术语"'),
|
||||
("**中文。**Next", "中文。"),
|
||||
("Prefix**“中文”**suffix", "“中文”"),
|
||||
])
|
||||
def test_cjk_strong_boundaries(source, strong):
|
||||
markdown = MarkdownIt("commonmark").use(cjk_strong_plugin)
|
||||
from html import escape
|
||||
result = markdown.render(source)
|
||||
assert "**" not in result
|
||||
assert "<strong>" + escape(strong) + "</strong>" in result
|
||||
|
||||
|
||||
def test_real_renderer_screenshot_list_and_adjacent_spans(tmp_path):
|
||||
renderer = MarkdownRenderer(tmp_path)
|
||||
rendered = renderer.render_source(SCREENSHOT_SOURCE, tmp_path / "sample.md")
|
||||
assert "**" not in renderer.markdown.render(SCREENSHOT_SOURCE)
|
||||
assert rendered.count("<strong>") == 4
|
||||
assert "<strong>先完成一项指标样稿。</strong>选择" in rendered
|
||||
assert renderer.markdown.render("**甲。**乙**丙。**丁").count("<strong>") == 2
|
||||
assert "<em><strong>中文。</strong></em>后文" in renderer.markdown.render("***中文。***后文")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("source", [
|
||||
r"\*\*中文。\*\*后文", "`**中文。**后文`", "```md\n**中文。**后文\n```",
|
||||
" **中文。**后文", "** 中文 **", "**中文 **", "**中文。", "*中文。*后文",
|
||||
"__中文。__后文", '**hello.**world', 'foo**"bar"**baz', '2 ** 3 ** 4',
|
||||
'<span title="**中文。**后文">text</span>', '[链接](https://example.com/**中文。**后文)',
|
||||
'<https://example.com/**中文。**后文>', '---\n\n****\n', "**普通加粗** 与 *普通斜体*",
|
||||
])
|
||||
def test_existing_markdown_and_literal_markers_stay_unchanged(source):
|
||||
normal = MarkdownIt("commonmark", {"html": True})
|
||||
compatible = MarkdownIt("commonmark", {"html": True}).use(cjk_strong_plugin)
|
||||
assert compatible.render(source) == normal.render(source)
|
||||
|
||||
|
||||
def test_math_is_still_protected(tmp_path):
|
||||
renderer = MarkdownRenderer(tmp_path)
|
||||
source = r'$\text{**说明。**后文} + x**2$'
|
||||
result = renderer.markdown.render(source)
|
||||
assert "math-inline" in result
|
||||
assert "<strong>" not in result
|
||||
assert "**说明。**后文" in result and "x**2" in result
|
||||
|
||||
|
||||
def test_headings_links_tables_and_nested_emphasis(tmp_path):
|
||||
renderer = MarkdownRenderer(tmp_path)
|
||||
source = '# **标题:**说明\n\n[**链接:**说明](https://example.com)\n\n| 项目 | 说明 |\n| --- | --- |\n| **术语:**正文 | **含*斜体*的句子。**继续 |\n'
|
||||
result = renderer.markdown.render(source)
|
||||
assert "**" not in result
|
||||
assert result.count("<strong>") == 4 and '<em>斜体</em>' in result
|
||||
|
||||
|
||||
@pytest.mark.parametrize("engine", ["builtin", "python-docx", "pandoc"])
|
||||
def test_word_export_preserves_cjk_bold_and_body_boundaries(tmp_path, engine):
|
||||
path = export_markdown_to_docx(SCREENSHOT_SOURCE, tmp_path / (engine + '.docx'), engine=engine)
|
||||
with zipfile.ZipFile(path) as package:
|
||||
root = ET.fromstring(package.read('word/document.xml'))
|
||||
ns = {'w': 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'}
|
||||
all_text = ''.join(root.xpath('//w:t/text()', namespaces=ns))
|
||||
bold_text = ''.join(root.xpath('//w:r[w:rPr/w:b[not(@w:val="0")]]/w:t/text()', namespaces=ns))
|
||||
assert '**' not in all_text
|
||||
for expected in ('先完成一项指标样稿。', '按分组开展调研。', '提交PPT并开展分享。', '根据讨论修订。'):
|
||||
assert expected in bold_text
|
||||
assert '选择一个' not in bold_text and '每完成一组' not in bold_text
|
||||
Reference in New Issue
Block a user