-
Mathieu Loiseau authoredd3ab3ecf
utils.py 1017 B
#!/usr/bin/env python3
from re import compile,sub,escape
def html_2_tex(text):
#https://code.activestate.com/recipes/81330-single-pass-multiple-replace/
dict = {' ': '~', '>':'>', '<': '<'}
""" Replace in 'text' all occurences of any key in the given
dictionary by its corresponding value. Returns the new tring."""
# Create a regular expression from the dictionary keys
regex = compile("(%s)" % "|".join(map(escape, dict.keys())))
# For each match, look-up corresponding value in dictionary
return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)
def remove_moodle_cdata(txt):
return txt.replace("<![CDATA[","").replace("]]>","")
def strip_tags(txt):
return html_2_tex(sub(compile('<.*?>'), '',remove_moodle_cdata(txt)))
def mlang_2_multiling(txt):
return sub(r"\{mlang en\}(.*?)\{mlang\}((\{mlang other\})|(\{mlang fr\}))(.*?)\{mlang\}", r"\\multiling{\5}{\1}",txt)
def score_2_str(v):
if int(v)==float(v):
res = str(int(v))
else:
res = str(float(v))
return res