-
Notifications
You must be signed in to change notification settings - Fork 25
/
maxpress.py
executable file
·224 lines (175 loc) · 7.93 KB
/
maxpress.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
from mistune import Markdown
from six import StringIO
import premailer, lesscpy
import sys, os, re, json, shutil
from os.path import join as join_path
ROOT = os.path.dirname(sys.argv[0])
# 处理配置文件
def import_config(file=join_path(ROOT, 'config.json')):
with open(file, encoding='utf-8') as json_file:
text = json_file.read()
json_text = re.search(r'\{[\s\S]*\}', text).group() # 去除json文件中的注释
config = json.loads(json_text)
non_style_keys = ['poster_url', 'banner_url',
'convert_list', 'ul_style',
'auto_archive', 'auto_rename']
# 读取配置文件中的变量,最多支持两级嵌套
cfg_lines = []
for key, value in config.items():
if not key in non_style_keys:
if not isinstance(value, dict):
cfg_lines.append('@{}: {};\n'.format(key, value))
else:
for inner_key, inner_value in value.items():
cfg_lines.append('@{}: {};\n'.format(
inner_key + '_' + key, inner_value))
variables = '\n'.join(cfg_lines) + '\n\n'
with open(join_path(ROOT, 'less', 'styles.less'), encoding='utf-8') as styles_file:
styles = styles_file.read()
with open(join_path(ROOT, 'less', 'default.less'), 'w', encoding='utf-8') as default_less:
default_less.write(variables + styles)
return config
# 解析less文件,生成默认样式表
def compile_styles(file=join_path(ROOT, 'less', 'default.less')):
with open(file, encoding='utf-8') as raw_file:
raw_text = raw_file.read()
css = lesscpy.compile(StringIO(raw_text))
with open(join_path(ROOT, 'css', 'default.css'), 'w', encoding='utf-8') as css_file:
css_file.write(css)
# 将待解析的md文档转换为适合微信编辑器的html
def md2html(text, styles=None, poster='', banner='', convert_list=True, ul_style='\u25CB'):
md = Markdown()
# 将markdown列表转化为带序号的普通段落(纯为适应微信中列表序号样式自动丢失的古怪现象)
if convert_list:
blocks = text.split('\n```')
for i in range(0, len(blocks)):
if i % 2 == 0:
blocks[i] = re.sub(r'(\n\d+)(\.\s.*?)', r'\n\1\\\2', blocks[i])
blocks[i] = re.sub(r'\n[\-\+\*](\s.*?)',
u'\n\n{} \1'.format(ul_style), blocks[i])
else:
continue # 跳过代码块内部内容
text = '\n```'.join(blocks)
inner_html = md(text)
result = premailer.transform(pack_html(inner_html, styles, poster, banner))
return result
def pack_html(html, styles=None, poster='', banner=''):
if not styles: styles = [join_path(ROOT, 'css','default.css')]
styles.append(join_path(ROOT, 'css','custom.css'))
style_tags = ['<link rel="stylesheet" type="text/css" href="{}">'.format(sheet)
for sheet in styles]
if len(poster.strip()) > 0:
poster_tag = '\n<br>\n<img src="{}" alt="poster"/>'.format(poster)
else: poster_tag = ''
if len(banner.strip()) > 0:
banner_tag = '<img src="{}" alt="banner"/>'.format(banner)
else: banner_tag = ''
head = """<!DOCTYPE html><html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title>result</title>
{styles}
</head>
<body>
<div class="wrapper">
{banner}\n""".format(styles='\n'.join(style_tags),
banner=banner_tag)
foot = """{}\n</div>\n</body>\n</html>""".format(poster_tag)
result = fix_tbl(fix_img(fix_li(head + html + foot)))
return result
def fix_li(html): # 修正粘贴到微信编辑器时列表格式丢失的问题
result = re.sub(r'<li>([\s\S]*?)</li>',
r'<li><span>\1</span></li>', html)
return result
def fix_img(html): # 修正HTML图片大小自适应问题
result = re.sub(r'(<p>)*?<img([\s\S]*?)>(</p>)*?',
r'<section class="img-wrapper"><img\2></section>', html)
return result
def fix_tbl(html): # 修正HTML表格左右留白问题
result = re.sub(r'<table>([\s\S]*?)</table>',
r'<section class="tbl-wrapper"><table>\1</table></section>', html)
return result
# 装饰器:提供报错功能
def report_error(func):
def wrapper(*args, **kwargs):
try:
result = func(*args, **kwargs)
return result
except Exception as e:
print('错误: {}'.format(e))
input('提示:运行前请将所有要转换的Markdown文档放入temp目录中\n'
'请按回车键退出程序:')
return wrapper
# 用于处理嵌套目录
def recursive_listdir(dir):
for root, subdirs, files in os.walk(dir):
for file in files:
yield (file, join_path(root, file))
# 用于处理冲突的文件名
def autoname(defaultpath):
try: ext = re.search(r'\.\w+?$', defaultpath).group()
except AttributeError: ext = None
count = 0
while count < 10000:
suffix = '(%d)' % count if count > 0 else ''
if ext:
newpath = defaultpath[:0 - len(ext)] + suffix + ext
else:
newpath = defaultpath + suffix
if not os.path.exists(newpath): return newpath
else: count += 1; continue
# 转换temp下的所有md文档
# @report_error
def convert_all(src=join_path(ROOT, 'temp'),
dst=join_path(ROOT, 'result', 'html'),
archive=None, styles=None): # 通过styles参数传入css文件名列表时,默认样式将失效
print('[+] 正在导入配置文件...', end=' ')
config = import_config()
if archive is None: archive = config['auto_archive']
print('导入成功')
if not styles:
print('[+] 正在编译CSS样式表...', end=' ')
compile_styles()
print('编译成功')
elif isinstance(styles, str): styles = [styles]
for file, filepath in recursive_listdir(src):
if file.endswith('.md'):
print('[+] 正在转换{}...'.format(file), end=' ')
with open(filepath, encoding='utf-8') as md_file:
text = md_file.read()
result = md2html(text, styles,
poster=config['poster_url'],
banner=config['banner_url'],
convert_list=config['convert_list'],
ul_style=config['ul_style'])
htmlpath = join_path(dst, file[:-3] + '.html')
if config['auto_rename']: htmlpath = autoname(htmlpath)
with open(htmlpath,'w', encoding='utf-8') as html_file:
html_file.write(result)
print('转换成功[{}]'.format(htmlpath.split('/')[-1]))
if archive:
print('[+] 正在存档{}...'.format(file), end=' ')
arch_dir = join_path(ROOT, 'result', 'archive')
if not os.path.exists(arch_dir): os.mkdir(arch_dir)
archpath = join_path(arch_dir, file)
if config['auto_rename']: archpath = autoname(archpath)
shutil.move(filepath, archpath)
print('存档成功[{}]'.format(archpath.split('/')[-1]))
else:
if archive:
# 非.md文件统一移到src一级目录下等待手动删除,以防意外丢失
if re.split(r'[/\\]', filepath)[-2] != re.split(r'[/\\]', src)[-1]:
shutil.move(filepath, autoname(join_path(src, file)))
else: continue
if archive:
# 删除src中剩余的空目录
for path in os.listdir(src):
try: shutil.rmtree(join_path(src, path))
except: pass
print('\n[+] 请进入result/html查看所有生成的HTML文档')
print('[+] 请进入result/archive查看所有存档的MarkDown文档')
if __name__ == '__main__':
# 全部转换并存档
convert_all()
# 只转换不存档
# convert_all(archive=False)