From e5297dd491f34bb06ce3c17d6d1504a06ed6e44d Mon Sep 17 00:00:00 2001 From: Evan Date: Mon, 16 Sep 2019 20:16:22 -0400 Subject: [PATCH] Move ANSI HTML-ifying to site; DMOJ/judge#478 --- judge/jinja2/reference.py | 6 +++++ .../0091_compiler_message_ansi2html.py | 23 +++++++++++++++++++ requirements.txt | 1 + templates/submission/status-testcases.html | 4 ++-- 4 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 judge/migrations/0091_compiler_message_ansi2html.py diff --git a/judge/jinja2/reference.py b/judge/jinja2/reference.py index eb7f3c2c58..b496634ba3 100644 --- a/judge/jinja2/reference.py +++ b/judge/jinja2/reference.py @@ -2,6 +2,7 @@ from collections import defaultdict from urllib.parse import urljoin +from ansi2html import Ansi2HTMLConverter from django.contrib.auth.models import AbstractUser from django.urls import reverse from lxml.html import Element @@ -178,3 +179,8 @@ def join(first, second, *rest): if not rest: return urljoin(first, second) return urljoin(urljoin(first, second), *rest) + + +@registry.filter(name='ansi2html') +def ansi2html(s): + return Ansi2HTMLConverter(inline=True).convert(s, full=False) diff --git a/judge/migrations/0091_compiler_message_ansi2html.py b/judge/migrations/0091_compiler_message_ansi2html.py new file mode 100644 index 0000000000..7240f95212 --- /dev/null +++ b/judge/migrations/0091_compiler_message_ansi2html.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +import lxml.html as lh +from django.db import migrations +from lxml.html.clean import clean_html + + +def strip_error_html(apps, schema_editor): + Submission = apps.get_model('judge', 'Submission') + for sub in Submission.objects.filter(error__isnull=False).iterator(): + if sub.error: + sub.error = clean_html(lh.fromstring(sub.error)).text_content() + sub.save(update_fields=['error']) + + +class Migration(migrations.Migration): + + dependencies = [ + ('judge', '0090_fix_contest_visibility'), + ] + + operations = [ + migrations.RunPython(strip_error_html, migrations.RunPython.noop, atomic=True), + ] diff --git a/requirements.txt b/requirements.txt index e96eb511a1..3a305689ec 100644 --- a/requirements.txt +++ b/requirements.txt @@ -28,3 +28,4 @@ jsonfield pymoss packaging celery +ansi2html diff --git a/templates/submission/status-testcases.html b/templates/submission/status-testcases.html index 46eb4f7c9c..2c5740b342 100644 --- a/templates/submission/status-testcases.html +++ b/templates/submission/status-testcases.html @@ -12,11 +12,11 @@

{{ _('We are waiting for a suitable judge to process your submission...') }}

{{ _('Your submission is being processed...') }}

{% elif submission.status == 'CE' %}

{{ _('Compilation Error') }}

-
{{ submission.error|safe }}
+
{{ submission.error|ansi2html }}
{% else %} {% if submission.error %}

{{ _('Compilation Warnings') }}

-
{{ submission.error|safe }}
+
{{ submission.error|ansi2html }}


{% endif %} {% if is_pretest %}