Skip to content

Commit cb2056e

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

File tree

9 files changed

+402
-179
lines changed

9 files changed

+402
-179
lines changed

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: 39 additions & 1 deletion
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
):
@@ -870,8 +871,45 @@ def expand_markup(
870871
env = Environment(
871872
loader=GitTemplateLoader(repo, commit_sha),
872873
undefined=StrictUndefined)
874+
873875
template = env.from_string(text)
874-
text = template.render(**jinja_env)
876+
kwargs = {}
877+
if jinja_env:
878+
kwargs.update(jinja_env)
879+
880+
# {{{ tex2img
881+
882+
def latex_not_enabled_warning(caller, *args, **kwargs):
883+
return (
884+
"<div class='alert alert-danger'>%s</div>" %
885+
("RELATE_LATEX_TO_IMAGE_ENABLED is set to False, "
886+
"no image will be generated."))
887+
888+
def jinja_tex_to_img_tag(caller, *args, **kwargs):
889+
try:
890+
from course.latex import tex_to_img_tag
891+
return tex_to_img_tag(caller(), *args, **kwargs)
892+
except Exception as e:
893+
raise ValueError(
894+
u"<pre><div class='alert alert-danger'>"
895+
u"Error: %s: %s</div></pre>"
896+
% (type(e).__name__, str(e)))
897+
898+
latex2image_enabled = getattr(
899+
settings, "RELATE_LATEX_TO_IMAGE_ENABLED", False)
900+
901+
if latex2image_enabled:
902+
env.globals["latex"] = jinja_tex_to_img_tag
903+
else:
904+
if not validate_only:
905+
env.globals["latex"] = latex_not_enabled_warning
906+
else:
907+
raise ImproperlyConfigured(
908+
_("RELATE_LATEX_TO_IMAGE_ENABLED is set to False, "
909+
"no image will be generated."))
910+
# }}}
911+
912+
text = template.render(**kwargs)
875913

876914
# }}}
877915

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)