-
Notifications
You must be signed in to change notification settings - Fork 18
/
utils.py
105 lines (83 loc) · 2.35 KB
/
utils.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
import re
import time
import functools
import markdown
from logger import *
from config import *
def run_time(func):
'''
running time
:param func:
:return:
'''
@functools.wraps(func)
def print_time(*args, **kwargs):
func_name = func.__name__
t0 = time.time()
func(*args, **kwargs)
t1 = time.time()
log_time('[%s] is run, run time is (%s)'%(func_name, t1-t0))
return print_time
def write_file(path, content):
with open(path, 'w') as f:
f.write(content)
def read_file(path):
with open(path, 'r') as f:
res = f.read()
return res
def md2html(filename):
''' markdown convers html '''
mdpath = os.path.join(CURRENTPATH, 'md', filename)
if not os.path.exists(mdpath):
return False, 'md file not exists!'
with open(mdpath, 'r') as f:
mdstr = f.read()
exts = ['markdown.extensions.extra', 'markdown.extensions.codehilite', 'markdown.extensions.tables',
'markdown.extensions.toc']
html = '''
<html lang="zh-cn">
<head>
<meta content="text/html; charset=utf-8" http-equiv="content-type" />
<link href="default.css" rel="stylesheet">
<link href="github.css" rel="stylesheet">
</head>
<body>
%s
</body>
</html>
'''
ret = markdown.markdown(mdstr, extensions=exts)
content = html % ret
htmlpath = os.path.join(CURRENTPATH, 'html', filename.replace('.md', '.html'))
with open(htmlpath, 'w') as f:
f.write(content)
print('finish')
return True, htmlpath
def filter_emoji(desstr, restr=''):
''' filter emoji in text '''
try:
co = re.compile(u'[\U00010000-\U0010ffff]')
except re.error:
co = re.compile(u'[\uD800-\uDBFF][\uDC00-\uDFFF]')
return co.sub(restr, desstr)
def get_md_content(filepath):
with open(filepath, 'r') as f:
res = f.read()
c = re.findall('---\n.*?---\n', res, re.DOTALL)
if not c:
return -1
return res.replace(c[0], '')
def get_md_title(filepath):
with open(filepath, 'r') as f:
res = f.read()
titles = re.findall('title:(.*?)\n', res)
if not titles:
return -1
return titles[0]
def get_md_tags(filepath):
with open(filepath, 'r') as f:
res = f.read()
tags = re.findall('tags:(.*?)\n', res)
if not tags:
return -1
return tags