NDOJ/judge/widgets/pagedown.py

122 lines
4.2 KiB
Python
Raw Normal View History

2020-01-21 06:35:58 +00:00
from django.contrib.admin import widgets as admin_widgets
from django.forms.utils import flatatt
from django.template.loader import get_template
from django.utils.encoding import force_text
from django.utils.html import conditional_escape
2022-07-15 06:00:34 +00:00
from django.conf import settings
2020-01-21 06:35:58 +00:00
from judge.widgets.mixins import CompressorWidgetMixin
2022-05-14 17:57:27 +00:00
__all__ = [
"PagedownWidget",
"AdminPagedownWidget",
"MathJaxPagedownWidget",
"MathJaxAdminPagedownWidget",
"HeavyPreviewPageDownWidget",
"HeavyPreviewAdminPageDownWidget",
]
2020-01-21 06:35:58 +00:00
try:
from pagedown.widgets import PagedownWidget as OldPagedownWidget
except ImportError:
PagedownWidget = None
AdminPagedownWidget = None
MathJaxPagedownWidget = None
MathJaxAdminPagedownWidget = None
HeavyPreviewPageDownWidget = None
HeavyPreviewAdminPageDownWidget = None
else:
2022-05-14 17:57:27 +00:00
2020-01-21 06:35:58 +00:00
class PagedownWidget(CompressorWidgetMixin, OldPagedownWidget):
# The goal here is to compress all the pagedown JS into one file.
# We do not want any further compress down the chain, because
# 1. we'll creating multiple large JS files to download.
# 2. this is not a problem here because all the pagedown JS files will be used together.
compress_js = True
def __init__(self, *args, **kwargs):
super(PagedownWidget, self).__init__(*args, **kwargs)
2022-07-15 06:00:34 +00:00
class Media:
css = {
"all": [
"pagedown_widget.css",
2022-10-25 04:59:04 +00:00
"markdown.css",
2022-07-15 06:00:34 +00:00
]
}
2020-01-21 06:35:58 +00:00
class AdminPagedownWidget(PagedownWidget, admin_widgets.AdminTextareaWidget):
class Media:
2022-05-14 17:57:27 +00:00
css = {
"all": [
"content-description.css",
"admin/css/pagedown.css",
2022-10-25 04:59:04 +00:00
"markdown.css",
2022-05-14 17:57:27 +00:00
]
}
js = ["admin/js/pagedown.js"]
2020-01-21 06:35:58 +00:00
class MathJaxPagedownWidget(PagedownWidget):
class Media:
js = [
2022-11-25 18:52:49 +00:00
"mathjax3_config.js",
2022-11-25 07:50:32 +00:00
"https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js",
2022-05-14 17:57:27 +00:00
"pagedown_math.js",
2020-01-21 06:35:58 +00:00
]
class MathJaxAdminPagedownWidget(AdminPagedownWidget, MathJaxPagedownWidget):
pass
class HeavyPreviewPageDownWidget(PagedownWidget):
def __init__(self, *args, **kwargs):
2022-07-15 06:00:34 +00:00
self.template = "pagedown.html"
self.id = kwargs.pop("id", None)
2022-05-14 17:57:27 +00:00
self.preview_url = kwargs.pop("preview")
self.preview_timeout = kwargs.pop("preview_timeout", None)
self.hide_preview_button = kwargs.pop("hide_preview_button", False)
2020-01-21 06:35:58 +00:00
super(HeavyPreviewPageDownWidget, self).__init__(*args, **kwargs)
def render(self, name, value, attrs=None, renderer=None):
if value is None:
2022-05-14 17:57:27 +00:00
value = ""
final_attrs = self.build_attrs(attrs, {"name": name})
if "class" not in final_attrs:
final_attrs["class"] = ""
final_attrs["class"] += " wmd-input"
2022-07-15 06:00:34 +00:00
if self.id:
final_attrs["id"] = self.id
2022-05-14 17:57:27 +00:00
return get_template(self.template).render(
self.get_template_context(final_attrs, value)
)
2020-01-21 06:35:58 +00:00
def get_template_context(self, attrs, value):
return {
2022-07-15 06:00:34 +00:00
"image_upload_enabled": getattr(
settings, "PAGEDOWN_IMAGE_UPLOAD_ENABLED", False
),
2022-05-14 17:57:27 +00:00
"attrs": flatatt(attrs),
"body": conditional_escape(force_text(value)),
2022-07-15 06:00:34 +00:00
"postfix": attrs["id"],
"show_preview": True,
2022-05-14 17:57:27 +00:00
"preview_url": self.preview_url,
"preview_timeout": self.preview_timeout,
"extra_classes": "dmmd-no-button" if self.hide_preview_button else None,
2020-01-21 06:35:58 +00:00
}
class Media:
2022-05-14 17:57:27 +00:00
css = {"all": ["dmmd-preview.css"]}
js = ["dmmd-preview.js"]
2020-01-21 06:35:58 +00:00
2022-05-14 17:57:27 +00:00
class HeavyPreviewAdminPageDownWidget(
AdminPagedownWidget, HeavyPreviewPageDownWidget
):
2020-01-21 06:35:58 +00:00
class Media:
2022-05-14 17:57:27 +00:00
css = {
"all": [
"pygment-github.css",
"table.css",
"ranks.css",
2022-10-25 04:59:04 +00:00
"markdown.css",
2022-05-14 17:57:27 +00:00
]
}