Skip to content

Commit 996b360

Browse files
prandlaveluca93
authored andcommitted
add previewing markdown in AWS
1 parent 9ff9a76 commit 996b360

8 files changed

Lines changed: 63 additions & 12 deletions

File tree

cms/server/admin/handlers/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@
7373
LoginHandler, \
7474
LogoutHandler, \
7575
ResourcesHandler, \
76-
NotificationsHandler
76+
NotificationsHandler, \
77+
MarkdownRenderHandler
7778
from .submission import \
7879
SubmissionHandler, \
7980
SubmissionCommentHandler, \
@@ -113,6 +114,7 @@
113114
(r"/resources/([0-9]+|all)/([0-9]+)", ResourcesHandler),
114115
(r"/notifications", NotificationsHandler),
115116
(r"/file/([a-f0-9]+)/([a-zA-Z0-9_.-]+)", FileFromDigestHandler),
117+
(r"/render_markdown", MarkdownRenderHandler),
116118

117119
# Contest
118120

cms/server/admin/handlers/main.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
from cms import ServiceCoord, get_service_shards, get_service_address
3232
from cms.db import Admin, Contest, Question
33+
from cms.server.jinja2_toolbox import markdown_filter
3334
from cmscommon.crypto import validate_password
3435
from cmscommon.datetime import make_datetime, make_timestamp
3536
from .base import BaseHandler, SimpleHandler, require_permission
@@ -167,3 +168,13 @@ def get(self):
167168
self.service.notifications = []
168169

169170
self.write(json.dumps(res))
171+
172+
class MarkdownRenderHandler(BaseHandler):
173+
"""Renders Markdown for AWS message previews."""
174+
175+
@require_permission(BaseHandler.AUTHENTICATED)
176+
def post(self):
177+
data = self.get_argument("input")
178+
rendered = markdown_filter(data)
179+
self.write(rendered)
180+

cms/server/admin/static/aws_style.css

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ strong, .bold {
5252
font-weight: bold;
5353
}
5454

55+
em {
56+
font-style: italic;
57+
}
58+
5559
.login_error {
5660
color: #F31313;
5761
}
@@ -420,7 +424,7 @@ th .column-sort {
420424
text-decoration: underline;
421425
}
422426

423-
.reply_question textarea {
427+
.markdown_input {
424428
width: 100%;
425429
}
426430

cms/server/admin/static/aws_utils.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -849,10 +849,29 @@ CMS.AWSUtils.prototype.question_reply_toggle = function(event, invoker) {
849849
* Updates visibility of answer box when choosing quick answers.
850850
*/
851851
CMS.AWSUtils.prototype.update_additional_answer = function(event, invoker) {
852-
var obj = invoker.parentElement.querySelector(".alternative_answer");
852+
var obj = $(invoker).parent().find(".alternative_answer");
853853
if (invoker.value == "other") {
854-
obj.style.display = "block";
854+
obj.css("display", "");
855855
} else {
856-
obj.style.display = "none";
856+
obj.css("display", "none");
857857
}
858858
}
859+
860+
/**
861+
* Used by templates/macro/markdown_input.html.
862+
* Asks the server to render the markdown input and displays it.
863+
*/
864+
CMS.AWSUtils.prototype.render_markdown_preview = function(target) {
865+
var form_element = $(target).closest("form");
866+
var md_text = form_element.find(".markdown_input").val();
867+
$.ajax({
868+
type: "POST",
869+
url: this.url("render_markdown"),
870+
data: {input: md_text},
871+
dataType: "text",
872+
headers: {"X-XSRFToken": get_cookie("_xsrf")},
873+
success: function(response) {
874+
form_element.find(".markdown_preview").html(response);
875+
},
876+
});
877+
}

cms/server/admin/templates/announcements.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{% extends "base.html" %}
2+
{% import 'macro/markdown_input.html' as macro_markdown %}
23

34
{% block core %}
45
<div class="core_title">
@@ -23,12 +24,12 @@ <h2 id="title_announcements" class="toggling_on">Announcements</h2>
2324
</datalist>
2425
</div>
2526
<div class="notification_text">
26-
<label for="new_text">Text</label>
27-
<textarea name="text" id="new_text" style="width: 100%" ></textarea><br/>
28-
You can use Markdown here. URLs are not automatically linked, surround them with &lt;&gt; like &lt;http://example.org&gt;.
27+
<label for="text">Text</label>
28+
{{ macro_markdown.markdown_input("text") }}
2929
</div>
3030
<input type="submit"
3131
value="Add announcement" />
32+
{{ macro_markdown.markdown_preview() }}
3233
</form>
3334
</div>
3435
</div>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{% macro markdown_input(name) %}
2+
<textarea name="{{ name }}" class="markdown_input"></textarea><br/>
3+
You can use Markdown here. URLs are not automatically linked, surround them with &lt;&gt; like &lt;http://example.org&gt;.
4+
<div class="markdown_preview"></div>
5+
{% endmacro %}
6+
7+
{% macro markdown_preview() %}
8+
<input type="button" value="Preview Markdown" onclick="utils.render_markdown_preview(this)" />
9+
{% endmacro %}

cms/server/admin/templates/macro/question.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{# This macro should be imported "with context". #}
22

3+
{% import 'macro/markdown_input.html' as macro_markdown %}
4+
35
{% macro question(msg, user_id) %}
46
{#
57
Render one question by a participant.
@@ -96,10 +98,12 @@
9698
<br/>
9799
<div class="alternative_answer">
98100
Alternative answer:<br/>
99-
<textarea name="reply_question_text"></textarea><br/>
100-
You can use Markdown here. URLs are not automatically linked, surround them with &lt;&gt; like &lt;http://example.org&gt;.
101+
{{ macro_markdown.markdown_input("reply_question_text") }}
101102
</div>
102103
<input type="submit" value="Send"/>
104+
<span class="alternative_answer">
105+
{{ macro_markdown.markdown_preview() }}
106+
</span>
103107
</form>
104108
</div>
105109
{% endif %}

cms/server/admin/templates/participation.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{% extends "base.html" %}
44
{% import 'macro/submission.html' as macro_submission %}
55
{% import 'macro/question.html' as macro_question with context %}
6+
{% import 'macro/markdown_input.html' as macro_markdown %}
67

78
{% block core %}
89
<h1>
@@ -148,10 +149,10 @@ <h2 id="title_messages" class="toggling_on">Messages</h2>
148149
</div>
149150
<div class="notification_text">
150151
Text:
151-
<textarea name="message_text" style="width: 100%" ></textarea><br/>
152-
You can use Markdown here. URLs are not automatically linked, surround them with &lt;&gt; like &lt;http://example.org&gt;.
152+
{{ macro_markdown.markdown_input("message_text") }}
153153
</div>
154154
<input type="submit" value="Send"/>
155+
{{ macro_markdown.markdown_preview() }}
155156
</div>
156157
</form>
157158
</div>

0 commit comments

Comments
 (0)