Coverage for src/ensae_teaching_cs/homeblog/latex_svg_gif.py: 17%

101 statements  

« prev     ^ index     » next       coverage.py v7.1.0, created at 2023-01-27 05:44 +0100

1""" 

2@file 

3@brief Svg, Latex 

4""" 

5import os 

6import re 

7import urllib 

8import urllib.parse 

9import urllib.request 

10from pyquickhelper.loghelper import fLOG 

11from .latex2html import convert_short_latex_into_png 

12 

13 

14def load_file(filename): 

15 try: 

16 f = open(filename, "r", encoding="utf8") 

17 text = f.read() 

18 f.close() 

19 return text 

20 except Exception as e: # pragma: no cover 

21 fLOG("e,", __name__, ": issue with ", filename) 

22 raise e 

23 

24 

25def get_url_latex(exp, gif): 

26 exp = exp.replace("\n", " ").strip("\n ").replace("\r", "") 

27 url = "http://latex.codecogs.com/gif.latex?" if gif else \ 

28 "http://latex.codecogs.com/svg.latex?" 

29 exp = exp.replace("+", "AZERTY").replace("&lt;", "<").replace("&gt;", ">") 

30 exp = exp.replace("&amp;", "%26").replace("%", "%25") 

31 exp = urllib.parse.quote_plus(exp) 

32 exp = exp.replace("+", "%20").replace("AZERTY", "+") 

33 url += exp 

34 return url 

35 

36 

37def get_svg_or_gif(url): 

38 u = urllib.request.urlopen(url) 

39 text = u.read() 

40 u.close() 

41 return text 

42 

43 

44def get_latex_contraction(formula): 

45 exp = re.compile("([^a-zA-Z0-9])") 

46 res = exp.sub("", formula) 

47 return res 

48 

49 

50def text_replace_div_gif(text, htmltext, alt, gif, prefix, inline, size): 

51 filename = os.path.split(gif)[-1] 

52 filename = prefix + "/" + filename 

53 if not inline: 

54 rep = "<!--\n%s\n-->\n" % htmltext.replace('"latex"', '"latex_help"') 

55 alt = alt.replace("\n", " ") 

56 px = "%dpx" % (size[0] / 2) 

57 rep += '<p class="latexcenter">\n<img src="%s" alt="%s" title="%s" width="%s" />\n</p>\n' % ( 

58 filename, alt, alt, px) 

59 else: 

60 rep = "<!--\n%s\n-->\n" % htmltext.replace( 

61 '"latex_inline"', '"latex_help_inline"') 

62 alt = alt.replace("\n", " ") 

63 px = "%dpx" % (size[0] / 2) 

64 rep += f' <img src="{filename}" alt="{alt}" title="{alt}" width="{px}" /> ' 

65 text = text.replace(htmltext, rep) 

66 return text 

67 

68 

69def extract_div(prefix, prefiximage, text, logfunction, temp_folder): 

70 exp = re.compile("(<div +lang=\\\"latex(_inline)?\\\">((.|\\n)+?)</div>)") 

71 res = exp.findall(text) 

72 images = [] 

73 for a, inline, b, _ in res: 

74 logfunction(" ------------ converting (div), prefix ", prefix) 

75 logfunction(" latex: " + b.strip("\n ").replace("\n", " ")) 

76 cont = get_latex_contraction(b) 

77 image = prefix + cont + ".gif" 

78 if not os.path.exists(image): 

79 # using a local installation of miktex 

80 image, size = convert_short_latex_into_png( 

81 b, temp_folder=temp_folder, fLOG=logfunction, final_name=image) 

82 images.append(image) 

83 else: 

84 from PIL import Image 

85 im = Image.open(image) 

86 size = im.size 

87 logfunction(" replacing: " + image) 

88 text = text_replace_div_gif( 

89 text, a, b, image, prefiximage, len(inline) > 0, size) 

90 

91 return len(res), text, images 

92 

93 

94def text_replace_span_gif(text, htmltext, alt, gif, prefix): 

95 filename = os.path.split(gif)[-1] 

96 filename = prefix + "/" + filename 

97 rep = "<!-- %s -->" % htmltext.replace('"latex"', '"latex_help"') 

98 alt = alt.replace("\n", " ") 

99 rep += f'<img src="{filename}" alt="{alt}" title="{alt}" />' 

100 text = text.replace(htmltext, rep) 

101 return text 

102 

103 

104def extract_span(prefix, prefiximage, text, logfunction): 

105 exp = re.compile("(<span +lang=\\\"latex\\\">((.|\\n)+?)</span>)") 

106 res = exp.findall(text) 

107 for a, b, _ in res: 

108 logfunction(" ------------ converting (span) ") 

109 logfunction(" latex: " + b.strip("\n ").replace("\n", " ")) 

110 cont = get_latex_contraction(b) 

111 image = prefix + cont + ".gif" 

112 if not os.path.exists(image): 

113 raise FileNotFoundError(image) 

114 logfunction(" replacing: " + image) 

115 text = text_replace_span_gif(text, a, b, image, prefiximage) 

116 

117 return text 

118 

119 

120def replace_file(file, outfile, prefix, giflatex, logfunction, temp_folder): 

121 logfunction(" replacing formulas in html for file ", file) 

122 text = load_file(file) 

123 keep_text = text 

124 prefix += file.replace("/", "_").replace("\\", 

125 "_").replace(":", "_") + "__" 

126 _, text, images = extract_div( 

127 prefix, giflatex, text, logfunction, temp_folder) 

128 text = extract_span(prefix, giflatex, text, logfunction) 

129 if text != keep_text: 

130 logfunction("found formulas in ", file, " nb images ", len(images)) 

131 f = open(outfile, "w", encoding="utf8") 

132 f.write(text) 

133 f.close() 

134 else: 

135 fLOG("i, no detected changes for ", file) 

136 return outfile, images 

137 

138 

139def print_function(*s): 

140 fLOG("i,", *s) 

141 return s