markconv 0.1.0__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.
@@ -0,0 +1,174 @@
1
+ Metadata-Version: 2.4
2
+ Name: markconv
3
+ Version: 0.1.0
4
+ Summary: 一个强大的 Markdown 转换工具库
5
+ Home-page: https://github.com/Zheng-Enci/markconv
6
+ Author: Zheng EnCi
7
+ Author-email: zheng_enci@qq.com
8
+ Project-URL: Bug Reports, https://github.com/Zheng-Enci/markconv/issues
9
+ Project-URL: Source, https://github.com/Zheng-Enci/markconv
10
+ Project-URL: Documentation, https://github.com/Zheng-Enci/markconv/blob/master/README.md
11
+ Keywords: markdown html converter
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Operating System :: OS Independent
19
+ Requires-Python: >=3.13
20
+ Description-Content-Type: text/markdown
21
+ Requires-Dist: markdown2>=2.5.5
22
+ Dynamic: author
23
+ Dynamic: author-email
24
+ Dynamic: classifier
25
+ Dynamic: description
26
+ Dynamic: description-content-type
27
+ Dynamic: home-page
28
+ Dynamic: keywords
29
+ Dynamic: project-url
30
+ Dynamic: requires-dist
31
+ Dynamic: requires-python
32
+ Dynamic: summary
33
+
34
+ # markconv
35
+
36
+ 一个强大的 Markdown 转换工具库。
37
+
38
+ **版本**: v0.1.0
39
+
40
+ ## 背景
41
+
42
+ 在日常开发中,我们经常需要将 Markdown 文档转换为其他格式(如 HTML、PDF、DOCX 等),但现有的解决方案存在诸多痛点:
43
+
44
+ - **配置繁琐**:许多库需要复杂的配置和依赖管理,学习成本高
45
+ - **中文支持差**:部分工具对中文显示支持不佳,需要额外配置字体和编码
46
+ - **样式定制困难**:自定义样式需要深入了解工具的内部机制
47
+ - **收费限制**:一些在线工具和商业软件需要付费,功能受限
48
+ - **依赖繁重**:某些库依赖过多,安装和使用都不够轻量
49
+
50
+ 为了解决这些问题,我开发了 **markconv** —— 一个简单、高效、免费的 Markdown 转换工具库。
51
+
52
+ ## 简介
53
+
54
+ markconv 是一个 Python 库,旨在提供简单易用的 Markdown 转换功能。该库支持将 Markdown 文档转换为多种格式,方便用户在不同场景下使用。
55
+
56
+ **当前版本支持**:
57
+ - Markdown → HTML
58
+
59
+ **计划支持**:
60
+ - Markdown → PDF
61
+ - Markdown → DOCX
62
+ - 更多格式...
63
+
64
+ ## 项目地址
65
+
66
+ - **Gitee**: https://gitee.com/zheng-enci050704/markconv
67
+ - **GitHub**: https://github.com/Zheng-Enci/markconv
68
+ - **GitCode**: https://gitcode.com/ZhengEnCi/markconv
69
+
70
+ ## 功能特性
71
+
72
+ - 📝 **简单易用**:提供简洁的 API 接口
73
+ - 🚀 **高性能**:基于高效的转换引擎
74
+ - 🔧 **可扩展**:支持自定义转换器和插件
75
+ - 📦 **轻量级**:最小化依赖,易于集成
76
+ - 🎨 **样式灵活**:支持自定义 CSS 样式
77
+ - 🌐 **中文友好**:内置中文字体支持,UTF-8 编码
78
+
79
+ ## 安装
80
+
81
+ ### 从源码安装
82
+
83
+ ```bash
84
+ # 选择一个源进行克隆
85
+ git clone https://github.com/Zheng-Enci/markconv.git
86
+ # 或
87
+ git clone https://gitee.com/zheng-enci050704/markconv.git
88
+ # 或
89
+ git clone https://gitcode.com/ZhengEnCi/markconv.git
90
+
91
+ # 安装依赖
92
+ cd markconv
93
+ pip install -r requirements.txt
94
+ ```
95
+
96
+ ### 使用安装脚本(推荐国内用户)
97
+
98
+ 项目提供了一个安装脚本,使用清华源加速依赖下载:
99
+
100
+ ```bash
101
+ cd markconv
102
+ python installl_requirements.py
103
+ ```
104
+
105
+ 该脚本会自动从清华源安装 `requirements.txt` 中的所有依赖项,速度更快。
106
+
107
+ ### 依赖项
108
+
109
+ - Python 3.13+
110
+
111
+ ## 快速开始
112
+
113
+ 查看 `examples/` 目录获取完整示例。
114
+
115
+ ## 开发计划
116
+
117
+ - [x] 实现 Markdown 到 HTML 的转换
118
+ - [x] 支持自定义 CSS 样式
119
+ - [x] 支持中文显示
120
+ - [x] 自动创建输出目录
121
+ - [ ] 添加更多代码高亮主题
122
+ - [ ] 支持自定义主题模板
123
+ - [ ] 添加命令行工具
124
+ - [ ] 完善单元测试
125
+ - [ ] 发布到 PyPI
126
+
127
+ ## 项目结构
128
+
129
+ ```
130
+ markconv/
131
+ ├── markconv/ # 主包目录
132
+ │ ├── __init__.py # 包初始化文件 (v0.1.0)
133
+ │ ├── html_converter.py # HTML转换器核心模块
134
+ │ ├── parsers/ # 解析器模块
135
+ │ │ ├── __init__.py
136
+ │ │ └── markdown_parser.py # Markdown解析器
137
+ │ └── exporters/ # 导出器模块
138
+ │ ├── __init__.py
139
+ │ └── html_exporter.py # HTML导出器
140
+ ├── examples/ # 示例代码
141
+ │ └── html_example/
142
+ │ ├── html_example.py # HTML转换示例
143
+ │ ├── sample.md # 示例Markdown文件
144
+ │ ├── custom.css # 自定义CSS样式
145
+ │ └── examples/
146
+ │ └── sample.html # 生成的HTML文件
147
+ ├── Note/ # 笔记目录
148
+ │ └── Update_logs/ # 更新日志
149
+ ├── README.md # 项目说明文档
150
+ ├── requirements.txt # 依赖列表
151
+ └── installl_requirements.py # 安装脚本
152
+ ```
153
+
154
+ ## 贡献指南
155
+
156
+ 欢迎贡献代码!请遵循以下步骤:
157
+
158
+ 1. Fork 本项目
159
+ 2. 创建特性分支 (`git checkout -b feature/AmazingFeature`)
160
+ 3. 提交更改 (`git commit -m 'Add some AmazingFeature'`)
161
+ 4. 推送到分支 (`git push origin feature/AmazingFeature`)
162
+ 5. 开启 Pull Request
163
+
164
+ ## 许可证
165
+
166
+ MIT License
167
+
168
+ ## 致谢
169
+
170
+ 感谢所有为本项目做出贡献的开发者!
171
+
172
+ ---
173
+
174
+ **注意**:本项目正在积极开发中,API 可能会有变化。建议关注版本更新日志。
@@ -0,0 +1,141 @@
1
+ # markconv
2
+
3
+ 一个强大的 Markdown 转换工具库。
4
+
5
+ **版本**: v0.1.0
6
+
7
+ ## 背景
8
+
9
+ 在日常开发中,我们经常需要将 Markdown 文档转换为其他格式(如 HTML、PDF、DOCX 等),但现有的解决方案存在诸多痛点:
10
+
11
+ - **配置繁琐**:许多库需要复杂的配置和依赖管理,学习成本高
12
+ - **中文支持差**:部分工具对中文显示支持不佳,需要额外配置字体和编码
13
+ - **样式定制困难**:自定义样式需要深入了解工具的内部机制
14
+ - **收费限制**:一些在线工具和商业软件需要付费,功能受限
15
+ - **依赖繁重**:某些库依赖过多,安装和使用都不够轻量
16
+
17
+ 为了解决这些问题,我开发了 **markconv** —— 一个简单、高效、免费的 Markdown 转换工具库。
18
+
19
+ ## 简介
20
+
21
+ markconv 是一个 Python 库,旨在提供简单易用的 Markdown 转换功能。该库支持将 Markdown 文档转换为多种格式,方便用户在不同场景下使用。
22
+
23
+ **当前版本支持**:
24
+ - Markdown → HTML
25
+
26
+ **计划支持**:
27
+ - Markdown → PDF
28
+ - Markdown → DOCX
29
+ - 更多格式...
30
+
31
+ ## 项目地址
32
+
33
+ - **Gitee**: https://gitee.com/zheng-enci050704/markconv
34
+ - **GitHub**: https://github.com/Zheng-Enci/markconv
35
+ - **GitCode**: https://gitcode.com/ZhengEnCi/markconv
36
+
37
+ ## 功能特性
38
+
39
+ - 📝 **简单易用**:提供简洁的 API 接口
40
+ - 🚀 **高性能**:基于高效的转换引擎
41
+ - 🔧 **可扩展**:支持自定义转换器和插件
42
+ - 📦 **轻量级**:最小化依赖,易于集成
43
+ - 🎨 **样式灵活**:支持自定义 CSS 样式
44
+ - 🌐 **中文友好**:内置中文字体支持,UTF-8 编码
45
+
46
+ ## 安装
47
+
48
+ ### 从源码安装
49
+
50
+ ```bash
51
+ # 选择一个源进行克隆
52
+ git clone https://github.com/Zheng-Enci/markconv.git
53
+ # 或
54
+ git clone https://gitee.com/zheng-enci050704/markconv.git
55
+ # 或
56
+ git clone https://gitcode.com/ZhengEnCi/markconv.git
57
+
58
+ # 安装依赖
59
+ cd markconv
60
+ pip install -r requirements.txt
61
+ ```
62
+
63
+ ### 使用安装脚本(推荐国内用户)
64
+
65
+ 项目提供了一个安装脚本,使用清华源加速依赖下载:
66
+
67
+ ```bash
68
+ cd markconv
69
+ python installl_requirements.py
70
+ ```
71
+
72
+ 该脚本会自动从清华源安装 `requirements.txt` 中的所有依赖项,速度更快。
73
+
74
+ ### 依赖项
75
+
76
+ - Python 3.13+
77
+
78
+ ## 快速开始
79
+
80
+ 查看 `examples/` 目录获取完整示例。
81
+
82
+ ## 开发计划
83
+
84
+ - [x] 实现 Markdown 到 HTML 的转换
85
+ - [x] 支持自定义 CSS 样式
86
+ - [x] 支持中文显示
87
+ - [x] 自动创建输出目录
88
+ - [ ] 添加更多代码高亮主题
89
+ - [ ] 支持自定义主题模板
90
+ - [ ] 添加命令行工具
91
+ - [ ] 完善单元测试
92
+ - [ ] 发布到 PyPI
93
+
94
+ ## 项目结构
95
+
96
+ ```
97
+ markconv/
98
+ ├── markconv/ # 主包目录
99
+ │ ├── __init__.py # 包初始化文件 (v0.1.0)
100
+ │ ├── html_converter.py # HTML转换器核心模块
101
+ │ ├── parsers/ # 解析器模块
102
+ │ │ ├── __init__.py
103
+ │ │ └── markdown_parser.py # Markdown解析器
104
+ │ └── exporters/ # 导出器模块
105
+ │ ├── __init__.py
106
+ │ └── html_exporter.py # HTML导出器
107
+ ├── examples/ # 示例代码
108
+ │ └── html_example/
109
+ │ ├── html_example.py # HTML转换示例
110
+ │ ├── sample.md # 示例Markdown文件
111
+ │ ├── custom.css # 自定义CSS样式
112
+ │ └── examples/
113
+ │ └── sample.html # 生成的HTML文件
114
+ ├── Note/ # 笔记目录
115
+ │ └── Update_logs/ # 更新日志
116
+ ├── README.md # 项目说明文档
117
+ ├── requirements.txt # 依赖列表
118
+ └── installl_requirements.py # 安装脚本
119
+ ```
120
+
121
+ ## 贡献指南
122
+
123
+ 欢迎贡献代码!请遵循以下步骤:
124
+
125
+ 1. Fork 本项目
126
+ 2. 创建特性分支 (`git checkout -b feature/AmazingFeature`)
127
+ 3. 提交更改 (`git commit -m 'Add some AmazingFeature'`)
128
+ 4. 推送到分支 (`git push origin feature/AmazingFeature`)
129
+ 5. 开启 Pull Request
130
+
131
+ ## 许可证
132
+
133
+ MIT License
134
+
135
+ ## 致谢
136
+
137
+ 感谢所有为本项目做出贡献的开发者!
138
+
139
+ ---
140
+
141
+ **注意**:本项目正在积极开发中,API 可能会有变化。建议关注版本更新日志。
@@ -0,0 +1,21 @@
1
+ """
2
+ mdtool - Markdown 转换工具库
3
+
4
+ 一个强大的 Python 库,用于将 Markdown 文件转换为 HTML 格式。
5
+
6
+ 主要功能:
7
+ - Markdown 到 HTML 的转换
8
+ - 批量文件转换
9
+ - 自定义样式支持
10
+ - 中文友好
11
+
12
+ Example:
13
+ >>> from mdtool import MDConverter
14
+ >>> converter = MDConverter()
15
+ >>> converter.to_html('input.md', 'output.html')
16
+ """
17
+
18
+ from .html_converter import MDConverter
19
+
20
+ __version__ = '0.1.0'
21
+ __all__ = ['MDConverter']
@@ -0,0 +1,9 @@
1
+ """
2
+ 导出器模块
3
+
4
+ 提供将解析后的 Markdown 数据导出为各种格式的功能
5
+ """
6
+
7
+ from .html_exporter import HTMLExporter
8
+
9
+ __all__ = ['HTMLExporter']
@@ -0,0 +1,229 @@
1
+ import os
2
+ from typing import Dict, Any
3
+ import markdown2
4
+
5
+
6
+ def _markdown_to_html(markdown_content: str) -> str:
7
+ """
8
+ 将 Markdown 内容转换为 HTML
9
+
10
+ 使用 markdown2 库进行转换,启用多种扩展功能
11
+
12
+ Args:
13
+ markdown_content (str): Markdown 格式的内容字符串
14
+
15
+ Returns:
16
+ str: 转换后的 HTML 内容
17
+
18
+ Note:
19
+ 启用的扩展功能包括:
20
+ - fenced-code-blocks: 围栏代码块
21
+ - tables: 表格支持
22
+ - toc: 目录生成
23
+ - code-friendly: 代码友好模式
24
+ - footnotes: 脚注
25
+ - strike: 删除线
26
+ - task_list: 任务列表
27
+ """
28
+ extras = [
29
+ 'fenced-code-blocks',
30
+ 'tables',
31
+ 'toc',
32
+ 'code-friendly',
33
+ 'footnotes',
34
+ 'strike',
35
+ 'task_list'
36
+ ]
37
+ return markdown2.markdown(markdown_content, extras=extras)
38
+
39
+
40
+ class HTMLExporter:
41
+ """
42
+ HTML 导出器类
43
+
44
+ 将解析后的 Markdown 数据转换为 HTML 文件
45
+
46
+ Attributes:
47
+ css_file (str): 自定义 CSS 样式文件路径,默认为 None
48
+ wrap_html (bool): 是否包装为完整的 HTML 文档,默认为 True
49
+ """
50
+
51
+ def __init__(self, css_file: str = None, wrap_html: bool = True):
52
+ """
53
+ 初始化 HTML 导出器
54
+
55
+ Args:
56
+ css_file (str): 自定义 CSS 样式文件路径,可选
57
+ wrap_html (bool): 是否包装为完整的 HTML 文档,默认为 True
58
+ 如果为 False,则只返回 HTML body 内容
59
+ """
60
+ self.css_file = css_file
61
+ self.wrap_html = wrap_html
62
+
63
+ def export(self, parsed_data: Dict[str, Any], output_path: str) -> None:
64
+ """
65
+ 将解析后的 Markdown 数据导出为 HTML 文件
66
+
67
+ 该方法执行以下步骤:
68
+ 1. 将 Markdown 内容转换为 HTML
69
+ 2. 如果需要,将 HTML 包装在完整的 HTML 文档结构中
70
+ 3. 将结果写入文件
71
+
72
+ Args:
73
+ parsed_data (Dict[str, Any]): 包含 Markdown 解析结果的字典
74
+ - content: Markdown 内容
75
+ - title: 文档标题(可选)
76
+ output_path (str): 输出 HTML 文件的路径
77
+ """
78
+ html_content = _markdown_to_html(parsed_data['content'])
79
+
80
+ if self.wrap_html:
81
+ html_content = self._wrap_html(html_content, parsed_data.get('title', 'Document'))
82
+
83
+ output_dir = os.path.dirname(output_path)
84
+ if output_dir and not os.path.exists(output_dir):
85
+ os.makedirs(output_dir, exist_ok=True)
86
+
87
+ with open(output_path, 'w', encoding='utf-8') as f:
88
+ f.write(html_content)
89
+
90
+ def export_to_string(self, parsed_data: Dict[str, Any]) -> str:
91
+ """
92
+ 将解析后的 Markdown 数据转换为 HTML 字符串
93
+
94
+ 与 export 方法类似,但返回字符串而不是写入文件
95
+
96
+ Args:
97
+ parsed_data (Dict[str, Any]): 包含 Markdown 解析结果的字典
98
+ - content: Markdown 内容
99
+ - title: 文档标题(可选)
100
+
101
+ Returns:
102
+ str: HTML 内容字符串
103
+ """
104
+ html_content = _markdown_to_html(parsed_data['content'])
105
+
106
+ if self.wrap_html:
107
+ html_content = self._wrap_html(html_content, parsed_data.get('title', 'Document'))
108
+
109
+ return html_content
110
+
111
+ def _wrap_html(self, body_content: str, title: str) -> str:
112
+ """
113
+ 将 HTML 内容包装在完整的 HTML 文档结构中
114
+
115
+ 添加 HTML 头部、元数据和样式,创建完整的 HTML 文档
116
+
117
+ Args:
118
+ body_content (str): HTML 主体内容
119
+ title (str): 文档标题
120
+
121
+ Returns:
122
+ str: 完整的 HTML 文档字符串
123
+
124
+ Note:
125
+ 内置样式包括:
126
+ - 中文字体支持(微软雅黑、黑体)
127
+ - 标题样式
128
+ - 代码块样式
129
+ - 表格样式
130
+ - 引用块样式
131
+ - 链接和图片样式
132
+ """
133
+ custom_css = self._get_custom_css()
134
+
135
+ return f"""
136
+ <!DOCTYPE html>
137
+ <html>
138
+ <head>
139
+ <meta charset="UTF-8">
140
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
141
+ <title>{title}</title>
142
+ <style>
143
+ body {{
144
+ font-family: 'Microsoft YaHei', 'SimHei', Arial, sans-serif;
145
+ line-height: 1.6;
146
+ margin: 20px;
147
+ color: #333;
148
+ max-width: 1200px;
149
+ margin: 0 auto;
150
+ padding: 20px;
151
+ }}
152
+ h1, h2, h3, h4, h5, h6 {{
153
+ color: #2c3e50;
154
+ margin-top: 20px;
155
+ margin-bottom: 10px;
156
+ }}
157
+ code {{
158
+ background-color: #f4f4f4;
159
+ padding: 2px 6px;
160
+ border-radius: 3px;
161
+ font-family: 'Consolas', 'Monaco', monospace;
162
+ }}
163
+ pre {{
164
+ background-color: #f4f4f4;
165
+ padding: 15px;
166
+ border-radius: 5px;
167
+ overflow-x: auto;
168
+ }}
169
+ pre code {{
170
+ background-color: transparent;
171
+ padding: 0;
172
+ }}
173
+ blockquote {{
174
+ border-left: 4px solid #ddd;
175
+ margin: 0;
176
+ padding-left: 20px;
177
+ color: #666;
178
+ }}
179
+ table {{
180
+ border-collapse: collapse;
181
+ width: 100%;
182
+ margin: 20px 0;
183
+ }}
184
+ th, td {{
185
+ border: 1px solid #ddd;
186
+ padding: 8px;
187
+ text-align: left;
188
+ }}
189
+ th {{
190
+ background-color: #f2f2f2;
191
+ }}
192
+ a {{
193
+ color: #3498db;
194
+ text-decoration: none;
195
+ }}
196
+ a:hover {{
197
+ text-decoration: underline;
198
+ }}
199
+ img {{
200
+ max-width: 100%;
201
+ height: auto;
202
+ }}
203
+ hr {{
204
+ border: none;
205
+ border-top: 1px solid #ddd;
206
+ margin: 20px 0;
207
+ }}
208
+ {custom_css}
209
+ </style>
210
+ </head>
211
+ <body>
212
+ {body_content}
213
+ </body>
214
+ </html>
215
+ """
216
+
217
+ def _get_custom_css(self) -> str:
218
+ """
219
+ 获取自定义 CSS 样式
220
+
221
+ 如果设置了自定义 CSS 文件,则读取并返回其内容
222
+
223
+ Returns:
224
+ str: CSS 样式字符串,如果没有设置或文件不存在则返回空字符串
225
+ """
226
+ if self.css_file and os.path.exists(self.css_file):
227
+ with open(self.css_file, 'r', encoding='utf-8') as f:
228
+ return f.read()
229
+ return ""
@@ -0,0 +1,66 @@
1
+ from typing import Optional
2
+ from .parsers import MarkdownParser
3
+ from .exporters import HTMLExporter
4
+
5
+
6
+ class MDConverter:
7
+ """
8
+ Markdown 转换器主类
9
+
10
+ 提供统一的接口将 Markdown 文件转换为 HTML 格式
11
+ 支持单个文件转换和批量转换
12
+
13
+ Attributes:
14
+ parser (MarkdownParser): Markdown 解析器实例
15
+ html_exporter (HTMLExporter): HTML 导出器实例
16
+ """
17
+
18
+ def __init__(self, encoding: str = 'utf-8', css_file: Optional[str] = None):
19
+ """
20
+ 初始化 Markdown 转换器
21
+
22
+ Args:
23
+ encoding (str): 文件编码格式,默认为 'utf-8'
24
+ css_file (Optional[str]): 自定义 CSS 样式文件路径,可选
25
+ """
26
+ self.parser = MarkdownParser(encoding=encoding)
27
+ self.html_exporter = HTMLExporter(css_file=css_file)
28
+
29
+ def to_html(self, input_path: str, output_path: Optional[str] = None, wrap_html: bool = True) -> None:
30
+ """
31
+ 将 Markdown 文件转换为 HTML
32
+
33
+ 该方法执行以下步骤:
34
+ 1. 验证输入文件是否存在
35
+ 2. 如果未指定输出路径,则根据输入文件名生成输出路径
36
+ 3. 解析 Markdown 文件
37
+ 4. 导出为 HTML 文件
38
+
39
+ Args:
40
+ input_path (str): 输入的 Markdown 文件路径
41
+ output_path (Optional[str]): 输出的 HTML 文件路径
42
+ 如果为 None,则在输入文件同目录下生成同名 HTML 文件
43
+ wrap_html (bool): 是否包装为完整的 HTML 文档,默认为 True
44
+ 如果为 False,则只输出 HTML body 内容
45
+
46
+ Returns:
47
+ str: 输出 HTML 文件的完整路径
48
+
49
+ Raises:
50
+ FileNotFoundError: 当输入文件不存在时抛出异常
51
+
52
+ Example:
53
+ >>> converter = MDConverter()
54
+ >>> converter.to_html('input.md', 'output.html')
55
+ 'output.html'
56
+ >>> converter.to_html('input.md')
57
+ 'input.html'
58
+ """
59
+
60
+ parsed_data = self.parser.parse_file(input_path)
61
+
62
+ html_exporter = HTMLExporter(css_file=self.html_exporter.css_file, wrap_html=wrap_html)
63
+ html_exporter.export(parsed_data, output_path)
64
+
65
+
66
+
@@ -0,0 +1,9 @@
1
+ """
2
+ 解析器模块
3
+
4
+ 提供 Markdown 文件和内容的解析功能
5
+ """
6
+
7
+ from .markdown_parser import MarkdownParser
8
+
9
+ __all__ = ['MarkdownParser']
@@ -0,0 +1,116 @@
1
+ import os
2
+ from typing import Dict, Any
3
+
4
+
5
+ class MarkdownParser:
6
+ """
7
+ Markdown 解析器类
8
+
9
+ 用于解析 Markdown 文件和内容,提取标题、元数据等信息
10
+
11
+ Attributes:
12
+ encoding (str): 文件编码格式,默认为 'utf-8'
13
+ """
14
+
15
+ def __init__(self, encoding: str = 'utf-8'):
16
+ """
17
+ 初始化 Markdown 解析器
18
+
19
+ Args:
20
+ encoding (str): 文件编码格式,默认为 'utf-8'
21
+ """
22
+ self.encoding = encoding
23
+
24
+ def parse_file(self, file_path: str) -> Dict[str, Any]:
25
+ """
26
+ 解析 Markdown 文件
27
+
28
+ 从指定路径读取 Markdown 文件,并解析其内容
29
+
30
+ Args:
31
+ file_path (str): Markdown 文件的路径
32
+
33
+ Returns:
34
+ Dict[str, Any]: 包含解析结果的字典,包括:
35
+ - content: 原始 Markdown 内容
36
+ - title: 提取的标题
37
+ - metadata: 元数据字典
38
+
39
+ Raises:
40
+ FileNotFoundError: 当文件不存在时抛出异常
41
+ """
42
+ if not os.path.exists(file_path):
43
+ raise FileNotFoundError(f"File not found: {file_path}")
44
+
45
+ with open(file_path, 'r', encoding=self.encoding) as f:
46
+ content = f.read()
47
+
48
+ return self.parse_content(content)
49
+
50
+ def parse_content(self, content: str) -> Dict[str, Any]:
51
+ """
52
+ 解析 Markdown 内容字符串
53
+
54
+ 直接解析 Markdown 内容字符串,提取标题和元数据
55
+
56
+ Args:
57
+ content (str): Markdown 内容字符串
58
+
59
+ Returns:
60
+ Dict[str, Any]: 包含解析结果的字典,包括:
61
+ - content: 原始 Markdown 内容
62
+ - title: 提取的标题
63
+ - metadata: 元数据字典
64
+ """
65
+ return {
66
+ 'content': content,
67
+ 'title': self._extract_title(content),
68
+ 'metadata': self._extract_metadata(content)
69
+ }
70
+
71
+ def _extract_title(self, content: str) -> str:
72
+ """
73
+ 从 Markdown 内容中提取标题
74
+
75
+ 查找第一个一级标题(# 开头)作为文档标题
76
+
77
+ Args:
78
+ content (str): Markdown 内容字符串
79
+
80
+ Returns:
81
+ str: 提取的标题,如果没有找到则返回 'Untitled'
82
+ """
83
+ lines = content.split('\n')
84
+ for line in lines:
85
+ line = line.strip()
86
+ if line.startswith('# '):
87
+ return line[2:].strip()
88
+ return 'Untitled'
89
+
90
+ def _extract_metadata(self, content: str) -> Dict[str, str]:
91
+ """
92
+ 从 Markdown 内容中提取元数据
93
+
94
+ 解析 YAML Front Matter 格式的元数据
95
+ 元数据位于文档开头的 --- 分隔符之间
96
+
97
+ Args:
98
+ content (str): Markdown 内容字符串
99
+
100
+ Returns:
101
+ Dict[str, str]: 元数据字典,键值对形式存储
102
+ """
103
+ metadata = {}
104
+ lines = content.split('\n')
105
+
106
+ in_metadata = False
107
+ for line in lines:
108
+ line = line.strip()
109
+ if line == '---':
110
+ in_metadata = not in_metadata
111
+ continue
112
+ if in_metadata and ':' in line:
113
+ key, value = line.split(':', 1)
114
+ metadata[key.strip()] = value.strip()
115
+
116
+ return metadata
@@ -0,0 +1,174 @@
1
+ Metadata-Version: 2.4
2
+ Name: markconv
3
+ Version: 0.1.0
4
+ Summary: 一个强大的 Markdown 转换工具库
5
+ Home-page: https://github.com/Zheng-Enci/markconv
6
+ Author: Zheng EnCi
7
+ Author-email: zheng_enci@qq.com
8
+ Project-URL: Bug Reports, https://github.com/Zheng-Enci/markconv/issues
9
+ Project-URL: Source, https://github.com/Zheng-Enci/markconv
10
+ Project-URL: Documentation, https://github.com/Zheng-Enci/markconv/blob/master/README.md
11
+ Keywords: markdown html converter
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Operating System :: OS Independent
19
+ Requires-Python: >=3.13
20
+ Description-Content-Type: text/markdown
21
+ Requires-Dist: markdown2>=2.5.5
22
+ Dynamic: author
23
+ Dynamic: author-email
24
+ Dynamic: classifier
25
+ Dynamic: description
26
+ Dynamic: description-content-type
27
+ Dynamic: home-page
28
+ Dynamic: keywords
29
+ Dynamic: project-url
30
+ Dynamic: requires-dist
31
+ Dynamic: requires-python
32
+ Dynamic: summary
33
+
34
+ # markconv
35
+
36
+ 一个强大的 Markdown 转换工具库。
37
+
38
+ **版本**: v0.1.0
39
+
40
+ ## 背景
41
+
42
+ 在日常开发中,我们经常需要将 Markdown 文档转换为其他格式(如 HTML、PDF、DOCX 等),但现有的解决方案存在诸多痛点:
43
+
44
+ - **配置繁琐**:许多库需要复杂的配置和依赖管理,学习成本高
45
+ - **中文支持差**:部分工具对中文显示支持不佳,需要额外配置字体和编码
46
+ - **样式定制困难**:自定义样式需要深入了解工具的内部机制
47
+ - **收费限制**:一些在线工具和商业软件需要付费,功能受限
48
+ - **依赖繁重**:某些库依赖过多,安装和使用都不够轻量
49
+
50
+ 为了解决这些问题,我开发了 **markconv** —— 一个简单、高效、免费的 Markdown 转换工具库。
51
+
52
+ ## 简介
53
+
54
+ markconv 是一个 Python 库,旨在提供简单易用的 Markdown 转换功能。该库支持将 Markdown 文档转换为多种格式,方便用户在不同场景下使用。
55
+
56
+ **当前版本支持**:
57
+ - Markdown → HTML
58
+
59
+ **计划支持**:
60
+ - Markdown → PDF
61
+ - Markdown → DOCX
62
+ - 更多格式...
63
+
64
+ ## 项目地址
65
+
66
+ - **Gitee**: https://gitee.com/zheng-enci050704/markconv
67
+ - **GitHub**: https://github.com/Zheng-Enci/markconv
68
+ - **GitCode**: https://gitcode.com/ZhengEnCi/markconv
69
+
70
+ ## 功能特性
71
+
72
+ - 📝 **简单易用**:提供简洁的 API 接口
73
+ - 🚀 **高性能**:基于高效的转换引擎
74
+ - 🔧 **可扩展**:支持自定义转换器和插件
75
+ - 📦 **轻量级**:最小化依赖,易于集成
76
+ - 🎨 **样式灵活**:支持自定义 CSS 样式
77
+ - 🌐 **中文友好**:内置中文字体支持,UTF-8 编码
78
+
79
+ ## 安装
80
+
81
+ ### 从源码安装
82
+
83
+ ```bash
84
+ # 选择一个源进行克隆
85
+ git clone https://github.com/Zheng-Enci/markconv.git
86
+ # 或
87
+ git clone https://gitee.com/zheng-enci050704/markconv.git
88
+ # 或
89
+ git clone https://gitcode.com/ZhengEnCi/markconv.git
90
+
91
+ # 安装依赖
92
+ cd markconv
93
+ pip install -r requirements.txt
94
+ ```
95
+
96
+ ### 使用安装脚本(推荐国内用户)
97
+
98
+ 项目提供了一个安装脚本,使用清华源加速依赖下载:
99
+
100
+ ```bash
101
+ cd markconv
102
+ python installl_requirements.py
103
+ ```
104
+
105
+ 该脚本会自动从清华源安装 `requirements.txt` 中的所有依赖项,速度更快。
106
+
107
+ ### 依赖项
108
+
109
+ - Python 3.13+
110
+
111
+ ## 快速开始
112
+
113
+ 查看 `examples/` 目录获取完整示例。
114
+
115
+ ## 开发计划
116
+
117
+ - [x] 实现 Markdown 到 HTML 的转换
118
+ - [x] 支持自定义 CSS 样式
119
+ - [x] 支持中文显示
120
+ - [x] 自动创建输出目录
121
+ - [ ] 添加更多代码高亮主题
122
+ - [ ] 支持自定义主题模板
123
+ - [ ] 添加命令行工具
124
+ - [ ] 完善单元测试
125
+ - [ ] 发布到 PyPI
126
+
127
+ ## 项目结构
128
+
129
+ ```
130
+ markconv/
131
+ ├── markconv/ # 主包目录
132
+ │ ├── __init__.py # 包初始化文件 (v0.1.0)
133
+ │ ├── html_converter.py # HTML转换器核心模块
134
+ │ ├── parsers/ # 解析器模块
135
+ │ │ ├── __init__.py
136
+ │ │ └── markdown_parser.py # Markdown解析器
137
+ │ └── exporters/ # 导出器模块
138
+ │ ├── __init__.py
139
+ │ └── html_exporter.py # HTML导出器
140
+ ├── examples/ # 示例代码
141
+ │ └── html_example/
142
+ │ ├── html_example.py # HTML转换示例
143
+ │ ├── sample.md # 示例Markdown文件
144
+ │ ├── custom.css # 自定义CSS样式
145
+ │ └── examples/
146
+ │ └── sample.html # 生成的HTML文件
147
+ ├── Note/ # 笔记目录
148
+ │ └── Update_logs/ # 更新日志
149
+ ├── README.md # 项目说明文档
150
+ ├── requirements.txt # 依赖列表
151
+ └── installl_requirements.py # 安装脚本
152
+ ```
153
+
154
+ ## 贡献指南
155
+
156
+ 欢迎贡献代码!请遵循以下步骤:
157
+
158
+ 1. Fork 本项目
159
+ 2. 创建特性分支 (`git checkout -b feature/AmazingFeature`)
160
+ 3. 提交更改 (`git commit -m 'Add some AmazingFeature'`)
161
+ 4. 推送到分支 (`git push origin feature/AmazingFeature`)
162
+ 5. 开启 Pull Request
163
+
164
+ ## 许可证
165
+
166
+ MIT License
167
+
168
+ ## 致谢
169
+
170
+ 感谢所有为本项目做出贡献的开发者!
171
+
172
+ ---
173
+
174
+ **注意**:本项目正在积极开发中,API 可能会有变化。建议关注版本更新日志。
@@ -0,0 +1,13 @@
1
+ README.md
2
+ setup.py
3
+ markconv/__init__.py
4
+ markconv/html_converter.py
5
+ markconv.egg-info/PKG-INFO
6
+ markconv.egg-info/SOURCES.txt
7
+ markconv.egg-info/dependency_links.txt
8
+ markconv.egg-info/requires.txt
9
+ markconv.egg-info/top_level.txt
10
+ markconv/exporters/__init__.py
11
+ markconv/exporters/html_exporter.py
12
+ markconv/parsers/__init__.py
13
+ markconv/parsers/markdown_parser.py
@@ -0,0 +1 @@
1
+ markdown2>=2.5.5
@@ -0,0 +1 @@
1
+ markconv
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,96 @@
1
+ """
2
+ markconv 包的 setup 配置文件
3
+ 用于将项目发布到 PyPI
4
+ """
5
+
6
+ from setuptools import setup, find_packages
7
+
8
+ # 读取 README.md 作为长描述(会显示在 PyPI 网站上)
9
+ with open('README.md', 'r', encoding='utf-8') as f:
10
+ long_description = f.read()
11
+
12
+ # 调用 setup 函数配置包信息
13
+ setup(
14
+ # ===== 基本信息 =====
15
+
16
+ # 包名称(用户执行 pip install 时使用的名称)
17
+ name='markconv',
18
+
19
+ # 版本号(遵循语义化版本规范:主版本.次版本.修订版本)
20
+ version='0.1.0',
21
+
22
+ # 作者姓名
23
+ author='Zheng EnCi',
24
+
25
+ # 作者邮箱(用于联系和问题反馈)
26
+ author_email='zheng_enci@qq.com',
27
+
28
+ # 简短描述(显示在 PyPI 搜索结果中)
29
+ description='一个强大的 Markdown 转换工具库',
30
+
31
+ # 长描述(显示在 PyPI 包详情页,从 README.md 读取)
32
+ long_description=long_description,
33
+
34
+ # 长描述的内容类型(告诉 PyPI 是 Markdown 格式)
35
+ long_description_content_type='text/markdown',
36
+
37
+ # 项目主页 URL(显示在 PyPI 网站上)
38
+ url='https://github.com/Zheng-Enci/markconv',
39
+
40
+ # ===== 包配置 =====
41
+
42
+ # 自动查找所有 Python 包(mdtool 及其子包)
43
+ packages=find_packages(),
44
+
45
+ # ===== 分类器(帮助用户在 PyPI 上搜索和发现包)=====
46
+ classifiers=[
47
+ # 开发状态:3 - Alpha(早期开发版本)
48
+ 'Development Status :: 3 - Alpha',
49
+
50
+ # 目标受众:开发者
51
+ 'Intended Audience :: Developers',
52
+
53
+ # 主题:软件开发 -> Python 模块
54
+ 'Topic :: Software Development :: Libraries :: Python Modules',
55
+
56
+ # 许可证:MIT 开源许可证
57
+ 'License :: OSI Approved :: MIT License',
58
+
59
+ # 编程语言:Python 3
60
+ 'Programming Language :: Python :: 3',
61
+
62
+ # 编程语言:Python 3.13 特定版本
63
+ 'Programming Language :: Python :: 3.13',
64
+
65
+ # 操作系统:操作系统无关(跨平台)
66
+ 'Operating System :: OS Independent',
67
+ ],
68
+
69
+ # ===== Python 版本要求 =====
70
+
71
+ # 要求 Python 版本 >= 3.13
72
+ python_requires='>=3.13',
73
+
74
+ # ===== 依赖项(安装包时自动安装的依赖库)=====
75
+ install_requires=[
76
+ # 需要 markdown2 库,版本 >= 2.5.5
77
+ 'markdown2>=2.5.5',
78
+ ],
79
+
80
+ # ===== 搜索关键词(帮助用户搜索包)=====
81
+
82
+ # 用户搜索时可以使用的关键词
83
+ keywords='markdown html converter',
84
+
85
+ # ===== 项目链接(显示在 PyPI 网站上)=====
86
+ project_urls={
87
+ # 问题反馈链接
88
+ 'Bug Reports': 'https://github.com/Zheng-Enci/markconv/issues',
89
+
90
+ # 源代码链接
91
+ 'Source': 'https://github.com/Zheng-Enci/markconv',
92
+
93
+ # 文档链接(指向 README.md)
94
+ 'Documentation': 'https://github.com/Zheng-Enci/markconv/blob/master/README.md',
95
+ },
96
+ )