qingxu99 commited on
Commit
9ca7a90
1 Parent(s): d1a18d2

fix equation

Browse files
Files changed (1) hide show
  1. toolbox.py +46 -3
toolbox.py CHANGED
@@ -6,7 +6,7 @@ import traceback
6
  import importlib
7
  import inspect
8
  import re
9
- from show_math import convert as convert_math
10
  from functools import wraps, lru_cache
11
 
12
 
@@ -203,8 +203,51 @@ def markdown_convertion(txt):
203
  """
204
  pre = '<div class="markdown-body">'
205
  suf = '</div>'
206
- if ('$' in txt) and ('```' not in txt):
207
- return pre + markdown.markdown(txt, extensions=['fenced_code', 'tables']) + '<br><br>' + markdown.markdown(convert_math(txt, splitParagraphs=False), extensions=['fenced_code', 'tables']) + suf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
  else:
209
  return pre + markdown.markdown(txt, extensions=['fenced_code', 'tables']) + suf
210
 
 
6
  import importlib
7
  import inspect
8
  import re
9
+ from latex2mathml.converter import convert as tex2mathml
10
  from functools import wraps, lru_cache
11
 
12
 
 
203
  """
204
  pre = '<div class="markdown-body">'
205
  suf = '</div>'
206
+ markdown_extension_configs = {
207
+ 'mdx_math': {
208
+ 'enable_dollar_delimiter': True,
209
+ 'use_gitlab_delimiters': False,
210
+ },
211
+ }
212
+ find_equation_pattern = r'<script type="math/tex(?:.*?)>(.*?)</script>'
213
+
214
+ def tex2mathml_catch_exception(content, *args, **kwargs):
215
+ try:
216
+ content = tex2mathml(content, *args, **kwargs)
217
+ except:
218
+ content = content
219
+ return content
220
+
221
+ def replace_math_no_render(match):
222
+ content = match.group(1)
223
+ if 'mode=display' in match.group(0):
224
+ content = content.replace('\n', '</br>')
225
+ return f"<font color=\"#00FF00\">$$</font><font color=\"#FF00FF\">{content}</font><font color=\"#00FF00\">$$</font>"
226
+ else:
227
+ return f"<font color=\"#00FF00\">$</font><font color=\"#FF00FF\">${content}</font><font color=\"#00FF00\">$</font>"
228
+
229
+ def replace_math_render(match):
230
+ content = match.group(1)
231
+ if 'mode=display' in match.group(0):
232
+ if '\\begin{aligned}' in content:
233
+ content = content.replace('\\begin{aligned}', '\\begin{array}')
234
+ content = content.replace('\\end{aligned}', '\\end{array}')
235
+ content = content.replace('&', ' ')
236
+ content = tex2mathml_catch_exception(content, display="block")
237
+ return content
238
+ else:
239
+ return tex2mathml_catch_exception(content)
240
+ if ('$' in txt) and ('```' not in txt): # 有$标识的公式符号,且没有代码段```的标识
241
+ # convert everything to html format
242
+ split = markdown.markdown(text='---')
243
+ convert_stage_1 = markdown.markdown(text=txt, extensions=['mdx_math', 'fenced_code', 'tables'], extension_configs=markdown_extension_configs)
244
+ # re.DOTALL: Make the '.' special character match any character at all, including a newline; without this flag, '.' will match anything except a newline. Corresponds to the inline flag (?s).
245
+ # 1. convert to easy-to-copy tex (do not render math)
246
+ convert_stage_2_1, n = re.subn(find_equation_pattern, replace_math_no_render, convert_stage_1, flags=re.DOTALL)
247
+ # 2. convert to rendered equation
248
+ convert_stage_2_2, n = re.subn(find_equation_pattern, replace_math_render, convert_stage_1, flags=re.DOTALL)
249
+ # cat them together
250
+ return pre + convert_stage_2_1 + f'{split}' + convert_stage_2_2 + suf
251
  else:
252
  return pre + markdown.markdown(txt, extensions=['fenced_code', 'tables']) + suf
253