summaryrefslogtreecommitdiff
path: root/yaksh
diff options
context:
space:
mode:
authormaheshgudi2018-08-20 17:35:55 +0530
committermaheshgudi2018-08-20 17:37:13 +0530
commit3fe840b52e4d780587f14d06a46fcab523ba23c3 (patch)
tree296d90e3786aec19813f5dcf233ff8949f428d3b /yaksh
parent1a2bea3da65a5b2b98aaa15085d474c50fb55038 (diff)
downloadonline_test-3fe840b52e4d780587f14d06a46fcab523ba23c3.tar.gz
online_test-3fe840b52e4d780587f14d06a46fcab523ba23c3.tar.bz2
online_test-3fe840b52e4d780587f14d06a46fcab523ba23c3.zip
Add syntax highlight for code answers in answerpaper
Diffstat (limited to 'yaksh')
-rw-r--r--yaksh/templates/yaksh/grade_user.html5
-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.py13
4 files changed, 23 insertions, 3 deletions
diff --git a/yaksh/templates/yaksh/grade_user.html b/yaksh/templates/yaksh/grade_user.html
index 2e5a403..bc9ed87 100644
--- a/yaksh/templates/yaksh/grade_user.html
+++ b/yaksh/templates/yaksh/grade_user.html
@@ -298,7 +298,9 @@ Status : <b style="color: red;"> Failed </b><br/>
<div class="panel-body">
{% if question.type == "code" %}
- <pre><code>{{ ans.answer.answer.strip|safe }}</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="well well-sm">
{% for testcases in question.get_test_cases %}
@@ -327,6 +329,7 @@ Status : <b style="color: red;"> Failed </b><br/>
{% else %}
<div class="well well-sm">
{{ ans.answer.answer.strip|safe }}
+
</div>
{% endif %}
</div>
diff --git a/yaksh/templates/yaksh/user_data.html b/yaksh/templates/yaksh/user_data.html
index 9449fcc..72397dc 100644
--- a/yaksh/templates/yaksh/user_data.html
+++ b/yaksh/templates/yaksh/user_data.html
@@ -233,7 +233,9 @@ User IP address: {{ paper.user_ip }}
{{ answer.answer.answer.strip|safe }}
</div>
{% else %}
- <pre><code>{{ answer.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>
{% endif %}
</div>
</div>
diff --git a/yaksh/templates/yaksh/view_answerpaper.html b/yaksh/templates/yaksh/view_answerpaper.html
index 8f3fad7..e3f7df4 100644
--- a/yaksh/templates/yaksh/view_answerpaper.html
+++ b/yaksh/templates/yaksh/view_answerpaper.html
@@ -229,7 +229,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 056421f..2fdc1d2 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,13 @@ def get_answer_for_arrange_options(ans, question):
@register.filter(name='replace_spaces')
def replace_spaces(name):
return name.replace(" ", "_")
+
+@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