summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--requirements/requirements-common.txt1
-rw-r--r--yaksh/templates/yaksh/grade_user.html6
-rw-r--r--yaksh/templates/yaksh/user_data.html4
-rw-r--r--yaksh/templates/yaksh/view_answerpaper.html4
-rw-r--r--yaksh/templatetags/custom_filters.py14
5 files changed, 25 insertions, 4 deletions
diff --git a/requirements/requirements-common.txt b/requirements/requirements-common.txt
index 913ef1f..54e4c84 100644
--- a/requirements/requirements-common.txt
+++ b/requirements/requirements-common.txt
@@ -9,3 +9,4 @@ selenium==2.53.6
coverage
ruamel.yaml==0.15.23
markdown==2.6.9
+pygments==2.2.0
diff --git a/yaksh/templates/yaksh/grade_user.html b/yaksh/templates/yaksh/grade_user.html
index a76f2c7..b1edba5 100644
--- a/yaksh/templates/yaksh/grade_user.html
+++ b/yaksh/templates/yaksh/grade_user.html
@@ -311,7 +311,9 @@ Status : <b style="color: red;"> Failed </b><br/>
<div class="card-body">
{% if question.type == "code" %}
- <pre><code>{{ ans.answer.answer.strip }}</code></pre>
+ {% pygmentise_user_answer question.language ans.answer.answer.strip as user_answer %}
+ <style type="text/css">{{user_answer.1}}</style>
+ <pre><code>{{user_answer.0|safe}}</code></pre>
{% elif question.type == "mcc"%}
<div class="card">
<div class="card-body">
@@ -346,7 +348,7 @@ Status : <b style="color: red;"> Failed </b><br/>
{% else %}
<div class="card">
<div class="card-body">
- {{ ans.answer.answer.strip }}
+ {{ ans.answer.answer.strip|safe }}
</div>
</div>
{% endif %}
diff --git a/yaksh/templates/yaksh/user_data.html b/yaksh/templates/yaksh/user_data.html
index 317cb15..687dc48 100644
--- a/yaksh/templates/yaksh/user_data.html
+++ b/yaksh/templates/yaksh/user_data.html
@@ -223,7 +223,9 @@
</div>
<div class="card-body">
{% if question.type == "code" %}
- <pre><code>{{ ans.answer.answer.strip|safe }}</code></pre>
+ {% pygmentise_user_answer question.language answer.answer.answer.strip as user_answer %}
+ <style type="text/css">{{user_answer.1}}</style>
+ <pre><code>{{user_answer.0|safe}}</code></pre>
{% elif question.type == "mcc"%}
<div class="card">
<div class="card-body">
diff --git a/yaksh/templates/yaksh/view_answerpaper.html b/yaksh/templates/yaksh/view_answerpaper.html
index b87c818..8e085b6 100644
--- a/yaksh/templates/yaksh/view_answerpaper.html
+++ b/yaksh/templates/yaksh/view_answerpaper.html
@@ -241,7 +241,9 @@
{% endfor %}
{% endwith %}
<div class="panel-body">
- <pre><code>{{ answer.answer.answer.strip }}</code></pre>
+ {% pygmentise_user_answer question.language answer.answer.answer.strip as user_answer %}
+ <style type="text/css">{{user_answer.1}}</style>
+ <pre><code>{{user_answer.0|safe}}</code></pre>
</div>
</div>
{% endif %}
diff --git a/yaksh/templatetags/custom_filters.py b/yaksh/templatetags/custom_filters.py
index 29f4b59..b59d320 100644
--- a/yaksh/templatetags/custom_filters.py
+++ b/yaksh/templatetags/custom_filters.py
@@ -6,6 +6,9 @@ try:
from itertools import zip_longest
except ImportError:
from itertools import izip_longest as zip_longest
+from pygments import highlight
+from pygments.lexers import get_lexer_by_name
+from pygments.formatters import HtmlFormatter
register = template.Library()
@@ -87,3 +90,14 @@ def replace_spaces(name):
@register.simple_tag
def course_grade(course, user):
return course.get_grade(user)
+
+
+@register.simple_tag
+def pygmentise_user_answer(language, answer):
+ lexer = get_lexer_by_name(language, stripall=True)
+ formatter = HtmlFormatter(linenos="inline",
+ cssclass="highlight",
+ style="colorful")
+ style = formatter.get_style_defs('.highlight')
+ result = highlight(answer, lexer, formatter)
+ return result, style