wechat-article-parser 0.0.12__tar.gz → 0.0.13__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.
- {wechat_article_parser-0.0.12 → wechat_article_parser-0.0.13}/.gitignore +2 -1
- {wechat_article_parser-0.0.12 → wechat_article_parser-0.0.13}/PKG-INFO +4 -1
- {wechat_article_parser-0.0.12 → wechat_article_parser-0.0.13}/README.md +3 -0
- {wechat_article_parser-0.0.12 → wechat_article_parser-0.0.13}/pyproject.toml +1 -1
- {wechat_article_parser-0.0.12 → wechat_article_parser-0.0.13}/src/wechat_article_parser/parser.py +30 -2
- {wechat_article_parser-0.0.12 → wechat_article_parser-0.0.13}/tests/test_markdown.py +48 -1
- {wechat_article_parser-0.0.12 → wechat_article_parser-0.0.13}/tests/test_parser.py +2 -0
- {wechat_article_parser-0.0.12 → wechat_article_parser-0.0.13}/LICENSE +0 -0
- {wechat_article_parser-0.0.12 → wechat_article_parser-0.0.13}/src/wechat_article_parser/__init__.py +0 -0
- {wechat_article_parser-0.0.12 → wechat_article_parser-0.0.13}/src/wechat_article_parser/models.py +0 -0
- {wechat_article_parser-0.0.12 → wechat_article_parser-0.0.13}/tests/conftest.py +0 -0
- {wechat_article_parser-0.0.12 → wechat_article_parser-0.0.13}/tests/test_miniprogram_content.py +0 -0
- {wechat_article_parser-0.0.12 → wechat_article_parser-0.0.13}/tests/test_models.py +0 -0
- {wechat_article_parser-0.0.12 → wechat_article_parser-0.0.13}/tests/test_paid_content.py +0 -0
- {wechat_article_parser-0.0.12 → wechat_article_parser-0.0.13}/tests/test_product_content.py +0 -0
- {wechat_article_parser-0.0.12 → wechat_article_parser-0.0.13}/tests/test_profile_content.py +0 -0
- {wechat_article_parser-0.0.12 → wechat_article_parser-0.0.13}/tests/test_repost_content.py +0 -0
- {wechat_article_parser-0.0.12 → wechat_article_parser-0.0.13}/tests/test_svg_content.py +0 -0
- {wechat_article_parser-0.0.12 → wechat_article_parser-0.0.13}/tests/test_swiper_content.py +0 -0
- {wechat_article_parser-0.0.12 → wechat_article_parser-0.0.13}/tests/test_type_routing.py +0 -0
- {wechat_article_parser-0.0.12 → wechat_article_parser-0.0.13}/tests/test_user_name.py +0 -0
- {wechat_article_parser-0.0.12 → wechat_article_parser-0.0.13}/tests/test_video_content.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: wechat-article-parser
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.13
|
|
4
4
|
Summary: WeChat MP article parser - extract metadata and content from WeChat public account articles
|
|
5
5
|
License-Expression: MIT
|
|
6
6
|
License-File: LICENSE
|
|
@@ -31,6 +31,9 @@ Description-Content-Type: text/markdown
|
|
|
31
31
|
`8` 对应图片轮播、`10` 对应文字消息,再通过 HTML 或脚本数据区分具体模板。
|
|
32
32
|
类型字段缺失、值未知或对应分支未提取到正文时,会按原有 HTML 特征匹配顺序兜底。
|
|
33
33
|
|
|
34
|
+
Markdown 转换前会展开无格式作用的 `span`,合并嵌套的重复加粗、斜体和删除线标签,
|
|
35
|
+
避免编辑器产生的数百层冗余行内标签导致递归超限,并保留文字、链接、图片和段落顺序。
|
|
36
|
+
|
|
34
37
|
图文正文仅包含视频号视频卡片或微信视频 iframe 时,Markdown 返回“这是一篇纯视频内容。”。
|
|
35
38
|
正文只有付费预览占位标签时,Markdown 返回“这是一篇付费内容,正文需付费后查看。”,
|
|
36
39
|
并保留公开元数据;已有的可见预览正文仍正常提取。
|
|
@@ -16,6 +16,9 @@
|
|
|
16
16
|
`8` 对应图片轮播、`10` 对应文字消息,再通过 HTML 或脚本数据区分具体模板。
|
|
17
17
|
类型字段缺失、值未知或对应分支未提取到正文时,会按原有 HTML 特征匹配顺序兜底。
|
|
18
18
|
|
|
19
|
+
Markdown 转换前会展开无格式作用的 `span`,合并嵌套的重复加粗、斜体和删除线标签,
|
|
20
|
+
避免编辑器产生的数百层冗余行内标签导致递归超限,并保留文字、链接、图片和段落顺序。
|
|
21
|
+
|
|
19
22
|
图文正文仅包含视频号视频卡片或微信视频 iframe 时,Markdown 返回“这是一篇纯视频内容。”。
|
|
20
23
|
正文只有付费预览占位标签时,Markdown 返回“这是一篇付费内容,正文需付费后查看。”,
|
|
21
24
|
并保留公开元数据;已有的可见预览正文仍正常提取。
|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "wechat-article-parser"
|
|
7
|
-
version = "0.0.
|
|
7
|
+
version = "0.0.13"
|
|
8
8
|
description = "WeChat MP article parser - extract metadata and content from WeChat public account articles"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.10"
|
{wechat_article_parser-0.0.12 → wechat_article_parser-0.0.13}/src/wechat_article_parser/parser.py
RENAMED
|
@@ -53,8 +53,36 @@ class _MarkdownConverter(MarkdownConverter):
|
|
|
53
53
|
return super().convert_th(el, text.replace("|", r"\|"), parent_tags)
|
|
54
54
|
|
|
55
55
|
|
|
56
|
+
def _prepare_markdown_soup(html: str) -> BeautifulSoup:
|
|
57
|
+
"""展开编辑器产生的冗余行内标签,避免 Markdown 转换时递归超限。"""
|
|
58
|
+
soup = BeautifulSoup(html, "html.parser")
|
|
59
|
+
styles = {
|
|
60
|
+
"strong": "bold", "b": "bold",
|
|
61
|
+
"em": "italic", "i": "italic",
|
|
62
|
+
"s": "strike", "del": "strike",
|
|
63
|
+
}
|
|
64
|
+
# 显式栈遍历,清理过程本身也不依赖 Python 的递归深度。
|
|
65
|
+
stack = [(soup, frozenset())]
|
|
66
|
+
to_unwrap = []
|
|
67
|
+
while stack:
|
|
68
|
+
node, active_styles = stack.pop()
|
|
69
|
+
style = styles.get(node.name)
|
|
70
|
+
if node.name == "span" or (style and style in active_styles):
|
|
71
|
+
to_unwrap.append(node)
|
|
72
|
+
if style:
|
|
73
|
+
active_styles = active_styles | {style}
|
|
74
|
+
stack.extend(
|
|
75
|
+
(child, active_styles) for child in node.children if isinstance(child, Tag)
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
# 先处理内层,保留标签中的文字、链接、图片和它们的原始顺序。
|
|
79
|
+
for node in reversed(to_unwrap):
|
|
80
|
+
node.unwrap()
|
|
81
|
+
return soup
|
|
82
|
+
|
|
83
|
+
|
|
56
84
|
def _to_markdown(html: str) -> str:
|
|
57
|
-
return _MarkdownConverter().
|
|
85
|
+
return _MarkdownConverter().convert_soup(_prepare_markdown_soup(html))
|
|
58
86
|
|
|
59
87
|
|
|
60
88
|
def _to_markdown_plain(html: str) -> str:
|
|
@@ -62,7 +90,7 @@ def _to_markdown_plain(html: str) -> str:
|
|
|
62
90
|
|
|
63
91
|
这些内容由 <br> 承载换行,使用基础转换器保留换行。
|
|
64
92
|
"""
|
|
65
|
-
return MarkdownConverter().
|
|
93
|
+
return MarkdownConverter().convert_soup(_prepare_markdown_soup(html))
|
|
66
94
|
|
|
67
95
|
|
|
68
96
|
# ---------------------------------------------------------------------------
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"""富文本 Markdown 转换的离线回归测试。"""
|
|
2
2
|
|
|
3
|
+
import sys
|
|
4
|
+
|
|
3
5
|
import pytest
|
|
4
6
|
|
|
5
|
-
from wechat_article_parser.parser import _parse_html
|
|
7
|
+
from wechat_article_parser.parser import _parse_html, _to_markdown_plain
|
|
6
8
|
|
|
7
9
|
|
|
8
10
|
def _convert(body: str) -> str:
|
|
@@ -83,3 +85,48 @@ def test_literal_asterisks_in_source_stay_literal_and_inline() -> None:
|
|
|
83
85
|
'<sup><span>[1]</span></sup><span>**:Nous Research 的自主 AI Agent</span>'
|
|
84
86
|
'</section></li></ul>'
|
|
85
87
|
) == r'* \*\*Hermes[1]\*\*:Nous Research 的自主 AI Agent'
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
@pytest.mark.parametrize("convert", [_convert, _to_markdown_plain])
|
|
91
|
+
def test_deep_editor_wrappers_preserve_content_and_formatting(convert) -> None:
|
|
92
|
+
recursion_limit = sys.getrecursionlimit()
|
|
93
|
+
# 模拟真实文章反复粘贴产生的 span/strong 嵌套,超过默认递归上限。
|
|
94
|
+
body = (
|
|
95
|
+
'<p>前文</p><p>' + '<span><strong>' * 600
|
|
96
|
+
+ '加粗 <em>斜体</em> <a href="https://example.com">链接</a>'
|
|
97
|
+
+ '<br><img src="https://example.com/image.png" alt="图片">'
|
|
98
|
+
+ '</strong></span>' * 600 + '</p><p>后文</p>'
|
|
99
|
+
)
|
|
100
|
+
expected = convert(
|
|
101
|
+
'<p>前文</p><p><strong>加粗 <em>斜体</em> '
|
|
102
|
+
'<a href="https://example.com">链接</a><br>'
|
|
103
|
+
'<img src="https://example.com/image.png" alt="图片">'
|
|
104
|
+
'</strong></p><p>后文</p>'
|
|
105
|
+
)
|
|
106
|
+
assert convert(body) == expected
|
|
107
|
+
assert sys.getrecursionlimit() == recursion_limit
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def test_redundant_styles_do_not_leak_into_siblings() -> None:
|
|
111
|
+
assert _convert(
|
|
112
|
+
'<p><strong>粗<span><b>体</b></span></strong> 普通 '
|
|
113
|
+
'<em>斜<span><i>体</i></span></em> '
|
|
114
|
+
'<s>删<span><del>除</del></span></s></p>'
|
|
115
|
+
'<p><b>独立加粗</b></p>'
|
|
116
|
+
) == '**粗体** 普通 *斜体* ~~删除~~\n\n**独立加粗**'
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def test_deep_spans_preserve_code_whitespace_and_images() -> None:
|
|
120
|
+
body = (
|
|
121
|
+
'<pre><code>' + '<span>' * 1200 + ' a < b<br> next\n\n end'
|
|
122
|
+
+ '</span>' * 1200 + '</code></pre>'
|
|
123
|
+
'<p><span><img data-src="https://example.com/image.png"></span>尾文</p>'
|
|
124
|
+
)
|
|
125
|
+
result = _parse_html(
|
|
126
|
+
'https://mp.weixin.qq.com/s/test',
|
|
127
|
+
f'<div class="rich_media_content">{body}</div>',
|
|
128
|
+
)
|
|
129
|
+
assert result.article_markdown == (
|
|
130
|
+
'```\n a < b\n next\n\n end\n```\n\n尾文'
|
|
131
|
+
)
|
|
132
|
+
assert result.images == ['https://example.com/image.png']
|
|
File without changes
|
{wechat_article_parser-0.0.12 → wechat_article_parser-0.0.13}/src/wechat_article_parser/__init__.py
RENAMED
|
File without changes
|
{wechat_article_parser-0.0.12 → wechat_article_parser-0.0.13}/src/wechat_article_parser/models.py
RENAMED
|
File without changes
|
|
File without changes
|
{wechat_article_parser-0.0.12 → wechat_article_parser-0.0.13}/tests/test_miniprogram_content.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|