Skip to content
Snippets Groups Projects
Commit b0d8e1ca authored by Mathieu Loiseau's avatar Mathieu Loiseau
Browse files

multiling + listing

parent db0f29d0
No related branches found
No related tags found
No related merge requests found
...@@ -25,3 +25,5 @@ Ce module marche pour MA feuille de style AMC, qui contient : ...@@ -25,3 +25,5 @@ Ce module marche pour MA feuille de style AMC, qui contient :
} }
``` ```
et où ``{mlang en}`` et ``{mlang other}`` sont utilisés dans moodle. et où ``{mlang en}`` et ``{mlang other}`` sont utilisés dans moodle.
Mettre ``unsafe`` à ``False`` pour ne pas remplacer les ``<span>`` (`utils.py`)
...@@ -56,7 +56,7 @@ class Quizz: ...@@ -56,7 +56,7 @@ class Quizz:
class Question: class Question:
def __init__(self,xmlQ,c,n,f): def __init__(self,xmlQ,c,n,f):
self.folder = f self.folder = f
self.q = mlang_2_multiling(strip_tags(xmlQ.find("questiontext/text").text, self.folder)) self.q = strip_tags(mlang_2_multiling(xmlQ.find("questiontext/text").text), self.folder)
self.category = c self.category = c
self.id = f"{c[c.rfind(':')+1:]}_{n}" self.id = f"{c[c.rfind(':')+1:]}_{n}"
self.env = "todo:"+xmlQ.attrib["type"] self.env = "todo:"+xmlQ.attrib["type"]
...@@ -66,15 +66,13 @@ class Question: ...@@ -66,15 +66,13 @@ class Question:
def parseImages(self, file_elements): def parseImages(self, file_elements):
for i in file_elements: for i in file_elements:
name = i.attrib["name"] name = i.attrib["name"]
print(name)
im = Image.open(BytesIO(base64.b64decode(i.text))) im = Image.open(BytesIO(base64.b64decode(i.text)))
ext = name[name.rfind('.')+1:].upper() ext = name[name.rfind('.')+1:].upper()
im.save(self.folder+"/"+name, ext) im.save(self.folder+"/"+name, ext)
def __str__(self): def __str__(self):
return """\\element{"""+self.category+"""}{ #return "\\element{" + self.category + "}{\n\t\\begin{" + self.env + "}{" + self.id + "}\\nbpoints{" + score_2_str(self.max) + "}\n\t\t" + self.q + "\\end{" + self.env + "}\n"
\\begin{"""+self.env+"""}{"""+self.id+"""}\\nbpoints{"""+score_2_str(self.max)+"""} return ""
"""+self.q+"\\end{"+self.env+"}\n"
class Answer: class Answer:
def __init__(self,t,b,max,fb): def __init__(self,t,b,max,fb):
...@@ -116,8 +114,8 @@ class MCQ(Question): ...@@ -116,8 +114,8 @@ class MCQ(Question):
for a in xmlQ.findall("answer"): for a in xmlQ.findall("answer"):
fb = a.find("feedback/text").text fb = a.find("feedback/text").text
if fb != None: if fb != None:
fb = mlang_2_multiling(strip_tags(fb, self.folder)) fb = strip_tags(mlang_2_multiling(fb), self.folder)
self.choices.append(Answer(mlang_2_multiling(strip_tags(a.find("text").text, self.folder)), a.attrib['fraction'], self.max, fb)) self.choices.append(Answer(strip_tags(mlang_2_multiling(a.find("text").text), self.folder), a.attrib['fraction'], self.max, fb))
def get_choice_env(self): def get_choice_env(self):
if self.choice_type == MCQ.VERTICAL: if self.choice_type == MCQ.VERTICAL:
...@@ -134,7 +132,7 @@ class MCQ(Question): ...@@ -134,7 +132,7 @@ class MCQ(Question):
res += "\n" res += "\n"
for c in self.choices: for c in self.choices:
res += str(c) res += str(c)
res += "\n\t\t\\end{"+self.get_choice_env()+"}\n\t\\end{"+self.env+"}\n}\n\n" res += "\t\t\\end{"+self.get_choice_env()+"}\n\t\\end{"+self.env+"}\n}\n\n"
return res return res
class TF(MCQ): class TF(MCQ):
...@@ -143,14 +141,14 @@ class TF(MCQ): ...@@ -143,14 +141,14 @@ class TF(MCQ):
self.choices = [] self.choices = []
self.env = "question" self.env = "question"
self.shuffle = False self.shuffle = False
if self.__class__.__name__ == "MCQ": if self.__class__.__name__ == "TF":
self.__parseAnswers(xmlQ) self.__parseAnswers(xmlQ)
def __parseAnswers(self, xmlQ): def __parseAnswers(self, xmlQ):
for a in xmlQ.findall("answer"): for a in xmlQ.findall("answer"):
fb = a.find("feedback/text").text fb = a.find("feedback/text").text
if fb != None: if fb != None:
fb = mlang_2_multiling(strip_tags(fb, self.folder)) fb = strip_tags(mlang_2_multiling(fb), self.folder)
if a.find("text").text == "true": if a.find("text").text == "true":
self.choices.append(Answer("\\multiling{vrai}{true}", a.attrib['fraction'], self.max, fb)) self.choices.append(Answer("\\multiling{vrai}{true}", a.attrib['fraction'], self.max, fb))
else: else:
......
...@@ -3,6 +3,8 @@ from re import compile,sub,escape ...@@ -3,6 +3,8 @@ from re import compile,sub,escape
from html import unescape from html import unescape
from urllib.parse import unquote from urllib.parse import unquote
unsafe = True
def html_2_tex(text): def html_2_tex(text):
#https://code.activestate.com/recipes/81330-single-pass-multiple-replace/ #https://code.activestate.com/recipes/81330-single-pass-multiple-replace/
# dict = {'&nbsp;': '~', '&gt;':'>', '&lt;': '<'} # dict = {'&nbsp;': '~', '&gt;':'>', '&lt;': '<'}
...@@ -17,19 +19,23 @@ def html_2_tex(text): ...@@ -17,19 +19,23 @@ def html_2_tex(text):
def remove_moodle_cdata(txt, folder): def remove_moodle_cdata(txt, folder):
return sub(r'<img src="[^/]*/([^"]*)" (alt="([^"]*)")?[^>]*>', r"""\\begin{figure}[h!] global unsafe
res = sub(r'<img src="[^/]*/([^"]*)" (alt="([^"]*)")?[^>]*>', r"""\\begin{figure}[h!]
\\begin{center} \\begin{center}
\\includegraphics[width=0.8\\linewidth]{$$$$folder$$$$/\1} \\includegraphics[width=0.8\\linewidth]{$$$$folder$$$$/\1}
\\end{center} \\end{center}
\\caption{\3} \\caption{\3}
\\end{figure} \\end{figure}
""",txt).replace("<![CDATA[","").replace("]]>","").replace("$$$$folder$$$$", folder) """,txt).replace("<![CDATA[","").replace("]]>","").replace("$$$$folder$$$$", folder).replace("<strong>","\\emph{").replace("</strong>","}").replace("<pre>","\\begin{lstlisting}[language=Python]\n").replace("</pre>","\\end{lstlisting}\n")
if unsafe:
res = res.replace('<span style="font:monospace">',"\lstinline[language=python]|").replace('<span style="font-family:monospace">',"\lstinline[language=python]|").replace("</span>","|")
return res
def strip_tags(txt, folder): def strip_tags(txt, folder):
return html_2_tex(sub(compile('<.*?>'), '',remove_moodle_cdata(unquote(txt), folder))) return html_2_tex(sub(compile('<.*?>'), '',remove_moodle_cdata(unquote(txt), folder)))
def mlang_2_multiling(txt): def mlang_2_multiling(txt):
return sub(r"\{mlang en\}(.*?)\{mlang\}((\{mlang other\})|(\{mlang fr\}))(.*?)\{mlang\}", r"\\multiling{\5}{\1}",txt) return sub(r"\{mlang\s*en\}([^\{]*?)\{mlang\}\s*((\{mlang\s*other\})|(\{mlang\s*fr\})|(\{mlang\}))([^\{]*)\{mlang\}", r"\\multiling{\6}{\1}",txt)
def score_2_str(v): def score_2_str(v):
if int(v)==float(v): if int(v)==float(v):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment