Skip to content

Commit 875ded4

Browse files
committed
feat : 新增 markdown 相关文档
1 parent cf8f305 commit 875ded4

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
## markdown转换为其他格式的文件
2+
3+
### markdown2html
4+
5+
```python
6+
import codecs
7+
import os
8+
9+
try:
10+
from markdown import markdown
11+
except ModuleNotFoundError as e:
12+
os.system("pip install markdown")
13+
os.system("pip install python-markdown-math")
14+
os.system("pip install markdown_checklist")
15+
from markdown import markdown
16+
17+
from src.constants import *
18+
from src.source import imgkit
19+
from src.source import pdfkit
20+
from src.templates.html_template import html_
21+
from src.utils.utils import md_extensions_, md_extensions_configs_
22+
23+
24+
class MARKDOWN:
25+
def __init__(self,
26+
wkhtmltopdf_path: str = None,
27+
wkhtmltoimage_path: str = None,
28+
encoding: str = 'utf-8'
29+
):
30+
self.encoding = encoding
31+
self.html_ = html_
32+
self.wkhtmltopdf_path = default_wkhtmltopdf_path if wkhtmltopdf_path is None else wkhtmltopdf_path
33+
self.wkhtmltoimage_path = default_wkhtmltoimage_path if wkhtmltoimage_path is None else wkhtmltoimage_path
34+
self.extensions = md_extensions_()
35+
self.extension_configs = md_extensions_configs_()
36+
37+
def markdown2html(self, input_path: str, output_path: str, is_center: bool = True, is_save: bool = True):
38+
"""
39+
"""
40+
try:
41+
with codecs.open(input_path, "r", encoding="utf-8") as md_:
42+
md_text_ = md_.read()
43+
except Exception as e:
44+
print("<Error>", e)
45+
46+
title = '.'.join(os.path.basename(input_path).split('.')[:-1])
47+
html_text_ = markdown(md_text_,
48+
output_format='html',
49+
extensions=self.extensions,
50+
extension_configs=self.extension_configs
51+
)
52+
53+
class_ = ' for="html-export"' if is_center else ""
54+
html_text_ = self.html_.format(title_=title, static_dir=static_dir, div_=html_text_, class_=class_)
55+
56+
if is_save:
57+
try:
58+
with codecs.open(output_path, 'w', encoding=self.encoding, errors="xmlcharrefreplace") as file_html_:
59+
file_html_.write(html_text_)
60+
return output_path
61+
except Exception:
62+
return False
63+
else:
64+
return html_text_
65+
66+
67+
if __name__ == '__main__':
68+
M = MARKDOWN()
69+
M.markdown2html(input_path=f'./_.md')
70+
```

0 commit comments

Comments
 (0)