-
Notifications
You must be signed in to change notification settings - Fork 1
/
deffer.py
104 lines (96 loc) · 4.14 KB
/
deffer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import os
try:
from markdown import markdown
except ModuleNotFoundError as e:
os.system("pip install markdown")
os.system("pip install python-markdown-math")
os.system("pip install markdown_checklist")
from markdown import markdown
try:
from pymdownx import superfences
except ModuleNotFoundError as e:
os.system("pip install pymdown-extensions")
from pymdownx import superfences
class mdtox:
def __init__(self, md_filename, encoding='utf-8'):
self.md_filename = md_filename
self.encoding = encoding
self.__args()
def __args(self):
self.html = '''
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimal-ui">
<title>{}</title>
<link rel="stylesheet" href="https://files.cnblogs.com/files/bpf-1024/linenum.css">
<link rel="stylesheet" href="https://files.cnblogs.com/files/bpf-1024/markdown.css">
<link rel="stylesheet" href="https://files.cnblogs.com/files/bpf-1024/tasklist.css">
<link rel="stylesheet" href="https://files.cnblogs.com/files/bpf-1024/codehighlight.css">
<link rel="stylesheet" href="https://files.cnblogs.com/files/bpf-1024/directory.css">
<script src="https://files.cnblogs.com/files/bpf-1024/directory.js"></script>
<script src="https://unpkg.com/[email protected]/dist/mermaid.min.js"></script>
</head>
<body>
<article class="markdown-body" id="markdown-body">
{}
</article>
</body>
</html>
'''
# 扩展配置
self.extensions = [
'toc', # 目录,[toc]
'extra', # 缩写词、属性列表、释义列表、围栏式代码块、脚注、在HTML的Markdown、表格
]
third_party_extensions = [
'mdx_math', # KaTeX数学公式,$E=mc^2$和$$E=mc^2$$
'markdown_checklist.extension', # checklist,- [ ]和- [x]
'pymdownx.magiclink', # 自动转超链接,
'pymdownx.caret', # 上标下标,
'pymdownx.superfences', # 多种块功能允许嵌套,各种图表
'pymdownx.betterem', # 改善强调的处理(粗体和斜体)
'pymdownx.mark', # 亮色突出文本
'pymdownx.highlight', # 高亮显示代码
'pymdownx.tasklist', # 任务列表
'pymdownx.tilde', # 删除线
]
self.extensions.extend(third_party_extensions)
self.extension_configs = {
'mdx_math': {
'enable_dollar_delimiter': True # 允许单个$
},
'pymdownx.superfences': {
"custom_fences": [
{
'name': 'mermaid', # 开启流程图等图
'class': 'mermaid',
'format': superfences.fence_div_format
}
]
},
'pymdownx.highlight': {
'linenums': True, # 显示行号
'linenums_style': 'pymdownx-inline' # 代码和行号分开
},
'pymdownx.tasklist': {
'clickable_checkbox': True, # 任务列表可点击
}
}
def to_html(self, html_name):
try:
with open(self.md_filename, "r", encoding=self.encoding) as file_md:
md_text = file_md.read()
except Exception as e:
print("<Error>", e)
return False
title = '.'.join(os.path.basename(self.md_filename).split('.')[:-1])
html_text = markdown(md_text, output_format='html', extensions=self.extensions, extension_configs=self.extension_configs) # MarkDown转HTML
self.html = self.html.format(title, html_text)
try:
with open(html_name, 'w', encoding=self.encoding) as file_html:
file_html.write(self.html)
except Exception as e:
print("<Error>", e)
return False
return True