Coverage for src/ptf/cmds/xml/ckeditor/utils.py: 0%
30 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-05 09:56 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-05 09:56 +0000
1##################################################################################################
2#
3# Utils functions to parse strings with HTML
4#
5##################################################################################################
7from .ckeditor_parser import CkeditorParser
10def replace_text_with_formula(text, delimiter, is_disp=False):
11 result = ""
12 cur_pos = begin_pos = 0
14 delimiter_end = delimiter
15 if delimiter_end == "\\(":
16 delimiter_end = "\\)"
17 elif delimiter_end == "\\[":
18 delimiter_end = "\\]"
20 begin_pos = text.find(delimiter, begin_pos)
22 while begin_pos > -1:
23 end_pos = text.find(delimiter_end, begin_pos + 1)
24 if end_pos > -1:
25 result += text[cur_pos:begin_pos]
26 if is_disp:
27 result += "<div><p>"
28 result += (
29 '<span class="mathjax-formula">'
30 + text[begin_pos + len(delimiter) : end_pos]
31 + "</span>"
32 )
33 if is_disp:
34 result += "</p></div>"
36 begin_pos = cur_pos = end_pos + len(delimiter_end)
37 begin_pos = text.find(delimiter, begin_pos)
38 else:
39 begin_pos = -1
41 result += text[cur_pos:]
42 return result
45def get_html_and_xml_from_text_with_formulas(text, delimiter_inline="$", delimiter_disp="$"):
46 """
47 1. Detect formulas inside '$' '$' (or other delimiter) and replace with <span class="mathjax-formula"> and </span>
48 2. Call CkeditorParser to get the html and xml
49 """
51 text = replace_text_with_formula(text, delimiter_inline)
52 if delimiter_inline != delimiter_disp:
53 text = replace_text_with_formula(text, delimiter_disp, is_disp=True)
55 xobj = CkeditorParser(html_value=text, mml_formulas=[], ignore_p=True)
56 return xobj.value_html, xobj.value_xml