-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathmdx_util.py
57 lines (48 loc) · 1.63 KB
/
mdx_util.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
# -*- coding: utf-8 -*-
# version: python 3.5
import sys
import re
from file_util import *
def get_definition_mdx(word, builder):
"""根据关键字得到MDX词典的解释"""
content = builder.mdx_lookup(word)
if len(content) < 1:
fp = os.popen('python lemma.py ' + word)
word = fp.read().strip()
fp.close()
print("lemma: " + word)
content = builder.mdx_lookup(word)
pattern = re.compile(r"@@@LINK=([\w\s]*)")
rst = pattern.match(content[0])
if rst is not None:
link = rst.group(1).strip()
content = builder.mdx_lookup(link)
str_content = ""
if len(content) > 0:
for c in content:
str_content += c.replace("\r\n","").replace("entry:/","")
injection = []
injection_html = ''
output_html = ''
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
# base_path = sys._MEIPASS
base_path = os.path.dirname(sys.executable)
except Exception:
base_path = os.path.abspath(".")
resource_path = os.path.join(base_path, 'mdx')
file_util_get_files(resource_path, injection)
for p in injection:
if file_util_is_ext(p, 'html'):
injection_html += file_util_read_text(p)
#return [bytes(str_content, encoding='utf-8')]
output_html = str_content + injection_html
return [output_html.encode('utf-8')]
def get_definition_mdd(word, builder):
"""根据关键字得到MDX词典的媒体"""
word = word.replace("/","\\")
content = builder.mdd_lookup(word)
if len(content) > 0:
return [content[0]]
else:
return []