Skip to content
Snippets Groups Projects
Commit 355b843d authored by Jan Maximilian Michal's avatar Jan Maximilian Michal
Browse files

Added simple Latex support. Escaping is not satisfying yet

parent 2a012ebf
No related branches found
No related tags found
No related merge requests found
......@@ -19,6 +19,7 @@ question.
* Add more functionality (gap, alignment, etc.)
* Make parsers more robust.
* reverse ILIAS authentication mechanism for automated upload.
* Enable LaTeX through custom lexer in mistune.
### Notes
......
#!/usr/local/bin/python3
try:
import mistune
except ImportError as err:
print("Please install mistune to make use of markdown parsing.")
print("\t pip install mistune")
import importlib
import argparse
import os
......@@ -13,8 +7,8 @@ import sys
# local import
from hallgrim.IliasXMLCreator import multi, single
import hallgrim.parser
from hallgrim.messages import *
from hallgrim.parser import *
def filename_to_module(name):
......@@ -51,12 +45,12 @@ def main():
script = importlib.import_module(filename_to_module(script_name))
data = {
'description': "_description",
'question_text': mistune.markdown(script.task),
'question_text': markdown(script.task),
'author': script.meta['author'],
'title': script.meta['title'],
'maxattempts': '0',
'shuffle': True,
'questions': hallgrim.parser.choice_parser(script.choices),
'questions': choice_parser(script.choices),
}
output = os.path.join(
......
import re
import mistune
import copy
try:
from mistune import Renderer, InlineGrammar, InlineLexer, Markdown
except ImportError as err:
print("Please install mistune to make use of markdown parsing.")
print("\t pip install mistune")
class LaTeXRenderer(Renderer):
def latex(self, formula):
return '<span class="latex">{}</span>'.format(formula)
class LaTeXInlineLexer(InlineLexer):
def enable_latex(self):
# add latex rules
self.rules.latex = re.compile(
r'\[\[' # [[
r'([^\]]+)' # Page 2|Page 2
r'\]\](?!\])' # ]]
)
# Add latex parser to default rules
# you can insert it some place you like
# but place matters, maybe 3 is not good
self.default_rules.insert(3, 'latex')
def output_latex(self, m):
formula = m.group(1)
# you can create an custom render
# you can also return the html if you like
return self.renderer.latex(formula)
def get_custom_markdown():
renderer = LaTeXRenderer()
inline = LaTeXInlineLexer(renderer)
# enable the feature
inline.enable_latex()
return Markdown(renderer, inline=inline)
def choice_parser(raw_choices):
lines = raw_choices.strip().split('\n')
parse = [re.match('\[(X| )\] (.*)', line).groups() for line in lines]
final = [(mistune.markdown(text), True if mark == 'X' else False, 0.5) for mark, text in parse]
final = [(markdown(text), True if mark == 'X' else False, 0.5) for mark, text in parse]
return final
markdown = get_custom_markdown()
......@@ -5,7 +5,7 @@ meta = {
}
task = """ Hier der Anfang der Datei `punkte.csv`, die Komma-getrennte Angaben
zu erreichten Übungspunkten enthält:
zu erreichten Übungspunkten enthält [[Example Formula \\sum_{i=0}^n i]]:
```
21600001,Herr,Bollman,Fritze-Peter,15
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment