Coverage for apps/ptf/cmds/xml/ckeditor/utils.py: 0%

30 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2024-05-30 16:32 +0000

1################################################################################################## 

2# 

3# Utils functions to parse strings with HTML 

4# 

5################################################################################################## 

6 

7from .ckeditor_parser import CkeditorParser 

8 

9 

10def replace_text_with_formula(text, delimiter, is_disp=False): 

11 result = "" 

12 cur_pos = begin_pos = 0 

13 

14 delimiter_end = delimiter 

15 if delimiter_end == "\\(": 

16 delimiter_end = "\\)" 

17 elif delimiter_end == "\\[": 

18 delimiter_end = "\\]" 

19 

20 begin_pos = text.find(delimiter, begin_pos) 

21 

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>" 

35 

36 begin_pos = cur_pos = end_pos + len(delimiter_end) 

37 begin_pos = text.find(delimiter, begin_pos) 

38 else: 

39 begin_pos = -1 

40 

41 result += text[cur_pos:] 

42 return result 

43 

44 

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 """ 

50 

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) 

54 

55 xobj = CkeditorParser(html_value=text, mml_formulas=[], ignore_p=True) 

56 return xobj.value_html, xobj.value_xml