Skip to content

Commit f4ca9a3

Browse files
authored
Merge pull request #25 from hao65103940/local_main
feat:html格式化工具
2 parents 07167f5 + 4680fac commit f4ca9a3

File tree

4 files changed

+116
-0
lines changed

4 files changed

+116
-0
lines changed
2.48 KB
Binary file not shown.

tools/html_compression/README.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# HTML内容处理工具 - 使用说明
2+
3+
## 一、项目介绍
4+
这是一个用于HTML内容清理和压缩的Python工具,能够自动处理HTML代码,移除不必要的标签、注释和空白字符,输出干净、紧凑的HTML格式。
5+
6+
## 二、核心功能
7+
- **自动清理标签**:移除最外层的`<html_rander>`包装标签
8+
- **智能压缩**:删除HTML注释,压缩多余空格和换行
9+
- **格式优化**:保持HTML结构的同时最小化代码体积
10+
- **保留内容**:不改变原始文本内容和标签属性
11+
12+
## 三、入参说明
13+
### 函数定义
14+
```python
15+
def process_html_content(html_content: str) -> str
16+
```
17+
## 参数详细说明
18+
19+
| 参数名 | 类型 | 必填 | 默认值 | 说明 | 示例 |
20+
|--------|------|------|--------|------|------|
21+
| `html_content` | `str` ||| 需要处理的原始HTML内容,可以包含或不包含 `<html_rander>` 标签 | `<html_rander><div>内容</div></html_rander>``<div>内容</div>` |
22+
23+
### 支持的输入格式:
24+
- 包含 `<html_rander>` 标签的完整内容
25+
- 普通 HTML 片段
26+
- 带有多余空格、换行和注释的 HTML
27+
- 任意合法的 HTML 字符串
28+
29+
30+
## 四、出参说明
31+
32+
### 返回结果
33+
| 属性 | 类型 | 说明 | 示例 |
34+
|------|------|------|------|
35+
| 返回值 | `str` | 处理后的压缩HTML字符串 | `<div class="test"><p>内容</p></div>` |
36+
37+
38+
## 五、使用示例
39+
### 示例1:基础用法
40+
```python
41+
from html_processor import process_html_content
42+
43+
# 输入HTML
44+
html_input = '''
45+
<html_rander>
46+
<div class="container">
47+
<p>Hello World</p>
48+
</div>
49+
</html_rander>
50+
'''
51+
52+
# 处理HTML
53+
result = process_html_content(html_input)
54+
print(result)
55+
# 输出:<div class="container"><p>Hello World</p></div>
56+
```
57+
### 示例2:处理带注释的HTML
58+
```python
59+
html_input = '''
60+
<html_rander>
61+
<!-- 这是注释 -->
62+
<section>
63+
<h1>标题</h1>
64+
<p>内容 段落</p>
65+
</section>
66+
</html_rander>
67+
'''
68+
69+
result = process_html_content(html_input)
70+
print(result)
71+
# 输出:<section><h1>标题</h1><p>内容 段落</p></section>
72+
```
73+
### 示例3:处理带多余空格和换行的HTML
74+
```python
75+
76+
html_input = '''
77+
<div>
78+
<ul>
79+
<li>项目1</li>
80+
<li>项目2</li>
81+
</ul>
82+
</div>
83+
'''
84+
85+
result = process_html_content(html_input)
86+
print(result)
87+
# 输出:<div><ul><li>项目1</li><li>项目2</li></ul></div>
88+
89+
```
90+
### 示例4:处理带属性的HTML
91+
```python
92+
html_input = '''
93+
<html_rander>
94+
<article id="post-123">
95+
<header>
96+
<h2 class="title">文章标题</h2>
97+
<time>2024-01-01</time>
98+
</header>
99+
<div class="content">
100+
<p>第一段文字。</p>
101+
<p>第二段 文字。</p>
102+
<!-- 广告位 -->
103+
<div class="ad"></div>
104+
</div>
105+
</article>
106+
</html_rander>
107+
'''
108+
result = process_html_content(html_input)
109+
print(result)
110+
# 输出:<article id="post-123"><header><h2 class="title">文章标题</h2><time>2024-01-01</time></header><div class="content"><p>第一段文字。</p><p>第二段 文字。</p><div class="ad"></div></div></article>
111+
```

tools/html_compression/data.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: HTML 压缩工具
2+
tags:
3+
- 内容处理
4+
title: HTML 压缩工具
5+
description: 用于压缩HTML内容,常用于 HTML 转 PDF 搭配使用。

tools/html_compression/logo.png

2.31 MB
Loading

0 commit comments

Comments
 (0)