Skip to content

Commit c38ac83

Browse files
committed
use mongodb to store the results.
1 parent edb7ace commit c38ac83

9 files changed

Lines changed: 404 additions & 178 deletions

File tree

course/check.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@ def latex2image_bin_check(app_configs, **kwargs):
5151
error = instance.check()
5252
if error:
5353
errors.append(error)
54-
return errors
54+
return errors

course/content.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,7 @@ def expand_markup(
855855
repo, # type: Repo_ish
856856
commit_sha, # type: bytes
857857
text, # type: Text
858+
validate_only=False, # type: bool
858859
use_jinja=True, # type: bool
859860
jinja_env={}, # type: Dict
860861
):
@@ -873,6 +874,46 @@ def expand_markup(
873874
template = env.from_string(text)
874875
text = template.render(**jinja_env)
875876

877+
# {{{ tex2img
878+
879+
from course.latex import tex_to_img_tag
880+
881+
def latex_not_enabled_warning(caller, *args, **kwargs):
882+
return (
883+
"<div class='alert alert-danger'>%s</div>" %
884+
("RELATE_LATEX_TO_IMAGE_ENABLED is set to False, "
885+
"no image will be generated."))
886+
887+
def jinja_tex_to_img_tag(caller, *args, **kwargs):
888+
try:
889+
return tex_to_img_tag(caller(), *args, **kwargs)
890+
except Exception as e:
891+
raise ValueError(
892+
u"<pre><div class='alert alert-danger'>"
893+
u"Error: %s: %s</div></pre>"
894+
% (type(e).__name__, str(e)))
895+
896+
template = env.from_string(text)
897+
latex2image_enabled = getattr(
898+
settings, "RELATE_LATEX_TO_IMAGE_ENABLED", False)
899+
900+
kwargs = {}
901+
if jinja_env:
902+
kwargs.update(jinja_env)
903+
904+
if latex2image_enabled:
905+
env.globals["latex"] = jinja_tex_to_img_tag
906+
text = template.render(**kwargs)
907+
else:
908+
if not validate_only:
909+
env.globals["latex"] = latex_not_enabled_warning
910+
else:
911+
raise ImproperlyConfigured(
912+
_("RELATE_LATEX_TO_IMAGE_ENABLED is set to False, "
913+
"no image will be generated."))
914+
text = template.render(**kwargs)
915+
# }}}
916+
876917
# }}}
877918

878919
return text

course/latex/__init__.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,16 @@
3737
TIKZ_PGF_RE = re.compile(r"\\begin\{(?:tikzpicture|pgfpicture)\}")
3838
DEFAULT_IMG_HTML_CLASS = "img-responsive"
3939

40+
# {{{ mypy
41+
42+
if False:
43+
from typing import Text, Any, Optional # noqa
44+
45+
# }}}
46+
4047

4148
def tex_to_img_tag(tex_source, *args, **kwargs):
49+
# type: (Text, *Any, **Any) -> Optional[Text]
4250
'''Convert LaTex to IMG tag'''
4351

4452
compiler = kwargs.get("compiler", None)
@@ -49,8 +57,6 @@ def tex_to_img_tag(tex_source, *args, **kwargs):
4957
if not image_format:
5058
raise ValueError(_("'image_format' must be specified."))
5159

52-
output_dir = kwargs.get("output_dir")
53-
5460
tex_filename = kwargs.get("tex_filename", None)
5561
tex_preamble = kwargs.get("tex_preamble", "")
5662
tex_preamble_extra = kwargs.get("tex_preamble_extra", "")
@@ -71,12 +77,13 @@ def tex_to_img_tag(tex_source, *args, **kwargs):
7177

7278
if html_class_extra:
7379
if isinstance(html_class_extra, list):
74-
html_class_extra = " ".join (html_class_extra)
80+
html_class_extra = " ".join(html_class_extra)
7581
elif not isinstance(html_class_extra, six.string_types):
7682
raise ValueError(
7783
_('"html_class_extra" must be a string or a list'))
78-
html_class = "%s %s" %(DEFAULT_IMG_HTML_CLASS, html_class_extra)
79-
else: html_class = DEFAULT_IMG_HTML_CLASS
84+
html_class = "%s %s" % (DEFAULT_IMG_HTML_CLASS, html_class_extra)
85+
else:
86+
html_class = DEFAULT_IMG_HTML_CLASS
8087

8188
texdoc = TexDoc(
8289
tex_source, preamble=tex_preamble,
@@ -88,22 +95,23 @@ def tex_to_img_tag(tex_source, *args, **kwargs):
8895

8996
if (compiler == "latex"
9097
and image_format == "png"
91-
and re.search(TIKZ_PGF_RE, tex_source)):
98+
and
99+
re.search(TIKZ_PGF_RE, tex_source)):
92100
image_format = "svg"
93101

94-
tex2img_class = get_tex2img_class(compiler, image_format)
102+
assert isinstance(compiler, six.text_type)
103+
104+
tex2img_class = get_tex2img_class(compiler, image_format) # type: ignore
95105

96106
if not alt:
97107
alt = texdoc.document
98108

99109
if alt:
100-
from django.utils.html import escape
101-
alt = "alt='%s'" % alt.strip().replace("\n","")
110+
alt = "alt='%s'" % alt.strip().replace("\n", "")
102111

103112
latex2img = tex2img_class(
104113
tex_source=texdoc.as_latex(),
105114
tex_filename=tex_filename,
106-
output_dir=output_dir
107115
)
108116

109117
return (

0 commit comments

Comments
 (0)